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.16 by greg, Thu Nov 8 00:31:17 2012 UTC vs.
Revision 2.22 by greg, Thu May 21 05:54:54 2015 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   const FVECT v1,
38   const FVECT v2
# Line 29 | Line 50 | const FVECT p2
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 87 | Line 106 | 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  
# Line 101 | Line 124 | const FVECT v1,
124   double f
125   )
126   {
127 <        vres[0] = v0[0] + f*v1[0];
105 <        vres[1] = v0[1] + f*v1[1];
106 <        vres[2] = v0[2] + f*v1[2];
127 >        VSUM(vres, v0, v1, f);
128   }
129  
130  
# Line 135 | Line 156 | FVECT  v
156  
157  
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 */
# Line 180 | Line 248 | double theta           /* right-hand radians */
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   }
# Line 195 | Line 263 | int meas               /* distance measure (radians, absolute, rela
263   )
264   {
265          FVECT   normtarg;
266 <        double  volen, dotprod, sint, cost;
266 >        double  volen, dotprod, sintr, cost;
267          int     i;
268  
269          VCOPY(normtarg, vtarg);         /* in case vtarg==vres */
# Line 222 | Line 290 | int meas               /* distance measure (radians, absolute, rela
290          else if (meas == GEOD_REL)
291                  t *= acos(dotprod);
292          cost = cos(t);
293 <        sint = sin(t);
293 >        sintr = sin(t) / sqrt(1. - dotprod*dotprod);
294          for (i = 0; i < 3; i++)
295                  vres[i] = volen*( cost*vres[i] +
296 <                                  sint*(normtarg[i] - dotprod*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