ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/fvect.c
Revision: 2.6
Committed: Sat Feb 22 02:07:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +61 -6 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
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 */
64
65 #include <math.h>
66 #include "fvect.h"
67
68
69 double
70 fdot(v1, v2) /* return the dot product of two vectors */
71 register FVECT v1, v2;
72 {
73 return(DOT(v1,v2));
74 }
75
76
77 double
78 dist2(p1, p2) /* return square of distance between points */
79 register FVECT p1, p2;
80 {
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
90
91 double
92 dist2line(p, ep1, ep2) /* return square of distance to line */
93 FVECT p; /* the point */
94 FVECT ep1, ep2; /* points on the line */
95 {
96 register double d, d1, d2;
97
98 d = dist2(ep1, ep2);
99 d1 = dist2(ep1, p);
100 d2 = d + d1 - dist2(ep2, p);
101
102 return(d1 - 0.25*d2*d2/d);
103 }
104
105
106 double
107 dist2lseg(p, ep1, ep2) /* return square of distance to line segment */
108 FVECT p; /* the point */
109 FVECT ep1, ep2; /* the end points */
110 {
111 register double d, d1, d2;
112
113 d = dist2(ep1, ep2);
114 d1 = dist2(ep1, p);
115 d2 = dist2(ep2, p);
116
117 if (d2 > d1) { /* check if past endpoints */
118 if (d2 - d1 > d)
119 return(d1);
120 } else {
121 if (d1 - d2 > d)
122 return(d2);
123 }
124 d2 = d + d1 - d2;
125
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 {
134 vres[0] = v1[1]*v2[2] - v1[2]*v2[1];
135 vres[1] = v1[2]*v2[0] - v1[0]*v2[2];
136 vres[2] = v1[0]*v2[1] - v1[1]*v2[0];
137 }
138
139
140 void
141 fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */
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];
147 vres[2] = v0[2] + f*v1[2];
148 }
149
150
151 double
152 normalize(v) /* normalize a vector, return old magnitude */
153 register FVECT v;
154 {
155 register double len, d;
156
157 d = DOT(v, v);
158
159 if (d <= 0.0)
160 return(0.0);
161
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 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 double sint, cost, normprod;
181 FVECT vperp;
182 register int i;
183
184 if (theta == 0.0) {
185 if (vres != vorig)
186 VCOPY(vres, vorig);
187 return;
188 }
189 cost = cos(theta);
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] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint;
195 }