Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | #include <openvdb/Exceptions.h> | ||
5 | #include <openvdb/math/QuantizedUnitVec.h> | ||
6 | #include <openvdb/math/Math.h> | ||
7 | #include <openvdb/math/Vec3.h> | ||
8 | |||
9 | #include <gtest/gtest.h> | ||
10 | |||
11 | #include <sstream> | ||
12 | #include <algorithm> | ||
13 | #include <cmath> | ||
14 | #include <ctime> | ||
15 | |||
16 | |||
17 | 1 | class TestQuantizedUnitVec: public ::testing::Test | |
18 | { | ||
19 | protected: | ||
20 | // Generate a random number in the range [0, 1]. | ||
21 | 120000 | double randNumber() { return double(rand()) / (double(RAND_MAX) + 1.0); } | |
22 | }; | ||
23 | |||
24 | |||
25 | //////////////////////////////////////// | ||
26 | |||
27 | |||
28 | namespace { | ||
29 | const uint16_t | ||
30 | MASK_XSIGN = 0x8000, // 1000000000000000 | ||
31 | MASK_YSIGN = 0x4000, // 0100000000000000 | ||
32 | MASK_ZSIGN = 0x2000; // 0010000000000000 | ||
33 | } | ||
34 | |||
35 | |||
36 | //////////////////////////////////////// | ||
37 | |||
38 | |||
39 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | TEST_F(TestQuantizedUnitVec, testQuantization) |
40 | { | ||
41 | using namespace openvdb; | ||
42 | using namespace openvdb::math; | ||
43 | |||
44 | // | ||
45 | // Check sign bits | ||
46 | // | ||
47 | Vec3s unitVec = Vec3s(-1.0, -1.0, -1.0); | ||
48 | 1 | unitVec.normalize(); | |
49 | |||
50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | uint16_t quantizedVec = QuantizedUnitVec::pack(unitVec); |
51 | |||
52 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_XSIGN)); |
53 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_YSIGN)); |
54 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_ZSIGN)); |
55 | |||
56 | 1 | unitVec[0] = -unitVec[0]; | |
57 | 1 | unitVec[2] = -unitVec[2]; | |
58 | 1 | quantizedVec = QuantizedUnitVec::pack(unitVec); | |
59 | |||
60 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_XSIGN)); |
61 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_YSIGN)); |
62 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_ZSIGN)); |
63 | |||
64 | 1 | unitVec[1] = -unitVec[1]; | |
65 | 1 | quantizedVec = QuantizedUnitVec::pack(unitVec); | |
66 | |||
67 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_XSIGN)); |
68 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_YSIGN)); |
69 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_ZSIGN)); |
70 | |||
71 | QuantizedUnitVec::flipSignBits(quantizedVec); | ||
72 | |||
73 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_XSIGN)); |
74 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_YSIGN)); |
75 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_ZSIGN)); |
76 | |||
77 | 1 | unitVec[2] = -unitVec[2]; | |
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | quantizedVec = QuantizedUnitVec::pack(unitVec); |
79 | QuantizedUnitVec::flipSignBits(quantizedVec); | ||
80 | |||
81 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_XSIGN)); |
82 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE((quantizedVec & MASK_YSIGN)); |
83 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
1 | EXPECT_TRUE(!(quantizedVec & MASK_ZSIGN)); |
84 | |||
85 | // | ||
86 | // Check conversion error | ||
87 | // | ||
88 | const double tol = 0.05; // component error tolerance | ||
89 | |||
90 | const int numNormals = 40000; | ||
91 | |||
92 | |||
93 | // init | ||
94 | 1 | srand(0); | |
95 | const int n = int(std::sqrt(double(numNormals))); | ||
96 | const double xScale = (2.0 * M_PI) / double(n); | ||
97 | const double yScale = M_PI / double(n); | ||
98 | |||
99 | double x, y, theta, phi; | ||
100 | Vec3s n0, n1; | ||
101 | |||
102 | // generate random normals, by uniformly distributing points on a unit-sphere. | ||
103 | |||
104 | // loop over a [0 to n) x [0 to n) grid. | ||
105 |
2/2✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1 times.
|
201 | for (int a = 0; a < n; ++a) { |
106 |
2/2✓ Branch 0 taken 40000 times.
✓ Branch 1 taken 200 times.
|
40200 | for (int b = 0; b < n; ++b) { |
107 | |||
108 | // jitter, move to random pos. inside the current cell | ||
109 | 40000 | x = double(a) + randNumber(); | |
110 | 40000 | y = double(b) + randNumber(); | |
111 | |||
112 | // remap to a lat/long map | ||
113 | 40000 | theta = y * yScale; // [0 to PI] | |
114 | 40000 | phi = x * xScale; // [0 to 2PI] | |
115 | |||
116 | // convert to cartesian coordinates on a unit sphere. | ||
117 | // spherical coordinate triplet (r=1, theta, phi) | ||
118 | 40000 | n0[0] = float(std::sin(theta)*std::cos(phi)); | |
119 | 40000 | n0[1] = float(std::sin(theta)*std::sin(phi)); | |
120 | 40000 | n0[2] = float(std::cos(theta)); | |
121 | |||
122 |
1/14✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
40000 | EXPECT_NEAR(1.0, n0.length(), 1e-6); |
123 | |||
124 | 40000 | n1 = QuantizedUnitVec::unpack(QuantizedUnitVec::pack(n0)); | |
125 | |||
126 |
1/14✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
40000 | EXPECT_NEAR(1.0, n1.length(), 1e-6); |
127 | |||
128 |
1/14✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
40000 | EXPECT_NEAR(n0[0], n1[0], tol); |
129 |
1/14✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
40000 | EXPECT_NEAR(n0[1], n1[1], tol); |
130 |
1/14✗ Branch 1 not taken.
✓ Branch 2 taken 40000 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
40000 | EXPECT_NEAR(n0[2], n1[2], tol); |
131 | |||
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40000 times.
|
40000 | float sumDiff = std::abs(n0[0] - n1[0]) + std::abs(n0[1] - n1[1]) |
133 | 40000 | + std::abs(n0[2] - n1[2]); | |
134 | |||
135 |
1/16✗ Branch 0 not taken.
✓ Branch 1 taken 40000 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
40000 | EXPECT_TRUE(sumDiff < (2.0 * tol)); |
136 | } | ||
137 | } | ||
138 | 1 | } | |
139 |