Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | #ifndef OPENVDB_UNITTEST_UTIL_HAS_BEEN_INCLUDED | ||
5 | #define OPENVDB_UNITTEST_UTIL_HAS_BEEN_INCLUDED | ||
6 | |||
7 | #include <openvdb/openvdb.h> | ||
8 | #include <openvdb/math/Math.h> // for math::Random01 | ||
9 | #include <openvdb/tools/Prune.h>// for pruneLevelSet | ||
10 | #include <sstream> | ||
11 | |||
12 | namespace unittest_util { | ||
13 | |||
14 | enum SphereMode { SPHERE_DENSE, SPHERE_DENSE_NARROW_BAND, SPHERE_SPARSE_NARROW_BAND }; | ||
15 | |||
16 | /// @brief Generates the signed distance to a sphere located at @a center | ||
17 | /// and with a specified @a radius (both in world coordinates). Only voxels | ||
18 | /// in the domain [0,0,0] -> @a dim are considered. Also note that the | ||
19 | /// level set is either dense, dense narrow-band or sparse narrow-band. | ||
20 | /// | ||
21 | /// @note This method is VERY SLOW and should only be used for debugging purposes! | ||
22 | /// However it works for any transform and even with open level sets. | ||
23 | /// A faster approch for closed narrow band generation is to only set voxels | ||
24 | /// sparsely and then use grid::signedFloodFill to define the sign | ||
25 | /// of the background values and tiles! This is implemented in openvdb/tools/LevelSetSphere.h | ||
26 | template<class GridType> | ||
27 | inline void | ||
28 | 81 | makeSphere(const openvdb::Coord& dim, const openvdb::Vec3f& center, float radius, | |
29 | GridType& grid, SphereMode mode) | ||
30 | { | ||
31 | typedef typename GridType::ValueType ValueT; | ||
32 | const ValueT | ||
33 | 6 | zero = openvdb::zeroVal<ValueT>(), | |
34 | 81 | outside = grid.background(), | |
35 | 79 | inside = -outside; | |
36 | |||
37 | typename GridType::Accessor acc = grid.getAccessor(); | ||
38 | openvdb::Coord xyz; | ||
39 |
2/2✓ Branch 0 taken 4296 times.
✓ Branch 1 taken 74 times.
|
4825 | for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) { |
40 |
2/2✓ Branch 0 taken 306944 times.
✓ Branch 1 taken 4296 times.
|
340360 | for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) { |
41 |
2/2✓ Branch 0 taken 26343168 times.
✓ Branch 1 taken 306944 times.
|
28513792 | for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) { |
42 | const openvdb::Vec3R p = grid.transform().indexToWorld(xyz); | ||
43 |
1/4✓ Branch 0 taken 1048576 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
28178176 | const float dist = float((p-center).length() - radius); |
44 | OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN | ||
45 | 27653888 | ValueT val = ValueT(zero + dist); | |
46 | OPENVDB_NO_TYPE_CONVERSION_WARNING_END | ||
47 |
3/4✓ Branch 0 taken 13430272 times.
✓ Branch 1 taken 262144 times.
✓ Branch 2 taken 12650752 times.
✗ Branch 3 not taken.
|
28178176 | switch (mode) { |
48 | 15265280 | case SPHERE_DENSE: | |
49 |
1/2✓ Branch 1 taken 13430272 times.
✗ Branch 2 not taken.
|
15265280 | acc.setValue(xyz, val); |
50 | break; | ||
51 | 262144 | case SPHERE_DENSE_NARROW_BAND: | |
52 |
5/9✓ Branch 0 taken 237841 times.
✓ Branch 1 taken 24303 times.
✓ Branch 2 taken 20170 times.
✓ Branch 3 taken 217671 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 262144 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
282314 | acc.setValue(xyz, val < inside ? inside : outside < val ? outside : val); |
53 | break; | ||
54 | 12650752 | case SPHERE_SPARSE_NARROW_BAND: | |
55 |
2/3✓ Branch 0 taken 191585 times.
✓ Branch 1 taken 12459167 times.
✗ Branch 2 not taken.
|
12650752 | if (val < inside) |
56 |
1/2✓ Branch 1 taken 191585 times.
✗ Branch 2 not taken.
|
191585 | acc.setValueOff(xyz, inside); |
57 |
2/3✓ Branch 0 taken 12089251 times.
✓ Branch 1 taken 369916 times.
✗ Branch 2 not taken.
|
12459167 | else if (outside < val) |
58 |
1/2✓ Branch 1 taken 12089251 times.
✗ Branch 2 not taken.
|
12089251 | acc.setValueOff(xyz, outside); |
59 | else | ||
60 |
1/2✓ Branch 1 taken 369916 times.
✗ Branch 2 not taken.
|
369916 | acc.setValue(xyz, val); |
61 | } | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | //if (mode == SPHERE_SPARSE_NARROW_BAND) grid.tree().prune(); | ||
66 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 58 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
81 | if (mode == SPHERE_SPARSE_NARROW_BAND) openvdb::tools::pruneLevelSet(grid.tree()); |
67 | 81 | } | |
68 | |||
69 | // Template specialization for boolean trees (mostly a dummy implementation) | ||
70 | template<> | ||
71 | inline void | ||
72 | 2 | makeSphere<openvdb::BoolGrid>(const openvdb::Coord& dim, const openvdb::Vec3f& center, | |
73 | float radius, openvdb::BoolGrid& grid, SphereMode) | ||
74 | { | ||
75 | openvdb::BoolGrid::Accessor acc = grid.getAccessor(); | ||
76 | openvdb::Coord xyz; | ||
77 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 2 times.
|
98 | for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) { |
78 |
2/2✓ Branch 0 taken 5120 times.
✓ Branch 1 taken 96 times.
|
5216 | for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) { |
79 |
2/2✓ Branch 0 taken 294912 times.
✓ Branch 1 taken 5120 times.
|
300032 | for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) { |
80 | const openvdb::Vec3R p = grid.transform().indexToWorld(xyz); | ||
81 | 294912 | const float dist = static_cast<float>((p-center).length() - radius); | |
82 |
3/6✓ Branch 0 taken 8831 times.
✓ Branch 1 taken 286081 times.
✓ Branch 3 taken 8831 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
294912 | if (dist <= 0) acc.setValue(xyz, true); |
83 | } | ||
84 | } | ||
85 | } | ||
86 | 2 | } | |
87 | |||
88 | // This method will soon be replaced by the one above!!!!! | ||
89 | template<class GridType> | ||
90 | inline void | ||
91 | 32 | makeSphere(const openvdb::Coord& dim, const openvdb::Vec3f& center, float radius, | |
92 | GridType &grid, float dx, SphereMode mode) | ||
93 | { | ||
94 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
32 | grid.setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/dx)); |
95 | 32 | makeSphere<GridType>(dim, center, radius, grid, mode); | |
96 | 32 | } | |
97 | |||
98 | // Generate random points by uniformly distributing points | ||
99 | // on a unit-sphere. | ||
100 | 5 | inline void genPoints(const int numPoints, std::vector<openvdb::Vec3R>& points) | |
101 | { | ||
102 | // init | ||
103 | openvdb::math::Random01 randNumber(0); | ||
104 | 5 | const int n = int(std::sqrt(double(numPoints))); | |
105 | 5 | const double xScale = (2.0 * M_PI) / double(n); | |
106 | 5 | const double yScale = M_PI / double(n); | |
107 | |||
108 | double x, y, theta, phi; | ||
109 | openvdb::Vec3R pos; | ||
110 | |||
111 | 5 | points.reserve(n*n); | |
112 | |||
113 | // loop over a [0 to n) x [0 to n) grid. | ||
114 |
2/2✓ Branch 0 taken 4472 times.
✓ Branch 1 taken 5 times.
|
4477 | for (int a = 0; a < n; ++a) { |
115 |
2/2✓ Branch 0 taken 15092384 times.
✓ Branch 1 taken 4472 times.
|
15096856 | for (int b = 0; b < n; ++b) { |
116 | |||
117 | // jitter, move to random pos. inside the current cell | ||
118 | 15092384 | x = double(a) + randNumber(); | |
119 | 15092384 | y = double(b) + randNumber(); | |
120 | |||
121 | // remap to a lat/long map | ||
122 | 15092384 | theta = y * yScale; // [0 to PI] | |
123 | 15092384 | phi = x * xScale; // [0 to 2PI] | |
124 | |||
125 | // convert to cartesian coordinates on a unit sphere. | ||
126 | // spherical coordinate triplet (r=1, theta, phi) | ||
127 | 15092384 | pos[0] = std::sin(theta)*std::cos(phi); | |
128 | 15092384 | pos[1] = std::sin(theta)*std::sin(phi); | |
129 | 15092384 | pos[2] = std::cos(theta); | |
130 | |||
131 | 15092384 | points.push_back(pos); | |
132 | } | ||
133 | } | ||
134 | 5 | } | |
135 | |||
136 | // @todo makePlane | ||
137 | |||
138 | } // namespace unittest_util | ||
139 | |||
140 | #endif // OPENVDB_UNITTEST_UTIL_HAS_BEEN_INCLUDED | ||
141 |