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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathGeom_vec4.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_vec4.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: mathGeom_vec4.c,v 1.5 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Vector handling (allocation, addition etc)
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief  4D Vector handling (Vec4).  
 50  *
 51  *  Four main groups of functions;
 52  *  
 53  *  - Vec4 allocation and initialisation.
 54  *  - Accessing values within a Vec4.
 55  *  - Vec4 manipulations (eg, addition, unit vector calculations etc).
 56  *  - Vec4 file/format/print handling.
 57  * 
 58 */
 59 
 60 #include "mathGeom_vec4.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/mathGeom_mat4.h>
 73 
 74 static Vec4     vec4_0 = {Vec4_id};             /* static data! */
 75 
 76 Vec4           *vec4_alloc(void)
 77 {
 78         Vec4           *v = ts_ralloc(Vec4);
 79 
 80         v->el[0] = 0.0;
 81         v->el[1] = 0.0;
 82         v->el[2] = 0.0;
 83         v->el[3] = 0.0;
 84         return (v);
 85 }
 86 
 87 Vec4           *vec4_make(Vec4 u)
 88 {
 89         Vec4           *v = ts_ralloc(Vec4);
 90 
 91         *v = u;
 92         return (v);
 93 }
 94 
 95 void            vec4_free(void *v)
 96 {
 97         rfree((void *) v);
 98 }
 99 
100 Vec4            vec4(double x, double y, double z, double w)
101 {
102         Vec4            v = {Vec4_id};
103 
104         v.el[0] = x;
105         v.el[1] = y;
106         v.el[2] = z;
107         v.el[3] = w;
108         return (v);
109 }
110 
111 Vec4            vec4_zero(void)
112 {
113         return (vec4_0);
114 }
115 
116 Vec4            vec4_ex(void)
117 {
118         return (vec4(1.0, 0.0, 0.0, 0.0));
119 }
120 
121 Vec4            vec4_ey(void)
122 {
123         return (vec4(0.0, 1.0, 0.0, 0.0));
124 }
125 
126 Vec4            vec4_ez(void)
127 {
128         return (vec4(0.0, 0.0, 1.0, 0.0));
129 }
130 
131 Vec4            vec4_ew(void)
132 {
133         return (vec4(0.0, 0.0, 0.0, 1.0));
134 }
135 
136 void            vec4_comps(Vec4 v, float *x, float *y, float *z, float *w)
137 {
138         *x = v.el[0];
139         *y = v.el[1];
140         *z = v.el[2];
141         *w = v.el[3];
142 }
143 
144 double          vec4_get_x(Vec4 v)      /* function version of vec4_x macro */
145 
146 {
147         return (v.el[0]);
148 }
149 
150 double          vec4_get_y(Vec4 v)      /* function version of vec4_y macro */
151 
152 {
153         return (v.el[1]);
154 }
155 
156 double          vec4_get_z(Vec4 v)      /* function version of vec4_z macro */
157 
158 {
159         return (v.el[2]);
160 }
161 
162 double          vec4_get_w(Vec4 v)      /* function version of vec4_w macro */
163 
164 {
165         return (v.el[3]);
166 }
167 
168 Vec4            vec4_sum(Vec4 v, Vec4 w)
169 {
170         Vec4            sum = {Vec4_id};
171 
172         sum.el[0] = v.el[0] + w.el[0];
173         sum.el[1] = v.el[1] + w.el[1];
174         sum.el[2] = v.el[2] + w.el[2];
175         sum.el[3] = v.el[3] + w.el[2];
176         return (sum);
177 }
178 
179 Vec4            vec4_sum3(Vec4 u, Vec4 v, Vec4 w)
180 {
181         Vec4            sum = {Vec4_id};
182 
183         sum.el[0] = u.el[0] + v.el[0] + w.el[0];
184         sum.el[1] = u.el[1] + v.el[1] + w.el[1];
185         sum.el[2] = u.el[2] + v.el[2] + w.el[2];
186         sum.el[3] = u.el[3] + v.el[3] + w.el[3];
187         return (sum);
188 }
189 
190 Vec4            vec4_sum4(Vec4 u, Vec4 v, Vec4 w, Vec4 x)
191 {
192         Vec4            sum = {Vec4_id};
193 
194         sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
195         sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
196         sum.el[2] = u.el[2] + v.el[2] + w.el[2] + x.el[2];
197         sum.el[3] = u.el[3] + v.el[3] + w.el[3] + x.el[3];
198         return (sum);
199 }
200 
201 Vec4            vec4_minus(Vec4 v)
202 {
203         Vec4            minus = {Vec4_id};
204 
205         minus.el[0] = -v.el[0];
206         minus.el[1] = -v.el[1];
207         minus.el[2] = -v.el[2];
208         minus.el[3] = -v.el[3];
209         return (minus);
210 }
211 
212 Vec4            vec4_diff(Vec4 v, Vec4 w)
213 {
214         Vec4            diff = {Vec4_id};
215 
216         diff.el[0] = v.el[0] - w.el[0];
217         diff.el[1] = v.el[1] - w.el[1];
218         diff.el[2] = v.el[2] - w.el[2];
219         diff.el[3] = v.el[3] - w.el[3];
220         return (diff);
221 }
222 
223 Vec4            vec4_times(double k, Vec4 v)
224 {
225         Vec4            prod = {Vec4_id};
226 
227         prod.el[0] = k * v.el[0];
228         prod.el[1] = k * v.el[1];
229         prod.el[2] = k * v.el[2];
230         prod.el[3] = k * v.el[3];
231         return (prod);
232 }
233 
234 Vec4            vec4_interp(double k, Vec4 v1, Vec4 v2)
235 {
236         double          k1 = 1.0 - k;
237         Vec4            interp = {Vec4_id};
238 
239         interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
240         interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
241         interp.el[2] = k1 * v1.el[2] + k * v2.el[2];
242         interp.el[3] = k1 * v1.el[3] + k * v2.el[3];
243         return (interp);
244 }
245 
246 double          vec4_dot(Vec4 v, Vec4 w)
247 {
248         return (v.el[0] * w.el[0] + v.el[1] * w.el[1]
249                 + v.el[2] * w.el[2] + v.el[3] * w.el[3]);
250 }
251 
252 /**
253 note:
254 in 4D cross product is an antisymmetric matrix
255 **/
256 
257 Mat4            vec4_cross(Vec4 v, Vec4 w)
258 {
259         Mat4            cross = {Mat4_id};
260         int             i, j;
261 
262         for (i = 0; i < 4; i++)
263                 for (j = 0; j < 4; j++)
264                         cross.el[i][j] = v.el[i] * w.el[j] - v.el[j] * w.el[i];
265         return (cross);
266 }
267 
268 double          vec4_mod(Vec4 v)
269 {
270         double          vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
271 
272         return (sqrt(vx * vx + vy * vy + vz * vz + vw * vw));
273 }
274 
275 double          vec4_sqrmod(Vec4 v)
276 {
277         double          vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
278 
279         return (vx * vx + vy * vy + vz * vz + vw * vw);
280 }
281 
282 double          vec4_modunit(Vec4 v, Vec4 * e)
283 {
284         double          vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
285         double          k, k1;
286 
287         k = sqrt(vx * vx + vy * vy + vz * vz + vw * vw);
288         if (k == 0.0)
289         {
290                 *e = vec4_ex();
291                 return (0.0);
292         }
293         k1 = 1.0 / k;
294         *e = vec4(vx * k1, vy * k1, vz * k1, vw * k1);
295         return (k);
296 }
297 
298 Vec4            vec4_unit(Vec4 v)
299 {
300         double          vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
301         double          k, k1;
302 
303         k = sqrt(vx * vx + vy * vy + vz * vz + vw * vw);
304         if (k == 0.0)
305                 return (vec4_ex());
306         k1 = 1.0 / k;
307         return (vec4(vx * k1, vy * k1, vz * k1, vw * k1));
308 }
309 
310 double          vec4_dist(Vec4 v, Vec4 w)
311 {
312         double          dx = v.el[0] - w.el[0];
313         double          dy = v.el[1] - w.el[1];
314         double          dz = v.el[2] - w.el[2];
315         double          dw = v.el[3] - w.el[3];
316 
317         return (sqrt(dx * dx + dy * dy + dz * dz + dw * dw));
318 }
319 
320 double          vec4_sqrdist(Vec4 v, Vec4 w)
321 {
322         double          dx = v.el[0] - w.el[0];
323         double          dy = v.el[1] - w.el[1];
324         double          dz = v.el[2] - w.el[2];
325         double          dw = v.el[3] - w.el[3];
326 
327         return (dx * dx + dy * dy + dz * dz + dw * dw);
328 }
329 
330 double          vec4_angle(Vec4 v, Vec4 w)
331 {
332         v = vec4_unit(v);
333         w = vec4_unit(w);
334         return (acos(vec4_dot(v, w)));
335 }
336 
337 Vec4            vec4_read(FILE * fp)
338 {
339         Vec4            v = {Vec4_id};
340 
341         if (fscanf(fp, "%f %f %f %f", &v.el[0], &v.el[1], &v.el[2], &v.el[3]) == 4)
342                 return (v);
343 
344         error("vec4_read: error in read", non_fatal);
345         return (vec4_zero());
346 }
347 
348 void            vec4_print(FILE * fp, Vec4 v)
349 {
350         (void) fprintf(fp, "%f %f %f %f ", v.el[0], v.el[1], v.el[2], v.el[3]);
351 }
352 
353 void            vec4_pprint(FILE * fp, char *msg, Vec4 v)
354 {
355         (void) fprintf(fp, "%s(%15.6f%15.6f%15.6f%15.6f)\n",
356                        msg, v.el[0], v.el[1], v.el[2], v.el[3]);
357 }
358 
359 void            vec4_format(Vec4 v)
360 {
361         format("%f %f %f %f\n", v.el[0], v.el[1], v.el[2], v.el[3]);
362 }
363 

~ [ 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.