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> 49 template<
typename T>
static uint16_t pack(
const Vec3<T>& vec);
50 static Vec3s unpack(
const uint16_t data);
52 static void flipSignBits(uint16_t&);
58 static const uint16_t MASK_SLOTS = 0x1FFF;
59 static const uint16_t MASK_XSLOT = 0x1F80;
60 static const uint16_t MASK_YSLOT = 0x007F;
61 static const uint16_t MASK_XSIGN = 0x8000;
62 static const uint16_t MASK_YSIGN = 0x4000;
63 static const uint16_t MASK_ZSIGN = 0x2000;
66 static float sNormalizationWeights[MASK_SLOTS + 1];
75 QuantizedUnitVec::pack(
const Vec3<T>& vec)
80 T x(vec[0]), y(vec[1]), z(vec[2]);
84 if (x < T(0.0)) { data |= MASK_XSIGN; x = -x; }
85 if (y < T(0.0)) { data |= MASK_YSIGN; y = -y; }
86 if (z < T(0.0)) { data |= MASK_ZSIGN; z = -z; }
90 T w = T(126.0) / (x + y + z);
91 uint16_t xbits =
static_cast<uint16_t
>((x * w));
92 uint16_t ybits =
static_cast<uint16_t
>((y * w));
101 xbits =
static_cast<uint16_t
>(127 - xbits);
102 ybits =
static_cast<uint16_t
>(127 - ybits);
106 data =
static_cast<uint16_t
>(data | (xbits << 7));
107 data =
static_cast<uint16_t
>(data | ybits);
113 QuantizedUnitVec::unpack(
const uint16_t data)
115 const float w = sNormalizationWeights[data & MASK_SLOTS];
117 uint16_t xbits =
static_cast<uint16_t
>((data & MASK_XSLOT) >> 7);
118 uint16_t ybits =
static_cast<uint16_t
>(data & MASK_YSLOT);
121 if ((xbits + ybits) > 126) {
122 xbits =
static_cast<uint16_t
>(127 - xbits);
123 ybits =
static_cast<uint16_t
>(127 - ybits);
126 Vec3s vec(
float(xbits) * w,
float(ybits) * w,
float(126 - xbits - ybits) * w);
128 if (data & MASK_XSIGN) vec[0] = -vec[0];
129 if (data & MASK_YSIGN) vec[1] = -vec[1];
130 if (data & MASK_ZSIGN) vec[2] = -vec[2];
139 QuantizedUnitVec::flipSignBits(uint16_t& v)
141 v =
static_cast<uint16_t
>((v & MASK_SLOTS) | (~v & ~MASK_SLOTS));
148 #endif // OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
Unit vector occupying only 16 bits.
Definition: QuantizedUnitVec.h:46
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
Definition: Exceptions.h:40
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308