ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fvect.c
(Generate patch)

Comparing ray/src/common/fvect.c (file contents):
Revision 2.2 by greg, Fri Oct 2 16:02:43 1992 UTC vs.
Revision 2.22 by greg, Thu May 21 05:54:54 2015 UTC

# Line 1 | Line 1
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 + #define _USE_MATH_DEFINES
11   #include  <math.h>
12   #include  "fvect.h"
13 + #include  "random.h"
14  
15 + double
16 + Acos(double x)                  /* insurance for touchy math library */
17 + {
18 +        if (x <= -1.+FTINY*FTINY)
19 +                return(M_PI);
20 +        if (x >= 1.-FTINY*FTINY)
21 +                return(.0);
22 +        return(acos(x));
23 + }
24  
25   double
26 < fdot(v1, v2)                    /* return the dot product of two vectors */
19 < register FVECT  v1, v2;
26 > Asin(double x)                  /* insurance for touchy math library */
27   {
28 +        if (x <= -1.+FTINY*FTINY)
29 +                return(-M_PI/2.);
30 +        if (x >= 1.-FTINY*FTINY)
31 +                return(M_PI/2);
32 +        return(asin(x));
33 + }
34 +
35 + double
36 + fdot(                           /* return the dot product of two vectors */
37 + const FVECT v1,
38 + const FVECT v2
39 + )
40 + {
41          return(DOT(v1,v2));
42   }
43  
44  
45   double
46 < dist2(p1, p2)                   /* return square of distance between points */
47 < register FVECT  p1, p2;
46 > dist2(                          /* return square of distance between points */
47 > const FVECT p1,
48 > const FVECT p2
49 > )
50   {
51 <        static FVECT  delta;
51 >        FVECT  delta;
52  
53 <        delta[0] = p2[0] - p1[0];
54 <        delta[1] = p2[1] - p1[1];
33 <        delta[2] = p2[2] - p1[2];
53 >        VSUB(delta, p2, p1);
54 >
55          return(DOT(delta, delta));
56   }
57  
58  
59   double
60 < dist2line(p, ep1, ep2)          /* return square of distance to line */
61 < FVECT  p;               /* the point */
62 < FVECT  ep1, ep2;        /* points on the line */
60 > dist2line(                      /* return square of distance to line */
61 > const FVECT p,          /* the point */
62 > const FVECT ep1,
63 > const FVECT ep2         /* points on the line */
64 > )
65   {
66 <        static double  d, d1, d2;
66 >        double  d, d1, d2;
67  
68          d = dist2(ep1, ep2);
69          d1 = dist2(ep1, p);
70 <        d2 = dist2(ep2, p);
70 >        d2 = d + d1 - dist2(ep2, p);
71  
72 <        return(d1 - (d+d1-d2)*(d+d1-d2)/d/4);
72 >        return(d1 - 0.25*d2*d2/d);
73   }
74  
75  
76   double
77 < dist2lseg(p, ep1, ep2)          /* return square of distance to line segment */
78 < FVECT  p;               /* the point */
79 < FVECT  ep1, ep2;        /* the end points */
77 > dist2lseg(                      /* return square of distance to line segment */
78 > const FVECT p,          /* the point */
79 > const FVECT ep1,
80 > const FVECT ep2         /* the end points */
81 > )
82   {
83 <        static double  d, d1, d2;
83 >        double  d, d1, d2;
84  
85          d = dist2(ep1, ep2);
86          d1 = dist2(ep1, p);
# Line 68 | Line 93 | FVECT  ep1, ep2;       /* the end points */
93                  if (d1 - d2 > d)
94                          return(d2);
95          }
96 +        d2 = d + d1 - d2;
97  
98 <        return(d1 - (d+d1-d2)*(d+d1-d2)/d/4);   /* distance to line */
98 >        return(d1 - 0.25*d2*d2/d);      /* distance to line */
99   }
100  
101  
102 < fcross(vres, v1, v2)            /* vres = v1 X v2 */
103 < register FVECT  vres, v1, v2;
102 > void
103 > fcross(                         /* vres = v1 X v2 */
104 > FVECT vres,
105 > const FVECT v1,
106 > const FVECT v2
107 > )
108   {
109 <        vres[0] = v1[1]*v2[2] - v1[2]*v2[1];
110 <        vres[1] = v1[2]*v2[0] - v1[0]*v2[2];
111 <        vres[2] = v1[0]*v2[1] - v1[1]*v2[0];
109 >        if ((vres == v1) | (vres == v2)) {
110 >                FVECT   vtmp;
111 >                VCROSS(vtmp, v1, v2);
112 >                VCOPY(vres, vtmp);
113 >                return;
114 >        }
115 >        VCROSS(vres, v1, v2);
116   }
117  
118  
119 < fvsum(vres, v0, v1, f)          /* vres = v0 + f*v1 */
120 < FVECT  vres, v0, v1;
121 < double  f;
119 > void
120 > fvsum(                          /* vres = v0 + f*v1 */
121 > FVECT vres,
122 > const FVECT v0,
123 > const FVECT v1,
124 > double f
125 > )
126   {
127 <        vres[0] = v0[0] + f*v1[0];
90 <        vres[1] = v0[1] + f*v1[1];
91 <        vres[2] = v0[2] + f*v1[2];
127 >        VSUM(vres, v0, v1, f);
128   }
129  
130  
131   double
132 < normalize(v)                    /* normalize a vector, return old magnitude */
133 < register FVECT  v;
132 > normalize(                      /* normalize a vector, return old magnitude */
133 > FVECT  v
134 > )
135   {
136 <        static double  len;
136 >        double  len, d;
137          
138 <        len = DOT(v, v);
138 >        d = DOT(v, v);
139          
140 <        if (len <= 0.0)
140 >        if (d == 0.0)
141                  return(0.0);
142          
143 <        /****** problematic
144 <        if (len >= (1.0-FTINY)*(1.0-FTINY) &&
145 <                        len <= (1.0+FTINY)*(1.0+FTINY))
146 <                return(1.0);
147 <        ******/
143 >        if ((d <= 1.0+FTINY) & (d >= 1.0-FTINY)) {
144 >                len = 0.5 + 0.5*d;      /* first order approximation */
145 >                d = 2.0 - len;
146 >        } else {
147 >                len = sqrt(d);
148 >                d = 1.0/len;
149 >        }
150 >        v[0] *= d;
151 >        v[1] *= d;
152 >        v[2] *= d;
153  
112        len = sqrt(len);
113        v[0] /= len;
114        v[1] /= len;
115        v[2] /= len;
154          return(len);
155   }
156  
157  
158 < spinvector(vres, vorig, vnorm, theta)   /* rotate vector around normal */
159 < FVECT  vres, vorig, vnorm;
160 < double  theta;
158 > int
159 > getperpendicular(               /* choose perpedicular direction */
160 > FVECT vp,                               /* returns normalized */
161 > const FVECT v,                          /* input vector must be normalized */
162 > int randomize                           /* randomize orientation */
163 > )
164   {
165 +        int     ord[3];
166 +        FVECT   v1;
167 +        int     i;
168 +
169 +        if (randomize) {                /* randomize coordinates? */
170 +                v1[0] = 0.5 - frandom();
171 +                v1[1] = 0.5 - frandom();
172 +                v1[2] = 0.5 - frandom();
173 +                switch (ord[0] = (int)(frandom()*2.99999)) {
174 +                case 0:
175 +                        ord[1] = 1 + (frandom() > .5);
176 +                        ord[2] = 2 - ord[1];
177 +                        break;
178 +                case 1:
179 +                        ord[1] = 2*(frandom() > .5);
180 +                        ord[2] = 2 - ord[1];
181 +                        break;
182 +                case 2:
183 +                        ord[1] = (frandom() > .5);
184 +                        ord[2] = 1 - ord[1];
185 +                        break;
186 +                }
187 +        } else {
188 +                v1[0] = v1[1] = v1[2] = .0;
189 +                ord[0] = 0; ord[1] = 1; ord[2] = 2;
190 +        }
191 +
192 +        for (i = 3; i--; )
193 +                if ((-0.6 < v[ord[i]]) & (v[ord[i]] < 0.6))
194 +                        break;
195 +        if (i < 0)
196 +                return(0);
197 +
198 +        v1[ord[i]] = 1.0;
199 +        fcross(vp, v1, v);
200 +
201 +        return(normalize(vp) > 0.0);
202 + }
203 +
204 +
205 + int
206 + closestapproach(                        /* closest approach of two rays */
207 + RREAL t[2],             /* returned distances along each ray */
208 + const FVECT rorg0,              /* first origin */
209 + const FVECT rdir0,              /* first direction (normalized) */
210 + const FVECT rorg1,              /* second origin */
211 + const FVECT rdir1               /* second direction (normalized) */
212 + )
213 + {
214 +        double  dotprod = DOT(rdir0, rdir1);
215 +        double  denom = 1. - dotprod*dotprod;
216 +        double  o1o2_d1;
217 +        FVECT   o0o1;
218 +
219 +        if (denom <= FTINY) {           /* check if lines are parallel */
220 +                t[0] = t[1] = 0.0;
221 +                return(0);
222 +        }
223 +        VSUB(o0o1, rorg0, rorg1);
224 +        o1o2_d1 = DOT(o0o1, rdir1);
225 +        t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom;
226 +        t[1] = o1o2_d1 + t[0]*dotprod;
227 +        return(1);
228 + }
229 +
230 +
231 + void
232 + spinvector(                             /* rotate vector around normal */
233 + FVECT vres,             /* returned vector (same magnitude as vorig) */
234 + const FVECT vorig,              /* original vector */
235 + const FVECT vnorm,              /* normalized vector for rotation */
236 + double theta            /* right-hand radians */
237 + )
238 + {
239          double  sint, cost, normprod;
240          FVECT  vperp;
241 <        register int  i;
241 >        int  i;
242          
243          if (theta == 0.0) {
244                  if (vres != vorig)
# Line 133 | Line 248 | double  theta;
248          cost = cos(theta);
249          sint = sin(theta);
250          normprod = DOT(vorig, vnorm)*(1.-cost);
251 <        fcross(vperp, vnorm, vorig);
251 >        VCROSS(vperp, vnorm, vorig);
252          for (i = 0; i < 3; i++)
253                  vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint;
254 + }
255 +
256 + double
257 + geodesic(               /* rotate vector on great circle towards target */
258 + FVECT vres,             /* returned vector (same magnitude as vorig) */
259 + const FVECT vorig,      /* original vector */
260 + const FVECT vtarg,      /* vector we are rotating towards */
261 + double t,               /* amount along arc directed towards vtarg */
262 + int meas                /* distance measure (radians, absolute, relative) */
263 + )
264 + {
265 +        FVECT   normtarg;
266 +        double  volen, dotprod, sintr, cost;
267 +        int     i;
268 +
269 +        VCOPY(normtarg, vtarg);         /* in case vtarg==vres */
270 +        if (vres != vorig)
271 +                VCOPY(vres, vorig);
272 +        if (t == 0.0)
273 +                return(VLEN(vres));     /* no rotation requested */
274 +        if ((volen = normalize(vres)) == 0.0)
275 +                return(0.0);
276 +        if (normalize(normtarg) == 0.0)
277 +                return(0.0);            /* target vector is zero */
278 +        dotprod = DOT(vres, normtarg);
279 +                                        /* check for colinear */
280 +        if (dotprod >= 1.0-FTINY*FTINY) {
281 +                if (meas != GEOD_REL)
282 +                        return(0.0);
283 +                vres[0] *= volen; vres[1] *= volen; vres[2] *= volen;
284 +                return(volen);
285 +        }
286 +        if (dotprod <= -1.0+FTINY*FTINY)
287 +                return(0.0);
288 +        if (meas == GEOD_ABS)
289 +                t /= volen;
290 +        else if (meas == GEOD_REL)
291 +                t *= acos(dotprod);
292 +        cost = cos(t);
293 +        sintr = sin(t) / sqrt(1. - dotprod*dotprod);
294 +        for (i = 0; i < 3; i++)
295 +                vres[i] = volen*( cost*vres[i] +
296 +                                  sintr*(normtarg[i] - dotprod*vres[i]) );
297 +
298 +        return(volen);                  /* return vector length */
299   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines