| 7 |
|
|
| 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 |
+ |
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 |
| 50 |
|
{ |
| 51 |
|
FVECT delta; |
| 52 |
|
|
| 53 |
< |
delta[0] = p2[0] - p1[0]; |
| 33 |
< |
delta[1] = p2[1] - p1[1]; |
| 34 |
< |
delta[2] = p2[2] - p1[2]; |
| 53 |
> |
VSUB(delta, p2, p1); |
| 54 |
|
|
| 55 |
|
return(DOT(delta, delta)); |
| 56 |
|
} |
| 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 |
|
|
| 124 |
|
double f |
| 125 |
|
) |
| 126 |
|
{ |
| 127 |
< |
vres[0] = v0[0] + f*v1[0]; |
| 105 |
< |
vres[1] = v0[1] + f*v1[1]; |
| 106 |
< |
vres[2] = v0[2] + f*v1[2]; |
| 127 |
> |
VSUM(vres, v0, v1, f); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
|
| 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 |
|
d = 2.0 - len; |
| 146 |
|
} else { |
| 156 |
|
|
| 157 |
|
|
| 158 |
|
int |
| 159 |
+ |
getperpendicular( /* choose random perpedicular direction */ |
| 160 |
+ |
FVECT vp, /* returns normalized */ |
| 161 |
+ |
const FVECT v /* input vector must be normalized */ |
| 162 |
+ |
) |
| 163 |
+ |
{ |
| 164 |
+ |
FVECT v1; |
| 165 |
+ |
int i; |
| 166 |
+ |
/* randomize other coordinates */ |
| 167 |
+ |
v1[0] = 0.5 - frandom(); |
| 168 |
+ |
v1[1] = 0.5 - frandom(); |
| 169 |
+ |
v1[2] = 0.5 - frandom(); |
| 170 |
+ |
for (i = 3; i--; ) |
| 171 |
+ |
if ((-0.6 < v[i]) & (v[i] < 0.6)) |
| 172 |
+ |
break; |
| 173 |
+ |
if (i < 0) |
| 174 |
+ |
return(0); |
| 175 |
+ |
v1[i] = 1.0; |
| 176 |
+ |
fcross(vp, v1, v); |
| 177 |
+ |
return(normalize(vp) > 0.0); |
| 178 |
+ |
} |
| 179 |
+ |
|
| 180 |
+ |
|
| 181 |
+ |
int |
| 182 |
|
closestapproach( /* closest approach of two rays */ |
| 183 |
|
RREAL t[2], /* returned distances along each ray */ |
| 184 |
|
const FVECT rorg0, /* first origin */ |
| 206 |
|
|
| 207 |
|
void |
| 208 |
|
spinvector( /* rotate vector around normal */ |
| 209 |
< |
FVECT vres, /* returned vector */ |
| 209 |
> |
FVECT vres, /* returned vector (same magnitude as vorig) */ |
| 210 |
|
const FVECT vorig, /* original vector */ |
| 211 |
|
const FVECT vnorm, /* normalized vector for rotation */ |
| 212 |
< |
double theta /* left-hand radians */ |
| 212 |
> |
double theta /* right-hand radians */ |
| 213 |
|
) |
| 214 |
|
{ |
| 215 |
|
double sint, cost, normprod; |
| 224 |
|
cost = cos(theta); |
| 225 |
|
sint = sin(theta); |
| 226 |
|
normprod = DOT(vorig, vnorm)*(1.-cost); |
| 227 |
< |
fcross(vperp, vnorm, vorig); |
| 227 |
> |
VCROSS(vperp, vnorm, vorig); |
| 228 |
|
for (i = 0; i < 3; i++) |
| 229 |
|
vres[i] = vorig[i]*cost + vnorm[i]*normprod + vperp[i]*sint; |
| 230 |
+ |
} |
| 231 |
+ |
|
| 232 |
+ |
double |
| 233 |
+ |
geodesic( /* rotate vector on great circle towards target */ |
| 234 |
+ |
FVECT vres, /* returned vector (same magnitude as vorig) */ |
| 235 |
+ |
const FVECT vorig, /* original vector */ |
| 236 |
+ |
const FVECT vtarg, /* vector we are rotating towards */ |
| 237 |
+ |
double t, /* amount along arc directed towards vtarg */ |
| 238 |
+ |
int meas /* distance measure (radians, absolute, relative) */ |
| 239 |
+ |
) |
| 240 |
+ |
{ |
| 241 |
+ |
FVECT normtarg; |
| 242 |
+ |
double volen, dotprod, sintr, cost; |
| 243 |
+ |
int i; |
| 244 |
+ |
|
| 245 |
+ |
VCOPY(normtarg, vtarg); /* in case vtarg==vres */ |
| 246 |
+ |
if (vres != vorig) |
| 247 |
+ |
VCOPY(vres, vorig); |
| 248 |
+ |
if (t == 0.0) |
| 249 |
+ |
return(VLEN(vres)); /* no rotation requested */ |
| 250 |
+ |
if ((volen = normalize(vres)) == 0.0) |
| 251 |
+ |
return(0.0); |
| 252 |
+ |
if (normalize(normtarg) == 0.0) |
| 253 |
+ |
return(0.0); /* target vector is zero */ |
| 254 |
+ |
dotprod = DOT(vres, normtarg); |
| 255 |
+ |
/* check for colinear */ |
| 256 |
+ |
if (dotprod >= 1.0-FTINY*FTINY) { |
| 257 |
+ |
if (meas != GEOD_REL) |
| 258 |
+ |
return(0.0); |
| 259 |
+ |
vres[0] *= volen; vres[1] *= volen; vres[2] *= volen; |
| 260 |
+ |
return(volen); |
| 261 |
+ |
} |
| 262 |
+ |
if (dotprod <= -1.0+FTINY*FTINY) |
| 263 |
+ |
return(0.0); |
| 264 |
+ |
if (meas == GEOD_ABS) |
| 265 |
+ |
t /= volen; |
| 266 |
+ |
else if (meas == GEOD_REL) |
| 267 |
+ |
t *= acos(dotprod); |
| 268 |
+ |
cost = cos(t); |
| 269 |
+ |
sintr = sin(t) / sqrt(1. - dotprod*dotprod); |
| 270 |
+ |
for (i = 0; i < 3; i++) |
| 271 |
+ |
vres[i] = volen*( cost*vres[i] + |
| 272 |
+ |
sintr*(normtarg[i] - dotprod*vres[i]) ); |
| 273 |
+ |
|
| 274 |
+ |
return(volen); /* return vector length */ |
| 275 |
|
} |