10 |
|
* 8/14/85 |
11 |
|
*/ |
12 |
|
|
13 |
+ |
#include <math.h> |
14 |
|
#include "fvect.h" |
15 |
|
|
15 |
– |
#define FTINY 1e-7 |
16 |
|
|
17 |
– |
|
17 |
|
double |
18 |
|
fdot(v1, v2) /* return the dot product of two vectors */ |
19 |
|
register FVECT v1, v2; |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
+ |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
86 |
+ |
FVECT vres, v0, v1; |
87 |
+ |
double f; |
88 |
+ |
{ |
89 |
+ |
vres[0] = v0[0] + f*v1[0]; |
90 |
+ |
vres[1] = v0[1] + f*v1[1]; |
91 |
+ |
vres[2] = v0[2] + f*v1[2]; |
92 |
+ |
} |
93 |
+ |
|
94 |
+ |
|
95 |
|
double |
96 |
|
normalize(v) /* normalize a vector, return old magnitude */ |
97 |
|
register FVECT v; |
100 |
|
|
101 |
|
len = DOT(v, v); |
102 |
|
|
103 |
< |
if (len <= FTINY*FTINY) |
103 |
> |
if (len <= 0.0) |
104 |
|
return(0.0); |
105 |
|
|
106 |
< |
if (len >= (1.0-FTINY)*(1.0-FTINY) && |
107 |
< |
len <= (1.0+FTINY)*(1.0+FTINY)) |
108 |
< |
return(1.0); |
106 |
> |
if (len <= 1.0+FTINY && len >= 1.0-FTINY) |
107 |
> |
len = 0.5 + 0.5*len; /* first order approximation */ |
108 |
> |
else |
109 |
> |
len = sqrt(len); |
110 |
|
|
101 |
– |
len = sqrt(len); |
111 |
|
v[0] /= len; |
112 |
|
v[1] /= len; |
113 |
|
v[2] /= len; |
114 |
+ |
|
115 |
|
return(len); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
|
119 |
+ |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
120 |
+ |
FVECT vres, vorig, vnorm; |
121 |
+ |
double theta; |
122 |
+ |
{ |
123 |
+ |
double sint, cost, normprod; |
124 |
+ |
FVECT vperp; |
125 |
+ |
register int i; |
126 |
+ |
|
127 |
+ |
if (theta == 0.0) { |
128 |
+ |
if (vres != vorig) |
129 |
+ |
VCOPY(vres, vorig); |
130 |
+ |
return; |
131 |
+ |
} |
132 |
+ |
cost = cos(theta); |
133 |
+ |
sint = sin(theta); |
134 |
+ |
normprod = DOT(vorig, vnorm)*(1.-cost); |
135 |
+ |
fcross(vperp, vnorm, vorig); |
136 |
+ |
for (i = 0; i < 3; i++) |
137 |
+ |
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint; |
138 |
|
} |