| 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 |
|
#include <math.h> |
| 11 |
|
#include "fvect.h" |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
double |
| 15 |
< |
fdot(v1, v2) /* return the dot product of two vectors */ |
| 16 |
< |
register FVECT v1, v2; |
| 15 |
> |
fdot( /* return the dot product of two vectors */ |
| 16 |
> |
register FVECT v1, |
| 17 |
> |
register FVECT v2 |
| 18 |
> |
) |
| 19 |
|
{ |
| 20 |
|
return(DOT(v1,v2)); |
| 21 |
|
} |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
double |
| 25 |
< |
dist2(p1, p2) /* return square of distance between points */ |
| 26 |
< |
register FVECT p1, p2; |
| 25 |
> |
dist2( /* return square of distance between points */ |
| 26 |
> |
register FVECT p1, |
| 27 |
> |
register FVECT p2 |
| 28 |
> |
) |
| 29 |
|
{ |
| 30 |
|
FVECT delta; |
| 31 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
double |
| 41 |
< |
dist2line(p, ep1, ep2) /* return square of distance to line */ |
| 42 |
< |
FVECT p; /* the point */ |
| 43 |
< |
FVECT ep1, ep2; /* points on the line */ |
| 41 |
> |
dist2line( /* return square of distance to line */ |
| 42 |
> |
FVECT p, /* the point */ |
| 43 |
> |
FVECT ep1, |
| 44 |
> |
FVECT ep2 /* points on the line */ |
| 45 |
> |
) |
| 46 |
|
{ |
| 47 |
|
register double d, d1, d2; |
| 48 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
double |
| 58 |
< |
dist2lseg(p, ep1, ep2) /* return square of distance to line segment */ |
| 59 |
< |
FVECT p; /* the point */ |
| 60 |
< |
FVECT ep1, ep2; /* the end points */ |
| 58 |
> |
dist2lseg( /* return square of distance to line segment */ |
| 59 |
> |
FVECT p, /* the point */ |
| 60 |
> |
FVECT ep1, |
| 61 |
> |
FVECT ep2 /* the end points */ |
| 62 |
> |
) |
| 63 |
|
{ |
| 64 |
|
register double d, d1, d2; |
| 65 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
void |
| 84 |
< |
fcross(vres, v1, v2) /* vres = v1 X v2 */ |
| 85 |
< |
register FVECT vres, v1, v2; |
| 84 |
> |
fcross( /* vres = v1 X v2 */ |
| 85 |
> |
register FVECT vres, |
| 86 |
> |
register FVECT v1, |
| 87 |
> |
register FVECT v2 |
| 88 |
> |
) |
| 89 |
|
{ |
| 90 |
|
vres[0] = v1[1]*v2[2] - v1[2]*v2[1]; |
| 91 |
|
vres[1] = v1[2]*v2[0] - v1[0]*v2[2]; |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
void |
| 97 |
< |
fvsum(vres, v0, v1, f) /* vres = v0 + f*v1 */ |
| 98 |
< |
register FVECT vres, v0, v1; |
| 99 |
< |
register double f; |
| 97 |
> |
fvsum( /* vres = v0 + f*v1 */ |
| 98 |
> |
register FVECT vres, |
| 99 |
> |
register FVECT v0, |
| 100 |
> |
register FVECT v1, |
| 101 |
> |
register double f |
| 102 |
> |
) |
| 103 |
|
{ |
| 104 |
|
vres[0] = v0[0] + f*v1[0]; |
| 105 |
|
vres[1] = v0[1] + f*v1[1]; |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
double |
| 111 |
< |
normalize(v) /* normalize a vector, return old magnitude */ |
| 112 |
< |
register FVECT v; |
| 111 |
> |
normalize( /* normalize a vector, return old magnitude */ |
| 112 |
> |
register FVECT v |
| 113 |
> |
) |
| 114 |
|
{ |
| 115 |
|
register double len, d; |
| 116 |
|
|
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
|
| 135 |
+ |
int |
| 136 |
+ |
closestapproach( /* closest approach of two rays */ |
| 137 |
+ |
RREAL t[2], /* returned distances along each ray */ |
| 138 |
+ |
FVECT rorg0, /* first origin */ |
| 139 |
+ |
FVECT rdir0, /* first direction (normalized) */ |
| 140 |
+ |
FVECT rorg1, /* second origin */ |
| 141 |
+ |
FVECT rdir1 /* second direction (normalized) */ |
| 142 |
+ |
) |
| 143 |
+ |
{ |
| 144 |
+ |
double dotprod = DOT(rdir0, rdir1); |
| 145 |
+ |
double denom = 1. - dotprod*dotprod; |
| 146 |
+ |
double o1o2_d1; |
| 147 |
+ |
FVECT o0o1; |
| 148 |
+ |
|
| 149 |
+ |
if (denom <= FTINY) { /* check if lines are parallel */ |
| 150 |
+ |
t[0] = t[1] = 0.0; |
| 151 |
+ |
return(0); |
| 152 |
+ |
} |
| 153 |
+ |
VSUB(o0o1, rorg0, rorg1); |
| 154 |
+ |
o1o2_d1 = DOT(o0o1, rdir1); |
| 155 |
+ |
t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)) / denom; |
| 156 |
+ |
t[1] = o1o2_d1 + t[0]*dotprod; |
| 157 |
+ |
return(1); |
| 158 |
+ |
} |
| 159 |
+ |
|
| 160 |
+ |
|
| 161 |
+ |
#if 0 |
| 162 |
+ |
int |
| 163 |
+ |
closestapproach( /* closest approach of two rays */ |
| 164 |
+ |
RREAL t[2], /* returned distances along each ray */ |
| 165 |
+ |
FVECT rorg0, /* first origin */ |
| 166 |
+ |
FVECT rdir0, /* first direction (unnormalized) */ |
| 167 |
+ |
FVECT rorg1, /* second origin */ |
| 168 |
+ |
FVECT rdir1 /* second direction (unnormalized) */ |
| 169 |
+ |
) |
| 170 |
+ |
{ |
| 171 |
+ |
double dotprod = DOT(rdir0, rdir1); |
| 172 |
+ |
double d0n2 = DOT(rdir0, rdir0); |
| 173 |
+ |
double d1n2 = DOT(rdir1, rdir1); |
| 174 |
+ |
double denom = d0n2*d1n2 - dotprod*dotprod; |
| 175 |
+ |
double o1o2_d1; |
| 176 |
+ |
FVECT o0o1; |
| 177 |
+ |
|
| 178 |
+ |
if (denom <= FTINY) { /* check if lines are parallel */ |
| 179 |
+ |
t[0] = t[1] = 0.0; |
| 180 |
+ |
return(0); |
| 181 |
+ |
} |
| 182 |
+ |
VSUB(o0o1, rorg0, rorg1); |
| 183 |
+ |
o1o2_d1 = DOT(o0o1, rdir1); |
| 184 |
+ |
t[0] = (o1o2_d1*dotprod - DOT(o0o1,rdir0)*d1n2) / denom; |
| 185 |
+ |
t[1] = (o1o2_d1 + t[0]*dotprod) / d1n2; |
| 186 |
+ |
return(1); |
| 187 |
+ |
} |
| 188 |
+ |
#endif |
| 189 |
+ |
|
| 190 |
+ |
|
| 191 |
|
void |
| 192 |
< |
spinvector(vres, vorig, vnorm, theta) /* rotate vector around normal */ |
| 193 |
< |
FVECT vres, vorig, vnorm; |
| 194 |
< |
double theta; |
| 192 |
> |
spinvector( /* rotate vector around normal */ |
| 193 |
> |
FVECT vres, /* returned vector */ |
| 194 |
> |
FVECT vorig, /* original vector */ |
| 195 |
> |
FVECT vnorm, /* normalized vector for rotation */ |
| 196 |
> |
double theta /* left-hand radians */ |
| 197 |
> |
) |
| 198 |
|
{ |
| 199 |
|
double sint, cost, normprod; |
| 200 |
|
FVECT vperp; |