ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fvect.c
Revision: 2.7
Committed: Tue Feb 25 02:47:21 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.6: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
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;
17 {
18 return(DOT(v1,v2));
19 }
20
21
22 double
23 dist2(p1, p2) /* return square of distance between points */
24 register FVECT p1, p2;
25 {
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
35
36 double
37 dist2line(p, ep1, ep2) /* return square of distance to line */
38 FVECT p; /* the point */
39 FVECT ep1, ep2; /* points on the line */
40 {
41 register double d, d1, d2;
42
43 d = dist2(ep1, ep2);
44 d1 = dist2(ep1, p);
45 d2 = d + d1 - dist2(ep2, p);
46
47 return(d1 - 0.25*d2*d2/d);
48 }
49
50
51 double
52 dist2lseg(p, ep1, ep2) /* return square of distance to line segment */
53 FVECT p; /* the point */
54 FVECT ep1, ep2; /* the end points */
55 {
56 register double d, d1, d2;
57
58 d = dist2(ep1, ep2);
59 d1 = dist2(ep1, p);
60 d2 = dist2(ep2, p);
61
62 if (d2 > d1) { /* check if past endpoints */
63 if (d2 - d1 > d)
64 return(d1);
65 } else {
66 if (d1 - d2 > d)
67 return(d2);
68 }
69 d2 = d + d1 - d2;
70
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 {
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 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 register double len, d;
101
102 d = DOT(v, v);
103
104 if (d <= 0.0)
105 return(0.0);
106
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 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 }