| 2 |
|
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 4 |
|
/* |
| 5 |
< |
* Compute a 4-byte direction code (int32 type defined in standard.h). |
| 5 |
> |
* Compute a 4-byte direction code (externals defined in rtmath.h). |
| 6 |
|
* |
| 7 |
|
* Mean accuracy is 0.0022 degrees, with a maximum error of 0.0058 degrees. |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
< |
#include "standard.h" |
| 10 |
> |
#include "rtmath.h" |
| 11 |
|
|
| 12 |
< |
#define DCSCALE 11585.2 /* (1<<13)*sqrt(2) */ |
| 12 |
> |
#define DCSCALE 11584.7 /* (1<<13)*sqrt(2) - .5 */ |
| 13 |
|
#define FXNEG 01 |
| 14 |
|
#define FYNEG 02 |
| 15 |
|
#define FZNEG 04 |
| 20 |
|
#define FMASK 0x1fff |
| 21 |
|
|
| 22 |
|
int32 |
| 23 |
< |
encodedir(dv) /* encode a normalized direction vector */ |
| 24 |
< |
FVECT dv; |
| 23 |
> |
encodedir(FVECT dv) /* encode a normalized direction vector */ |
| 24 |
|
{ |
| 25 |
< |
register int32 dc = 0; |
| 25 |
> |
int32 dc = 0; |
| 26 |
|
int cd[3], cm; |
| 27 |
< |
register int i; |
| 27 |
> |
int i; |
| 28 |
|
|
| 29 |
|
for (i = 0; i < 3; i++) |
| 30 |
|
if (dv[i] < 0.) { |
| 31 |
< |
cd[i] = (int)(dv[i] * -DCSCALE); |
| 31 |
> |
cd[i] = (int)(dv[i] * -DCSCALE + .5); |
| 32 |
|
dc |= FXNEG<<i; |
| 33 |
|
} else |
| 34 |
< |
cd[i] = (int)(dv[i] * DCSCALE); |
| 34 |
> |
cd[i] = (int)(dv[i] * DCSCALE + .5); |
| 35 |
|
if (!(cd[0] | cd[1] | cd[2])) |
| 36 |
|
return(0); /* zero normal */ |
| 37 |
|
if (cd[0] <= cd[1]) { |
| 50 |
|
return(dc); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
+ |
#if 0 /* original version for reference */ |
| 54 |
|
|
| 55 |
|
void |
| 56 |
< |
decodedir(dv, dc) /* decode a normalized direction vector */ |
| 57 |
< |
register FVECT dv; /* returned */ |
| 58 |
< |
register int32 dc; |
| 56 |
> |
decodedir(FVECT dv, int32 dc) /* decode a normalized direction vector */ |
| 57 |
|
{ |
| 58 |
|
double d1, d2, der; |
| 59 |
|
|
| 78 |
|
if (dc & FZNEG) dv[2] = -dv[2]; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
+ |
#else |
| 82 |
|
|
| 83 |
+ |
void |
| 84 |
+ |
decodedir(FVECT dv, int32 dc) /* decode a normalized direction vector */ |
| 85 |
+ |
{ |
| 86 |
+ |
static const short itab[4][3] = { |
| 87 |
+ |
{1,0,2},{0,1,2},{1,2,0},{0,2,1} |
| 88 |
+ |
}; |
| 89 |
+ |
static const RREAL neg[2] = {1., -1.}; |
| 90 |
+ |
const int ndx = ((dc & F2Z) != 0)<<1 | ((dc & F1X) != 0); |
| 91 |
+ |
double d1, d2, der; |
| 92 |
+ |
|
| 93 |
+ |
if (!dc) { /* special code for zero normal */ |
| 94 |
+ |
dv[0] = dv[1] = dv[2] = 0.; |
| 95 |
+ |
return; |
| 96 |
+ |
} |
| 97 |
+ |
d1 = (dc>>F1SFT & FMASK)*(1./DCSCALE); |
| 98 |
+ |
d2 = (dc>>F2SFT & FMASK)*(1./DCSCALE); |
| 99 |
+ |
der = sqrt(1. - d1*d1 - d2*d2); |
| 100 |
+ |
dv[itab[ndx][0]] = d1; |
| 101 |
+ |
dv[itab[ndx][1]] = d2; |
| 102 |
+ |
dv[itab[ndx][2]] = der; |
| 103 |
+ |
dv[0] *= neg[(dc&FXNEG)!=0]; |
| 104 |
+ |
dv[1] *= neg[(dc&FYNEG)!=0]; |
| 105 |
+ |
dv[2] *= neg[(dc&FZNEG)!=0]; |
| 106 |
+ |
} |
| 107 |
+ |
|
| 108 |
+ |
#endif |
| 109 |
+ |
|
| 110 |
|
double |
| 111 |
< |
dir2diff(dc1, dc2) /* approx. radians^2 between directions */ |
| 86 |
< |
int32 dc1, dc2; |
| 111 |
> |
dir2diff(int32 dc1, int32 dc2) /* approx. radians^2 between directions */ |
| 112 |
|
{ |
| 113 |
|
FVECT v1, v2; |
| 114 |
|
|
| 115 |
+ |
if (dc1 == dc2) |
| 116 |
+ |
return 0.; |
| 117 |
+ |
|
| 118 |
|
decodedir(v1, dc1); |
| 119 |
|
decodedir(v2, dc2); |
| 120 |
|
|
| 121 |
|
return(2. - 2.*DOT(v1,v2)); |
| 122 |
|
} |
| 123 |
|
|
| 96 |
– |
|
| 124 |
|
double |
| 125 |
< |
fdir2diff(dc1, v2) /* approx. radians^2 between directions */ |
| 99 |
< |
int32 dc1; |
| 100 |
< |
register FVECT v2; |
| 125 |
> |
fdir2diff(int32 dc1, FVECT v2) /* approx. radians^2 between directions */ |
| 126 |
|
{ |
| 127 |
|
FVECT v1; |
| 128 |
|
|