OpenVDB  8.1.1
openvdb_houdini/openvdb_houdini/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 
37 using GridCRef = const openvdb::GridBase&;
38 
39 
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 
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;
112 }; // class VdbPrimCIterator
113 
114 
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 
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 
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 
217 GU_PrimVDB* createVdbPrimitive(GU_Detail& gdp, GridPtr grid, const char* name = nullptr);
218 
219 
231 GU_PrimVDB* replaceVdbPrimitive(GU_Detail& gdp, GridPtr grid, GEO_PrimVDB& src,
232  const bool copyAttrs = true, const char* name = nullptr);
233 
234 
238 bool evalGridBBox(GridCRef grid, UT_Vector3 corners[8], bool expandHalfVoxel = false);
239 
240 
243 openvdb::CoordBBox makeCoordBBox(const UT_BoundingBox&, const openvdb::math::Transform&);
244 
245 
258 void startLogForwarding(OP_OpTypeId);
259 
271 void stopLogForwarding(OP_OpTypeId);
272 
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 
293 
299 
302  openvdb::DoubleGrid>;
303 
308 
311 
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
VdbPrimIterator & operator++()
Advance to the next VDB primitive.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:154
A list of types (not necessarily unique)
Definition: TypeList.h:365
std::function< bool(const GU_PrimVDB &)> FilterFunc
Definition: openvdb_houdini/openvdb_houdini/Utils.h:49
Grid< FloatTree > FloatGrid
Definition: openvdb.h:45
Interrupter(const char *title=nullptr)
Definition: openvdb_houdini/openvdb_houdini/Utils.h:176
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.
~Interrupter()
Definition: openvdb_houdini/openvdb_houdini/Utils.h:179
OPENVDB_HOUDINI_API void startLogForwarding(OP_OpTypeId)
Start forwarding OpenVDB log messages to the Houdini error manager for all operators of the given typ...
#define OPENVDB_HOUDINI_API
Definition: Platform.h:248
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
void start(const char *name=nullptr)
Signal the start of an interruptible operation.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:186
Grid< BoolTree > BoolGrid
Common grid types.
Definition: openvdb.h:43
GU_PrimVDB * operator*() const
Return a pointer to the current VDB primitive (nullptr if at end).
Definition: openvdb_houdini/openvdb_houdini/Utils.h:161
GA_Offset getOffset() const
Definition: openvdb_houdini/openvdb_houdini/Utils.h:80
openvdb::GridBase::Ptr GridPtr
Definition: openvdb_houdini/openvdb_houdini/Utils.h:34
const GU_PrimVDB * operator->() const
Return a pointer to the current VDB primitive (nullptr if at end).
Definition: openvdb_houdini/openvdb_houdini/Utils.h:76
void end()
Signal the end of an interruptible operation.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:190
Abstract base class for typed grids.
Definition: Grid.h:77
typename typelist_internal::TSAppendImpl< Self, TypesToAppend... >::type Append
Append types, or the members of another TypeList, to this list.
Definition: TypeList.h:440
OPENVDB_HOUDINI_API openvdb::CoordBBox makeCoordBBox(const UT_BoundingBox &, const openvdb::math::Transform &)
Construct an index-space CoordBBox from a UT_BoundingBox.
VdbPrimCIterator & operator++()
Advance to the next VDB primitive.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:69
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...
Iterator over non-const VDB primitives on a geometry detail.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:121
VdbPrimIterator(GEO_Detail *gdp, GA_Range::safedeletions, const GA_PrimitiveGroup *group=nullptr, FilterFunc filter=FilterFunc())
Allow primitives to be deleted during iteration.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:146
Grid< Vec3DTree > Vec3DGrid
Definition: openvdb.h:50
GA_Index getIndex() const
Definition: openvdb_houdini/openvdb_houdini/Utils.h:81
const GU_PrimVDB * getPrimitive() const
Return a pointer to the current VDB primitive (nullptr if at end).
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
Grid< PointDataTree > PointDataGrid
Point data grid.
Definition: PointDataGrid.h:194
bool wasInterrupted(int percent=-1)
Check if an interruptible operation should be aborted.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:195
ScalarGridTypes::Append< Vec3GridTypes > VolumeGridTypes
Definition: openvdb_houdini/openvdb_houdini/Utils.h:312
Grid< Int32Tree > Int32Grid
Definition: openvdb.h:46
Iterator over const VDB primitives on a geometry detail.
Definition: openvdb_houdini/openvdb_houdini/Utils.h:46
OPENVDB_HOUDINI_API bool evalGridBBox(GridCRef grid, UT_Vector3 corners[8], bool expandHalfVoxel=false)
Return in corners the corners of the given grid&#39;s active voxel bounding box.
bool GEOvdbApply(const GEO_PrimVDB &vdb, OpT &op)
If the given primitive&#39;s grid resolves to one of the listed grid types, invoke the functor op on the ...
Definition: openvdb_houdini/openvdb_houdini/Utils.h:322
VolumeGridTypes::Append< PointGridTypes > AllGridTypes
Definition: openvdb_houdini/openvdb_houdini/Utils.h:314
Wrapper class that adapts a Houdini UT_Interrupt object for use with OpenVDB library routines...
Definition: openvdb_houdini/openvdb_houdini/Utils.h:173
Definition: AttributeTransferUtil.h:33
FilterFunc mFilter
Definition: openvdb_houdini/openvdb_houdini/Utils.h:111
Grid< DoubleTree > DoubleGrid
Definition: openvdb.h:44
openvdb::GridBase::ConstPtr GridCPtr
Definition: openvdb_houdini/openvdb_houdini/Utils.h:35
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.
Grid< Vec3STree > Vec3SGrid
Definition: openvdb.h:52
UT_SharedPtr< GA_GBPrimitiveIterator > mIter
Definition: openvdb_houdini/openvdb_houdini/Utils.h:110
VdbPrimIterator(GEO_Detail *gdp, const GA_PrimitiveGroup *group=nullptr, FilterFunc filter=FilterFunc())
Definition: openvdb_houdini/openvdb_houdini/Utils.h:133
OPENVDB_HOUDINI_API void stopLogForwarding(OP_OpTypeId)
Stop forwarding OpenVDB log messages to the Houdini error manager for all operators of the given type...
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
const GU_PrimVDB * operator*() const
Return a pointer to the current VDB primitive (nullptr if at end).
Definition: openvdb_houdini/openvdb_houdini/Utils.h:75
GU_PrimVDB * operator->() const
Return a pointer to the current VDB primitive (nullptr if at end).
Definition: openvdb_houdini/openvdb_houdini/Utils.h:162