HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QuantizedUnitVec.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
33 
34 #include <openvdb/Platform.h>
35 #include <openvdb/version.h>
36 #include "Vec3.h"
37 
38 namespace openvdb {
40 namespace OPENVDB_VERSION_NAME {
41 namespace math {
42 
43 /// @brief Unit vector occupying only 16 bits
44 /// @details Stores two quantized components. Based on the
45 /// "Higher Accuracy Quantized Normals" article from GameDev.Net LLC, 2000
47 {
48 public:
49  template<typename T> static uint16_t pack(const Vec3<T>& vec);
50  static Vec3s unpack(const uint16_t data);
51 
52  static void flipSignBits(uint16_t&);
53 
54 private:
55  QuantizedUnitVec() {}
56 
57  // bit masks
58  static const uint16_t MASK_SLOTS = 0x1FFF; // 0001111111111111
59  static const uint16_t MASK_XSLOT = 0x1F80; // 0001111110000000
60  static const uint16_t MASK_YSLOT = 0x007F; // 0000000001111111
61  static const uint16_t MASK_XSIGN = 0x8000; // 1000000000000000
62  static const uint16_t MASK_YSIGN = 0x4000; // 0100000000000000
63  static const uint16_t MASK_ZSIGN = 0x2000; // 0010000000000000
64 
65  // normalization weights, 32 kilobytes.
66  static float sNormalizationWeights[MASK_SLOTS + 1];
67 }; // class QuantizedUnitVec
68 
69 
70 ////////////////////////////////////////
71 
72 
73 template<typename T>
74 inline uint16_t
76 {
77  if (math::isZero(vec)) return 0;
78 
79  uint16_t data = 0;
80  T x(vec[0]), y(vec[1]), z(vec[2]);
81 
82  // The sign of the three components are first stored using
83  // 3-bits and can then safely be discarded.
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; }
87 
88  // The z component is discarded and x & y are quantized in
89  // the 0 to 126 range.
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));
93 
94  // The remaining 13 bits in our 16 bit word are dividied into a
95  // 6-bit x-slot and a 7-bit y-slot. Both the xbits and the ybits
96  // can still be represented using (2^7 - 1) quantization levels.
97 
98  // If the xbits requre more than 6-bits, store the complement.
99  // (xbits + ybits < 127, thus if xbits > 63 => ybits <= 63)
100  if (xbits > 63) {
101  xbits = static_cast<uint16_t>(127 - xbits);
102  ybits = static_cast<uint16_t>(127 - ybits);
103  }
104 
105  // Pack components into their respective slots.
106  data = static_cast<uint16_t>(data | (xbits << 7));
107  data = static_cast<uint16_t>(data | ybits);
108  return data;
109 }
110 
111 
112 inline Vec3s
114 {
115  const float w = sNormalizationWeights[data & MASK_SLOTS];
116 
117  uint16_t xbits = static_cast<uint16_t>((data & MASK_XSLOT) >> 7);
118  uint16_t ybits = static_cast<uint16_t>(data & MASK_YSLOT);
119 
120  // Check if the complement components where stored and revert.
121  if ((xbits + ybits) > 126) {
122  xbits = static_cast<uint16_t>(127 - xbits);
123  ybits = static_cast<uint16_t>(127 - ybits);
124  }
125 
126  Vec3s vec(float(xbits) * w, float(ybits) * w, float(126 - xbits - ybits) * w);
127 
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];
131  return vec;
132 }
133 
134 
135 ////////////////////////////////////////
136 
137 
138 inline void
140 {
141  v = static_cast<uint16_t>((v & MASK_SLOTS) | (~v & ~MASK_SLOTS));
142 }
143 
144 } // namespace math
145 } // namespace OPENVDB_VERSION_NAME
146 } // namespace openvdb
147 
148 #endif // OPENVDB_MATH_QUANTIZED_UNIT_VEC_HAS_BEEN_INCLUDED
149 
150 // Copyright (c) 2012-2018 DreamWorks Animation LLC
151 // All rights reserved. This software is distributed under the
152 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
const GLdouble * v
Definition: glcorearb.h:836
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
GLint y
Definition: glcorearb.h:102
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
static uint16_t pack(const Vec3< T > &vec)
GLboolean * data
Definition: glcorearb.h:130
GLint GLenum GLint x
Definition: glcorearb.h:408
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:308