1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
< |
* fvect.c - routines for float vector calculations |
9 |
< |
* |
10 |
< |
* 8/14/85 |
5 |
> |
* fvect.c - routines for floating-point vector calculations |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "copyright.h" |
9 |
+ |
|
10 |
|
#include <math.h> |
11 |
|
#include "fvect.h" |
12 |
|
|
13 |
|
|
14 |
|
double |
15 |
< |
fdot(v1, v2) /* return the dot product of two vectors */ |
16 |
< |
register FVECT v1, v2; |
15 |
> |
fdot( /* return the dot product of two vectors */ |
16 |
> |
const FVECT v1, |
17 |
> |
const FVECT v2 |
18 |
> |
) |
19 |
|
{ |
20 |
|
return(DOT(v1,v2)); |
21 |
|
} |
22 |
|
|
23 |
|
|
24 |
|
double |
25 |
< |
dist2(p1, p2) /* return square of distance between points */ |
26 |
< |
register FVECT p1, p2; |
25 |
> |
dist2( /* return square of distance between points */ |
26 |
> |
const FVECT p1, |
27 |
> |
const FVECT p2 |
28 |
> |
) |
29 |
|
{ |
30 |
|
FVECT delta; |
31 |
|
|
32 |
< |
delta[0] = p2[0] - p1[0]; |
33 |
< |
delta[1] = p2[1] - p1[1]; |
33 |
< |
delta[2] = p2[2] - p1[2]; |
32 |
> |
VSUB(delta, p2, p1); |
33 |
> |
|
34 |
|
return(DOT(delta, delta)); |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
double |
39 |
< |
dist2line(p, ep1, ep2) /* return square of distance to line */ |
40 |
< |
FVECT p; /* the point */ |
41 |
< |
FVECT ep1, ep2; /* points on the line */ |
39 |
> |
dist2line( /* return square of distance to line */ |
40 |
> |
const FVECT p, /* the point */ |
41 |
> |
const FVECT ep1, |
42 |
> |
const FVECT ep2 /* points on the line */ |
43 |
> |
) |
44 |
|
{ |
45 |
< |
register double d, d1, d2; |
45 |
> |
double d, d1, d2; |
46 |
|
|
47 |
|
d = dist2(ep1, ep2); |
48 |
|
d1 = dist2(ep1, p); |
49 |
< |
d2 = dist2(ep2, p); |
49 |
> |
d2 = d + d1 - dist2(ep2, p); |
50 |
|
|
51 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); |
51 |
> |
return(d1 - 0.25*d2*d2/d); |
52 |
|
} |
53 |
|
|
54 |
|
|
55 |
|
double |
56 |
< |
dist2lseg(p, ep1, ep2) /* return square of distance to line segment */ |
57 |
< |
FVECT p; /* the point */ |
58 |
< |
FVECT ep1, ep2; /* the end points */ |
56 |
> |
dist2lseg( /* return square of distance to line segment */ |
57 |
> |
const FVECT p, /* the point */ |
58 |
> |
const FVECT ep1, |
59 |
> |
const FVECT ep2 /* the end points */ |
60 |
> |
) |
61 |
|
{ |
62 |
< |
register double d, d1, d2; |
62 |
> |
double d, d1, d2; |
63 |
|
|
64 |
|
d = dist2(ep1, ep2); |
65 |
|
d1 = dist2(ep1, p); |
72 |
|
if (d1 - d2 > d) |
73 |
|
return(d2); |
74 |
|
} |
75 |
+ |
d2 = d + d1 - d2; |
76 |
|
|
77 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); /* distance to line */ |
77 |
> |
return(d1 - 0.25*d2*d2/d); /* distance to line */ |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
< |
fcross(vres, v1, v2) /* vres = v1 X v2 */ |
82 |
< |
register FVECT vres, v1, v2; |
81 |
> |
void |
82 |
> |
fcross( /* vres = v1 X v2 */ |
83 |
> |
FVECT vres, |
84 |
> |
const FVECT v1, |
85 |
> |
const FVECT v2 |
86 |
> |
) |
87 |
|
{ |
88 |
< |
vres[0] = v1[1]*v2[2] - v1[2]*v2[1]; |
80 |
< |
vres[1] = v1[2]*v2[0] - v1[0]*v2[2]; |
81 |
< |
vres[2] = v1[0]*v2[1] - v1[1]*v2[0]; |
88 |
> |
VCROSS(vres, v1, v2); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
< |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
93 |
< |
FVECT vres, v0, v1; |
94 |
< |
double f; |
92 |
> |
void |
93 |
> |
fvsum( /* vres = v0 + f*v1 */ |
94 |
> |
FVECT vres, |
95 |
> |
const FVECT v0, |
96 |
> |
const FVECT v1, |
97 |
> |
double f |
98 |
> |
) |
99 |
|
{ |
100 |
< |
vres[0] = v0[0] + f*v1[0]; |
90 |
< |
vres[1] = v0[1] + f*v1[1]; |
91 |
< |
vres[2] = v0[2] + f*v1[2]; |
100 |
> |
VSUM(vres, v0, v1, f); |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
double |
105 |
< |
normalize(v) /* normalize a vector, return old magnitude */ |
106 |
< |
register FVECT v; |
105 |
> |
normalize( /* normalize a vector, return old magnitude */ |
106 |
> |
FVECT v |
107 |
> |
) |
108 |
|
{ |
109 |
< |
register double len; |
109 |
> |
double len, d; |
110 |
|
|
111 |
< |
len = DOT(v, v); |
111 |
> |
d = DOT(v, v); |
112 |
|
|
113 |
< |
if (len <= 0.0) |
113 |
> |
if (d == 0.0) |
114 |
|
return(0.0); |
115 |
|
|
116 |
< |
if (len <= 1.0+FTINY && len >= 1.0-FTINY) |
117 |
< |
len = 0.5 + 0.5*len; /* first order approximation */ |
118 |
< |
else |
119 |
< |
len = sqrt(len); |
116 |
> |
if ((d <= 1.0+FTINY) & (d >= 1.0-FTINY)) { |
117 |
> |
len = 0.5 + 0.5*d; /* first order approximation */ |
118 |
> |
d = 2.0 - len; |
119 |
> |
} else { |
120 |
> |
len = sqrt(d); |
121 |
> |
d = 1.0/len; |
122 |
> |
} |
123 |
> |
v[0] *= d; |
124 |
> |
v[1] *= d; |
125 |
> |
v[2] *= d; |
126 |
|
|
111 |
– |
v[0] /= len; |
112 |
– |
v[1] /= len; |
113 |
– |
v[2] /= len; |
114 |
– |
|
127 |
|
return(len); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
< |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
132 |
< |
FVECT vres, vorig, vnorm; |
133 |
< |
double theta; |
131 |
> |
int |
132 |
> |
closestapproach( /* closest approach of two rays */ |
133 |
> |
RREAL t[2], /* returned distances along each ray */ |
134 |
> |
const FVECT rorg0, /* first origin */ |
135 |
> |
const FVECT rdir0, /* first direction (normalized) */ |
136 |
> |
const FVECT rorg1, /* second origin */ |
137 |
> |
const FVECT rdir1 /* second direction (normalized) */ |
138 |
> |
) |
139 |
|
{ |
140 |
+ |
double dotprod = DOT(rdir0, rdir1); |
141 |
+ |
double denom = 1. - dotprod*dotprod; |
142 |
+ |
double o1o2_d1; |
143 |
+ |
FVECT o0o1; |
144 |
+ |
|
145 |
+ |
if (denom <= FTINY) { /* check if lines are parallel */ |
146 |
+ |
t[0] = t[1] = 0.0; |
147 |
+ |
return(0); |
148 |
+ |
} |
149 |
+ |
VSUB(o0o1, rorg0, rorg1); |
150 |
+ |
o1o2_d1 = DOT(o0o1, rdir1); |
151 |
+ |
t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom; |
152 |
+ |
t[1] = o1o2_d1 + t[0]*dotprod; |
153 |
+ |
return(1); |
154 |
+ |
} |
155 |
+ |
|
156 |
+ |
|
157 |
+ |
void |
158 |
+ |
spinvector( /* rotate vector around normal */ |
159 |
+ |
FVECT vres, /* returned vector (same magnitude as vorig) */ |
160 |
+ |
const FVECT vorig, /* original vector */ |
161 |
+ |
const FVECT vnorm, /* normalized vector for rotation */ |
162 |
+ |
double theta /* right-hand radians */ |
163 |
+ |
) |
164 |
+ |
{ |
165 |
|
double sint, cost, normprod; |
166 |
|
FVECT vperp; |
167 |
< |
register int i; |
167 |
> |
int i; |
168 |
|
|
169 |
|
if (theta == 0.0) { |
170 |
|
if (vres != vorig) |
174 |
|
cost = cos(theta); |
175 |
|
sint = sin(theta); |
176 |
|
normprod = DOT(vorig, vnorm)*(1.-cost); |
177 |
< |
fcross(vperp, vnorm, vorig); |
177 |
> |
VCROSS(vperp, vnorm, vorig); |
178 |
|
for (i = 0; i < 3; i++) |
179 |
|
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint; |
180 |
+ |
} |
181 |
+ |
|
182 |
+ |
double |
183 |
+ |
geodesic( /* rotate vector on great circle towards target */ |
184 |
+ |
FVECT vres, /* returned vector (same magnitude as vorig) */ |
185 |
+ |
const FVECT vorig, /* original vector */ |
186 |
+ |
const FVECT vtarg, /* vector we are rotating towards */ |
187 |
+ |
double t, /* amount along arc directed towards vtarg */ |
188 |
+ |
int meas /* distance measure (radians, absolute, relative) */ |
189 |
+ |
) |
190 |
+ |
{ |
191 |
+ |
FVECT normtarg; |
192 |
+ |
double volen, dotprod, sintr, cost; |
193 |
+ |
int i; |
194 |
+ |
|
195 |
+ |
VCOPY(normtarg, vtarg); /* in case vtarg==vres */ |
196 |
+ |
if (vres != vorig) |
197 |
+ |
VCOPY(vres, vorig); |
198 |
+ |
if (t == 0.0) |
199 |
+ |
return(VLEN(vres)); /* no rotation requested */ |
200 |
+ |
if ((volen = normalize(vres)) == 0.0) |
201 |
+ |
return(0.0); |
202 |
+ |
if (normalize(normtarg) == 0.0) |
203 |
+ |
return(0.0); /* target vector is zero */ |
204 |
+ |
dotprod = DOT(vres, normtarg); |
205 |
+ |
/* check for colinear */ |
206 |
+ |
if (dotprod >= 1.0-FTINY*FTINY) { |
207 |
+ |
if (meas != GEOD_REL) |
208 |
+ |
return(0.0); |
209 |
+ |
vres[0] *= volen; vres[1] *= volen; vres[2] *= volen; |
210 |
+ |
return(volen); |
211 |
+ |
} |
212 |
+ |
if (dotprod <= -1.0+FTINY*FTINY) |
213 |
+ |
return(0.0); |
214 |
+ |
if (meas == GEOD_ABS) |
215 |
+ |
t /= volen; |
216 |
+ |
else if (meas == GEOD_REL) |
217 |
+ |
t *= acos(dotprod); |
218 |
+ |
cost = cos(t); |
219 |
+ |
sintr = sin(t) / sqrt(1. - dotprod*dotprod); |
220 |
+ |
for (i = 0; i < 3; i++) |
221 |
+ |
vres[i] = volen*( cost*vres[i] + |
222 |
+ |
sintr*(normtarg[i] - dotprod*vres[i]) ); |
223 |
+ |
|
224 |
+ |
return(volen); /* return vector length */ |
225 |
|
} |