4 #ifndef OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED 5 #define OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED 8 #include <openvdb/version.h> 22 template<
typename T>
static uint16_t pack(
const Vec3<T>& vec);
23 static Vec3s unpack(
const uint16_t data);
25 static void flipSignBits(uint16_t&);
31 static const uint16_t MASK_SLOTS = 0x1FFF;
32 static const uint16_t MASK_XSLOT = 0x1F80;
33 static const uint16_t MASK_YSLOT = 0x007F;
34 static const uint16_t MASK_XSIGN = 0x8000;
35 static const uint16_t MASK_YSIGN = 0x4000;
36 static const uint16_t MASK_ZSIGN = 0x2000;
39 static float sNormalizationWeights[MASK_SLOTS + 1];
48 QuantizedUnitVec::pack(
const Vec3<T>& vec)
53 T x(vec[0]), y(vec[1]), z(vec[2]);
57 if (x < T(0.0)) { data |= MASK_XSIGN; x = -x; }
58 if (y < T(0.0)) { data |= MASK_YSIGN; y = -y; }
59 if (z < T(0.0)) { data |= MASK_ZSIGN; z = -z; }
63 T w = T(126.0) / (x + y + z);
64 uint16_t xbits =
static_cast<uint16_t
>((x * w));
65 uint16_t ybits =
static_cast<uint16_t
>((y * w));
74 xbits =
static_cast<uint16_t
>(127 - xbits);
75 ybits =
static_cast<uint16_t
>(127 - ybits);
79 data =
static_cast<uint16_t
>(data | (xbits << 7));
80 data =
static_cast<uint16_t
>(data | ybits);
86 QuantizedUnitVec::unpack(
const uint16_t data)
88 const float w = sNormalizationWeights[data & MASK_SLOTS];
90 uint16_t xbits =
static_cast<uint16_t
>((data & MASK_XSLOT) >> 7);
91 uint16_t ybits =
static_cast<uint16_t
>(data & MASK_YSLOT);
94 if ((xbits + ybits) > 126) {
95 xbits =
static_cast<uint16_t
>(127 - xbits);
96 ybits =
static_cast<uint16_t
>(127 - ybits);
99 Vec3s vec(
float(xbits) * w,
float(ybits) * w,
float(126 - xbits - ybits) * w);
101 if (data & MASK_XSIGN) vec[0] = -vec[0];
102 if (data & MASK_YSIGN) vec[1] = -vec[1];
103 if (data & MASK_ZSIGN) vec[2] = -vec[2];
112 QuantizedUnitVec::flipSignBits(uint16_t& v)
114 v =
static_cast<uint16_t
>((v & MASK_SLOTS) | (~v & ~MASK_SLOTS));
121 #endif // OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:337
Definition: Exceptions.h:13
Unit vector occupying only 16 bits.
Definition: QuantizedUnitVec.h:19
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212