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 |
> |
FVECT v1, |
17 |
> |
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 |
> |
FVECT p1, |
27 |
> |
FVECT p2 |
28 |
> |
) |
29 |
|
{ |
30 |
|
FVECT delta; |
31 |
|
|
32 |
|
delta[0] = p2[0] - p1[0]; |
33 |
|
delta[1] = p2[1] - p1[1]; |
34 |
|
delta[2] = p2[2] - p1[2]; |
35 |
+ |
|
36 |
|
return(DOT(delta, delta)); |
37 |
|
} |
38 |
|
|
39 |
|
|
40 |
|
double |
41 |
< |
dist2line(p, ep1, ep2) /* return square of distance to line */ |
42 |
< |
FVECT p; /* the point */ |
43 |
< |
FVECT ep1, ep2; /* points on the line */ |
41 |
> |
dist2line( /* return square of distance to line */ |
42 |
> |
FVECT p, /* the point */ |
43 |
> |
FVECT ep1, |
44 |
> |
FVECT ep2 /* points on the line */ |
45 |
> |
) |
46 |
|
{ |
47 |
< |
register double d, d1, d2; |
47 |
> |
double d, d1, d2; |
48 |
|
|
49 |
|
d = dist2(ep1, ep2); |
50 |
|
d1 = dist2(ep1, p); |
51 |
< |
d2 = dist2(ep2, p); |
51 |
> |
d2 = d + d1 - dist2(ep2, p); |
52 |
|
|
53 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); |
53 |
> |
return(d1 - 0.25*d2*d2/d); |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
|
double |
58 |
< |
dist2lseg(p, ep1, ep2) /* return square of distance to line segment */ |
59 |
< |
FVECT p; /* the point */ |
60 |
< |
FVECT ep1, ep2; /* the end points */ |
58 |
> |
dist2lseg( /* return square of distance to line segment */ |
59 |
> |
FVECT p, /* the point */ |
60 |
> |
FVECT ep1, |
61 |
> |
FVECT ep2 /* the end points */ |
62 |
> |
) |
63 |
|
{ |
64 |
< |
register double d, d1, d2; |
64 |
> |
double d, d1, d2; |
65 |
|
|
66 |
|
d = dist2(ep1, ep2); |
67 |
|
d1 = dist2(ep1, p); |
74 |
|
if (d1 - d2 > d) |
75 |
|
return(d2); |
76 |
|
} |
77 |
+ |
d2 = d + d1 - d2; |
78 |
|
|
79 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); /* distance to line */ |
79 |
> |
return(d1 - 0.25*d2*d2/d); /* distance to line */ |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
< |
fcross(vres, v1, v2) /* vres = v1 X v2 */ |
84 |
< |
register FVECT vres, v1, v2; |
83 |
> |
void |
84 |
> |
fcross( /* vres = v1 X v2 */ |
85 |
> |
FVECT vres, |
86 |
> |
FVECT v1, |
87 |
> |
FVECT v2 |
88 |
> |
) |
89 |
|
{ |
90 |
|
vres[0] = v1[1]*v2[2] - v1[2]*v2[1]; |
91 |
|
vres[1] = v1[2]*v2[0] - v1[0]*v2[2]; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
< |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
97 |
< |
FVECT vres, v0, v1; |
98 |
< |
double f; |
96 |
> |
void |
97 |
> |
fvsum( /* vres = v0 + f*v1 */ |
98 |
> |
FVECT vres, |
99 |
> |
FVECT v0, |
100 |
> |
FVECT v1, |
101 |
> |
double f |
102 |
> |
) |
103 |
|
{ |
104 |
|
vres[0] = v0[0] + f*v1[0]; |
105 |
|
vres[1] = v0[1] + f*v1[1]; |
108 |
|
|
109 |
|
|
110 |
|
double |
111 |
< |
normalize(v) /* normalize a vector, return old magnitude */ |
112 |
< |
register FVECT v; |
111 |
> |
normalize( /* normalize a vector, return old magnitude */ |
112 |
> |
FVECT v |
113 |
> |
) |
114 |
|
{ |
115 |
< |
register double len; |
115 |
> |
double len, d; |
116 |
|
|
117 |
< |
len = DOT(v, v); |
117 |
> |
d = DOT(v, v); |
118 |
|
|
119 |
< |
if (len <= 0.0) |
119 |
> |
if (d == 0.0) |
120 |
|
return(0.0); |
121 |
|
|
122 |
< |
if (len <= 1.0+FTINY && len >= 1.0-FTINY) |
123 |
< |
len = 0.5 + 0.5*len; /* first order approximation */ |
122 |
> |
if (d <= 1.0+FTINY && d >= 1.0-FTINY) |
123 |
> |
len = 0.5 + 0.5*d; /* first order approximation */ |
124 |
|
else |
125 |
< |
len = sqrt(len); |
125 |
> |
len = sqrt(d); |
126 |
|
|
127 |
< |
v[0] /= len; |
128 |
< |
v[1] /= len; |
129 |
< |
v[2] /= len; |
127 |
> |
v[0] *= d = 1.0/len; |
128 |
> |
v[1] *= d; |
129 |
> |
v[2] *= d; |
130 |
|
|
131 |
|
return(len); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
< |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
136 |
< |
FVECT vres, vorig, vnorm; |
137 |
< |
double theta; |
135 |
> |
int |
136 |
> |
closestapproach( /* closest approach of two rays */ |
137 |
> |
RREAL t[2], /* returned distances along each ray */ |
138 |
> |
FVECT rorg0, /* first origin */ |
139 |
> |
FVECT rdir0, /* first direction (normalized) */ |
140 |
> |
FVECT rorg1, /* second origin */ |
141 |
> |
FVECT rdir1 /* second direction (normalized) */ |
142 |
> |
) |
143 |
|
{ |
144 |
+ |
double dotprod = DOT(rdir0, rdir1); |
145 |
+ |
double denom = 1. - dotprod*dotprod; |
146 |
+ |
double o1o2_d1; |
147 |
+ |
FVECT o0o1; |
148 |
+ |
|
149 |
+ |
if (denom <= FTINY) { /* check if lines are parallel */ |
150 |
+ |
t[0] = t[1] = 0.0; |
151 |
+ |
return(0); |
152 |
+ |
} |
153 |
+ |
VSUB(o0o1, rorg0, rorg1); |
154 |
+ |
o1o2_d1 = DOT(o0o1, rdir1); |
155 |
+ |
t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom; |
156 |
+ |
t[1] = o1o2_d1 + t[0]*dotprod; |
157 |
+ |
return(1); |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
|
161 |
+ |
void |
162 |
+ |
spinvector( /* rotate vector around normal */ |
163 |
+ |
FVECT vres, /* returned vector */ |
164 |
+ |
FVECT vorig, /* original vector */ |
165 |
+ |
FVECT vnorm, /* normalized vector for rotation */ |
166 |
+ |
double theta /* left-hand radians */ |
167 |
+ |
) |
168 |
+ |
{ |
169 |
|
double sint, cost, normprod; |
170 |
|
FVECT vperp; |
171 |
< |
register int i; |
171 |
> |
int i; |
172 |
|
|
173 |
|
if (theta == 0.0) { |
174 |
|
if (vres != vorig) |