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 =
static_cast<uint16_t
>((x * w));
101 uint16_t ybits =
static_cast<uint16_t
>((y * w));
110 xbits =
static_cast<uint16_t
>(127 - xbits);
111 ybits =
static_cast<uint16_t
>(127 - ybits);
115 data =
static_cast<uint16_t
>(data | (xbits << 7));
116 data =
static_cast<uint16_t
>(data | ybits);
122 QuantizedUnitVec::unpack(
const uint16_t data)
124 if (!sInitialized) init();
126 const float w = sNormalizationWeights[data & MASK_SLOTS];
128 uint16_t xbits =
static_cast<uint16_t
>((data & MASK_XSLOT) >> 7);
129 uint16_t ybits =
static_cast<uint16_t
>(data & MASK_YSLOT);
132 if ((xbits + ybits) > 126) {
133 xbits =
static_cast<uint16_t
>(127 - xbits);
134 ybits =
static_cast<uint16_t
>(127 - ybits);
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 =
static_cast<uint16_t
>((v & MASK_SLOTS) | (~v & ~MASK_SLOTS));
160 #endif // OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Definition: Exceptions.h:39
Definition: QuantizedUnitVec.h:49
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71