ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fvect.c
Revision: 2.3
Committed: Fri Dec 10 09:53:30 1993 UTC (30 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +5 -6 lines
Log Message:
added first order approximation for sqrt() to normalize()

File Contents

# Content
1 /* Copyright (c) 1986 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * fvect.c - routines for float vector calculations
9 *
10 * 8/14/85
11 */
12
13 #include <math.h>
14 #include "fvect.h"
15
16
17 double
18 fdot(v1, v2) /* return the dot product of two vectors */
19 register FVECT v1, v2;
20 {
21 return(DOT(v1,v2));
22 }
23
24
25 double
26 dist2(p1, p2) /* return square of distance between points */
27 register FVECT p1, p2;
28 {
29 static FVECT delta;
30
31 delta[0] = p2[0] - p1[0];
32 delta[1] = p2[1] - p1[1];
33 delta[2] = p2[2] - p1[2];
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 */
42 {
43 static double d, d1, d2;
44
45 d = dist2(ep1, ep2);
46 d1 = dist2(ep1, p);
47 d2 = dist2(ep2, p);
48
49 return(d1 - (d+d1-d2)*(d+d1-d2)/d/4);
50 }
51
52
53 double
54 dist2lseg(p, ep1, ep2) /* return square of distance to line segment */
55 FVECT p; /* the point */
56 FVECT ep1, ep2; /* the end points */
57 {
58 static double d, d1, d2;
59
60 d = dist2(ep1, ep2);
61 d1 = dist2(ep1, p);
62 d2 = dist2(ep2, p);
63
64 if (d2 > d1) { /* check if past endpoints */
65 if (d2 - d1 > d)
66 return(d1);
67 } else {
68 if (d1 - d2 > d)
69 return(d2);
70 }
71
72 return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); /* distance to line */
73 }
74
75
76 fcross(vres, v1, v2) /* vres = v1 X v2 */
77 register FVECT vres, v1, v2;
78 {
79 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];
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;
98 {
99 static double len;
100
101 len = DOT(v, v);
102
103 if (len <= 0.0)
104 return(0.0);
105
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
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 }