--- ray/src/common/dircode.c 2019/05/14 17:21:50 2.9 +++ ray/src/common/dircode.c 2020/03/04 02:55:43 2.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dircode.c,v 2.9 2019/05/14 17:21:50 greg Exp $"; +static const char RCSid[] = "$Id: dircode.c,v 2.11 2020/03/04 02:55:43 greg Exp $"; #endif /* * Compute a 4-byte direction code (externals defined in rtmath.h). @@ -9,7 +9,7 @@ static const char RCSid[] = "$Id: dircode.c,v 2.9 2019 #include "rtmath.h" -#define DCSCALE 11585.2 /* (1<<13)*sqrt(2) */ +#define DCSCALE 11584.7 /* (1<<13)*sqrt(2) - .5 */ #define FXNEG 01 #define FYNEG 02 #define FZNEG 04 @@ -28,10 +28,10 @@ encodedir(FVECT dv) /* encode a normalized direction for (i = 0; i < 3; i++) if (dv[i] < 0.) { - cd[i] = (int)(dv[i] * -DCSCALE); + cd[i] = (int)(dv[i] * -DCSCALE + .5); dc |= FXNEG<>F1SFT & FMASK)+.5)*(1./DCSCALE); - d2 = ((dc>>F2SFT & FMASK)+.5)*(1./DCSCALE); + d1 = (dc>>F1SFT & FMASK)*(1./DCSCALE); + d2 = (dc>>F2SFT & FMASK)*(1./DCSCALE); der = sqrt(1. - d1*d1 - d2*d2); dv[itab[ndx][0]] = d1; dv[itab[ndx][1]] = d2; @@ -111,6 +111,9 @@ double dir2diff(int32 dc1, int32 dc2) /* approx. radians^2 between directions */ { FVECT v1, v2; + + if (dc1 == dc2) + return 0.; decodedir(v1, dc1); decodedir(v2, dc2);