OpenVDB: Utils.h Source File

OpenVDB 7.1.0

Utils.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
7
8 #ifndef OPENVDB_HOUDINI_UTILS_HAS_BEEN_INCLUDED
9 #define OPENVDB_HOUDINI_UTILS_HAS_BEEN_INCLUDED
10
11 #include "GU_PrimVDB.h"
12 #include <OP/OP_Node.h> // for OP_OpTypeId
13 #include <UT/UT_SharedPtr.h>
14 #include <UT/UT_Interrupt.h>
15 #include <openvdb/openvdb.h>
16 #include <functional>
17 #include <type_traits>
18
19
20 #ifdef SESI_OPENVDB
21 #ifdef OPENVDB_HOUDINI_API
22 #undef OPENVDB_HOUDINI_API
23 #define OPENVDB_HOUDINI_API
24 #endif
25 #endif
26
27 class GEO_PrimVDB;
28 class GU_Detail;
29 class UT_String;
30
31 namespace openvdb_houdini {
32
33 using Grid = openvdb::GridBase;
34 using GridPtr = openvdb::GridBase::Ptr;
35 using GridCPtr = openvdb::GridBase::ConstPtr;
36 using GridRef = openvdb::GridBase&;
37 using GridCRef = const openvdb::GridBase&;
38
39
46 class OPENVDB_HOUDINI_API VdbPrimCIterator
47 {
48 public:
49 using FilterFunc = std::function<bool (const GU_PrimVDB&)>;
50
60 explicit VdbPrimCIterator(const GEO_Detail* gdp, const GA_PrimitiveGroup* group = nullptr,
61 FilterFunc filter = FilterFunc());
62
63 VdbPrimCIterator(const VdbPrimCIterator&);
64 VdbPrimCIterator& operator=(const VdbPrimCIterator&);
65
67 void advance();
69 VdbPrimCIterator& operator++() { advance(); return *this; }
71
73 const GU_PrimVDB* getPrimitive() const;
75 const GU_PrimVDB* operator*() const { return getPrimitive(); }
76 const GU_PrimVDB* operator->() const { return getPrimitive(); }
78
80 GA_Offset getOffset() const { return getPrimitive()->getMapOffset(); }
81 GA_Index getIndex() const { return getPrimitive()->getMapIndex(); }
83
85 operator bool() const { return getPrimitive() != nullptr; }
86
91 UT_String getPrimitiveName(const UT_String& defaultName = "") const;
92
95 UT_String getPrimitiveNameOrIndex() const;
96
103 UT_String getPrimitiveIndexAndName(bool keepEmptyName = true) const;
104
105 protected:
107 VdbPrimCIterator(const GEO_Detail*, GA_Range::safedeletions,
108 const GA_PrimitiveGroup* = nullptr, FilterFunc = FilterFunc());
109
110 UT_SharedPtr<GA_GBPrimitiveIterator> mIter;
111 FilterFunc mFilter;
112 }; // class VdbPrimCIterator
113
114
121 class OPENVDB_HOUDINI_API VdbPrimIterator: public VdbPrimCIterator
122 {
123 public:
133 explicit VdbPrimIterator(GEO_Detail* gdp, const GA_PrimitiveGroup* group = nullptr,
134 FilterFunc filter = FilterFunc()):
135 VdbPrimCIterator(gdp, group, filter) {}
146 VdbPrimIterator(GEO_Detail* gdp, GA_Range::safedeletions,
147 const GA_PrimitiveGroup* group = nullptr, FilterFunc filter = FilterFunc()):
148 VdbPrimCIterator(gdp, GA_Range::safedeletions(), group, filter) {}
149
150 VdbPrimIterator(const VdbPrimIterator&);
151 VdbPrimIterator& operator=(const VdbPrimIterator&);
152
154 VdbPrimIterator& operator++() { advance(); return *this; }
155
157 GU_PrimVDB* getPrimitive() const {
159 return const_cast<GU_PrimVDB*>(VdbPrimCIterator::getPrimitive());
160 }
161 GU_PrimVDB* operator*() const { return getPrimitive(); }
162 GU_PrimVDB* operator->() const { return getPrimitive(); }
164 }; // class VdbPrimIterator
165
166
168
169
173 class Interrupter
174 {
175 public:
176 explicit Interrupter(const char* title = nullptr):
177 mUTI{UTgetInterrupt()}, mRunning{false}, mTitle{title ? title : ""}
178 {}
179 ~Interrupter() { if (mRunning) this->end(); }
180
181 Interrupter(const Interrupter&) = default;
182 Interrupter& operator=(const Interrupter&) = default;
183
186 void start(const char* name = nullptr) {
187 if (!mRunning) { mRunning = true; mUTI->opStart(name ? name : mTitle.c_str()); }
188 }
190 void end() { if (mRunning) { mUTI->opEnd(); mRunning = false; } }
191
195 bool wasInterrupted(int percent=-1) { return mUTI->opInterrupt(percent); }
196
197 private:
198 UT_Interrupt* mUTI;
199 bool mRunning;
200 std::string mTitle;
201 };
202
203
205
206
207 // Utility methods
208
216 OPENVDB_HOUDINI_API
217 GU_PrimVDB* createVdbPrimitive(GU_Detail& gdp, GridPtr grid, const char* name = nullptr);
218
219
230 OPENVDB_HOUDINI_API
231 GU_PrimVDB* replaceVdbPrimitive(GU_Detail& gdp, GridPtr grid, GEO_PrimVDB& src,
232 const bool copyAttrs = true, const char* name = nullptr);
233
234
237 OPENVDB_HOUDINI_API
238 bool evalGridBBox(GridCRef grid, UT_Vector3 corners[8], bool expandHalfVoxel = false);
239
240
242 OPENVDB_HOUDINI_API
243 openvdb::CoordBBox makeCoordBBox(const UT_BoundingBox&, const openvdb::math::Transform&);
244
245
257 OPENVDB_HOUDINI_API
258 void startLogForwarding(OP_OpTypeId);
259
270 OPENVDB_HOUDINI_API
271 void stopLogForwarding(OP_OpTypeId);
272
276 OPENVDB_HOUDINI_API
277 bool isLogForwarding(OP_OpTypeId);
279
280
282
283
284 // Grid type lists, for use with GEO_PrimVDB::apply(), GEOvdbApply(),
285 // or openvdb::GridBase::apply()
286
287 using ScalarGridTypes = openvdb::TypeList<
288 openvdb::BoolGrid,
289 openvdb::FloatGrid,
290 openvdb::DoubleGrid,
291 openvdb::Int32Grid,
292 openvdb::Int64Grid>;
293
294 using NumericGridTypes = openvdb::TypeList<
295 openvdb::FloatGrid,
296 openvdb::DoubleGrid,
297 openvdb::Int32Grid,
298 openvdb::Int64Grid>;
299
300 using RealGridTypes = openvdb::TypeList<
301 openvdb::FloatGrid,
302 openvdb::DoubleGrid>;
303
304 using Vec3GridTypes = openvdb::TypeList<
305 openvdb::Vec3SGrid,
306 openvdb::Vec3DGrid,
307 openvdb::Vec3IGrid>;
308
309 using PointGridTypes = openvdb::TypeList<
310 openvdb::points::PointDataGrid>;
311
312 using VolumeGridTypes = ScalarGridTypes::Append<Vec3GridTypes>;
313
314 using AllGridTypes = VolumeGridTypes::Append<PointGridTypes>;
315
316
320 template<typename GridTypeListT, typename OpT>
321 inline bool
322 GEOvdbApply(const GEO_PrimVDB& vdb, OpT& op)
323 {
324 if (auto gridPtr = vdb.getConstGridPtr()) {
325 return gridPtr->apply<GridTypeListT>(op);
326 }
327 return false;
328 }
329
335 template<typename GridTypeListT, typename OpT>
336 inline bool
337 GEOvdbApply(GEO_PrimVDB& vdb, OpT& op, bool makeUnique = true)
338 {
339 if (vdb.hasGrid()) {
340 auto gridPtr = vdb.getGridPtr();
341 if (makeUnique) {
342 auto treePtr = gridPtr->baseTreePtr();
343 if (treePtr.use_count() > 2) { // grid + treePtr = 2
344 // If the grid resolves to one of the listed types and its tree
345 // is shared with other grids, replace the tree with a deep copy.
346 gridPtr->apply<GridTypeListT>(
347 [](Grid& baseGrid) { baseGrid.setTree(baseGrid.constBaseTree().copy()); });
348 }
349 }
350 return gridPtr->apply<GridTypeListT>(op);
351 }
352 return false;
353 }
354
355 } // namespace openvdb_houdini
356
357 #endif // OPENVDB_HOUDINI_UTILS_HAS_BEEN_INCLUDED
openvdb::v7_1::TypeList
A list of types (not necessarily unique)
Definition: Types.h:653
openvdb::v7_1::Vec3SGrid
Grid< Vec3STree > Vec3SGrid
Definition: openvdb.h:52
openvdb::v7_1::GridBase
Abstract base class for typed grids.
Definition: Grid.h:78
openvdb::v7_1::points::PointDataGrid
Grid< PointDataTree > PointDataGrid
Point data grid.
Definition: PointDataGrid.h:189
openvdb_houdini::VdbPrimIterator::operator*
GU_PrimVDB * operator*() const
Definition: Utils.h:161
openvdb_houdini
Definition: AttributeTransferUtil.h:33
openvdb_houdini::Interrupter::operator=
Interrupter & operator=(const Interrupter &)=default
openvdb_houdini::stopLogForwarding
OPENVDB_HOUDINI_API void stopLogForwarding(OP_OpTypeId)
Stop forwarding OpenVDB log messages to the Houdini error manager for all operators of the given type...
openvdb_houdini::Interrupter::wasInterrupted
bool wasInterrupted(int percent=-1)
Check if an interruptible operation should be aborted.
Definition: Utils.h:195
openvdb_houdini::VdbPrimCIterator::VdbPrimCIterator
VdbPrimCIterator(const GEO_Detail *, GA_Range::safedeletions, const GA_PrimitiveGroup *=nullptr, FilterFunc=FilterFunc())
Allow primitives to be deleted during iteration.
openvdb::v7_1::TypeList::Append
typename internal::TSAppendImpl< Self, TypesToAppend... >::type Append
Append types, or the members of another TypeList, to this list.
Definition: Types.h:725
openvdb::v7_1::GridBase::Ptr
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
openvdb_houdini::Interrupter
Wrapper class that adapts a Houdini UT_Interrupt object for use with OpenVDB library routines.
Definition: Utils.h:174
openvdb_houdini::VdbPrimIterator::operator=
VdbPrimIterator & operator=(const VdbPrimIterator &)
openvdb_houdini::VdbPrimCIterator::getOffset
GA_Offset getOffset() const
Definition: Utils.h:80
openvdb_houdini::VdbPrimCIterator::VdbPrimCIterator
VdbPrimCIterator(const VdbPrimCIterator &)
openvdb_houdini::VolumeGridTypes
ScalarGridTypes::Append< Vec3GridTypes > VolumeGridTypes
Definition: Utils.h:312
openvdb_houdini::AllGridTypes
VolumeGridTypes::Append< PointGridTypes > AllGridTypes
Definition: Utils.h:314
openvdb_houdini::Interrupter::Interrupter
Interrupter(const char *title=nullptr)
Definition: Utils.h:176
openvdb_houdini::VdbPrimCIterator::getPrimitiveName
UT_String getPrimitiveName(const UT_String &defaultName="") const
Return the value of the current VDB primitive's name attribute.
openvdb_houdini::VdbPrimIterator
Iterator over non-const VDB primitives on a geometry detail.
Definition: Utils.h:122
openvdb_houdini::VdbPrimCIterator::VdbPrimCIterator
VdbPrimCIterator(const GEO_Detail *gdp, const GA_PrimitiveGroup *group=nullptr, FilterFunc filter=FilterFunc())
openvdb::v7_1::Grid
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
openvdb::v7_1::math::CoordBBox
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:249
openvdb_houdini::startLogForwarding
OPENVDB_HOUDINI_API void startLogForwarding(OP_OpTypeId)
Start forwarding OpenVDB log messages to the Houdini error manager for all operators of the given typ...
openvdb_houdini::VdbPrimCIterator::operator=
VdbPrimCIterator & operator=(const VdbPrimCIterator &)
openvdb::v7_1::BoolGrid
Grid< BoolTree > BoolGrid
Common grid types.
Definition: openvdb.h:43
openvdb_houdini::GridCPtr
openvdb::GridBase::ConstPtr GridCPtr
Definition: Utils.h:35
openvdb::v7_1::Vec3DGrid
Grid< Vec3DTree > Vec3DGrid
Definition: openvdb.h:50
openvdb_houdini::VdbPrimCIterator::getPrimitiveIndexAndName
UT_String getPrimitiveIndexAndName(bool keepEmptyName=true) const
Return a string of the form "N (NAME)", where N is the current VDB primitive's index and NAME is the ...
openvdb_houdini::VdbPrimCIterator::getIndex
GA_Index getIndex() const
Definition: Utils.h:81
openvdb_houdini::VdbPrimCIterator::getPrimitive
const GU_PrimVDB * getPrimitive() const
Return a pointer to the current VDB primitive (nullptr if at end).
GU_PrimVDB.h
openvdb_houdini::VdbPrimCIterator::operator*
const GU_PrimVDB * operator*() const
Definition: Utils.h:75
openvdb_houdini::VdbPrimCIterator::getPrimitiveNameOrIndex
UT_String getPrimitiveNameOrIndex() const
Return the value of the current VDB primitive's name attribute or, if the name is empty,...
openvdb_houdini::VdbPrimCIterator::mIter
UT_SharedPtr< GA_GBPrimitiveIterator > mIter
Definition: Utils.h:110
openvdb_houdini::VdbPrimCIterator::operator->
const GU_PrimVDB * operator->() const
Definition: Utils.h:76
openvdb_houdini::VdbPrimCIterator
Iterator over const VDB primitives on a geometry detail.
Definition: Utils.h:47
openvdb_houdini::GridPtr
openvdb::GridBase::Ptr GridPtr
Definition: Utils.h:34
openvdb_houdini::createVdbPrimitive
OPENVDB_HOUDINI_API GU_PrimVDB * createVdbPrimitive(GU_Detail &gdp, GridPtr grid, const char *name=nullptr)
Store a VDB grid in a new VDB primitive and add the primitive to a geometry detail.
openvdb::v7_1::GridBase::ConstPtr
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
openvdb_houdini::VdbPrimIterator::VdbPrimIterator
VdbPrimIterator(GEO_Detail *gdp, GA_Range::safedeletions, const GA_PrimitiveGroup *group=nullptr, FilterFunc filter=FilterFunc())
Allow primitives to be deleted during iteration.
Definition: Utils.h:146
openvdb_houdini::makeCoordBBox
OPENVDB_HOUDINI_API openvdb::CoordBBox makeCoordBBox(const UT_BoundingBox &, const openvdb::math::Transform &)
Construct an index-space CoordBBox from a UT_BoundingBox.
openvdb_houdini::VdbPrimIterator::VdbPrimIterator
VdbPrimIterator(GEO_Detail *gdp, const GA_PrimitiveGroup *group=nullptr, FilterFunc filter=FilterFunc())
Definition: Utils.h:133
openvdb_houdini::Interrupter::end
void end()
Signal the end of an interruptible operation.
Definition: Utils.h:190
openvdb::v7_1::Int32Grid
Grid< Int32Tree > Int32Grid
Definition: openvdb.h:46
openvdb_houdini::VdbPrimIterator::operator++
VdbPrimIterator & operator++()
Advance to the next VDB primitive.
Definition: Utils.h:154
openvdb_houdini::Interrupter::~Interrupter
~Interrupter()
Definition: Utils.h:179
openvdb_houdini::VdbPrimIterator::VdbPrimIterator
VdbPrimIterator(const VdbPrimIterator &)
openvdb_houdini::evalGridBBox
OPENVDB_HOUDINI_API bool evalGridBBox(GridCRef grid, UT_Vector3 corners[8], bool expandHalfVoxel=false)
Return in corners the corners of the given grid's active voxel bounding box.
openvdb_houdini::VdbPrimCIterator::FilterFunc
std::function< bool(const GU_PrimVDB &)> FilterFunc
Definition: Utils.h:49
openvdb_houdini::GEOvdbApply
bool GEOvdbApply(const GEO_PrimVDB &vdb, OpT &op)
If the given primitive's grid resolves to one of the listed grid types, invoke the functor op on the ...
Definition: Utils.h:322
openvdb_houdini::Interrupter::start
void start(const char *name=nullptr)
Signal the start of an interruptible operation.
Definition: Utils.h:186
openvdb_houdini::replaceVdbPrimitive
OPENVDB_HOUDINI_API GU_PrimVDB * replaceVdbPrimitive(GU_Detail &gdp, GridPtr grid, GEO_PrimVDB &src, const bool copyAttrs=true, const char *name=nullptr)
Replace an existing VDB primitive with a new primitive that contains the given grid.
openvdb::v7_1::FloatGrid
Grid< FloatTree > FloatGrid
Definition: openvdb.h:45
openvdb_houdini::isLogForwarding
OPENVDB_HOUDINI_API bool isLogForwarding(OP_OpTypeId)
Return true if OpenVDB messages logged by operators of the given type are forwarded to the Houdini er...
OPENVDB_HOUDINI_API
#define OPENVDB_HOUDINI_API
Definition: Platform.h:238
openvdb::v7_1::DoubleGrid
Grid< DoubleTree > DoubleGrid
Definition: openvdb.h:44
openvdb_houdini::VdbPrimCIterator::mFilter
FilterFunc mFilter
Definition: Utils.h:111
openvdb.h
openvdb_houdini::Interrupter::Interrupter
Interrupter(const Interrupter &)=default
openvdb_houdini::VdbPrimCIterator::operator++
VdbPrimCIterator & operator++()
Definition: Utils.h:69
openvdb_houdini::VdbPrimIterator::operator->
GU_PrimVDB * operator->() const
Definition: Utils.h:162