GCC Code Coverage Report


Directory: ./
File: openvdb/openvdb/unittest/PointBuilder.h
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 29 29 100.0%
Functions: 11 11 100.0%
Branches: 76 151 50.3%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #ifndef OPENVDB_UNITTEST_POINT_BUILDER_HAS_BEEN_INCLUDED
5 #define OPENVDB_UNITTEST_POINT_BUILDER_HAS_BEEN_INCLUDED
6
7 #include <openvdb/openvdb.h>
8 #include <openvdb/tools/PointIndexGrid.h>
9 #include <openvdb/points/PointAttribute.h>
10 #include <openvdb/points/PointConversion.h>
11
12 /// @brief Get 8 corner points from a cube with a given scale, ordered such
13 /// that if used for conversion to OpenVDB Points, that the default
14 /// iteration order remains consistent
15 inline std::vector<openvdb::Vec3f>
16 22 getBoxPoints(const float scale = 1.0f)
17 {
18 // This order is configured to be the same layout when
19 // a vdb points grid is constructed and so matches methods
20 // like setGroup or populateAttribute
21 std::vector<openvdb::Vec3f> pos = {
22 openvdb::Vec3f(-1.0f, -1.0f, -1.0f),
23 openvdb::Vec3f(-1.0f, -1.0f, 1.0f),
24 openvdb::Vec3f(-1.0f, 1.0f, -1.0f),
25 openvdb::Vec3f(-1.0f, 1.0f, 1.0f),
26 openvdb::Vec3f(1.0f, -1.0f, -1.0f),
27 openvdb::Vec3f(1.0f, -1.0f, 1.0f),
28 openvdb::Vec3f(1.0f, 1.0f, -1.0f),
29 openvdb::Vec3f(1.0f, 1.0f, 1.0f)
30 22 };
31
32
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 22 times.
198 for (auto& p : pos) p *= scale;
33 22 return pos;
34 }
35
36 /// @brief Builder pattern for creating PointDataGrids which simplifies
37 /// a lot of the repetitive boilerplate
38 struct PointBuilder
39 {
40 using PointDataTreeT = openvdb::points::PointDataTree;
41 using PointIndexTreeT = openvdb::tools::PointIndexTree;
42
43 using CallbackT1 = std::function<void(PointDataTreeT&, const PointIndexTreeT&)>;
44 using CallbackT2 = std::function<void(PointDataTreeT&)>;
45
46 // init the builder with a set of positions
47
31/62
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 2 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 2 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 2 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 2 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 2 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 2 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 2 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 2 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 2 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 2 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 2 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 1 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 76 taken 1 times.
✗ Branch 77 not taken.
✓ Branch 79 taken 1 times.
✗ Branch 80 not taken.
✓ Branch 82 taken 1 times.
✗ Branch 83 not taken.
✓ Branch 85 taken 1 times.
✗ Branch 86 not taken.
✓ Branch 88 taken 1 times.
✗ Branch 89 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
48 PointBuilder(const std::vector<openvdb::Vec3f>& pos) : positions(pos) {}
48
49 // set the desired voxel size
50
31/62
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 1 times.
✗ Branch 47 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 58 taken 1 times.
✗ Branch 59 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 64 taken 1 times.
✗ Branch 65 not taken.
✓ Branch 67 taken 1 times.
✗ Branch 68 not taken.
✓ Branch 70 taken 1 times.
✗ Branch 71 not taken.
✓ Branch 73 taken 1 times.
✗ Branch 74 not taken.
✓ Branch 76 taken 1 times.
✗ Branch 77 not taken.
✓ Branch 79 taken 1 times.
✗ Branch 80 not taken.
✓ Branch 82 taken 1 times.
✗ Branch 83 not taken.
✓ Branch 85 taken 1 times.
✗ Branch 86 not taken.
✓ Branch 88 taken 1 times.
✗ Branch 89 not taken.
✓ Branch 91 taken 1 times.
✗ Branch 92 not taken.
34 PointBuilder& voxelsize(double in) { vs = in; return *this; }
51
52 // add a group to be created with membership data
53 16 PointBuilder& group(const std::vector<short>& in,
54 const std::string& name = "group")
55 {
56
1/2
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
16 callbacks.emplace_back([in, name](PointDataTreeT& tree, const PointIndexTreeT& index) {
57 16 openvdb::points::appendGroup(tree, name);
58 16 openvdb::points::setGroup(tree, index, in, name);
59 16 });
60 16 return *this;
61 }
62
63 // add a uniform attribute
64 template <typename ValueT>
65 19 PointBuilder& attribute(const ValueT& in, const std::string& name)
66 {
67
3/9
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 17 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
109 callbacks.emplace_back([in, name](PointDataTreeT& tree, const PointIndexTreeT&) {
68 18 openvdb::points::appendAttribute<ValueT>(tree, name, in);
69 });
70 19 return *this;
71 }
72
73 // add a varying attribute
74 template <typename ValueT>
75 46 PointBuilder& attribute(const std::vector<ValueT>& in, const std::string& name)
76 {
77
1/2
✓ Branch 3 taken 23 times.
✗ Branch 4 not taken.
92 callbacks.emplace_back([in, name](PointDataTreeT& tree, const PointIndexTreeT& index) {
78 23 openvdb::points::PointAttributeVector<ValueT> rwrap(in);
79 23 openvdb::points::appendAttribute<ValueT>(tree, name);
80 23 openvdb::points::populateAttribute(tree, index, name, rwrap);
81 });
82 46 return *this;
83 }
84
85 // add a custom callback of T1
86 PointBuilder& callback(const CallbackT1& c)
87 {
88 callbacks.emplace_back(c); return *this;
89 }
90
91 // add a custom callback of T2
92 PointBuilder& callback(const CallbackT2& c)
93 {
94 auto wrap = [c](PointDataTreeT& tree, const PointIndexTreeT&) { c(tree); };
95 callbacks.emplace_back(wrap); return *this;
96 }
97
98 // build and return the points
99 48 openvdb::points::PointDataGrid::Ptr get()
100 {
101 openvdb::math::Transform::Ptr transform =
102 48 openvdb::math::Transform::createLinearTransform(vs);
103
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 openvdb::points::PointAttributeVector<openvdb::Vec3f> wrap(positions);
104
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 auto index = openvdb::tools::createPointIndexGrid<openvdb::tools::PointIndexGrid>(wrap, vs);
105 auto points = openvdb::points::createPointDataGrid<openvdb::points::NullCodec,
106
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 openvdb::points::PointDataGrid>(*index, wrap, *transform);
107
4/6
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 48 times.
✓ Branch 3 taken 57 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 57 times.
✗ Branch 7 not taken.
105 for (auto c : callbacks) c(points->tree(), index->tree());
108 48 return points;
109 }
110
111 private:
112 double vs = 0.1;
113 std::vector<openvdb::Vec3f> positions = {};
114 std::vector<CallbackT1> callbacks = {};
115 };
116
117 #endif // OPENVDB_UNITTEST_POINT_BUILDER_HAS_BEEN_INCLUDED
118