| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
2.6 |
static const char RCSid[] = "$Id$";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
greg |
2.6 |
/*
|
| 5 |
|
|
* fvect.c - routines for floating-point vector calculations
|
| 6 |
|
|
*/
|
| 7 |
greg |
1.1 |
|
| 8 |
greg |
2.6 |
/* ====================================================================
|
| 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 |
greg |
1.1 |
*
|
| 59 |
greg |
2.6 |
* 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 |
greg |
1.1 |
*/
|
| 64 |
|
|
|
| 65 |
greg |
2.2 |
#include <math.h>
|
| 66 |
greg |
1.1 |
#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 |
gwlarson |
2.4 |
FVECT delta;
|
| 82 |
greg |
1.1 |
|
| 83 |
|
|
delta[0] = p2[0] - p1[0];
|
| 84 |
|
|
delta[1] = p2[1] - p1[1];
|
| 85 |
|
|
delta[2] = p2[2] - p1[2];
|
| 86 |
gwlarson |
2.5 |
|
| 87 |
greg |
1.1 |
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 |
gwlarson |
2.4 |
register double d, d1, d2;
|
| 97 |
greg |
1.1 |
|
| 98 |
|
|
d = dist2(ep1, ep2);
|
| 99 |
|
|
d1 = dist2(ep1, p);
|
| 100 |
gwlarson |
2.5 |
d2 = d + d1 - dist2(ep2, p);
|
| 101 |
greg |
1.1 |
|
| 102 |
gwlarson |
2.5 |
return(d1 - 0.25*d2*d2/d);
|
| 103 |
greg |
1.1 |
}
|
| 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 |
gwlarson |
2.4 |
register double d, d1, d2;
|
| 112 |
greg |
1.1 |
|
| 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 |
gwlarson |
2.5 |
d2 = d + d1 - d2;
|
| 125 |
greg |
1.1 |
|
| 126 |
gwlarson |
2.5 |
return(d1 - 0.25*d2*d2/d); /* distance to line */
|
| 127 |
greg |
1.1 |
}
|
| 128 |
|
|
|
| 129 |
|
|
|
| 130 |
greg |
2.6 |
void
|
| 131 |
greg |
1.1 |
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 |
greg |
2.6 |
void
|
| 141 |
greg |
1.4 |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */
|
| 142 |
gwlarson |
2.5 |
register FVECT vres, v0, v1;
|
| 143 |
|
|
register double f;
|
| 144 |
greg |
1.4 |
{
|
| 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 |
greg |
1.1 |
double
|
| 152 |
|
|
normalize(v) /* normalize a vector, return old magnitude */
|
| 153 |
|
|
register FVECT v;
|
| 154 |
|
|
{
|
| 155 |
gwlarson |
2.5 |
register double len, d;
|
| 156 |
greg |
1.1 |
|
| 157 |
gwlarson |
2.5 |
d = DOT(v, v);
|
| 158 |
greg |
1.1 |
|
| 159 |
gwlarson |
2.5 |
if (d <= 0.0)
|
| 160 |
greg |
1.1 |
return(0.0);
|
| 161 |
|
|
|
| 162 |
gwlarson |
2.5 |
if (d <= 1.0+FTINY && d >= 1.0-FTINY)
|
| 163 |
|
|
len = 0.5 + 0.5*d; /* first order approximation */
|
| 164 |
greg |
2.3 |
else
|
| 165 |
gwlarson |
2.5 |
len = sqrt(d);
|
| 166 |
greg |
1.1 |
|
| 167 |
gwlarson |
2.5 |
v[0] *= d = 1.0/len;
|
| 168 |
|
|
v[1] *= d;
|
| 169 |
|
|
v[2] *= d;
|
| 170 |
greg |
2.3 |
|
| 171 |
greg |
1.1 |
return(len);
|
| 172 |
|
|
}
|
| 173 |
greg |
1.5 |
|
| 174 |
|
|
|
| 175 |
greg |
2.6 |
void
|
| 176 |
greg |
1.5 |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */
|
| 177 |
|
|
FVECT vres, vorig, vnorm;
|
| 178 |
|
|
double theta;
|
| 179 |
|
|
{
|
| 180 |
greg |
1.6 |
double sint, cost, normprod;
|
| 181 |
greg |
1.5 |
FVECT vperp;
|
| 182 |
|
|
register int i;
|
| 183 |
|
|
|
| 184 |
|
|
if (theta == 0.0) {
|
| 185 |
greg |
1.6 |
if (vres != vorig)
|
| 186 |
|
|
VCOPY(vres, vorig);
|
| 187 |
greg |
1.5 |
return;
|
| 188 |
|
|
}
|
| 189 |
greg |
1.6 |
cost = cos(theta);
|
| 190 |
greg |
1.5 |
sint = sin(theta);
|
| 191 |
greg |
1.6 |
normprod = DOT(vorig, vnorm)*(1.-cost);
|
| 192 |
greg |
1.5 |
fcross(vperp, vnorm, vorig);
|
| 193 |
|
|
for (i = 0; i < 3; i++)
|
| 194 |
greg |
1.6 |
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint;
|
| 195 |
greg |
1.5 |
}
|