Skip to content

Commit 19ea1ec

Browse files
committed
Add AdaptiveTree and Grid registration
Signed-off-by: Dan Bailey <danbailey@ilm.com>
1 parent f5f92dc commit 19ea1ec

6 files changed

Lines changed: 261 additions & 3 deletions

File tree

openvdb/openvdb/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ set(OPENVDB_LIBRARY_IO_INCLUDE_FILES
403403
io/TempFile.h
404404
)
405405

406+
set(OPENVDB_LIBRARY_ADAPTIVE_INCLUDE_FILES
407+
adaptive/AdaptiveGrid.h
408+
)
409+
406410
set(OPENVDB_LIBRARY_MATH_INCLUDE_FILES
407411
math/BBox.h
408412
math/ConjGradient.h
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Copyright Contributors to the OpenVDB Project
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
#ifndef OPENVDB_ADAPTIVE_ADAPTIVE_GRID_HAS_BEEN_INCLUDED
5+
#define OPENVDB_ADAPTIVE_ADAPTIVE_GRID_HAS_BEEN_INCLUDED
6+
7+
#include <openvdb/version.h>
8+
#include <openvdb/Grid.h>
9+
#include <openvdb/tree/Tree.h>
10+
11+
12+
namespace openvdb {
13+
OPENVDB_USE_VERSION_NAMESPACE
14+
namespace OPENVDB_VERSION_NAME {
15+
16+
17+
////////////////////////////////////////
18+
19+
20+
namespace adaptive {
21+
22+
23+
template<typename _ValueType>
24+
class AdaptiveTree final: public TreeBase
25+
{
26+
public:
27+
using Ptr = SharedPtr<AdaptiveTree>;
28+
using ConstPtr = SharedPtr<const AdaptiveTree>;
29+
30+
using ValueType = _ValueType;
31+
using BuildType = _ValueType;
32+
33+
static const Index DEPTH = 1;
34+
35+
AdaptiveTree() = default;
36+
37+
AdaptiveTree& operator=(const AdaptiveTree&) = delete; // disallow assignment
38+
39+
/// Deep copy constructor
40+
AdaptiveTree(const AdaptiveTree& other): TreeBase(other), mBackground(other.mBackground) { }
41+
42+
/// Empty tree constructor
43+
AdaptiveTree(const ValueType& background): mBackground(background) { }
44+
45+
~AdaptiveTree() override = default;
46+
47+
/// Return the name of this type of tree.
48+
static const Name& treeType();
49+
50+
/// Return the name of this tree's type.
51+
const Name& type() const override { OPENVDB_THROW(NotImplementedError, ""); }
52+
53+
/// Return the name of the type of a voxel's value (e.g., "float" or "vec3d").
54+
Name valueType() const override { OPENVDB_THROW(NotImplementedError, ""); }
55+
56+
/// Return @c true if this tree is of the same type as the template parameter.
57+
template<typename TreeType>
58+
bool isType() const { return (this->type() == TreeType::treeType()); }
59+
60+
/// Return a pointer to a deep copy of this tree
61+
TreeBase::Ptr copy() const override { OPENVDB_THROW(NotImplementedError, ""); }
62+
63+
/// @brief Return this tree's background value.
64+
const ValueType& background() const { return mBackground; }
65+
66+
/// @brief Return @c true if this tree contains no nodes.
67+
bool empty() const { return true; }
68+
69+
/// Remove all nodes.
70+
void clear() { }
71+
72+
/// @brief Not implemented.
73+
void prune(const ValueType& /*tolerance*/ = zeroVal<ValueType>()) { OPENVDB_THROW(NotImplementedError, ""); }
74+
75+
/// @brief Not implemented.
76+
void clip(const CoordBBox&) { OPENVDB_THROW(NotImplementedError, ""); }
77+
78+
//
79+
// Tree methods
80+
//
81+
82+
/// @brief Return in @a bbox the axis-aligned bounding box of all
83+
/// active tiles and leaf nodes with active values.
84+
/// @details This is faster than calling evalActiveVoxelBoundingBox,
85+
/// which visits the individual active voxels, and hence
86+
/// evalLeafBoundingBox produces a less tight, i.e. approximate, bbox.
87+
/// @return @c false if the bounding box is empty (in which case
88+
/// the bbox is set to its default value).
89+
bool evalLeafBoundingBox(CoordBBox& /*bbox*/) const override { OPENVDB_THROW(NotImplementedError, ""); }
90+
91+
/// @brief Return in @a dim the dimensions of the axis-aligned bounding box
92+
/// of all leaf nodes.
93+
/// @return @c false if the bounding box is empty.
94+
bool evalLeafDim(Coord& /*dim*/) const override { OPENVDB_THROW(NotImplementedError, ""); }
95+
96+
/// @brief Return in @a bbox the axis-aligned bounding box of all
97+
/// active voxels and tiles.
98+
/// @details This method produces a more accurate, i.e. tighter,
99+
/// bounding box than evalLeafBoundingBox which is approximate but
100+
/// faster.
101+
/// @return @c false if the bounding box is empty (in which case
102+
/// the bbox is set to its default value).
103+
bool evalActiveVoxelBoundingBox(CoordBBox& /*bbox*/) const override { OPENVDB_THROW(NotImplementedError, ""); }
104+
105+
/// @brief Return in @a dim the dimensions of the axis-aligned bounding box of all
106+
/// active voxels. This is a tighter bounding box than the leaf node bounding box.
107+
/// @return @c false if the bounding box is empty.
108+
bool evalActiveVoxelDim(Coord& /*dim*/) const override { OPENVDB_THROW(NotImplementedError, ""); }
109+
110+
void getIndexRange(CoordBBox& /*bbox*/) const override { OPENVDB_THROW(NotImplementedError, ""); }
111+
112+
/// @brief Replace with background tiles any nodes whose voxel buffers
113+
/// have not yet been allocated.
114+
/// @details Typically, unallocated nodes are leaf nodes whose voxel buffers
115+
/// are not yet resident in memory because delayed loading is in effect.
116+
/// @sa readNonresidentBuffers, io::File::open
117+
void clipUnallocatedNodes() override { OPENVDB_THROW(NotImplementedError, ""); }
118+
/// Return the total number of unallocated leaf nodes residing in this tree.
119+
Index32 unallocatedLeafCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
120+
121+
122+
//
123+
// Statistics
124+
//
125+
/// @brief Return the depth of this tree.
126+
///
127+
/// A tree with only a root node and leaf nodes has depth 2, for example.
128+
Index treeDepth() const override { OPENVDB_THROW(NotImplementedError, ""); }
129+
/// Return the number of leaf nodes.
130+
Index32 leafCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
131+
/// Return a vector with node counts. The number of nodes of type NodeType
132+
/// is given as element NodeType::LEVEL in the return vector. Thus, the size
133+
/// of this vector corresponds to the height (or depth) of this tree.
134+
std::vector<Index32> nodeCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
135+
/// Return the number of non-leaf nodes.
136+
Index32 nonLeafCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
137+
/// Return the number of active voxels stored in leaf nodes.
138+
Index64 activeLeafVoxelCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
139+
/// Return the number of inactive voxels stored in leaf nodes.
140+
Index64 inactiveLeafVoxelCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
141+
/// Return the total number of active voxels.
142+
Index64 activeVoxelCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
143+
/// Return the number of inactive voxels within the bounding box of all active voxels.
144+
Index64 inactiveVoxelCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
145+
/// Return the total number of active tiles.
146+
Index64 activeTileCount() const override { OPENVDB_THROW(NotImplementedError, ""); }
147+
148+
149+
//
150+
// I/O methods
151+
//
152+
/// Read all data buffers for this tree.
153+
void readBuffers(std::istream&, bool /*saveFloatAsHalf*/ = false) override { OPENVDB_THROW(NotImplementedError, ""); }
154+
/// Read all of this tree's data buffers that intersect the given bounding box.
155+
void readBuffers(std::istream&, const CoordBBox&, bool /*saveFloatAsHalf*/ = false) override { OPENVDB_THROW(NotImplementedError, ""); }
156+
/// @brief Read all of this tree's data buffers that are not yet resident in memory
157+
/// (because delayed loading is in effect).
158+
/// @details If this tree was read from a memory-mapped file, this operation
159+
/// disconnects the tree from the file.
160+
/// @sa clipUnallocatedNodes, io::File::open, io::MappedFile
161+
void readNonresidentBuffers() const override { OPENVDB_THROW(NotImplementedError, ""); }
162+
/// Write out all the data buffers for this tree.
163+
void writeBuffers(std::ostream&, bool /*saveFloatAsHalf*/ = false) const override { OPENVDB_THROW(NotImplementedError, ""); }
164+
165+
/// @brief Print statistics, memory usage and other information about this tree.
166+
/// @param os a stream to which to write textual information
167+
/// @param verboseLevel 1: print tree configuration only;
168+
/// 2: include node and voxel statistics;
169+
/// 3: include memory usage;
170+
/// 4: include minimum and maximum voxel values
171+
/// @warning @a verboseLevel 4 forces loading of any unallocated nodes.
172+
void print(std::ostream& /*os*/ = std::cout, int /*verboseLevel*/ = 1) const override { OPENVDB_THROW(NotImplementedError, ""); }
173+
174+
/// @brief Dummy declarations to keep Grid class happy
175+
using LeafNodeType = void;
176+
using ValueAllIter = void;
177+
using ValueAllCIter = void;
178+
using ValueOnIter = void;
179+
using ValueOnCIter = void;
180+
using ValueOffIter = void;
181+
using ValueOffCIter = void;
182+
183+
private:
184+
ValueType mBackground = zeroVal<ValueType>();
185+
}; // class AdaptiveTree
186+
187+
188+
189+
////////////////////////////////////////
190+
191+
192+
template<typename ValueType>
193+
inline const Name&
194+
AdaptiveTree<ValueType>::treeType()
195+
{
196+
static std::string sTreeTypeName = []()
197+
{
198+
std::ostringstream ostr;
199+
ostr << "Adaptive_Tree_" << typeNameAsString<BuildType>();
200+
return ostr.str();
201+
}();
202+
return sTreeTypeName;
203+
}
204+
205+
206+
/// @brief Adaptive grid.
207+
template <typename T>
208+
using AdaptiveGrid = Grid<AdaptiveTree<T>>;
209+
210+
using FloatAdaptiveGrid = AdaptiveGrid<float>;
211+
212+
using AdaptiveGridTypes = TypeList<FloatAdaptiveGrid>;
213+
214+
} // namespace adaptive
215+
} // namespace OPENVDB_VERSION_NAME
216+
} // namespace openvdb
217+
218+
#endif // OPENVDB_ADAPTIVE_ADAPTIVE_GRID_HAS_BEEN_INCLUDED

openvdb/openvdb/openvdb.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ initialize()
8080

8181
// Register common grid types.
8282
GridBase::clearRegistry();
83-
GridTypes::foreach<RegisterGrid>();
83+
AllGridTypes::foreach<RegisterGrid>();
8484

8585
// Register types associated with point index grids.
8686
Metadata::registerType(typeNameAsString<PointIndex32>(), Int32Metadata::createMetadata);

openvdb/openvdb/openvdb.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Grid.h"
1414
#include "tree/Tree.h"
1515
#include "points/PointDataGrid.h"
16+
#include "adaptive/AdaptiveGrid.h"
1617
#include "io/File.h"
1718

1819

@@ -98,8 +99,8 @@ using NumericGridTypes = RealGridTypes::Append<IntegerGridTypes>;
9899
/// The Vec3 Grid types which OpenVDB will register by default.
99100
using Vec3GridTypes = TypeList<Vec3IGrid, Vec3SGrid, Vec3DGrid>;
100101

101-
/// The Grid types which OpenVDB will register by default.
102-
using GridTypes =
102+
/// The sparse Grid types.
103+
using SparseGridTypes =
103104
NumericGridTypes::
104105
Append<Vec3GridTypes>::
105106
Append<tools::PointIndexGrid>::
@@ -110,6 +111,15 @@ using GridTypes =
110111
Append<BoolGrid, MaskGrid>;
111112
/// @}
112113

114+
// for backwards-compatibility
115+
using GridTypes = SparseGridTypes;
116+
117+
/// The Grid types which OpenVDB will register by default.
118+
using AllGridTypes =
119+
SparseGridTypes::
120+
Append<adaptive::AdaptiveGridTypes>;
121+
/// @}
122+
113123

114124
namespace internal {
115125
template <typename T> using ToTreeType = typename T::TreeType;

openvdb/openvdb/unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if(OPENVDB_TESTS)
7676
else()
7777
list(APPEND UNITTEST_SOURCE_FILES
7878
TestActivate.cc
79+
TestAdaptive.cc
7980
TestAttributeArray.cc
8081
TestAttributeArrayString.cc
8182
TestAttributeGroup.cc
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright Contributors to the OpenVDB Project
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
#include <openvdb/openvdb.h>
5+
#include <openvdb/adaptive/AdaptiveGrid.h>
6+
7+
#include <gtest/gtest.h>
8+
9+
class TestAdaptive: public ::testing::Test
10+
{
11+
public:
12+
void SetUp() override { openvdb::initialize(); }
13+
void TearDown() override { openvdb::initialize(); }
14+
};
15+
16+
17+
////////////////////////////////////////
18+
19+
20+
TEST_F(TestAdaptive, test)
21+
{
22+
openvdb::adaptive::AdaptiveGrid<float> adaptiveGrid(5.0f);
23+
24+
EXPECT_EQ(adaptiveGrid.background(), 5.0f);
25+
}

0 commit comments

Comments
 (0)