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 |
5 |
> |
* fvect.c - routines for floating-point vector calculations |
6 |
> |
*/ |
7 |
> |
|
8 |
> |
/* ==================================================================== |
9 |
> |
* The Radiance Software License, Version 1.0 |
10 |
|
* |
11 |
< |
* 8/14/85 |
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 |
|
*/ |
64 |
|
|
65 |
+ |
#include <math.h> |
66 |
|
#include "fvect.h" |
67 |
|
|
15 |
– |
#define FTINY 1e-7 |
68 |
|
|
17 |
– |
|
69 |
|
double |
70 |
|
fdot(v1, v2) /* return the dot product of two vectors */ |
71 |
|
register FVECT v1, v2; |
78 |
|
dist2(p1, p2) /* return square of distance between points */ |
79 |
|
register FVECT p1, p2; |
80 |
|
{ |
81 |
< |
static FVECT delta; |
81 |
> |
FVECT delta; |
82 |
|
|
83 |
|
delta[0] = p2[0] - p1[0]; |
84 |
|
delta[1] = p2[1] - p1[1]; |
85 |
|
delta[2] = p2[2] - p1[2]; |
86 |
+ |
|
87 |
|
return(DOT(delta, delta)); |
88 |
|
} |
89 |
|
|
93 |
|
FVECT p; /* the point */ |
94 |
|
FVECT ep1, ep2; /* points on the line */ |
95 |
|
{ |
96 |
< |
static double d, d1, d2; |
96 |
> |
register double d, d1, d2; |
97 |
|
|
98 |
|
d = dist2(ep1, ep2); |
99 |
|
d1 = dist2(ep1, p); |
100 |
< |
d2 = dist2(ep2, p); |
100 |
> |
d2 = d + d1 - dist2(ep2, p); |
101 |
|
|
102 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); |
102 |
> |
return(d1 - 0.25*d2*d2/d); |
103 |
|
} |
104 |
|
|
105 |
|
|
108 |
|
FVECT p; /* the point */ |
109 |
|
FVECT ep1, ep2; /* the end points */ |
110 |
|
{ |
111 |
< |
static double d, d1, d2; |
111 |
> |
register double d, d1, d2; |
112 |
|
|
113 |
|
d = dist2(ep1, ep2); |
114 |
|
d1 = dist2(ep1, p); |
121 |
|
if (d1 - d2 > d) |
122 |
|
return(d2); |
123 |
|
} |
124 |
+ |
d2 = d + d1 - d2; |
125 |
|
|
126 |
< |
return(d1 - (d+d1-d2)*(d+d1-d2)/d/4); /* distance to line */ |
126 |
> |
return(d1 - 0.25*d2*d2/d); /* distance to line */ |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
+ |
void |
131 |
|
fcross(vres, v1, v2) /* vres = v1 X v2 */ |
132 |
|
register FVECT vres, v1, v2; |
133 |
|
{ |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
+ |
void |
141 |
|
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
142 |
< |
FVECT vres, v0, v1; |
143 |
< |
double f; |
142 |
> |
register FVECT vres, v0, v1; |
143 |
> |
register double f; |
144 |
|
{ |
145 |
|
vres[0] = v0[0] + f*v1[0]; |
146 |
|
vres[1] = v0[1] + f*v1[1]; |
152 |
|
normalize(v) /* normalize a vector, return old magnitude */ |
153 |
|
register FVECT v; |
154 |
|
{ |
155 |
< |
static double len; |
155 |
> |
register double len, d; |
156 |
|
|
157 |
< |
len = DOT(v, v); |
157 |
> |
d = DOT(v, v); |
158 |
|
|
159 |
< |
if (len <= 0.0) |
159 |
> |
if (d <= 0.0) |
160 |
|
return(0.0); |
161 |
|
|
162 |
< |
/****** problematic |
163 |
< |
if (len >= (1.0-FTINY)*(1.0-FTINY) && |
164 |
< |
len <= (1.0+FTINY)*(1.0+FTINY)) |
165 |
< |
return(1.0); |
111 |
< |
******/ |
162 |
> |
if (d <= 1.0+FTINY && d >= 1.0-FTINY) |
163 |
> |
len = 0.5 + 0.5*d; /* first order approximation */ |
164 |
> |
else |
165 |
> |
len = sqrt(d); |
166 |
|
|
167 |
< |
len = sqrt(len); |
168 |
< |
v[0] /= len; |
169 |
< |
v[1] /= len; |
170 |
< |
v[2] /= len; |
167 |
> |
v[0] *= d = 1.0/len; |
168 |
> |
v[1] *= d; |
169 |
> |
v[2] *= d; |
170 |
> |
|
171 |
|
return(len); |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
+ |
void |
176 |
|
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
177 |
|
FVECT vres, vorig, vnorm; |
178 |
|
double theta; |
179 |
|
{ |
180 |
< |
extern double sin(), cos(); |
126 |
< |
double sint, cost, dotp; |
180 |
> |
double sint, cost, normprod; |
181 |
|
FVECT vperp; |
182 |
|
register int i; |
183 |
|
|
184 |
|
if (theta == 0.0) { |
185 |
< |
VCOPY(vres, vorig); |
185 |
> |
if (vres != vorig) |
186 |
> |
VCOPY(vres, vorig); |
187 |
|
return; |
188 |
|
} |
134 |
– |
sint = sin(theta); |
189 |
|
cost = cos(theta); |
190 |
< |
dotp = DOT(vorig, vnorm); |
190 |
> |
sint = sin(theta); |
191 |
> |
normprod = DOT(vorig, vnorm)*(1.-cost); |
192 |
|
fcross(vperp, vnorm, vorig); |
193 |
|
for (i = 0; i < 3; i++) |
194 |
< |
vres[i] = vnorm[i]*dotp*(1.-cost) + |
140 |
< |
vorig[i]*cost + vperp[i]*sint; |
194 |
> |
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint; |
195 |
|
} |