| 1 | greg | 1.1 | /* Copyright (c) 1994 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | * Routines for 3-d vectors | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include <stdio.h> | 
| 12 |  |  | #include <math.h> | 
| 13 |  |  | #include "parser.h" | 
| 14 |  |  |  | 
| 15 |  |  |  | 
| 16 |  |  | double | 
| 17 |  |  | normalize(v)                    /* normalize a vector, return old magnitude */ | 
| 18 |  |  | register FVECT  v; | 
| 19 |  |  | { | 
| 20 |  |  | static double  len; | 
| 21 |  |  |  | 
| 22 | greg | 1.2 | len = DOT(v, v); | 
| 23 | greg | 1.1 |  | 
| 24 |  |  | if (len <= 0.0) | 
| 25 |  |  | return(0.0); | 
| 26 |  |  |  | 
| 27 | greg | 1.2 | if (len <= 1.0+FTINY && len >= 1.0-FTINY) | 
| 28 |  |  | len = 0.5 + 0.5*len;    /* first order approximation */ | 
| 29 |  |  | else | 
| 30 |  |  | len = sqrt(len); | 
| 31 | greg | 1.1 |  | 
| 32 |  |  | v[0] /= len; | 
| 33 |  |  | v[1] /= len; | 
| 34 |  |  | v[2] /= len; | 
| 35 |  |  |  | 
| 36 |  |  | return(len); | 
| 37 |  |  | } | 
| 38 |  |  |  | 
| 39 |  |  |  | 
| 40 |  |  | fcross(vres, v1, v2)            /* vres = v1 X v2 */ | 
| 41 |  |  | register FVECT  vres, v1, v2; | 
| 42 |  |  | { | 
| 43 |  |  | vres[0] = v1[1]*v2[2] - v1[2]*v2[1]; | 
| 44 |  |  | vres[1] = v1[2]*v2[0] - v1[0]*v2[2]; | 
| 45 |  |  | vres[2] = v1[0]*v2[1] - v1[1]*v2[0]; | 
| 46 |  |  | } |