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.6 by greg, Sat Feb 22 02:07:22 2003 UTC vs.
Revision 2.24 by greg, Thu Jul 23 18:22:26 2015 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  fvect.c - routines for floating-point vector calculations
6   */
7  
8 < /* ====================================================================
9 < * The Radiance Software License, Version 1.0
10 < *
11 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 < *
14 < * Redistribution and use in source and binary forms, with or without
15 < * modification, are permitted provided that the following conditions
16 < * are met:
17 < *
18 < * 1. Redistributions of source code must retain the above copyright
19 < *         notice, this list of conditions and the following disclaimer.
20 < *
21 < * 2. Redistributions in binary form must reproduce the above copyright
22 < *       notice, this list of conditions and the following disclaimer in
23 < *       the documentation and/or other materials provided with the
24 < *       distribution.
25 < *
26 < * 3. The end-user documentation included with the redistribution,
27 < *           if any, must include the following acknowledgment:
28 < *             "This product includes Radiance software
29 < *                 (http://radsite.lbl.gov/)
30 < *                 developed by the Lawrence Berkeley National Laboratory
31 < *               (http://www.lbl.gov/)."
32 < *       Alternately, this acknowledgment may appear in the software itself,
33 < *       if and wherever such third-party acknowledgments normally appear.
34 < *
35 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 < *       and "The Regents of the University of California" must
37 < *       not be used to endorse or promote products derived from this
38 < *       software without prior written permission. For written
39 < *       permission, please contact [email protected].
40 < *
41 < * 5. Products derived from this software may not be called "Radiance",
42 < *       nor may "Radiance" appear in their name, without prior written
43 < *       permission of Lawrence Berkeley National Laboratory.
44 < *
45 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 < * SUCH DAMAGE.
57 < * ====================================================================
58 < *
59 < * This software consists of voluntary contributions made by many
60 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 < * information on Lawrence Berkeley National Laboratory, please see
62 < * <http://www.lbl.gov/>.
63 < */
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 */
71 < 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          FVECT  delta;
52  
53 <        delta[0] = p2[0] - p1[0];
84 <        delta[1] = p2[1] - p1[1];
85 <        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 <        register double  d, d1, d2;
66 >        double  d, d1, d2;
67  
68          d = dist2(ep1, ep2);
69          d1 = dist2(ep1, p);
# Line 104 | Line 74 | FVECT  ep1, ep2;       /* points on the line */
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 <        register double  d, d1, d2;
83 >        double  d, d1, d2;
84  
85          d = dist2(ep1, ep2);
86          d1 = dist2(ep1, p);
# Line 128 | Line 100 | FVECT  ep1, ep2;       /* the end points */
100  
101  
102   void
103 < fcross(vres, v1, v2)            /* vres = v1 X v2 */
104 < register FVECT  vres, v1, v2;
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   void
120 < fvsum(vres, v0, v1, f)          /* vres = v0 + f*v1 */
121 < register FVECT  vres, v0, v1;
122 < register double  f;
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];
146 <        vres[1] = v0[1] + f*v1[1];
147 <        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 <        register double  len, d;
136 >        double  len, d;
137          
138          d = DOT(v, v);
139          
140 <        if (d <= 0.0)
140 >        if (d == 0.0)
141                  return(0.0);
142          
143 <        if (d <= 1.0+FTINY && d >= 1.0-FTINY)
143 >        if ((d <= 1.0+FTINY) & (d >= 1.0-FTINY)) {
144                  len = 0.5 + 0.5*d;      /* first order approximation */
145 <        else
145 >                d = 2.0 - len;
146 >        } else {
147                  len = sqrt(d);
148 <
149 <        v[0] *= d = 1.0/len;
148 >                d = 1.0/len;
149 >        }
150 >        v[0] *= d;
151          v[1] *= d;
152          v[2] *= d;
153  
# Line 172 | Line 155 | register FVECT  v;
155   }
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 ((int)(frandom()*6.)) {
174 +                case 0: ord[0] = 0; ord[1] = 1; ord[2] = 2; break;
175 +                case 1: ord[0] = 0; ord[1] = 2; ord[2] = 1; break;
176 +                case 2: ord[0] = 1; ord[1] = 0; ord[2] = 2; break;
177 +                case 3: ord[0] = 1; ord[1] = 2; ord[2] = 0; break;
178 +                case 4: ord[0] = 2; ord[1] = 0; ord[2] = 1; break;
179 +                default: ord[0] = 2; ord[1] = 1; ord[2] = 0; break;
180 +                }
181 +        } else {
182 +                v1[0] = v1[1] = v1[2] = 0.0;
183 +                ord[0] = 0; ord[1] = 1; ord[2] = 2;
184 +        }
185 +
186 +        for (i = 3; i--; )
187 +                if ((-0.6 < v[ord[i]]) & (v[ord[i]] < 0.6))
188 +                        break;
189 +        if (i < 0)
190 +                return(0);
191 +
192 +        v1[ord[i]] = 1.0;
193 +        fcross(vp, v1, v);
194 +
195 +        return(normalize(vp) > 0.0);
196 + }
197 +
198 +
199 + int
200 + closestapproach(                        /* closest approach of two rays */
201 + RREAL t[2],             /* returned distances along each ray */
202 + const FVECT rorg0,              /* first origin */
203 + const FVECT rdir0,              /* first direction (normalized) */
204 + const FVECT rorg1,              /* second origin */
205 + const FVECT rdir1               /* second direction (normalized) */
206 + )
207 + {
208 +        double  dotprod = DOT(rdir0, rdir1);
209 +        double  denom = 1. - dotprod*dotprod;
210 +        double  o1o2_d1;
211 +        FVECT   o0o1;
212 +
213 +        if (denom <= FTINY) {           /* check if lines are parallel */
214 +                t[0] = t[1] = 0.0;
215 +                return(0);
216 +        }
217 +        VSUB(o0o1, rorg0, rorg1);
218 +        o1o2_d1 = DOT(o0o1, rdir1);
219 +        t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom;
220 +        t[1] = o1o2_d1 + t[0]*dotprod;
221 +        return(1);
222 + }
223 +
224 +
225   void
226 < spinvector(vres, vorig, vnorm, theta)   /* rotate vector around normal */
227 < FVECT  vres, vorig, vnorm;
228 < double  theta;
226 > spinvector(                             /* rotate vector around normal */
227 > FVECT vres,             /* returned vector (same magnitude as vorig) */
228 > const FVECT vorig,              /* original vector */
229 > const FVECT vnorm,              /* normalized vector for rotation */
230 > double theta            /* right-hand radians */
231 > )
232   {
233          double  sint, cost, normprod;
234          FVECT  vperp;
235 <        register int  i;
235 >        int  i;
236          
237          if (theta == 0.0) {
238                  if (vres != vorig)
# Line 189 | Line 242 | double  theta;
242          cost = cos(theta);
243          sint = sin(theta);
244          normprod = DOT(vorig, vnorm)*(1.-cost);
245 <        fcross(vperp, vnorm, vorig);
245 >        VCROSS(vperp, vnorm, vorig);
246          for (i = 0; i < 3; i++)
247                  vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint;
248 + }
249 +
250 + double
251 + geodesic(               /* rotate vector on great circle towards target */
252 + FVECT vres,             /* returned vector (same magnitude as vorig) */
253 + const FVECT vorig,      /* original vector */
254 + const FVECT vtarg,      /* vector we are rotating towards */
255 + double t,               /* amount along arc directed towards vtarg */
256 + int meas                /* distance measure (radians, absolute, relative) */
257 + )
258 + {
259 +        FVECT   normtarg;
260 +        double  volen, dotprod, sintr, cost;
261 +        int     i;
262 +
263 +        VCOPY(normtarg, vtarg);         /* in case vtarg==vres */
264 +        if (vres != vorig)
265 +                VCOPY(vres, vorig);
266 +        if (t == 0.0)
267 +                return(VLEN(vres));     /* no rotation requested */
268 +        if ((volen = normalize(vres)) == 0.0)
269 +                return(0.0);
270 +        if (normalize(normtarg) == 0.0)
271 +                return(0.0);            /* target vector is zero */
272 +        dotprod = DOT(vres, normtarg);
273 +                                        /* check for colinear */
274 +        if (dotprod >= 1.0-FTINY*FTINY) {
275 +                if (meas != GEOD_REL)
276 +                        return(0.0);
277 +                vres[0] *= volen; vres[1] *= volen; vres[2] *= volen;
278 +                return(volen);
279 +        }
280 +        if (dotprod <= -1.0+FTINY*FTINY)
281 +                return(0.0);
282 +        if (meas == GEOD_ABS)
283 +                t /= volen;
284 +        else if (meas == GEOD_REL)
285 +                t *= acos(dotprod);
286 +        cost = cos(t);
287 +        sintr = sin(t) / sqrt(1. - dotprod*dotprod);
288 +        for (i = 0; i < 3; i++)
289 +                vres[i] = volen*( cost*vres[i] +
290 +                                  sintr*(normtarg[i] - dotprod*vres[i]) );
291 +
292 +        return(volen);                  /* return vector length */
293   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines