25#if !defined(SESI_OPENVDB) && !defined(SESI_OPENVDB_PRIM)
27#include <GEO/GEO_PrimVDB.h>
30using ::GEO_VolumeOptions;
36#ifndef __HDK_GEO_PrimVDB__
37#define __HDK_GEO_PrimVDB__
39#include <GEO/GEO_Primitive.h>
40#include <GEO/GEO_VolumeOptions.h>
41#include <GA/GA_Defines.h>
43#include <SYS/SYS_AtomicInt.h>
45#include <UT/UT_BoundingBox.h>
54class GEO_PrimVolumeXform;
55class UT_MemoryCounter;
62 typedef uint64 UniqueId;
67 GEO_PrimVDB(GEO_Detail *d, GA_Offset offset = GA_INVALID_OFFSET);
69 ~GEO_PrimVDB()
override;
71 static GA_PrimitiveFamilyMask buildFamilyMask()
72 {
return GA_FAMILY_NONE; }
76 bool isDegenerate()
const override;
77 bool getBBox(UT_BoundingBox *bbox)
const override;
78 void reverse()
override;
79 UT_Vector3 computeNormal()
const override;
80 UT_Vector3D computeNormalD()
const override;
81 void copyPrimitive(
const GEO_Primitive *src)
override;
82 void copySubclassData(
const GA_Primitive *source)
override;
89 CE_VDBGrid *getCEGrid(
bool read,
bool write)
const;
93 void flushCEWriteCaches()
override;
97 void flushCECaches()
override;
100 void stealCEBuffers(
const GA_Primitive *src)
override;
102 using GEO_Primitive::getVertexOffset;
103 using GEO_Primitive::getPointOffset;
104 using GEO_Primitive::setPointOffset;
105 using GEO_Primitive::getPos3;
106 using GEO_Primitive::setPos3;
108 GA_Offset getVertexOffset()
const
109 {
return getVertexOffset(0); }
111 GA_Offset getPointOffset()
const
112 {
return getPointOffset(0); }
114 void setPointOffset(GA_Offset pt)
115 { setPointOffset(0, pt); }
117 UT_Vector3 getPos3()
const
118 {
return getPos3(0); }
120 void setPos3(
const UT_Vector3 &pos)
125 void indexToPos(
int x,
int y,
int z, UT_Vector3 &pos)
const;
126 void findexToPos(UT_Vector3 index, UT_Vector3 &pos)
const;
127 void indexToPos(exint x, exint y, exint z, UT_Vector3D &pos)
const;
128 void findexToPos(UT_Vector3D index, UT_Vector3D &pos)
const;
131 void posToIndex(UT_Vector3 pos,
int &x,
int &y,
int &z)
const;
132 void posToIndex(UT_Vector3 pos, UT_Vector3 &index)
const;
133 void posToIndex(UT_Vector3D pos, exint &x, exint &y, exint &z)
const;
134 void posToIndex(UT_Vector3D pos, UT_Vector3D &index)
const;
139 fpreal getValueF(
const UT_Vector3 &pos)
const;
140 fpreal getValueAtIndexF(
int ix,
int iy,
int iz)
const;
141 UT_Vector3D getValueV3(
const UT_Vector3 &pos)
const;
142 UT_Vector3D getValueAtIndexV3(
int ix,
int iy,
int iz)
const;
144 void getValues(
float *f,
int stride,
const UT_Vector3 *pos,
int num)
const;
145 void getValues(
int *f,
int stride,
const UT_Vector3 *pos,
int num)
const;
146 void getValuesAtIndices(
float *f,
int stride,
const int *ix,
const int *iy,
const int *iz,
int num)
const;
147 void getValuesAtIndices(
int *f,
int stride,
const int *ix,
const int *iy,
const int *iz,
int num)
const;
150 void getValues(UT_Vector3 *f,
int stride,
const UT_Vector3 *pos,
int num)
const;
151 void getValuesAtIndices(UT_Vector3 *f,
int stride,
const int *ix,
const int *iy,
const int *iz,
int num)
const;
153 void getValues(
double *f,
int stride,
const UT_Vector3D *pos,
int num)
const;
154 void getValues(exint *f,
int stride,
const UT_Vector3D *pos,
int num)
const;
155 void getValuesAtIndices(
double *f,
int stride,
const exint *ix,
const exint *iy,
const exint *iz,
int num)
const;
156 void getValuesAtIndices(exint *f,
int stride,
const exint *ix,
const exint *iy,
const exint *iz,
int num)
const;
159 void getValues(UT_Vector3D *f,
int stride,
const UT_Vector3D *pos,
int num)
const;
160 void getValuesAtIndices(UT_Vector3D *f,
int stride,
const exint *ix,
const exint *iy,
const exint *iz,
int num)
const;
163 UT_Vector3 getGradient(
const UT_Vector3 &pos)
const;
170 UT_Vector3 *gradients,
171 int gradients_stride,
172 const UT_Vector3 *positions,
174 bool normalize =
false)
const;
178 UT_VDBType getStorageType()
const
179 {
return myGridAccessor.getStorageType(); }
182 int getTupleSize()
const
183 {
return UTvdbGetGridTupleSize(getStorageType()); }
188 bool isAligned(
const GEO_PrimVDB *vdb)
const;
190 bool isActiveRegionMatched(
const GEO_PrimVDB *vdb)
const;
194 bool isWorldAxisAligned()
const;
198 void transform(
const UT_Matrix4 &mat)
override;
202 bool isEmpty()
const {
return getGridPtr()->empty(); }
205 fpreal backgroundF()
const;
206 UT_Vector3D backgroundV3()
const;
212 void setTransform4(
const UT_DMatrix4 &xform4);
213 void setTransform4(
const UT_Matrix4 &xform4);
214 UT_Matrix4D getTransform4()
const;
222 int detachPoints(GA_PointGroup &grp)
override;
226 GA_DereferenceStatus dereferencePoint(GA_Offset point,
227 bool dry_run=
false)
override;
228 GA_DereferenceStatus dereferencePoints(
const GA_RangeMemberQuery &pt_q,
229 bool dry_run=
false)
override;
230 const GA_PrimitiveJSON *getJSON()
const override;
234 void assignVertex(GA_Offset new_vtx,
bool update_topology);
237 bool evaluatePointRefMap(
238 GA_Offset result_vtx,
239 GA_AttributeRefMap &hlist,
241 uint du, uint dv)
const override;
245 float u,
float v = 0,
246 unsigned du=0,
unsigned dv=0)
const override
248 return GEO_Primitive::evaluatePointV4(pos, u, v,
262 GEO_PrimVolumeXform getIndexSpaceTransform()
const;
268 GEO_PrimVolumeXform getSpaceTransform()
const;
277 GEO_PrimVolumeXform getSpaceTransform(
const UT_BoundingBoxD &bbox)
const;
283 void setSpaceTransform(
const GEO_PrimVolumeXform &space,
284 const UT_Vector3R &resolution,
285 bool force_taper =
false);
289 fpreal getTaper()
const;
293 void getRes(
int &rx,
int &ry,
int &rz)
const;
297 fpreal getVoxelDiameter()
const;
300 UT_Vector3 getVoxelSize()
const;
303 fpreal calcMinimum()
const;
304 fpreal calcMaximum()
const;
305 fpreal calcAverage()
const;
313 bool getFrustumBounds(UT_BoundingBox &idxbox)
const;
315 enum ActivateOperation
328 void activateIndexBBox(
330 ActivateOperation operation,
331 bool setvalue, fpreal
value)
333 activateIndexBBoxAdapter(
334 &bbox, operation, setvalue,
value);
341 void activateByVDB(
const GEO_PrimVDB *vdb,
342 ActivateOperation operation,
343 bool setvalue, fpreal
value,
344 bool ignore_transform=
false);
349 GEO_Primitive *copy(
int preserve_shared_pts = 0)
const override;
352 void stashed(
bool beingstashed,
353 GA_Offset offset=GA_INVALID_OFFSET)
override;
360 UT_Vector3 baryCenter()
const override;
361 fpreal calcVolume(
const UT_Vector3 &refpt)
const override;
364 fpreal calcArea()
const override;
372 bool enlargeBoundingBox(
374 const GA_Attribute *P)
const override;
375 bool enlargeBoundingBox(
377 const GA_Attribute *P)
const override;
378 void enlargePointBounds(UT_BoundingBox &e)
const override;
384 bool enlargeBoundingSphere(
385 UT_BoundingSphere &b,
386 const GA_Attribute *P)
const override;
392 void getLocalTransform(UT_Matrix3D &result)
const override;
393 void setLocalTransform(
const UT_Matrix3D &new_mat3)
override;
399 static bool conditionMatrix(UT_Matrix4D &mat4);
403 const GEO_VolumeOptions &getVisOptions()
const {
return myVis; }
404 void setVisOptions(
const GEO_VolumeOptions &vis)
405 { setVisualization(vis.myMode, vis.myIso, vis.myDensity, vis.myLod); }
407 void setVisualization(
408 GEO_VolumeVis vismode,
411 GEO_VolumeVisLod lod = GEO_VOLUMEVISLOD_FULL)
413 myVis.myMode = vismode;
415 myVis.myDensity = density;
418 GEO_VolumeVis getVisualization()
const {
return myVis.myMode; }
419 fpreal getVisIso()
const {
return myVis.myIso; }
420 fpreal getVisDensity()
const {
return myVis.myDensity; }
421 GEO_VolumeVisLod getVisLod()
const {
return myVis.myLod; }
425 bool loadOrder(
const UT_JSONValue &p);
429 bool saveVDB(UT_JSONWriter &w,
const GA_SaveMap &sm,
430 bool as_shmem =
false)
const;
431 bool loadVDB(UT_JSONParser &p,
432 bool as_shmem =
false);
435 bool saveVisualization(
437 const GA_SaveMap &map)
const;
438 bool loadVisualization(
440 const GA_LoadMap &map);
443 GA_Offset fastVertexOffset(GA_Size UT_IF_ASSERT_P(index))
const
445 UT_ASSERT_P(index < 1);
446 return getVertexOffset();
449 void setVertexPoint(
int i, GA_Offset pt)
457 fpreal calcPositiveDensity()
const;
460 bool hasGrid()
const {
return myGridAccessor.hasGrid(); }
466 void makeGridUnique()
467 { myGridAccessor.makeGridUnique(); }
471 bool isGridUnique()
const
472 {
return myGridAccessor.isGridUnique(); }
478 {
return myGridAccessor.getConstGrid(*
this); }
483 {
return getConstGrid(); }
491 return myGridAccessor.getGrid(*
this);
498 openvdb::GridBase::ConstPtr getConstGridPtr()
const
499 {
return myGridAccessor.getConstGridPtr(*
this); }
504 openvdb::GridBase::ConstPtr getGridPtr()
const
505 {
return getConstGridPtr(); }
511 openvdb::GridBase::Ptr getGridPtr()
514 return myGridAccessor.getGridPtr(*
this);
523 myGridAccessor.setGrid(grid, *
this, copyPosition);
529 {
return getConstGrid(); }
533 {
return getConstGrid(); }
539 incrMetadataUniqueId();
540 return myGridAccessor.getGrid(*
this);
545 const char * getGridName()
const;
549 UniqueId getUniqueId()
const
550 {
return static_cast<UniqueId
>(myUniqueId.relaxedLoad()); }
556 UniqueId getTreeUniqueId()
const
557 {
return static_cast<UniqueId
>(myTreeUniqueId.relaxedLoad()); }
562 UniqueId getMetadataUniqueId()
const
563 {
return static_cast<UniqueId
>(myMetadataUniqueId.relaxedLoad()); }
568 UniqueId getTransformUniqueId()
const
569 {
return static_cast<UniqueId
>(myTransformUniqueId.relaxedLoad()); }
584 template<
typename Gr
idTypeListT,
typename OpT>
585 bool apply(OpT& op)
const
586 {
return hasGrid() ? getConstGrid().apply<GridTypeListT>(op) :
false; }
606 template<
typename Gr
idTypeListT,
typename OpT>
607 bool apply(OpT& op,
bool makeUnique =
true)
610 auto& grid = myGridAccessor.getGrid(*
this);
613 if (treePtr.use_count() > 2) {
617 baseGrid.setTree(baseGrid.constBaseTree().copy());
618 this->incrTreeUniqueId();
622 if (grid.
apply<GridTypeListT>(op)) {
631 typedef SYS_AtomicCounter AtomicUniqueId;
634 GA_DECLARE_INTRINSICS(
override)
637 static
bool isIntrinsicMetadata(const
char *name);
640 GA_Offset vertexPoint(GA_Size)
const
641 {
return getPointOffset(); }
645 int64 getBaseMemoryUsage()
const;
649 void countBaseMemory(UT_MemoryCounter &counter)
const;
653 static UniqueId nextUniqueId();
655 void incrTreeUniqueId()
656 { myTreeUniqueId.maximum(nextUniqueId()); }
657 void incrMetadataUniqueId()
658 { myMetadataUniqueId.maximum(nextUniqueId()); }
659 void incrTransformUniqueId()
660 { myTransformUniqueId.maximum(nextUniqueId()); }
661 void incrGridUniqueIds()
664 incrMetadataUniqueId();
665 incrTransformUniqueId();
670 void copyGridFrom(
const GEO_PrimVDB&,
bool copyPosition=
true);
689 GridAccessor() : myStorageType(UT_VDB_INVALID)
696 myStorageType = UT_VDB_INVALID;
701 getGrid(
const GEO_PrimVDB &prim)
702 { updateGridTranslates(prim);
return *myGrid; }
706 getConstGrid(
const GEO_PrimVDB &prim)
const
707 { updateGridTranslates(prim);
return *myGrid; }
710 openvdb::GridBase::Ptr
711 getGridPtr(
const GEO_PrimVDB &prim)
712 { updateGridTranslates(prim);
return myGrid; }
715 openvdb::GridBase::ConstPtr
716 getConstGridPtr(
const GEO_PrimVDB &prim)
const
717 { updateGridTranslates(prim);
return myGrid; }
722 void setGrid(
const openvdb::GridBase& grid, GEO_PrimVDB& prim,
bool copyPosition=
true)
723 { setGridAdapter(&grid, prim, copyPosition); }
726 const openvdb::math::Transform &xform,
728 { setTransformAdapter(&xform, prim); }
730 void makeGridUnique();
731 bool isGridUnique()
const;
734 UT_VDBType getStorageType()
const {
return myStorageType; }
737 bool hasGrid()
const {
return myGrid != 0; }
740 void updateGridTranslates(
const GEO_PrimVDB &prim)
const;
743 void setVertexPosition(
744 const openvdb::math::Transform &xform,
746 { setVertexPositionAdapter(&xform, prim); }
748 void setGridAdapter(
const void* grid, GEO_PrimVDB&,
bool copyPosition);
749 void setTransformAdapter(
const void* xform, GEO_PrimVDB&);
750 void setVertexPositionAdapter(
const void* xform, GEO_PrimVDB&);
753 openvdb::GridBase::Ptr myGrid;
754 UT_VDBType myStorageType;
758 void activateIndexBBoxAdapter(
761 bool setvalue, fpreal
value);
764 GridAccessor myGridAccessor;
766 GEO_VolumeOptions myVis;
768 mutable CE_VDBGrid *myCEGrid;
769 mutable bool myCEGridAuthorative;
770 mutable bool myCEGridIsOwned;
772 AtomicUniqueId myUniqueId;
773 AtomicUniqueId myTreeUniqueId;
774 AtomicUniqueId myMetadataUniqueId;
775 AtomicUniqueId myTransformUniqueId;
782using ::GEO_VolumeOptions;
791namespace UT_VDBUtils {
798template<
typename Gr
idType,
typename OpType>
800callTypedGrid(GEO_PrimVDB& prim, OpType& op)
802 prim.makeGridUnique();
803 op.template operator()<
GridType>(*(UTverify_cast<GridType*>(&prim.getGrid())));
807template<
typename Gr
idType,
typename OpType>
809callTypedGrid(
const GEO_PrimVDB& prim, OpType& op)
811 op.template operator()<
GridType>(*(UTverify_cast<const GridType*>(&prim.getConstGrid())));
818UT_VDB_DECL_PROCESS_TYPED_GRID(GEO_PrimVDB&)
819UT_VDB_DECL_PROCESS_TYPED_GRID(
const GEO_PrimVDB&)
828template <
typename OpT>
829inline bool GEOvdbProcessTypedGrid(
const GEO_PrimVDB &vdb, OpT &op)
831 return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb.getGrid(), op);
834template <
typename OpT>
835inline bool GEOvdbProcessTypedGridReal(
const GEO_PrimVDB &vdb, OpT &op)
837 return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb.getGrid(), op);
840template <
typename OpT>
841inline bool GEOvdbProcessTypedGridScalar(
const GEO_PrimVDB &vdb, OpT &op)
843 return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb.getGrid(), op);
846template <
typename OpT>
847inline bool GEOvdbProcessTypedGridTopology(
const GEO_PrimVDB &vdb, OpT &op)
849 return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb.getGrid(), op);
852template <
typename OpT>
853inline bool GEOvdbProcessTypedGridVec3(
const GEO_PrimVDB &vdb, OpT &op)
855 return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb.getGrid(), op);
858template <
typename OpT>
859inline bool GEOvdbProcessTypedGridPoint(
const GEO_PrimVDB &vdb, OpT &op)
861 return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb.getGrid(), op);
872template <
typename OpT>
873inline bool GEOvdbProcessTypedGrid(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
875 if (makeUnique)
return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb, op);
876 return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb.getGrid(), op);
879template <
typename OpT>
880inline bool GEOvdbProcessTypedGridReal(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
882 if (makeUnique)
return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb, op);
883 return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb.getGrid(), op);
886template <
typename OpT>
887inline bool GEOvdbProcessTypedGridScalar(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
889 if (makeUnique)
return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb, op);
890 return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb.getGrid(), op);
893template <
typename OpT>
894inline bool GEOvdbProcessTypedGridTopology(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
896 if (makeUnique)
return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb, op);
897 return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb.getGrid(), op);
900template <
typename OpT>
901inline bool GEOvdbProcessTypedGridVec3(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
903 if (makeUnique)
return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb, op);
904 return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb.getGrid(), op);
907template <
typename OpT>
908inline bool GEOvdbProcessTypedGridPoint(GEO_PrimVDB &vdb, OpT &op,
bool makeUnique =
true)
910 if (makeUnique)
return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb, op);
911 return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb.getGrid(), op);
ValueT value
Definition: GridBuilder.h:1290
Abstract base class for typed grids.
Definition: Grid.h:78
bool apply(OpT &) const
If this grid resolves to one of the listed grid types, invoke the given functor on the resolved grid.
Definition: Grid.h:1768
TreeBase::Ptr baseTreePtr()
Return a pointer to this grid's tree, which might be shared with other grids. The pointer is guarante...
Definition: Grid.h:1227
Axis-aligned bounding box of signed integer coordinates.
Definition: Coord.h:249
bool hasGrid(const std::string &fileName, const std::string &gridName)
Return true if the file contains a grid with the specified name.
Definition: IO.h:709
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:243
Definition: AttributeTransferUtil.h:34