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

Linux Cross Reference
Tina4/src/math/geom/vec4.c

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

  1 /**@(#)Vector handling (allocation, addition etc)
  2 **/
  3 
  4 #include <stdio.h>
  5 #include <math.h>
  6 #include <tina/sys.h>
  7 #include <tina/sysfuncs.h>
  8 #include <tina/math.h>
  9 #include <tina/mathfuncs.h>
 10 
 11 static Vec4 vec4_0 = {Vec4_id};
 12 
 13 Vec4   *vec4_alloc(void)
 14 {
 15     Vec4   *v = ts_ralloc(Vec4);
 16 
 17     v->el[0] = 0.0;
 18     v->el[1] = 0.0;
 19     v->el[2] = 0.0;
 20     v->el[3] = 0.0;
 21     return (v);
 22 }
 23 
 24 Vec4   *vec4_make(Vec4 u)
 25 {
 26     Vec4   *v = ts_ralloc(Vec4);
 27 
 28     *v = u;
 29     return (v);
 30 }
 31 
 32 void    vec4_free(void *v)
 33 {
 34     rfree((void *) v);
 35 }
 36 
 37 Vec4    vec4(double x, double y, double z, double w)
 38 {
 39     Vec4    v = {Vec4_id};
 40 
 41     v.el[0] = x;
 42     v.el[1] = y;
 43     v.el[2] = z;
 44     v.el[3] = w;
 45     return (v);
 46 }
 47 
 48 Vec4    vec4_zero(void)
 49 {
 50     return (vec4_0);
 51 }
 52 
 53 Vec4    vec4_ex(void)
 54 {
 55     return (vec4(1.0, 0.0, 0.0, 0.0));
 56 }
 57 
 58 Vec4    vec4_ey(void)
 59 {
 60     return (vec4(0.0, 1.0, 0.0, 0.0));
 61 }
 62 
 63 Vec4    vec4_ez(void)
 64 {
 65     return (vec4(0.0, 0.0, 1.0, 0.0));
 66 }
 67 
 68 Vec4    vec4_ew(void)
 69 {
 70     return (vec4(0.0, 0.0, 0.0, 1.0));
 71 }
 72 
 73 void    vec4_comps(Vec4 v, float *x, float *y, float *z, float *w)
 74 {
 75     *x = v.el[0];
 76     *y = v.el[1];
 77     *z = v.el[2];
 78     *w = v.el[3];
 79 }
 80 
 81 double  vec4_get_x(Vec4 v)      /* function version of vec4_x macro */
 82 
 83 {
 84     return (v.el[0]);
 85 }
 86 
 87 double  vec4_get_y(Vec4 v)      /* function version of vec4_y macro */
 88 
 89 {
 90     return (v.el[1]);
 91 }
 92 
 93 double  vec4_get_z(Vec4 v)      /* function version of vec4_z macro */
 94 
 95 {
 96     return (v.el[2]);
 97 }
 98 
 99 double  vec4_get_w(Vec4 v)      /* function version of vec4_w macro */
100 
101 {
102     return (v.el[3]);
103 }
104 
105 Vec4    vec4_sum(Vec4 v, Vec4 w)
106 {
107     Vec4    sum = {Vec4_id};
108 
109     sum.el[0] = v.el[0] + w.el[0];
110     sum.el[1] = v.el[1] + w.el[1];
111     sum.el[2] = v.el[2] + w.el[2];
112     sum.el[3] = v.el[3] + w.el[2];
113     return (sum);
114 }
115 
116 Vec4    vec4_sum3(Vec4 u, Vec4 v, Vec4 w)
117 {
118     Vec4    sum = {Vec4_id};
119 
120     sum.el[0] = u.el[0] + v.el[0] + w.el[0];
121     sum.el[1] = u.el[1] + v.el[1] + w.el[1];
122     sum.el[2] = u.el[2] + v.el[2] + w.el[2];
123     sum.el[3] = u.el[3] + v.el[3] + w.el[3];
124     return (sum);
125 }
126 
127 Vec4    vec4_sum4(Vec4 u, Vec4 v, Vec4 w, Vec4 x)
128 {
129     Vec4    sum = {Vec4_id};
130 
131     sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
132     sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
133     sum.el[2] = u.el[2] + v.el[2] + w.el[2] + x.el[2];
134     sum.el[3] = u.el[3] + v.el[3] + w.el[3] + x.el[3];
135     return (sum);
136 }
137 
138 Vec4    vec4_minus(Vec4 v)
139 {
140     Vec4    minus = {Vec4_id};
141 
142     minus.el[0] = -v.el[0];
143     minus.el[1] = -v.el[1];
144     minus.el[2] = -v.el[2];
145     minus.el[3] = -v.el[3];
146     return (minus);
147 }
148 
149 Vec4    vec4_diff(Vec4 v, Vec4 w)
150 {
151     Vec4    diff = {Vec4_id};
152 
153     diff.el[0] = v.el[0] - w.el[0];
154     diff.el[1] = v.el[1] - w.el[1];
155     diff.el[2] = v.el[2] - w.el[2];
156     diff.el[3] = v.el[3] - w.el[3];
157     return (diff);
158 }
159 
160 Vec4    vec4_times(double k, Vec4 v)
161 {
162     Vec4    prod = {Vec4_id};
163 
164     prod.el[0] = k * v.el[0];
165     prod.el[1] = k * v.el[1];
166     prod.el[2] = k * v.el[2];
167     prod.el[3] = k * v.el[3];
168     return (prod);
169 }
170 
171 Vec4    vec4_interp(double k, Vec4 v1, Vec4 v2)
172 {
173     double  k1 = 1.0 - k;
174     Vec4    interp = {Vec4_id};
175 
176     interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
177     interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
178     interp.el[2] = k1 * v1.el[2] + k * v2.el[2];
179     interp.el[3] = k1 * v1.el[3] + k * v2.el[3];
180     return (interp);
181 }
182 
183 double  vec4_dot(Vec4 v, Vec4 w)
184 {
185     return (v.el[0] * w.el[0] + v.el[1] * w.el[1]
186             + v.el[2] * w.el[2] + v.el[3] * w.el[3]);
187 }
188 
189 /**
190 note:
191 in 4D cross product is an antisymmetric matrix
192 **/
193 
194 Mat4    vec4_cross(Vec4 v, Vec4 w)
195 {
196     Mat4    cross = {Mat4_id};
197     int     i, j;
198 
199     for (i = 0; i < 4; i++)
200         for (j = 0; j < 4; j++)
201             cross.el[i][j] = v.el[i] * w.el[j] - v.el[j] * w.el[i];
202     return (cross);
203 }
204 
205 double  vec4_mod(Vec4 v)
206 {
207     double  vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
208 
209     return (sqrt(vx * vx + vy * vy + vz * vz + vw * vw));
210 }
211 
212 double  vec4_sqrmod(Vec4 v)
213 {
214     double  vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
215 
216     return (vx * vx + vy * vy + vz * vz + vw * vw);
217 }
218 
219 double  vec4_modunit(Vec4 v, Vec4 * e)
220 {
221     double  vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
222     double  k, k1;
223 
224     k = sqrt(vx * vx + vy * vy + vz * vz + vw * vw);
225     if (k == 0.0)
226     {
227         *e = vec4_ex();
228         return (0.0);
229     }
230     k1 = 1.0 / k;
231     *e = vec4(vx * k1, vy * k1, vz * k1, vw * k1);
232     return (k);
233 }
234 
235 Vec4    vec4_unit(Vec4 v)
236 {
237     double  vx = v.el[0], vy = v.el[1], vz = v.el[2], vw = v.el[3];
238     double  k, k1;
239 
240     k = sqrt(vx * vx + vy * vy + vz * vz + vw * vw);
241     if (k == 0.0)
242         return (vec4_ex());
243     k1 = 1.0 / k;
244     return (vec4(vx * k1, vy * k1, vz * k1, vw * k1));
245 }
246 
247 double  vec4_dist(Vec4 v, Vec4 w)
248 {
249     double  dx = v.el[0] - w.el[0];
250     double  dy = v.el[1] - w.el[1];
251     double  dz = v.el[2] - w.el[2];
252     double  dw = v.el[3] - w.el[3];
253 
254     return (sqrt(dx * dx + dy * dy + dz * dz + dw * dw));
255 }
256 
257 double  vec4_sqrdist(Vec4 v, Vec4 w)
258 {
259     double  dx = v.el[0] - w.el[0];
260     double  dy = v.el[1] - w.el[1];
261     double  dz = v.el[2] - w.el[2];
262     double  dw = v.el[3] - w.el[3];
263 
264     return (dx * dx + dy * dy + dz * dz + dw * dw);
265 }
266 
267 double  vec4_angle(Vec4 v, Vec4 w)
268 {
269     v = vec4_unit(v);
270     w = vec4_unit(w);
271     return (acos(vec4_dot(v, w)));
272 }
273 
274 Vec4    vec4_read(FILE * fp)
275 {
276     Vec4    v = {Vec4_id};
277 
278     if (fscanf(fp, "%f %f %f %f", &v.el[0], &v.el[1], &v.el[2], &v.el[3]) == 4)
279         return (v);
280 
281     error("vec4_read: error in read", non_fatal);
282     return (vec4_zero());
283 }
284 
285 void    vec4_print(FILE * fp, Vec4 v)
286 {
287     (void) fprintf(fp, "%f %f %f %f ", v.el[0], v.el[1], v.el[2], v.el[3]);
288 }
289 
290 void    vec4_pprint(FILE * fp, char *msg, Vec4 v)
291 {
292     (void) fprintf(fp, "%s(%15.6f%15.6f%15.6f%15.6f)\n",
293                    msg, v.el[0], v.el[1], v.el[2], v.el[3]);
294 }
295 
296 void    vec4_format(Vec4 v)
297 {
298     format("%f %f %f %f\n", v.el[0], v.el[1], v.el[2], v.el[3]);
299 }
300 

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