31 #ifndef OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
34 #include <openvdb/Platform.h>
35 #include <openvdb/version.h>
37 #include <tbb/atomic.h>
54 static uint16_t pack(
const Vec3<T>& vec);
55 static Vec3s unpack(
const uint16_t data);
57 static void flipSignBits(uint16_t&);
66 static const uint16_t MASK_SLOTS = 0x1FFF;
67 static const uint16_t MASK_XSLOT = 0x1F80;
68 static const uint16_t MASK_YSLOT = 0x007F;
69 static const uint16_t MASK_XSIGN = 0x8000;
70 static const uint16_t MASK_YSIGN = 0x4000;
71 static const uint16_t MASK_ZSIGN = 0x2000;
74 static bool sInitialized;
77 static float sNormalizationWeights[MASK_SLOTS + 1];
86 QuantizedUnitVec::pack(
const Vec3<T>& vec)
89 T x(vec[0]), y(vec[1]), z(vec[2]);
93 if (x < T(0.0)) { data |= MASK_XSIGN; x = -x; }
94 if (y < T(0.0)) { data |= MASK_YSIGN; y = -y; }
95 if (z < T(0.0)) { data |= MASK_ZSIGN; z = -z; }
99 T w = T(126.0) / (x + y + z);
100 uint16_t xbits = uint16_t((x * w) + T(0.5));
101 uint16_t ybits = uint16_t((y * w) + T(0.5));
122 QuantizedUnitVec::unpack(
const uint16_t data)
124 if (!sInitialized) init();
126 const float w = sNormalizationWeights[data & MASK_SLOTS];
128 uint16_t xbits = (data & MASK_XSLOT) >> 7;
129 uint16_t ybits = data & MASK_YSLOT;
132 if ((xbits + ybits) > 126) {
137 Vec3s vec(
float(xbits) * w,
float(ybits) * w,
float(126 - xbits - ybits) * w);
139 if(data & MASK_XSIGN) vec[0] = -vec[0];
140 if(data & MASK_YSIGN) vec[1] = -vec[1];
141 if(data & MASK_ZSIGN) vec[2] = -vec[2];
150 QuantizedUnitVec::flipSignBits(uint16_t& v)
152 v = (v & MASK_SLOTS) | (~v & ~MASK_SLOTS);
160 #endif // OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
#define OPENVDB_VERSION_NAME
Definition: version.h:45
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
Definition: QuantizedUnitVec.h:49