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.9 by greg, Wed Mar 23 01:54:55 2005 UTC vs.
Revision 2.20 by greg, Thu Dec 4 05:26:27 2014 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
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 + 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 < register FVECT v1,
38 < register FVECT v2
37 > const FVECT v1,
38 > const FVECT v2
39   )
40   {
41          return(DOT(v1,v2));
# Line 23 | Line 44 | register FVECT v2
44  
45   double
46   dist2(                          /* return square of distance between points */
47 < register FVECT p1,
48 < register FVECT p2
47 > const FVECT p1,
48 > const FVECT p2
49   )
50   {
51          FVECT  delta;
52  
53 <        delta[0] = p2[0] - p1[0];
33 <        delta[1] = p2[1] - p1[1];
34 <        delta[2] = p2[2] - p1[2];
53 >        VSUB(delta, p2, p1);
54  
55          return(DOT(delta, delta));
56   }
# Line 39 | Line 58 | register FVECT p2
58  
59   double
60   dist2line(                      /* return square of distance to line */
61 < FVECT p,                /* the point */
62 < FVECT ep1,
63 < FVECT ep2               /* points on the line */
61 > const FVECT p,          /* the point */
62 > const FVECT ep1,
63 > const FVECT ep2         /* points on the line */
64   )
65   {
66 <        register double  d, d1, d2;
66 >        double  d, d1, d2;
67  
68          d = dist2(ep1, ep2);
69          d1 = dist2(ep1, p);
# Line 56 | Line 75 | FVECT ep2              /* points on the line */
75  
76   double
77   dist2lseg(                      /* return square of distance to line segment */
78 < FVECT p,                /* the point */
79 < FVECT ep1,
80 < FVECT ep2               /* the end points */
78 > const FVECT p,          /* the point */
79 > const FVECT ep1,
80 > const FVECT ep2         /* the end points */
81   )
82   {
83 <        register double  d, d1, d2;
83 >        double  d, d1, d2;
84  
85          d = dist2(ep1, ep2);
86          d1 = dist2(ep1, p);
# Line 82 | Line 101 | FVECT ep2              /* the end points */
101  
102   void
103   fcross(                         /* vres = v1 X v2 */
104 < register FVECT vres,
105 < register FVECT v1,
106 < register FVECT 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];
91 <        vres[1] = v1[2]*v2[0] - v1[0]*v2[2];
92 <        vres[2] = v1[0]*v2[1] - v1[1]*v2[0];
109 >        VCROSS(vres, v1, v2);
110   }
111  
112  
113   void
114   fvsum(                          /* vres = v0 + f*v1 */
115 < register FVECT vres,
116 < register FVECT v0,
117 < register FVECT v1,
118 < register double f
115 > FVECT vres,
116 > const FVECT v0,
117 > const FVECT v1,
118 > double f
119   )
120   {
121 <        vres[0] = v0[0] + f*v1[0];
105 <        vres[1] = v0[1] + f*v1[1];
106 <        vres[2] = v0[2] + f*v1[2];
121 >        VSUM(vres, v0, v1, f);
122   }
123  
124  
125   double
126   normalize(                      /* normalize a vector, return old magnitude */
127 < register FVECT  v
127 > FVECT  v
128   )
129   {
130 <        register double  len, d;
130 >        double  len, d;
131          
132          d = DOT(v, v);
133          
134 <        if (d <= FTINY*FTINY)
134 >        if (d == 0.0)
135                  return(0.0);
136          
137 <        if (d <= 1.0+FTINY && d >= 1.0-FTINY)
137 >        if ((d <= 1.0+FTINY) & (d >= 1.0-FTINY)) {
138                  len = 0.5 + 0.5*d;      /* first order approximation */
139 <        else
139 >                d = 2.0 - len;
140 >        } else {
141                  len = sqrt(d);
142 <
143 <        v[0] *= d = 1.0/len;
142 >                d = 1.0/len;
143 >        }
144 >        v[0] *= d;
145          v[1] *= d;
146          v[2] *= d;
147  
# Line 133 | Line 150 | register FVECT  v
150  
151  
152   int
153 < closestapproach(                        /* closest approach of two rays */
154 < RREAL t[2],             /* returned distances along each ray */
155 < FVECT rorg0,            /* first origin */
139 < FVECT rdir0,            /* first direction (normalized) */
140 < FVECT rorg1,            /* second origin */
141 < FVECT rdir1             /* second direction (normalized) */
153 > getperpendicular(               /* choose random perpedicular direction */
154 >        FVECT vp,                       /* returns normalized */
155 >        const FVECT v                   /* input vector must be normalized */
156   )
157   {
158 <        double  dotprod = DOT(rdir0, rdir1);
159 <        double  denom = 1. - dotprod*dotprod;
160 <        double  o1o2_d1;
161 <        FVECT   o0o1;
162 <
163 <        if (denom <= FTINY) {           /* check if lines are parallel */
164 <                t[0] = t[1] = 0.0;
158 >        FVECT   v1;
159 >        int     i;
160 >                                        /* randomize other coordinates */
161 >        v1[0] = 0.5 - frandom();
162 >        v1[1] = 0.5 - frandom();
163 >        v1[2] = 0.5 - frandom();
164 >        for (i = 3; i--; )
165 >                if ((-0.6 < v[i]) & (v[i] < 0.6))
166 >                        break;
167 >        if (i < 0)
168                  return(0);
169 <        }
170 <        VSUB(o0o1, rorg0, rorg1);
171 <        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);
169 >        v1[i] = 1.0;
170 >        VCROSS(vp, v1, v);
171 >        return(normalize(vp) > 0.0);
172   }
173  
160
161 #if 0
174   int
175   closestapproach(                        /* closest approach of two rays */
176   RREAL t[2],             /* returned distances along each ray */
177 < FVECT rorg0,            /* first origin */
178 < FVECT rdir0,            /* first direction (unnormalized) */
179 < FVECT rorg1,            /* second origin */
180 < FVECT rdir1             /* second direction (unnormalized) */
177 > const FVECT rorg0,              /* first origin */
178 > const FVECT rdir0,              /* first direction (normalized) */
179 > const FVECT rorg1,              /* second origin */
180 > const FVECT rdir1               /* second direction (normalized) */
181   )
182   {
183          double  dotprod = DOT(rdir0, rdir1);
184 <        double  d0n2 = DOT(rdir0, rdir0);
173 <        double  d1n2 = DOT(rdir1, rdir1);
174 <        double  denom = d0n2*d1n2 - dotprod*dotprod;
184 >        double  denom = 1. - dotprod*dotprod;
185          double  o1o2_d1;
186          FVECT   o0o1;
187  
# Line 181 | Line 191 | FVECT rdir1            /* second direction (unnormalized) */
191          }
192          VSUB(o0o1, rorg0, rorg1);
193          o1o2_d1 = DOT(o0o1, rdir1);
194 <        t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)*d1n2) / denom;
195 <        t[1] = (o1o2_d1 + t[0]*dotprod) / d1n2;
194 >        t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom;
195 >        t[1] = o1o2_d1 + t[0]*dotprod;
196          return(1);
197   }
188 #endif
198  
199  
200   void
201   spinvector(                             /* rotate vector around normal */
202 < FVECT vres,             /* returned vector */
203 < FVECT vorig,            /* original vector */
204 < FVECT vnorm,            /* normalized vector for rotation */
205 < double theta            /* left-hand radians */
202 > FVECT vres,             /* returned vector (same magnitude as vorig) */
203 > const FVECT vorig,              /* original vector */
204 > const FVECT vnorm,              /* normalized vector for rotation */
205 > double theta            /* right-hand radians */
206   )
207   {
208          double  sint, cost, normprod;
209          FVECT  vperp;
210 <        register int  i;
210 >        int  i;
211          
212          if (theta == 0.0) {
213                  if (vres != vorig)
# Line 208 | Line 217 | double theta           /* left-hand radians */
217          cost = cos(theta);
218          sint = sin(theta);
219          normprod = DOT(vorig, vnorm)*(1.-cost);
220 <        fcross(vperp, vnorm, vorig);
220 >        VCROSS(vperp, vnorm, vorig);
221          for (i = 0; i < 3; i++)
222                  vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint;
223 + }
224 +
225 + double
226 + geodesic(               /* rotate vector on great circle towards target */
227 + FVECT vres,             /* returned vector (same magnitude as vorig) */
228 + const FVECT vorig,      /* original vector */
229 + const FVECT vtarg,      /* vector we are rotating towards */
230 + double t,               /* amount along arc directed towards vtarg */
231 + int meas                /* distance measure (radians, absolute, relative) */
232 + )
233 + {
234 +        FVECT   normtarg;
235 +        double  volen, dotprod, sintr, cost;
236 +        int     i;
237 +
238 +        VCOPY(normtarg, vtarg);         /* in case vtarg==vres */
239 +        if (vres != vorig)
240 +                VCOPY(vres, vorig);
241 +        if (t == 0.0)
242 +                return(VLEN(vres));     /* no rotation requested */
243 +        if ((volen = normalize(vres)) == 0.0)
244 +                return(0.0);
245 +        if (normalize(normtarg) == 0.0)
246 +                return(0.0);            /* target vector is zero */
247 +        dotprod = DOT(vres, normtarg);
248 +                                        /* check for colinear */
249 +        if (dotprod >= 1.0-FTINY*FTINY) {
250 +                if (meas != GEOD_REL)
251 +                        return(0.0);
252 +                vres[0] *= volen; vres[1] *= volen; vres[2] *= volen;
253 +                return(volen);
254 +        }
255 +        if (dotprod <= -1.0+FTINY*FTINY)
256 +                return(0.0);
257 +        if (meas == GEOD_ABS)
258 +                t /= volen;
259 +        else if (meas == GEOD_REL)
260 +                t *= acos(dotprod);
261 +        cost = cos(t);
262 +        sintr = sin(t) / sqrt(1. - dotprod*dotprod);
263 +        for (i = 0; i < 3; i++)
264 +                vres[i] = volen*( cost*vres[i] +
265 +                                  sintr*(normtarg[i] - dotprod*vres[i]) );
266 +
267 +        return(volen);                  /* return vector length */
268   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines