~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/math/mathGeom_vec3.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathGeom_vec3.c,v $
 37  * Date    :  $Date: 2009/03/19 18:36:26 $
 38  * Version :  $Revision: 1.6 $
 39  * CVS Id  :  $Id: mathGeom_vec3.c,v 1.6 2009/03/19 18:36:26 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Vector handling (allocation, additon etc)
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief  3D Vector handling (Vec3).  
 50  *
 51  *  Four main groups of functions;
 52  *  
 53  *  - Vec3 allocation and initialisation.
 54  *  - Accessing values within a Vec3.
 55  *  - Vec3 manipulations (eg, addition, unit vector calculations etc).
 56  *  - Vec3 file/format/print handling.
 57  * 
 58 */
 59 
 60 #include "mathGeom_vec3.h"
 61 
 62 #if HAVE_CONFIG_H
 63 #include <config.h>
 64 #endif
 65 
 66 
 67 #include <stdio.h>
 68 #include <math.h>
 69 #include <tina/sys/sysDef.h>
 70 #include <tina/sys/sysPro.h>
 71 #include <tina/math/math_GeomDef.h>
 72 #include <tina/math/mathUtil_rand.h>
 73 #include <tina/math/mathGeom_geom3.h>
 74 
 75 
 76 static Vec3     vec3_0 = {Vec3_id};             /* static data! */
 77 
 78 Vec3           *vec3_alloc(void)
 79 {
 80         Vec3           *v = ts_ralloc(Vec3);
 81 
 82         v->el[0] = 0.0;
 83         v->el[1] = 0.0;
 84         v->el[2] = 0.0;
 85         return (v);
 86 }
 87 
 88 Vec3           *vec3_make(Vec3 u)
 89 
 90 
 91 /* float x, y, z; */
 92 {
 93         Vec3           *v = ts_ralloc(Vec3);
 94 
 95         *v = u;
 96         return (v);
 97 }
 98 
 99 
100 Vec3           *vec3_copy(Vec3 * u)
101 {
102         return (vec3_make(*u));
103 }
104 
105 
106 void            vec3_free(void *v)
107 {
108         rfree((void *) v);
109 }
110 
111 Vec3            vec3(double x, double y, double z)
112 {
113         Vec3            v = {Vec3_id};
114 
115         v.el[0] = x;
116         v.el[1] = y;
117         v.el[2] = z;
118         return (v);
119 }
120 
121 Vec3            vec3_zero(void)
122 {
123         return (vec3_0);
124 }
125 
126 Vec3            vec3_ex(void)
127 {
128         return (vec3(1.0, 0.0, 0.0));
129 }
130 
131 Vec3            vec3_ey(void)
132 {
133         return (vec3(0.0, 1.0, 0.0));
134 }
135 
136 Vec3            vec3_ez(void)
137 {
138         return (vec3(0.0, 0.0, 1.0));
139 }
140 
141 void            vec3_comps(Vec3 v, float *x, float *y, float *z)
142 {
143         *x = v.el[0];
144         *y = v.el[1];
145         *z = v.el[2];
146 }
147 
148 double          vec3_get_x(Vec3 v)      /* function version of vec3_x macro */
149 
150 {
151         return (v.el[0]);
152 }
153 
154 double          vec3_get_y(Vec3 v)      /* function version of vec3_y macro */
155 
156 {
157         return (v.el[1]);
158 }
159 
160 double          vec3_get_z(Vec3 v)      /* function version of vec3_z macro */
161 
162 {
163         return (v.el[2]);
164 }
165 
166 Vec3            vec3_sum(Vec3 v, Vec3 w)
167 {
168         Vec3            sum = {Vec3_id};
169 
170         sum.el[0] = v.el[0] + w.el[0];
171         sum.el[1] = v.el[1] + w.el[1];
172         sum.el[2] = v.el[2] + w.el[2];
173         return (sum);
174 }
175 
176 Vec3            vec3_sum3(Vec3 u, Vec3 v, Vec3 w)
177 {
178         Vec3            sum = {Vec3_id};
179 
180         sum.el[0] = u.el[0] + v.el[0] + w.el[0];
181         sum.el[1] = u.el[1] + v.el[1] + w.el[1];
182         sum.el[2] = u.el[2] + v.el[2] + w.el[2];
183         return (sum);
184 }
185 
186 Vec3            vec3_sum4(Vec3 u, Vec3 v, Vec3 w, Vec3 x)
187 {
188         Vec3            sum = {Vec3_id};
189 
190         sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
191         sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
192         sum.el[2] = u.el[2] + v.el[2] + w.el[2] + x.el[2];
193         return (sum);
194 }
195 
196 Vec3            vec3_minus(Vec3 v)
197 {
198         Vec3            minus = {Vec3_id};
199 
200         minus.el[0] = -v.el[0];
201         minus.el[1] = -v.el[1];
202         minus.el[2] = -v.el[2];
203         return (minus);
204 }
205 
206 Vec3            vec3_diff(Vec3 v, Vec3 w)
207 {
208         Vec3            diff = {Vec3_id};
209 
210         diff.el[0] = v.el[0] - w.el[0];
211         diff.el[1] = v.el[1] - w.el[1];
212         diff.el[2] = v.el[2] - w.el[2];
213         return (diff);
214 }
215 
216 Vec3            vec3_times(double k, Vec3 v)
217 {
218         Vec3            prod = {Vec3_id};
219 
220         prod.el[0] = k * v.el[0];
221         prod.el[1] = k * v.el[1];
222         prod.el[2] = k * v.el[2];
223         return (prod);
224 }
225 
226 Vec3            vec3_interp(double k, Vec3 v1, Vec3 v2)
227 {
228         double          k1 = 1.0 - k;
229         Vec3            interp = {Vec3_id};
230 
231         interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
232         interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
233         interp.el[2] = k1 * v1.el[2] + k * v2.el[2];
234         return (interp);
235 }
236 
237 double          vec3_dot(Vec3 v, Vec3 w)
238 {
239         return (v.el[0] * w.el[0] + v.el[1] * w.el[1] + v.el[2] * w.el[2]);
240 }
241 
242 Vec3            vec3_cross(Vec3 v, Vec3 w)
243 {
244         Vec3            cross = {Vec3_id};
245 
246         cross.el[0] = v.el[1] * w.el[2] - v.el[2] * w.el[1];
247         cross.el[1] = v.el[2] * w.el[0] - v.el[0] * w.el[2];
248         cross.el[2] = v.el[0] * w.el[1] - v.el[1] * w.el[0];
249         return (cross);
250 }
251 
252 Vec3            vec3_unitcross(Vec3 v, Vec3 w)
253 {
254         Vec3            cross = {Vec3_id};
255         Vec3            e = {Vec3_id};
256 
257         cross.el[0] = v.el[1] * w.el[2] - v.el[2] * w.el[1];
258         cross.el[1] = v.el[2] * w.el[0] - v.el[0] * w.el[2];
259         cross.el[2] = v.el[0] * w.el[1] - v.el[1] * w.el[0];
260         if (vec3_modunit(cross, &e) == 0.0)
261                 return (vec3_perp(v));
262         return (e);
263 }
264 
265 double          vec3_mod(Vec3 v)
266 {
267         double          vx = v.el[0], vy = v.el[1], vz = v.el[2];
268 
269         return (sqrt(vx * vx + vy * vy + vz * vz));
270 }
271 
272 double          vec3_sqrmod(Vec3 v)
273 {
274         double          vx = v.el[0], vy = v.el[1], vz = v.el[2];
275 
276         return (vx * vx + vy * vy + vz * vz);
277 }
278 
279 double          vec3_modunit(Vec3 v, Vec3 * e)
280 {
281         double          vx = v.el[0], vy = v.el[1], vz = v.el[2];
282         double          k, k1;
283 
284         k = sqrt(vx * vx + vy * vy + vz * vz);
285         if (k == 0.0)
286         {
287                 *e = vec3_ex();
288                 return (0.0);
289         }
290         k1 = 1.0 / k;
291         *e = vec3(vx * k1, vy * k1, vz * k1);
292         return (k);
293 }
294 
295 Vec3            vec3_unit(Vec3 v)
296 {
297         double          vx = v.el[0], vy = v.el[1], vz = v.el[2];
298         double          k, k1;
299 
300         k = sqrt(vx * vx + vy * vy + vz * vz);
301         if (k == 0.0)
302                 return (vec3_ex());
303         k1 = 1.0 / k;
304         return (vec3(vx * k1, vy * k1, vz * k1));
305 }
306 
307 double          vec3_dist(Vec3 v, Vec3 w)
308 {
309         double          dx = v.el[0] - w.el[0];
310         double          dy = v.el[1] - w.el[1];
311         double          dz = v.el[2] - w.el[2];
312 
313         return (sqrt(dx * dx + dy * dy + dz * dz));
314 }
315 
316 double          vec3_sqrdist(Vec3 v, Vec3 w)
317 {
318         double          dx = v.el[0] - w.el[0];
319         double          dy = v.el[1] - w.el[1];
320         double          dz = v.el[2] - w.el[2];
321 
322         return (dx * dx + dy * dy + dz * dz);
323 }
324 
325 double          vec3_angle(Vec3 v, Vec3 w)
326 {
327         v = vec3_unit(v);
328         w = vec3_unit(w);
329         return (atan2(vec3_mod(vec3_cross(v, w)), vec3_dot(v, w)));
330 }
331 
332 Vec3            vec3_perp(Vec3 v)
333 {
334         Vec3            e = {Vec3_id};
335 
336         v = vec3_unit(v);
337         if (vec3_modunit(vec3_cross(vec3_ey(), v), &e) != 0.0)
338                 return (e);
339         return (vec3_unitcross(vec3_ex(), v));
340 }
341 
342 void            vec3_basis(Vec3 aim, Vec3 down, Vec3 * ex, Vec3 * ey, Vec3 * ez)
343 {
344         Vec3            v = {Vec3_id};
345         double          mdown = vec3_mod(down);
346 
347         *ez = vec3_unit(aim);
348         v = vec3_projperp(down, *ez);
349         if (vec3_mod(v) / mdown < 0.01)
350         {
351                 v = vec3_projperp(vec3_ey(), *ez);
352                 if (vec3_mod(v) < 0.01)
353                         v = vec3_projperp(vec3_ex(), *ez);
354         }
355         *ey = vec3_unit(v);
356         *ex = vec3_unitcross(*ey, *ez);
357 }
358 
359 Vec3            vec3_read(FILE * fp)
360 {
361         Vec3            v = {Vec3_id};
362 
363         if (fscanf(fp, "%f %f %f", &v.el[0], &v.el[1], &v.el[2]) == 3)
364                 return (v);
365 
366         error("vec3_read: error in read", non_fatal);
367         return (vec3_zero());
368 }
369 
370 void            vec3_print(FILE * fp, Vec3 v)
371 {
372         (void) fprintf(fp, "%f %f %f ", v.el[0], v.el[1], v.el[2]);
373 }
374 
375 void            vec3_pprint(FILE * fp, char *msg, Vec3 v)
376 {
377         (void) fprintf(fp, "%s(%15.6f%15.6f%15.6f)\n",
378                        msg, v.el[0], v.el[1], v.el[2]);
379 }
380 
381 void            vec3_format(Vec3 v)
382 {
383         format("%f %f %f\n", v.el[0], v.el[1], v.el[2]);
384 }
385 
386 Vec3            vec3_rand_sphere(Vec3 c, double r)
387 {
388         double          x, y, z, r2;
389 
390         do
391         {
392                 x = rand_unif(-1.0, 1.0);
393                 y = rand_unif(-1.0, 1.0);
394                 z = rand_unif(-1.0, 1.0);
395                 r2 = x * x + y * y + z * z;
396         } while (r2 > 1.0);
397         r /= sqrt(r2);
398         return (vec3_sum(c, vec3_times(r, vec3(x, y, z))));
399 }
400 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.