HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GEO_PrimVDB.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GEO_PrimVDB.h ( GEO Library, C++)
7  *
8  * COMMENTS: Custom VDB primitive.
9  */
10 
11 #ifndef __GEO_PrimVDB__
12 #define __GEO_PrimVDB__
13 
14 #include "GEO_API.h"
15 
16 #include "GEO_Primitive.h"
17 
18 // NOTE: Not using SYS_Version for now.
19 #include <UT/UT_Version.h>
20 
21 #if (UT_VERSION_INT < 0x0c010072) // earlier than 12.1.114
22 #include "GEO_PrimVolume.h"
23 #else
24 #include "GEO_VolumeOptions.h"
25 #endif
26 
27 #include <GA/GA_Defines.h>
28 
29 #include <SYS/SYS_AtomicInt.h> // for SYS_AtomicCounter
30 
31 #include <UT/UT_BoundingBox.h>
32 #include <UT/UT_VDBUtils.h>
33 
34 #include <openvdb/Platform.h>
35 #include <openvdb/openvdb.h>
36 
37 
38 class GEO_Detail;
39 class GEO_PrimVolume;
41 class UT_MemoryCounter;
42 
43 #if (UT_VERSION_INT < 0x0c050000) // earlier than 12.5.0
45 {
47  {
48  }
50  : myMode(mode)
51  , myIso(iso)
52  , myDensity(density)
53  {
54  }
55  bool operator==(const GEO_VolumeOptions &v) const
56  {
58  return (myMode == v.myMode
59  && myIso == v.myIso
60  && myDensity == v.myDensity);
62  }
63  bool operator!=(const GEO_VolumeOptions &v) const
64  {
65  return !(*this == v);
66  }
67 
71 };
72 #endif
73 
74 
76 {
77 public:
78  typedef uint64 UniqueId;
79 
80 protected:
81  /// NOTE: The constructor should only be called from subclass
82  /// constructors.
84 
85 #if UT_VERSION_INT < 0x1000011F // earlier than 16.0.287
86  /// NOTE: The constructor should only be called from subclass
87  /// constructors.
88  GEO_PrimVDB(const GA_MergeMap &map, GA_Detail &detail,
89  GA_Offset offset, const GEO_PrimVDB &src_prim);
90 #endif
91 
92 #if (UT_VERSION_INT < 0x10000162)
93  /// NOTE: The destructor should only be called from subclass
94  /// destructors.
95  virtual ~GEO_PrimVDB();
96 #endif
97 
98 public:
100  { return GA_FAMILY_NONE; }
101 
102  /// @{
103  /// Required interface methods
104  virtual bool isDegenerate() const;
105  virtual int getBBox(UT_BoundingBox *bbox) const;
106  virtual void reverse();
107  virtual UT_Vector3 computeNormal() const;
108 #if (UT_VERSION_INT >= 0x0d000000)
109  virtual void copyPrimitive(const GEO_Primitive *src);
110 #else
111  virtual void copyPrimitive(const GEO_Primitive *src,
112  GEO_Point **ptredirect);
113 #endif
114  virtual void copyUnwiredForMerge(const GA_Primitive *src,
115  const GA_MergeMap &map);
116 
117 #if (UT_VERSION_INT < 0x10000162)
118  // Query the number of vertices in the array. This number may be smaller
119  // than the actual size of the array.
120  virtual GA_Size getVertexCount() const;
121  virtual GA_Offset getVertexOffset(GA_Size /*index*/) const
122  { return myVertex; }
123 #endif
124 
125 #if (UT_VERSION_INT >= 0x10000162)
132  GA_Offset getVertexOffset() const
133  { return getVertexOffset(0); }
135  GA_Offset getPointOffset() const
136  { return getPointOffset(0); }
138  void setPointOffset(GA_Offset pt)
139  { setPointOffset(0, pt); }
141  UT_Vector3 getPos3() const
142  { return getPos3(0); }
144  void setPos3(const UT_Vector3 &pos)
145  { setPos3(0, pos); }
146 #endif
147 
148  /// Convert an index in the voxel array into the corresponding worldspace
149  /// location
150  void indexToPos(int x, int y, int z, UT_Vector3 &pos) const;
151  void findexToPos(UT_Vector3 index, UT_Vector3 &pos) const;
152 
153  /// Convert a 3d position into the closest index value.
154  void posToIndex(UT_Vector3 pos, int &x, int &y, int &z) const;
155  void posToIndex(UT_Vector3 pos, UT_Vector3 &index) const;
156 
157  /// Evaluate the voxel value at the given world space position.
158  /// Note that depending on the underlying VDB type, this may not
159  /// be sensible, in which case a zero will silently be returned
160  fpreal getValueF(const UT_Vector3 &pos) const;
161  fpreal getValueAtIndexF(int ix, int iy, int iz) const;
162  UT_Vector3D getValueV3(const UT_Vector3 &pos) const;
163  UT_Vector3D getValueAtIndexV3(int ix, int iy, int iz) const;
164  void getValues(float *f, int stride, const UT_Vector3 *pos, int num) const;
165  void getValues(int *f, int stride, const UT_Vector3 *pos, int num) const;
166  void getValuesAtIndices(float *f, int stride, const int *ix, const int *iy, const int *iz, int num) const;
167  void getValuesAtIndices(int *f, int stride, const int *ix, const int *iy, const int *iz, int num) const;
168 
169  /// Vector grid variants.
170  void getValues(UT_Vector3 *f, int stride, const UT_Vector3 *pos, int num) const;
171  void getValuesAtIndices(UT_Vector3 *f, int stride, const int *ix, const int *iy, const int *iz, int num) const;
172 
173  // Worldspace gradient at the given position
174  UT_Vector3 getGradient(const UT_Vector3 &pos) const;
175 
176  /// Evaluate this grid's gradients at the given world space positions.
177  /// Does nothing and returns false if grid is non-scalar.
178  /// If normalize is true, then the gradients will be normalized to be unit
179  /// length.
180  bool evalGradients(
181  UT_Vector3 *gradients,
182  int gradients_stride,
183  const UT_Vector3 *positions,
184  int num_positions,
185  bool normalize = false) const;
186 
187  /// Get the storage type of the grid
189  { return myGridAccessor.getStorageType(); }
190  /// Get the tuple size, usually 1 or 3
191  int getTupleSize() const
192  { return UTvdbGetGridTupleSize(getStorageType()); }
193 
194  bool isSDF() const;
195 
196  /// True if the two volumes map the same indices to the same positions.
197  bool isAligned(const GEO_PrimVDB *vdb) const;
198  /// True if the two volumes have the same active regions
199  bool isActiveRegionMatched(const GEO_PrimVDB *vdb) const;
200 
201  /// True if we are aligned with the world axes. Ie, all our
202  /// off diagonals are zero and our diagonal is positive.
203  bool isWorldAxisAligned() const;
204 
205  // Transform the matrix associated with this primitive. Translate is
206  // ignored.
207  virtual void transform(const UT_Matrix4 &mat);
208 
209  /// Accessors for the 4x4 matrix representing the affine transform that
210  /// converts from index space voxel coordinates to world space. For frustum
211  /// maps, this will be transform as if the taper value is set to 1.
212  /// @{
213  void setTransform4(const UT_DMatrix4 &xform4);
214  void setTransform4(const UT_Matrix4 &xform4);
215  UT_Matrix4D getTransform4() const;
216  /// @}
217 
218  // Take the whole set of points into consideration when applying the
219  // point removal operation to this primitive. The method returns 0 if
220  // successful, -1 if it failed because it would have become degenerate,
221  // and -2 if it failed because it would have had to remove the primitive
222  // altogether.
223  virtual int detachPoints(GA_PointGroup &grp);
224  /// Before a point is deleted, all primitives using the point will be
225  /// notified. The method should return "false" if it's impossible to
226  /// delete the point. Otherwise, the vertices should be removed.
227  virtual GA_DereferenceStatus dereferencePoint(GA_Offset point,
228  bool dry_run=false);
229  virtual GA_DereferenceStatus dereferencePoints(
230  const GA_RangeMemberQuery &pt_q,
231  bool dry_run=false);
232  virtual const GA_PrimitiveJSON *getJSON() const;
233 
234  /// This method assigns a preallocated vertex to the quadric, optionally
235  /// creating the topological link between the primitive and new vertex.
236  void assignVertex(GA_Offset new_vtx, bool update_topology);
237 
238 #if (UT_VERSION_INT < 0x10000162)
239  /// Defragmentation
240  virtual void swapVertexOffsets(const GA_Defragment &defrag);
241 #endif
242 
243  /// Evalaute a point given a u,v coordinate (with derivatives)
244  virtual bool evaluatePointRefMap(GA_Offset result_vtx,
245  GA_AttributeRefMap &hlist,
246  fpreal u, fpreal v, uint du, uint dv) const;
247  /// Evalaute position given a u,v coordinate (with derivatives)
248  virtual int evaluatePointV4(UT_Vector4 &pos, float u, float v = 0,
249  unsigned du=0, unsigned dv=0) const
250  {
251  return GEO_Primitive::evaluatePointV4(pos, u, v,
252  du, dv);
253  }
254  /// @}
255 
256  /// Convert transforms between native volumes and VDBs
257  /// @{
258 
259  /// Get a GEO_PrimVolumeXform which represent's the grid's full transform.
260  /// The returned space's fromVoxelSpace() method will convert index space
261  /// voxel coordinates to world space positions (and the vice versa for
262  /// toVoxelSpace()).
263  GEO_PrimVolumeXform getIndexSpaceTransform() const;
264 
265  /// Equivalent to getSpaceTransform(getGrid().evalActiveVoxelBoundingBox()).
266  /// The returned space's fromVoxelSpace() method will convert 0-1
267  /// coordinates over the active voxel bounding box to world space (and vice
268  /// versa for toVoxelSpace()).
269  GEO_PrimVolumeXform getSpaceTransform() const;
270 
271  /// Gives the equivalent to GEO_PrimVolume's getSpaceTransform() by using
272  /// the given bounding box to determine the bounds of the transform.
273  /// The resulting world space sample points will be offset by half a voxel
274  /// so that they match GEO_PrimVolume.
275  /// The returned space's fromVoxelSpace() method will convert 0-1
276  /// coordinates over the bbox extents to world space (and vice versa for
277  /// toVoxelSpace()).
278  GEO_PrimVolumeXform getSpaceTransform(const UT_BoundingBoxD &bbox) const;
279 
280  /// Sets the transform from a GEO_PrimVolume's getSpaceTransform() by using
281  /// the index space [(0,0,0), resolution] bbox. If force_taper is true,
282  /// then the resulting transform will always be a NonlinearFrustumMap even
283  /// if there is no tapering.
284  void setSpaceTransform(const GEO_PrimVolumeXform &space,
285  const UT_Vector3R &resolution,
286  bool force_taper = false);
287 
288  /// @}
289 
290  fpreal getTaper() const;
291 
292  /// Returns the resolution of the active voxel array.
293  /// Does *not* mean the indices go from 0..rx, however!
294  void getRes(int &rx, int &ry, int &rz) const;
295 
296  /// Computes the voxel diameter by taking a step in x, y, and z
297  /// converting to world space and taking the length of that vector.
298  fpreal getVoxelDiameter() const;
299 
300  /// Returns the length of the voxel when you take an x, y, and z step
301  UT_Vector3 getVoxelSize() const;
302 
303  /// VDBs may either be unbounded, or created with a specific frustum
304  /// range. The latter is important for tapered VDBs that otherwise
305  /// have a singularity at the camera location. Tools can use the
306  /// presence of an idxbox as a clipping box in index space.
307  /// This does *NOT* relate to getRes - it may be much larger or
308  /// even in some cases smaller.
309  bool getFrustumBounds(UT_BoundingBox &idxbox) const;
310 
312  {
313  ACTIVATE_UNION, // Activate anything in source
314  ACTIVATE_INTERSECT, // Deactivate anything not in source
315  ACTIVATE_SUBTRACT, // Deactivate anything in source
316  ACTIVATE_COPY // Set our activation to match source
317  };
318 
319  /// Activates voxels given an *index* space bounding box. This
320  /// is an inclusive box.
321  /// If this is Frustum VDB, the activation will be clipped by that.
322  /// Setting the value only takes effect if the voxels are activated,
323  /// deactivated voxels are set to the background.
325  const openvdb::CoordBBox& bbox,
326  ActivateOperation operation,
327  bool setvalue, fpreal value)
328  {
329  activateIndexBBoxAdapter(
330  &bbox, operation, setvalue, value);
331  }
332 
333  /// Activates all of the voxels in this VDB that are touched
334  /// by active voxels in the source.
335  void activateByVDB(const GEO_PrimVDB *vdb,
336  ActivateOperation operation,
337  bool setvalue, fpreal value);
338 
339  /// @{
340  /// Though not strictly required (i.e. not pure virtual), these methods
341  /// should be implemented for proper behaviour.
342  virtual GEO_Primitive *copy(int preserve_shared_pts = 0) const;
343 
344  // Have we been deactivated and stashed?
345 #if (UT_VERSION_INT >= 0x0d000000) // 13.0 or later
346  virtual void stashed(bool beingstashed, GA_Offset offset=GA_INVALID_OFFSET);
347 #else
348  virtual void stashed(int onoff, GA_Offset offset=GA_INVALID_OFFSET);
349 #endif
350 
351 #if (UT_VERSION_INT < 0x10000162)
352  // We need to invalidate the vertex offsets
353  virtual void clearForDeletion();
354 #endif
355 
356 #if (UT_VERSION_INT < 0x0c050132) // Before 12.5.306
357  virtual void copyOffsetPrimitive(const GEO_Primitive *src, int base);
358 #elif (UT_VERSION_INT < 0x0d000000) // Before 13.0, when the function was deleted
359  virtual void copyOffsetPrimitive(const GEO_Primitive *src, GA_Index base);
360 #endif
361  /// @}
362 
363  /// @{
364  /// Optional interface methods. Though not required, implementing these
365  /// will give better behaviour for the new primitive.
366  virtual UT_Vector3 baryCenter() const;
367  virtual fpreal calcVolume(const UT_Vector3 &refpt) const;
368  /// Calculate the surface area of the active voxels where
369  /// a voxel face contributes if it borders an inactive voxel.
370  virtual fpreal calcArea() const;
371  /// @}
372 
373  /// @{
374  /// Enlarge a bounding box by the bounding box of the primitive. A
375  /// return value of false indicates an error in the operation, most
376  /// likely an invalid P. For any attribute other than the position
377  /// these methods simply enlarge the bounding box based on the vertex.
378  virtual bool enlargeBoundingBox(UT_BoundingRect &b,
379  const GA_Attribute *P) const;
380  virtual bool enlargeBoundingBox(UT_BoundingBox &b,
381  const GA_Attribute *P) const;
382  virtual void enlargePointBounds(UT_BoundingBox &e) const;
383  /// @}
384  /// Enlarge a bounding sphere to encompass the primitive. A return value
385  /// of false indicates an error in the operation, most likely an invalid
386  /// P. For any attribute other than the position this method simply
387  /// enlarges the sphere based on the vertex.
389  const GA_Attribute *P) const;
390 
391  /// Accessor for the local 3x3 affine transform matrix for the primitive.
392  /// For frustum maps, this will be transform as if the taper value is set
393  /// to 1.
394  /// @{
395  virtual void getLocalTransform(UT_Matrix3D &result) const;
396  virtual void setLocalTransform(const UT_Matrix3D &new_mat3);
397  /// @}
398 
399  /// @internal Hack to condition 4x4 matrices that we avoid creating what
400  /// OpenVDB erroneously thinks are singular matrices. Returns true if mat4
401  /// was modified.
402  static bool conditionMatrix(UT_Matrix4D &mat4);
403 
404  /// Visualization accessors
405  /// @{
406  const GEO_VolumeOptions &getVisOptions() const { return myVis; }
407 
409  GEO_VolumeVis vismode,
410  fpreal iso,
411  fpreal density)
412  {
413  myVis.myMode = vismode;
414  myVis.myIso = iso;
415  myVis.myDensity = density;
416  }
417  GEO_VolumeVis getVisualization() const { return myVis.myMode; }
418  fpreal getVisIso() const { return myVis.myIso; }
419  fpreal getVisDensity() const { return myVis.myDensity; }
420  /// @}
421 
422  /// Load the order from a JSON value
423  bool loadOrder(const UT_JSONValue &p);
424 
425  /// @{
426  /// Save/Load vdb to a JSON stream
427  bool saveVDB(UT_JSONWriter &w, const GA_SaveMap &sm,
428  bool as_shmem = false) const;
429  bool loadVDB(UT_JSONParser &p,
430  bool as_shmem = false);
431  /// @}
432 
433  bool saveVisualization(
434  UT_JSONWriter &w,
435  const GA_SaveMap &map) const;
436  bool loadVisualization(
437  UT_JSONParser &p,
438  const GA_LoadMap &map);
439 
440  /// Method to perform quick lookup of vertex without the virtual call
442  {
443  UT_ASSERT_P(index < 1);
444 #if (UT_VERSION_INT >= 0x10000162)
445  return getVertexOffset();
446 #else
447  return myVertex;
448 #endif
449  }
450 
451  void setVertexPoint(int i, GA_Offset pt)
452  {
453  if (i == 0)
454 #if (UT_VERSION_INT >= 0x10000162)
455  setPointOffset(pt);
456 #else
457  wireVertex(myVertex, pt);
458 #endif
459  }
460 
461  /// @brief Computes the total density of the volume, scaled by
462  /// the volume's size. Negative values will be ignored.
463  fpreal calcPositiveDensity() const;
464 
465  bool hasGrid() const { return myGridAccessor.hasGrid(); }
466 
467  /// @brief If this primitive's grid's voxel data (i.e., its tree)
468  /// is shared, replace the tree with a deep copy of itself that is
469  /// not shared with anyone else.
471  { myGridAccessor.makeGridUnique(); }
472 
473  /// @brief Return a reference to this primitive's grid.
474  /// @note Calling setGrid() invalidates all references previously returned.
475  const openvdb::GridBase & getConstGrid() const
476  { return myGridAccessor.getConstGrid(*this); }
477  /// @brief Return a reference to this primitive's grid.
478  /// @note Calling setGrid() invalidates all references previously returned.
479  const openvdb::GridBase & getGrid() const
480  { return getConstGrid(); }
481  /// @brief Return a reference to this primitive's grid.
482  /// @note Calling setGrid() invalidates all references previously returned.
483  /// @warning Call makeGridUnique() before modifying the grid's voxel data.
484  openvdb::GridBase & getGrid()
485  {
486  incrGridUniqueIds();
487  return myGridAccessor.getGrid(*this);
488  }
489 
490  /// @brief Return a shared pointer to this primitive's grid.
491  /// @note Calling setGrid() causes the grid to which the shared pointer
492  /// refers to be disassociated with this primitive.
493  openvdb::GridBase::ConstPtr getConstGridPtr() const
494  { return myGridAccessor.getConstGridPtr(*this); }
495  /// @brief Return a shared pointer to this primitive's grid.
496  /// @note Calling setGrid() causes the grid to which the shared pointer
497  /// refers to be disassociated with this primitive.
498  openvdb::GridBase::ConstPtr getGridPtr() const
499  { return getConstGridPtr(); }
500  /// @brief Return a shared pointer to this primitive's grid.
501  /// @note Calling setGrid() causes the grid to which the shared pointer
502  /// refers to be disassociated with this primitive.
503  /// @warning Call makeGridUnique() before modifying the grid's voxel data.
504  openvdb::GridBase::Ptr getGridPtr()
505  {
506  incrGridUniqueIds();
507  return myGridAccessor.getGridPtr(*this);
508  }
509 
510  /// @brief Set this primitive's grid to a shallow copy of the given grid.
511  /// @note Invalidates all previous getGrid() and getConstGrid() references
512  void setGrid(const openvdb::GridBase &grid)
513  {
514  incrGridUniqueIds();
515  myGridAccessor.setGrid(grid, *this);
516  }
517 
518  /// @brief Return a reference to this primitive's grid metadata.
519  /// @note Calling setGrid() invalidates all references previously returned.
520  const openvdb::MetaMap& getConstMetadata() const
521  { return getConstGrid(); }
522  /// @brief Return a reference to this primitive's grid metadata.
523  /// @note Calling setGrid() invalidates all references previously returned.
524  const openvdb::MetaMap& getMetadata() const
525  { return getConstGrid(); }
526  /// @brief Return a reference to this primitive's grid metadata.
527  /// @note Calling setGrid() invalidates all references previously returned.
528  openvdb::MetaMap& getMetadata()
529  {
530  incrMetadataUniqueId();
531  return myGridAccessor.getGrid(*this);
532  }
533 
534  /// @brief Return the value of this primitive's "name" attribute
535  /// in the given detail.
536  const char * getGridName() const;
537 
538  /// @brief Return this primitive's serial number.
539  /// @details A primitive's serial number never changes.
540  /// @todo Because serial numbers are currently 32-bit, it is possible,
541  /// though unlikely, for two primitives to have the same serial number.
543  { return static_cast<UniqueId>(myUniqueId); }
544 
545  /// @brief Return the serial number of this primitive's voxel data.
546  /// @details The serial number is incremented whenever a non-const
547  /// reference or pointer to this primitive's grid is requested
548  /// (whether or not the voxel data is ultimately modified).
550  { return static_cast<UniqueId>(myTreeUniqueId); }
551  /// @brief Return the serial number of this primitive's grid metadata.
552  /// @details The serial number is incremented whenever a non-const
553  /// reference to the metadata or non-const access to the grid is requested
554  /// (whether or not the metadata is ultimately modified).
556  { return static_cast<UniqueId>(myMetadataUniqueId); }
557  /// @brief Return the serial number of this primitive's transform.
558  /// @details The serial number is incremented whenever the transform
559  /// is modified or non-const access to this primitive's grid is requested
560  /// (whether or not the transform is ultimately modified).
562  { return static_cast<UniqueId>(myTransformUniqueId); }
563 
564 protected:
565  typedef SYS_AtomicCounter AtomicUniqueId; // 32-bit on non-AMD systems
566 
567  /// Register intrinsic attributes
568 #if (UT_VERSION_INT >= 0x0c010048) // 12.1.72 or later
570 #else
573 #endif
574 
575  /// Return true if the given metadata token is an intrinsic
576  static bool isIntrinsicMetadata(const char *name);
577 
578  /// @warning vertexPoint() doesn't check the bounds. Use with caution.
580 #if (UT_VERSION_INT >= 0x10000162)
581  { return getPointOffset(); }
582 #else
583  { return getDetail().vertexPoint(myVertex); }
584 #endif
585 
586  /// Report approximate memory usage, excluding sizeof(*this),
587  /// because the subclass doesn't have access to myGridAccessor.
588  int64 getBaseMemoryUsage() const;
589 
590  // This is called by the subclasses to count the
591  // memory used by this, excluding sizeof(*this).
592  void countBaseMemory(UT_MemoryCounter &counter) const;
593 
594  /// @brief Return an ID number that is guaranteed to be unique across
595  /// all VDB primitives.
596  static UniqueId nextUniqueId();
597 
599  { myTreeUniqueId.maximum(nextUniqueId()); }
601  { myMetadataUniqueId.maximum(nextUniqueId()); }
603  { myTransformUniqueId.maximum(nextUniqueId()); }
605  {
606  incrTreeUniqueId();
607  incrMetadataUniqueId();
608  incrTransformUniqueId();
609  }
610 
611  /// @brief Replace this primitive's grid with a shallow copy
612  /// of another primitive's grid.
613  void copyGridFrom(const GEO_PrimVDB&);
614 
615 #if (UT_VERSION_INT < 0x10000162) // earlier than 16.0.354
617 #endif
618 
619  /// @brief GridAccessor manages access to a GEO_PrimVDB's grid.
620  /// @details In keeping with OpenVDB library conventions, the grid
621  /// is stored internally by shared pointer. However, grid objects
622  /// are never shared among primitives, though their voxel data
623  /// (i.e., their trees) may be shared.
624  /// <p>Among other things, GridAccessor
625  /// - ensures that each primitive's transform and metadata are unique
626  /// (i.e., not shared with anyone else)
627  /// - allows primitives to share voxel data but, via makeGridUnique(),
628  /// provides a way to break the connection
629  /// - ensures that the primitive's transform and the grid's transform
630  /// are in sync (specifically, the translation component, which is
631  /// stored independently as a vertex offset).
633  {
634  public:
635  GridAccessor() : myStorageType(UT_VDB_INVALID)
636  { }
637 
638  void clear()
639  {
640  myGrid.reset();
641  myStorageType = UT_VDB_INVALID;
642  }
643 
644  openvdb::GridBase &
645  getGrid(const GEO_PrimVDB &prim)
646  { updateGridTranslates(prim); return *myGrid; }
647 
648  const openvdb::GridBase &
649  getConstGrid(const GEO_PrimVDB &prim) const
650  { updateGridTranslates(prim); return *myGrid; }
651 
652  openvdb::GridBase::Ptr
653  getGridPtr(const GEO_PrimVDB &prim)
654  { updateGridTranslates(prim); return myGrid; }
655 
656  openvdb::GridBase::ConstPtr
657  getConstGridPtr(const GEO_PrimVDB &prim) const
658  { updateGridTranslates(prim); return myGrid; }
659 
660  // These accessors will ensure the transform's translate is set into
661  // the vertex position.
662  void setGrid(const openvdb::GridBase& grid, GEO_PrimVDB& prim)
663  { setGridAdapter(&grid, prim); }
665  const openvdb::math::Transform &xform,
666  GEO_PrimVDB &prim)
667  { setTransformAdapter(&xform, prim); }
668 
669  void makeGridUnique();
670 
671  UT_VDBType getStorageType() const { return myStorageType; }
672  bool hasGrid() const { return myGrid != 0; }
673 
674  private:
675  void updateGridTranslates(const GEO_PrimVDB &prim) const;
676  void setVertexPosition(
677  const openvdb::math::Transform &xform,
678  GEO_PrimVDB &prim)
679  { setVertexPositionAdapter(&xform, prim); }
680 
681  void setGridAdapter(const void* grid, GEO_PrimVDB&);
682  void setTransformAdapter(const void* xform, GEO_PrimVDB&);
683  void setVertexPositionAdapter(const void* xform, GEO_PrimVDB&);
684 
685  private:
686  openvdb::GridBase::Ptr myGrid;
687  UT_VDBType myStorageType;
688  };
689 
690 private:
691  void activateIndexBBoxAdapter(
692  const void* bbox,
693  ActivateOperation,
694  bool setvalue, fpreal value);
695 
696 
697  GridAccessor myGridAccessor;
698 
699  GEO_VolumeOptions myVis;
700 
701 #if (UT_VERSION_INT < 0x0c050000) // earlier than 12.5.0
702  bool myStashedState;
703 #endif
704 
705  AtomicUniqueId myUniqueId;
706  AtomicUniqueId myTreeUniqueId;
707  AtomicUniqueId myMetadataUniqueId;
708  AtomicUniqueId myTransformUniqueId;
709 
710 }; // class GEO_PrimVDB
711 
712 
713 #if 0 // ndef SESI_OPENVDB
714 namespace openvdb_houdini {
715 using ::GEO_VolumeOptions;
716 using ::GEO_PrimVDB;
717 }
718 #endif
719 
720 
721 ////////////////////////////////////////
722 
723 
724 namespace UT_VDBUtils {
725 
726 // This overload of UT_VDBUtils::callTypedGrid(), for GridBaseType = GEO_PrimVDB,
727 // calls makeGridUnique() on the primitive just before instantiating and
728 // invoking the functor on the primitive's grid. This delays the call
729 // to makeGridUnique() until it is known to be necessary and thus avoids
730 // making deep copies of grids of types that won't be processed.
731 template<typename GridType, typename OpType>
732 inline void
733 callTypedGrid(GEO_PrimVDB& prim, OpType& op)
734 {
735  prim.makeGridUnique();
736 
737 #ifdef _MSC_VER
738  op.operator()<GridType>(*(UTverify_cast<GridType*>(&prim.getGrid())));
739 #else
740  op.template operator()<GridType>(*(UTverify_cast<GridType*>(&prim.getGrid())));
741 #endif
742 }
743 
744 // Overload of callTypedGrid() for GridBaseType = const GEO_PrimVDB
745 template<typename GridType, typename OpType>
746 inline void
747 callTypedGrid(const GEO_PrimVDB& prim, OpType& op)
748 {
749 #ifdef _MSC_VER
750  op.operator()<GridType>(*(UTverify_cast<const GridType*>(&prim.getConstGrid())));
751 #else
752  op.template operator()<GridType>(*(UTverify_cast<const GridType*>(&prim.getConstGrid())));
753 #endif
754 }
755 
756 } // namespace UT_VDBUtils
757 
758 // Define UTvdbProcessTypedGrid*() (see UT_VDBUtils.h) for grids
759 // belonging to primitives, for various subsets of grid types.
762 
763 
764 ////////////////////////////////////////
765 
766 
767 /// @brief Utility function to process the grid of a const primitive using functor @a op.
768 /// @details It will invoke @code op.operator()<GridT>(const GridT &grid) @endcode
769 /// @{
770 template <typename OpT>
771 inline bool GEOvdbProcessTypedGrid(const GEO_PrimVDB &vdb, OpT &op)
772 {
773  return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb.getGrid(), op);
774 }
775 
776 template <typename OpT>
777 inline bool GEOvdbProcessTypedGridReal(const GEO_PrimVDB &vdb, OpT &op)
778 {
779  return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb.getGrid(), op);
780 }
781 
782 template <typename OpT>
783 inline bool GEOvdbProcessTypedGridScalar(const GEO_PrimVDB &vdb, OpT &op)
784 {
785  return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb.getGrid(), op);
786 }
787 
788 template <typename OpT>
789 inline bool GEOvdbProcessTypedGridTopology(const GEO_PrimVDB &vdb, OpT &op)
790 {
791  return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb.getGrid(), op);
792 }
793 
794 template <typename OpT>
795 inline bool GEOvdbProcessTypedGridVec3(const GEO_PrimVDB &vdb, OpT &op)
796 {
797  return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb.getGrid(), op);
798 }
799 
800 template <typename OpT>
801 inline bool GEOvdbProcessTypedGridPoint(const GEO_PrimVDB &vdb, OpT &op)
802 {
803  return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb.getGrid(), op);
804 }
805 /// @}
806 
807 /// @brief Utility function to process the grid of a primitive using functor @a op.
808 /// @param vdb the primitive whose grid is to be processed
809 /// @param op a functor with a call operator of the form
810 /// @code op.operator()<GridT>(GridT &grid) @endcode
811 /// @param makeUnique if @c true, call <tt>vdb.makeGridUnique()</tt> before
812 /// invoking the functor
813 /// @{
814 template <typename OpT>
815 inline bool GEOvdbProcessTypedGrid(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
816 {
817  if (makeUnique) return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb, op);
818  return UTvdbProcessTypedGrid(vdb.getStorageType(), vdb.getGrid(), op);
819 }
820 
821 template <typename OpT>
822 inline bool GEOvdbProcessTypedGridReal(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
823 {
824  if (makeUnique) return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb, op);
825  return UTvdbProcessTypedGridReal(vdb.getStorageType(), vdb.getGrid(), op);
826 }
827 
828 template <typename OpT>
829 inline bool GEOvdbProcessTypedGridScalar(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
830 {
831  if (makeUnique) return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb, op);
832  return UTvdbProcessTypedGridScalar(vdb.getStorageType(), vdb.getGrid(), op);
833 }
834 
835 template <typename OpT>
836 inline bool GEOvdbProcessTypedGridTopology(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
837 {
838  if (makeUnique) return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb, op);
839  return UTvdbProcessTypedGridTopology(vdb.getStorageType(), vdb.getGrid(), op);
840 }
841 
842 template <typename OpT>
843 inline bool GEOvdbProcessTypedGridVec3(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
844 {
845  if (makeUnique) return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb, op);
846  return UTvdbProcessTypedGridVec3(vdb.getStorageType(), vdb.getGrid(), op);
847 }
848 
849 template <typename OpT>
850 inline bool GEOvdbProcessTypedGridPoint(GEO_PrimVDB &vdb, OpT &op, bool makeUnique = true)
851 {
852  if (makeUnique) return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb, op);
853  return UTvdbProcessTypedGridPoint(vdb.getStorageType(), vdb.getGrid(), op);
854 }
855 /// @}
856 
857 #endif // __GEO_PrimVDB__
virtual void copyUnwiredForMerge(const GA_Primitive *src, const GA_MergeMap &map)
bool operator==(const GEO_VolumeOptions &v) const
Definition: GEO_PrimVDB.h:55
Definition of a geometry attribute.
Definition: GA_Attribute.h:189
GridAccessor manages access to a GEO_PrimVDB's grid.
Definition: GEO_PrimVDB.h:632
virtual GEO_Primitive * copy(int preserve_shared_pts=0) const
void setTransform(const openvdb::math::Transform &xform, GEO_PrimVDB &prim)
Definition: GEO_PrimVDB.h:664
UT_VDBType getStorageType() const
Get the storage type of the grid.
Definition: GEO_PrimVDB.h:188
SYS_FORCE_INLINE GA_Offset getPointOffset(GA_Size i) const
Definition: GA_Primitive.h:249
virtual UT_Vector3 baryCenter() const
openvdb::GridBase::ConstPtr getGridPtr() const
Return a shared pointer to this primitive's grid.
Definition: GEO_PrimVDB.h:498
GA_Offset fastVertexOffset(GA_Size UT_IF_ASSERT_P(index)) const
Method to perform quick lookup of vertex without the virtual call.
Definition: GEO_PrimVDB.h:441
SYS_FORCE_INLINE GA_Detail & getDetail() const
Definition: GA_Primitive.h:132
virtual void copyPrimitive(const GEO_Primitive *src)=0
virtual void clearForDeletion()
Definition: GA_Primitive.h:598
bool GEOvdbProcessTypedGridTopology(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:789
Used to pass options and map offset values during saving.
Definition: GA_SaveMap.h:48
SYS_FORCE_INLINE GA_Size getVertexCount() const
Return the number of vertices used by this primitive.
Definition: GA_Primitive.h:227
const GLdouble * v
Definition: glcorearb.h:836
bool operator!=(const GEO_VolumeOptions &v) const
Definition: GEO_PrimVDB.h:63
SYS_AtomicCounter AtomicUniqueId
Definition: GEO_PrimVDB.h:565
const openvdb::MetaMap & getMetadata() const
Return a reference to this primitive's grid metadata.
Definition: GEO_PrimVDB.h:524
GA_Offset myVertex
Definition: GEO_PrimVDB.h:616
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
virtual int evaluatePointV4(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const
Evalaute position given a u,v coordinate (with derivatives)
Definition: GEO_PrimVDB.h:248
The merge map keeps track of information when merging details.
Definition: GA_MergeMap.h:53
SYS_FORCE_INLINE void setPointOffset(GA_Size i, GA_Offset ptoff)
Definition: GA_Primitive.h:255
UniqueId getUniqueId() const
Return this primitive's serial number.
Definition: GEO_PrimVDB.h:542
virtual bool enlargeBoundingBox(UT_BoundingRect &b, const GA_Attribute *P) const
virtual GA_DereferenceStatus dereferencePoint(GA_Offset point, bool dry_run=false)=0
void incrTransformUniqueId()
Definition: GEO_PrimVDB.h:602
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:72
#define GA_DECLARE_INTRINSICS(OVERRIDE)
Definition: GA_Primitive.h:79
virtual GA_Offset getVertexOffset(GA_Size) const
Definition: GEO_PrimVDB.h:121
int getTupleSize() const
Get the tuple size, usually 1 or 3.
Definition: GEO_PrimVDB.h:191
GEO_VolumeVis myMode
Definition: GEO_PrimVDB.h:68
GLint y
Definition: glcorearb.h:102
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
Abstract base class for a range membership query object.
UT_VDBType
Definition: UT_VDBUtils.h:11
#define GA_NO_OVERRIDE
Definition: GA_Primitive.h:75
const GEO_VolumeOptions & getVisOptions() const
Definition: GEO_PrimVDB.h:406
void setGrid(const openvdb::GridBase &grid)
Set this primitive's grid to a shallow copy of the given grid.
Definition: GEO_PrimVDB.h:512
bool GEOvdbProcessTypedGridPoint(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:801
UniqueId getMetadataUniqueId() const
Return the serial number of this primitive's grid metadata.
Definition: GEO_PrimVDB.h:555
void incrGridUniqueIds()
Definition: GEO_PrimVDB.h:604
png_uint_32 i
Definition: png.h:2877
virtual void swapVertexOffsets(const GA_Defragment &defrag)
openvdb::GridBase::Ptr getGridPtr(const GEO_PrimVDB &prim)
Definition: GEO_PrimVDB.h:653
void wireVertex(GA_Offset vertex, GA_Offset point)
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:211
SYS_FORCE_INLINE int64 getBaseMemoryUsage() const
Report approximate memory usage for myVertexList for subclasses.
Definition: GA_Primitive.h:753
GA_PrimitiveFamilyMask
virtual UT_Vector3 computeNormal() const =0
Return a normal vector for the primitive.
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
bool GEOvdbProcessTypedGridReal(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:777
bool GEOvdbProcessTypedGridVec3(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:795
#define UT_IF_ASSERT_P(ZZ)
Definition: UT_Assert.h:128
uint64 UniqueId
Definition: GEO_PrimVDB.h:78
virtual bool isDegenerate() const =0
Is the primitive degenerate.
GA_Size GA_Offset
Definition: GA_Types.h:617
virtual bool enlargeBoundingSphere(UT_BoundingSphere &b, const GA_Attribute *P) const
long long int64
Definition: SYS_Types.h:106
#define UT_VDB_DECL_PROCESS_TYPED_GRID(GRID_BASE_T)
Utility function that, given a generic grid pointer, calls a functor on the fully-resolved grid...
Definition: UT_VDBUtils.h:248
const openvdb::GridBase & getConstGrid(const GEO_PrimVDB &prim) const
Definition: GEO_PrimVDB.h:649
SYS_FORCE_INLINE UT_Vector3 getPos3(GA_Size i) const
Definition: GA_Primitive.h:269
GLfloat f
Definition: glcorearb.h:1925
virtual void setLocalTransform(const UT_Matrix3D &matrix)
Set the local transform. The default implementation does nothing.
GEO_VolumeOptions(GEO_VolumeVis mode, fpreal iso, fpreal density)
Definition: GEO_PrimVDB.h:49
unsigned long long uint64
Definition: SYS_Types.h:107
openvdb::GridBase::ConstPtr getConstGridPtr() const
Return a shared pointer to this primitive's grid.
Definition: GEO_PrimVDB.h:493
openvdb::GridBase::Ptr getGridPtr()
Return a shared pointer to this primitive's grid.
Definition: GEO_PrimVDB.h:504
bool GEOvdbProcessTypedGridScalar(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:783
virtual fpreal calcArea() const
void incrMetadataUniqueId()
Definition: GEO_PrimVDB.h:600
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
virtual const GA_PrimitiveJSON * getJSON() const =0
void setVertexPoint(int i, GA_Offset pt)
Definition: GEO_PrimVDB.h:451
GLintptr offset
Definition: glcorearb.h:664
Provide a JSON interface to a primitive.
bool hasGrid() const
Definition: GEO_PrimVDB.h:465
#define GEO_API
Definition: GEO_API.h:10
void activateIndexBBox(const openvdb::CoordBBox &bbox, ActivateOperation operation, bool setvalue, fpreal value)
Definition: GEO_PrimVDB.h:324
GEO_VolumeVis
virtual void reverse()=0
Reverse the order of vertices.
A handle to simplify manipulation of multiple attributes.
UniqueId getTreeUniqueId() const
Return the serial number of this primitive's voxel data.
Definition: GEO_PrimVDB.h:549
Options during loading.
Definition: GA_LoadMap.h:42
virtual int getBBox(UT_BoundingBox *bbox) const =0
#define OPENVDB_NO_FP_EQUALITY_WARNING_BEGIN
Definition: Math.h:82
Defragmentation of IndexMaps.
Definition: GA_Defragment.h:45
SYS_FORCE_INLINE GA_Offset vertexPoint(GA_Offset vertex) const
Given a vertex, return the point it references.
Definition: GA_Detail.h:469
void makeGridUnique()
If this primitive's grid's voxel data (i.e., its tree) is shared, replace the tree with a deep copy o...
Definition: GEO_PrimVDB.h:470
GLuint const GLchar * name
Definition: glcorearb.h:785
SYS_FORCE_INLINE TO_T UTverify_cast(FROM_T from)
Definition: UT_Assert.h:175
GridType::Ptr normalize(const GridType &grid, bool threaded, InterruptT *interrupt)
Normalize the vectors of the given vector-valued grid.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
const openvdb::GridBase & getGrid() const
Return a reference to this primitive's grid.
Definition: GEO_PrimVDB.h:479
GA_Size GA_Index
Define the strictness of GA_Offset/GA_Index.
Definition: GA_Types.h:611
virtual GA_DereferenceStatus dereferencePoints(const GA_RangeMemberQuery &pt_q, bool dry_run=false)=0
unsigned int uint
Definition: SYS_Types.h:39
openvdb::GridBase::ConstPtr getConstGridPtr(const GEO_PrimVDB &prim) const
Definition: GEO_PrimVDB.h:657
openvdb::GridBase & getGrid(const GEO_PrimVDB &prim)
Definition: GEO_PrimVDB.h:645
SYS_FORCE_INLINE void setPos3(GA_Size i, const UT_Vector3 &pos) const
Definition: GA_Primitive.h:275
UniqueId getTransformUniqueId() const
Return the serial number of this primitive's transform.
Definition: GEO_PrimVDB.h:561
GLenum mode
Definition: glcorearb.h:98
void setGrid(const openvdb::GridBase &grid, GEO_PrimVDB &prim)
Definition: GEO_PrimVDB.h:662
virtual void transform(const UT_Matrix4 &)
virtual void enlargePointBounds(UT_BoundingBox &box) const
virtual void getLocalTransform(UT_Matrix3D &matrix) const
GLsizei const GLfloat * value
Definition: glcorearb.h:823
double fpreal
Definition: SYS_Types.h:269
virtual int detachPoints(GA_PointGroup &grp)=0
fpreal getVisDensity() const
Definition: GEO_PrimVDB.h:419
openvdb::MetaMap & getMetadata()
Return a reference to this primitive's grid metadata.
Definition: GEO_PrimVDB.h:528
int UTvdbGetGridTupleSize(UT_VDBType type)
Returns the tuple size of a grid given its value type.
Definition: UT_VDBUtils.h:115
void setVisualization(GEO_VolumeVis vismode, fpreal iso, fpreal density)
Definition: GEO_PrimVDB.h:408
GLuint index
Definition: glcorearb.h:785
GLint GLenum GLint x
Definition: glcorearb.h:408
virtual fpreal calcVolume(const UT_Vector3 &) const
SYS_FORCE_INLINE GA_Offset getVertexOffset(GA_Size primvertexnum) const
Definition: GA_Primitive.h:235
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:75
virtual bool evaluatePointRefMap(GA_Offset result_vtx, GA_AttributeRefMap &map, fpreal u, fpreal v=0, uint du=0, uint dv=0) const =0
Container class for all geometry.
Definition: GA_Detail.h:96
png_infop png_uint_32 int num
Definition: png.h:2158
const openvdb::GridBase & getConstGrid() const
Return a reference to this primitive's grid.
Definition: GEO_PrimVDB.h:475
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
static GA_PrimitiveFamilyMask buildFamilyMask()
Definition: GEO_PrimVDB.h:99
void callTypedGrid(GEO_PrimVDB &prim, OpType &op)
Definition: GEO_PrimVDB.h:733
Definition of a geometric primitive.
#define const
Definition: zconf.h:214
openvdb::GridBase & getGrid()
Return a reference to this primitive's grid.
Definition: GEO_PrimVDB.h:484
fpreal getVisIso() const
Definition: GEO_PrimVDB.h:418
static GA_IntrinsicManager::Registrar registerIntrinsics(GA_PrimitiveDefinition &defn)
void countBaseMemory(UT_MemoryCounter &counter) const
GEO_VolumeVis getVisualization() const
Definition: GEO_PrimVDB.h:417
bool GEOvdbProcessTypedGrid(const GEO_PrimVDB &vdb, OpT &op)
Utility function to process the grid of a const primitive using functor op.
Definition: GEO_PrimVDB.h:771
void incrTreeUniqueId()
Definition: GEO_PrimVDB.h:598
virtual int evaluatePointV4(UT_Vector4 &pos, float u, float v=0, unsigned du=0, unsigned dv=0) const
virtual void stashed(bool beingstashed, GA_Offset offset=GA_INVALID_OFFSET)
GA_Offset vertexPoint(GA_Size) const
Definition: GEO_PrimVDB.h:579
UT_VDBType getStorageType() const
Definition: GEO_PrimVDB.h:671
const openvdb::MetaMap & getConstMetadata() const
Return a reference to this primitive's grid metadata.
Definition: GEO_PrimVDB.h:520
GLenum src
Definition: glcorearb.h:1792
#define OPENVDB_NO_FP_EQUALITY_WARNING_END
Definition: Math.h:83