1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
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 |
|
|
15 |
– |
#define FTINY 1e-7 |
13 |
|
|
17 |
– |
|
14 |
|
double |
15 |
|
fdot(v1, v2) /* return the dot product of two vectors */ |
16 |
|
register FVECT v1, v2; |
23 |
|
dist2(p1, p2) /* return square of distance between points */ |
24 |
|
register FVECT p1, p2; |
25 |
|
{ |
26 |
< |
static FVECT delta; |
26 |
> |
FVECT delta; |
27 |
|
|
28 |
|
delta[0] = p2[0] - p1[0]; |
29 |
|
delta[1] = p2[1] - p1[1]; |
30 |
|
delta[2] = p2[2] - p1[2]; |
31 |
+ |
|
32 |
|
return(DOT(delta, delta)); |
33 |
|
} |
34 |
|
|
38 |
|
FVECT p; /* the point */ |
39 |
|
FVECT ep1, ep2; /* points on the line */ |
40 |
|
{ |
41 |
< |
static double d, d1, d2; |
41 |
> |
register double d, d1, d2; |
42 |
|
|
43 |
|
d = dist2(ep1, ep2); |
44 |
|
d1 = dist2(ep1, p); |
45 |
< |
d2 = dist2(ep2, p); |
45 |
> |
d2 = d + d1 - dist2(ep2, p); |
46 |
|
|
47 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); |
47 |
> |
return(d1 - 0.25*d2*d2/d); |
48 |
|
} |
49 |
|
|
50 |
|
|
53 |
|
FVECT p; /* the point */ |
54 |
|
FVECT ep1, ep2; /* the end points */ |
55 |
|
{ |
56 |
< |
static double d, d1, d2; |
56 |
> |
register double d, d1, d2; |
57 |
|
|
58 |
|
d = dist2(ep1, ep2); |
59 |
|
d1 = dist2(ep1, p); |
66 |
|
if (d1 - d2 > d) |
67 |
|
return(d2); |
68 |
|
} |
69 |
+ |
d2 = d + d1 - d2; |
70 |
|
|
71 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); /* distance to line */ |
71 |
> |
return(d1 - 0.25*d2*d2/d); /* distance to line */ |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
+ |
void |
76 |
|
fcross(vres, v1, v2) /* vres = v1 X v2 */ |
77 |
|
register FVECT vres, v1, v2; |
78 |
|
{ |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
+ |
void |
86 |
+ |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
87 |
+ |
register FVECT vres, v0, v1; |
88 |
+ |
register double f; |
89 |
+ |
{ |
90 |
+ |
vres[0] = v0[0] + f*v1[0]; |
91 |
+ |
vres[1] = v0[1] + f*v1[1]; |
92 |
+ |
vres[2] = v0[2] + f*v1[2]; |
93 |
+ |
} |
94 |
+ |
|
95 |
+ |
|
96 |
|
double |
97 |
|
normalize(v) /* normalize a vector, return old magnitude */ |
98 |
|
register FVECT v; |
99 |
|
{ |
100 |
< |
static double len; |
100 |
> |
register double len, d; |
101 |
|
|
102 |
< |
len = DOT(v, v); |
102 |
> |
d = DOT(v, v); |
103 |
|
|
104 |
< |
if (len <= 0.0) |
104 |
> |
if (d <= 0.0) |
105 |
|
return(0.0); |
106 |
|
|
107 |
< |
/****** problematic |
108 |
< |
if (len >= (1.0-FTINY)*(1.0-FTINY) && |
109 |
< |
len <= (1.0+FTINY)*(1.0+FTINY)) |
110 |
< |
return(1.0); |
101 |
< |
******/ |
107 |
> |
if (d <= 1.0+FTINY && d >= 1.0-FTINY) |
108 |
> |
len = 0.5 + 0.5*d; /* first order approximation */ |
109 |
> |
else |
110 |
> |
len = sqrt(d); |
111 |
|
|
112 |
< |
len = sqrt(len); |
113 |
< |
v[0] /= len; |
114 |
< |
v[1] /= len; |
115 |
< |
v[2] /= len; |
112 |
> |
v[0] *= d = 1.0/len; |
113 |
> |
v[1] *= d; |
114 |
> |
v[2] *= d; |
115 |
> |
|
116 |
|
return(len); |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
|
120 |
+ |
void |
121 |
+ |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
122 |
+ |
FVECT vres, vorig, vnorm; |
123 |
+ |
double theta; |
124 |
+ |
{ |
125 |
+ |
double sint, cost, normprod; |
126 |
+ |
FVECT vperp; |
127 |
+ |
register int i; |
128 |
+ |
|
129 |
+ |
if (theta == 0.0) { |
130 |
+ |
if (vres != vorig) |
131 |
+ |
VCOPY(vres, vorig); |
132 |
+ |
return; |
133 |
+ |
} |
134 |
+ |
cost = cos(theta); |
135 |
+ |
sint = sin(theta); |
136 |
+ |
normprod = DOT(vorig, vnorm)*(1.-cost); |
137 |
+ |
fcross(vperp, vnorm, vorig); |
138 |
+ |
for (i = 0; i < 3; i++) |
139 |
+ |
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint; |
140 |
|
} |