--- ray/src/common/dircode.c 2003/06/20 00:25:49 2.5 +++ ray/src/common/dircode.c 2019/08/27 23:51:23 2.10 @@ -1,13 +1,13 @@ #ifndef lint -static const char RCSid[] = "$Id: dircode.c,v 2.5 2003/06/20 00:25:49 greg Exp $"; +static const char RCSid[] = "$Id: dircode.c,v 2.10 2019/08/27 23:51:23 greg Exp $"; #endif /* - * Compute a 4-byte direction code (int32 type defined in standard.h). + * Compute a 4-byte direction code (externals defined in rtmath.h). * * Mean accuracy is 0.0022 degrees, with a maximum error of 0.0058 degrees. */ -#include "standard.h" +#include "rtmath.h" #define DCSCALE 11585.2 /* (1<<13)*sqrt(2) */ #define FXNEG 01 @@ -20,12 +20,11 @@ static const char RCSid[] = "$Id: dircode.c,v 2.5 2003 #define FMASK 0x1fff int32 -encodedir(dv) /* encode a normalized direction vector */ -FVECT dv; +encodedir(FVECT dv) /* encode a normalized direction vector */ { - register int32 dc = 0; + int32 dc = 0; int cd[3], cm; - register int i; + int i; for (i = 0; i < 3; i++) if (dv[i] < 0.) { @@ -33,6 +32,8 @@ FVECT dv; dc |= FXNEG<>F1SFT & FMASK)+.5)*(1./DCSCALE); d2 = ((dc>>F2SFT & FMASK)+.5)*(1./DCSCALE); der = sqrt(1. - d1*d1 - d2*d2); @@ -74,24 +78,51 @@ register int32 dc; if (dc & FZNEG) dv[2] = -dv[2]; } +#else +void +decodedir(FVECT dv, int32 dc) /* decode a normalized direction vector */ +{ + static const short itab[4][3] = { + {1,0,2},{0,1,2},{1,2,0},{0,2,1} + }; + static const RREAL neg[2] = {1., -1.}; + const int ndx = ((dc & F2Z) != 0)<<1 | ((dc & F1X) != 0); + double d1, d2, der; + + if (!dc) { /* special code for zero normal */ + dv[0] = dv[1] = dv[2] = 0.; + return; + } + d1 = ((dc>>F1SFT & FMASK)+.5)*(1./DCSCALE); + d2 = ((dc>>F2SFT & FMASK)+.5)*(1./DCSCALE); + der = sqrt(1. - d1*d1 - d2*d2); + dv[itab[ndx][0]] = d1; + dv[itab[ndx][1]] = d2; + dv[itab[ndx][2]] = der; + dv[0] *= neg[(dc&FXNEG)!=0]; + dv[1] *= neg[(dc&FYNEG)!=0]; + dv[2] *= neg[(dc&FZNEG)!=0]; +} + +#endif + double -dir2diff(dc1, dc2) /* approx. radians^2 between directions */ -int32 dc1, dc2; +dir2diff(int32 dc1, int32 dc2) /* approx. radians^2 between directions */ { FVECT v1, v2; + if (dc1 == dc2) + return 0.; + decodedir(v1, dc1); decodedir(v2, dc2); return(2. - 2.*DOT(v1,v2)); } - double -fdir2diff(dc1, v2) /* approx. radians^2 between directions */ -int32 dc1; -register FVECT v2; +fdir2diff(int32 dc1, FVECT v2) /* approx. radians^2 between directions */ { FVECT v1;