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

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

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

  1 /**@(#)Vector handling (allocation, additon 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 static Vec3 vec3_0 = {Vec3_id};
 11 
 12 Vec3   *vec3_alloc(void)
 13 {
 14     Vec3   *v = ts_ralloc(Vec3);
 15 
 16     v->el[0] = 0.0;
 17     v->el[1] = 0.0;
 18     v->el[2] = 0.0;
 19     return (v);
 20 }
 21 
 22 Vec3   *vec3_make(Vec3 u)
 23 
 24 
 25 /* float x, y, z; */
 26 {
 27     Vec3   *v = ts_ralloc(Vec3);
 28 
 29     *v = u;
 30     return (v);
 31 }
 32 
 33 
 34 Vec3  *vec3_copy(Vec3 *u)
 35 {
 36   return(vec3_make(*u));
 37 }
 38 
 39 
 40 void    vec3_free(void *v)
 41 {
 42     rfree((void *) v);
 43 }
 44 
 45 Vec3    vec3(double x, double y, double z)
 46 {
 47     Vec3    v = {Vec3_id};
 48 
 49     v.el[0] = x;
 50     v.el[1] = y;
 51     v.el[2] = z;
 52     return (v);
 53 }
 54 
 55 Vec3    vec3_zero(void)
 56 {
 57     return (vec3_0);
 58 }
 59 
 60 Vec3    vec3_ex(void)
 61 {
 62     return (vec3(1.0, 0.0, 0.0));
 63 }
 64 
 65 Vec3    vec3_ey(void)
 66 {
 67     return (vec3(0.0, 1.0, 0.0));
 68 }
 69 
 70 Vec3    vec3_ez(void)
 71 {
 72     return (vec3(0.0, 0.0, 1.0));
 73 }
 74 
 75 void    vec3_comps(Vec3 v, float *x, float *y, float *z)
 76 {
 77     *x = v.el[0];
 78     *y = v.el[1];
 79     *z = v.el[2];
 80 }
 81 
 82 double  vec3_get_x(Vec3 v)      /* function version of vec3_x macro */
 83 
 84 {
 85     return (v.el[0]);
 86 }
 87 
 88 double  vec3_get_y(Vec3 v)      /* function version of vec3_y macro */
 89 
 90 {
 91     return (v.el[1]);
 92 }
 93 
 94 double  vec3_get_z(Vec3 v)      /* function version of vec3_z macro */
 95 
 96 {
 97     return (v.el[2]);
 98 }
 99 
100 Vec3    vec3_sum(Vec3 v, Vec3 w)
101 {
102     Vec3    sum = {Vec3_id};
103 
104     sum.el[0] = v.el[0] + w.el[0];
105     sum.el[1] = v.el[1] + w.el[1];
106     sum.el[2] = v.el[2] + w.el[2];
107     return (sum);
108 }
109 
110 Vec3    vec3_sum3(Vec3 u, Vec3 v, Vec3 w)
111 {
112     Vec3    sum = {Vec3_id};
113 
114     sum.el[0] = u.el[0] + v.el[0] + w.el[0];
115     sum.el[1] = u.el[1] + v.el[1] + w.el[1];
116     sum.el[2] = u.el[2] + v.el[2] + w.el[2];
117     return (sum);
118 }
119 
120 Vec3    vec3_sum4(Vec3 u, Vec3 v, Vec3 w, Vec3 x)
121 {
122     Vec3    sum = {Vec3_id};
123 
124     sum.el[0] = u.el[0] + v.el[0] + w.el[0] + x.el[0];
125     sum.el[1] = u.el[1] + v.el[1] + w.el[1] + x.el[1];
126     sum.el[2] = u.el[2] + v.el[2] + w.el[2] + x.el[2];
127     return (sum);
128 }
129 
130 Vec3    vec3_minus(Vec3 v)
131 {
132     Vec3    minus = {Vec3_id};
133 
134     minus.el[0] = -v.el[0];
135     minus.el[1] = -v.el[1];
136     minus.el[2] = -v.el[2];
137     return (minus);
138 }
139 
140 Vec3    vec3_diff(Vec3 v, Vec3 w)
141 {
142     Vec3    diff = {Vec3_id};
143 
144     diff.el[0] = v.el[0] - w.el[0];
145     diff.el[1] = v.el[1] - w.el[1];
146     diff.el[2] = v.el[2] - w.el[2];
147     return (diff);
148 }
149 
150 Vec3    vec3_times(double k, Vec3 v)
151 {
152     Vec3    prod = {Vec3_id};
153 
154     prod.el[0] = k * v.el[0];
155     prod.el[1] = k * v.el[1];
156     prod.el[2] = k * v.el[2];
157     return (prod);
158 }
159 
160 Vec3    vec3_interp(double k, Vec3 v1, Vec3 v2)
161 {
162     double  k1 = 1.0 - k;
163     Vec3    interp = {Vec3_id};
164 
165     interp.el[0] = k1 * v1.el[0] + k * v2.el[0];
166     interp.el[1] = k1 * v1.el[1] + k * v2.el[1];
167     interp.el[2] = k1 * v1.el[2] + k * v2.el[2];
168     return (interp);
169 }
170 
171 double  vec3_dot(Vec3 v, Vec3 w)
172 {
173     return (v.el[0] * w.el[0] + v.el[1] * w.el[1] + v.el[2] * w.el[2]);
174 }
175 
176 Vec3    vec3_cross(Vec3 v, Vec3 w)
177 {
178     Vec3    cross = {Vec3_id};
179 
180     cross.el[0] = v.el[1] * w.el[2] - v.el[2] * w.el[1];
181     cross.el[1] = v.el[2] * w.el[0] - v.el[0] * w.el[2];
182     cross.el[2] = v.el[0] * w.el[1] - v.el[1] * w.el[0];
183     return (cross);
184 }
185 
186 Vec3    vec3_unitcross(Vec3 v, Vec3 w)
187 {
188     Vec3    cross = {Vec3_id};
189     Vec3    e = {Vec3_id};
190 
191     cross.el[0] = v.el[1] * w.el[2] - v.el[2] * w.el[1];
192     cross.el[1] = v.el[2] * w.el[0] - v.el[0] * w.el[2];
193     cross.el[2] = v.el[0] * w.el[1] - v.el[1] * w.el[0];
194     if (vec3_modunit(cross, &e) == 0.0)
195         return (vec3_perp(v));
196     return (e);
197 }
198 
199 double  vec3_mod(Vec3 v)
200 {
201     double  vx = v.el[0], vy = v.el[1], vz = v.el[2];
202 
203     return (sqrt(vx * vx + vy * vy + vz * vz));
204 }
205 
206 double  vec3_sqrmod(Vec3 v)
207 {
208     double  vx = v.el[0], vy = v.el[1], vz = v.el[2];
209 
210     return (vx * vx + vy * vy + vz * vz);
211 }
212 
213 double  vec3_modunit(Vec3 v, Vec3 * e)
214 {
215     double  vx = v.el[0], vy = v.el[1], vz = v.el[2];
216     double  k, k1;
217 
218     k = sqrt(vx * vx + vy * vy + vz * vz);
219     if (k == 0.0)
220     {
221         *e = vec3_ex();
222         return (0.0);
223     }
224     k1 = 1.0 / k;
225     *e = vec3(vx * k1, vy * k1, vz * k1);
226     return (k);
227 }
228 
229 Vec3    vec3_unit(Vec3 v)
230 {
231     double  vx = v.el[0], vy = v.el[1], vz = v.el[2];
232     double  k, k1;
233 
234     k = sqrt(vx * vx + vy * vy + vz * vz);
235     if (k == 0.0)
236         return (vec3_ex());
237     k1 = 1.0 / k;
238     return (vec3(vx * k1, vy * k1, vz * k1));
239 }
240 
241 double  vec3_dist(Vec3 v, Vec3 w)
242 {
243     double  dx = v.el[0] - w.el[0];
244     double  dy = v.el[1] - w.el[1];
245     double  dz = v.el[2] - w.el[2];
246 
247     return (sqrt(dx * dx + dy * dy + dz * dz));
248 }
249 
250 double  vec3_sqrdist(Vec3 v, Vec3 w)
251 {
252     double  dx = v.el[0] - w.el[0];
253     double  dy = v.el[1] - w.el[1];
254     double  dz = v.el[2] - w.el[2];
255 
256     return (dx * dx + dy * dy + dz * dz);
257 }
258 
259 double  vec3_angle(Vec3 v, Vec3 w)
260 {
261     v = vec3_unit(v);
262     w = vec3_unit(w);
263     return (atan2(vec3_mod(vec3_cross(v, w)), vec3_dot(v, w)));
264 }
265 
266 Vec3    vec3_perp(Vec3 v)
267 {
268     Vec3    e = {Vec3_id};
269 
270     v = vec3_unit(v);
271     if (vec3_modunit(vec3_cross(vec3_ey(), v), &e) != 0.0)
272         return (e);
273     return (vec3_unitcross(vec3_ex(), v));
274 }
275 
276 void    vec3_basis(Vec3 aim, Vec3 down, Vec3 * ex, Vec3 * ey, Vec3 * ez)
277 {
278     Vec3    vec3_projperp();
279     Vec3    v = {Vec3_id};
280     double  mdown = vec3_mod(down);
281 
282     *ez = vec3_unit(aim);
283     v = vec3_projperp(down, *ez);
284     if (vec3_mod(v) / mdown < 0.01)
285     {
286         v = vec3_projperp(vec3_ey(), *ez);
287         if (vec3_mod(v) < 0.01)
288             v = vec3_projperp(vec3_ex(), *ez);
289     }
290     *ey = vec3_unit(v);
291     *ex = vec3_unitcross(*ey, *ez);
292 }
293 
294 Vec3    vec3_read(FILE * fp)
295 {
296     Vec3    v = {Vec3_id};
297 
298     if (fscanf(fp, "%f %f %f", &v.el[0], &v.el[1], &v.el[2]) == 3)
299         return (v);
300 
301     error("vec3_read: error in read", non_fatal);
302     return (vec3_zero());
303 }
304 
305 void    vec3_print(FILE * fp, Vec3 v)
306 {
307     (void) fprintf(fp, "%f %f %f ", v.el[0], v.el[1], v.el[2]);
308 }
309 
310 void    vec3_pprint(FILE * fp, char *msg, Vec3 v)
311 {
312     (void) fprintf(fp, "%s(%15.6f%15.6f%15.6f)\n",
313                    msg, v.el[0], v.el[1], v.el[2]);
314 }
315 
316 void    vec3_format(Vec3 v)
317 {
318     format("%f %f %f\n", v.el[0], v.el[1], v.el[2]);
319 }
320 
321 Vec3    vec3_rand_sphere(Vec3 c, double r)
322 {
323     double  x, y, z, r2;
324 
325     do
326     {
327         x = rand_unif(-1.0, 1.0);
328         y = rand_unif(-1.0, 1.0);
329         z = rand_unif(-1.0, 1.0);
330         r2 = x * x + y * y + z * z;
331     } while (r2 > 1.0);
332     r /= sqrt(r2);
333     return (vec3_sum(c, vec3_times(r, vec3(x, y, z))));
334 }
335 

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