HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GU_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: GU_PrimVDB.h ( GU Library, C++)
7  *
8  * COMMENTS: Custom VDB primitive.
9  */
10 
11 #ifndef __GU_PrimVDB__
12 #define __GU_PrimVDB__
13 
14 #include "GU_API.h"
16 #include <GEO/GEO_PrimVDB.h>
17 #include "GU_Detail.h"
18 #include <UT/UT_Matrix4.h>
19 #include <UT/UT_VoxelArray.h>
20 #include <openvdb/Platform.h>
21 #include <stddef.h>
22 
23 
24 class GA_Attribute;
25 class GEO_PrimVolume;
26 class UT_MemoryCounter;
29 
30 
32 {
33 protected:
34  /// NOTE: Primitives should not be deleted directly. They are managed
35  /// by the GA_PrimitiveList and the stash.
36  ~GU_PrimVDB() override {}
37 
38 public:
39  /// NOTE: This constructor should only be called via GU_PrimitiveFactory.
41  : GEO_PrimVDB(gdp, offset)
42  {}
43 
44  /// Report approximate memory usage.
45  int64 getMemoryUsage() const override;
46 
47  /// Count memory usage using a UT_MemoryCounter in order to count
48  /// shared memory correctly.
49  /// NOTE: This should always include sizeof(*this).
50  void countMemory(UT_MemoryCounter &counter) const override;
51 
52 #if 0 //ndef SESI_OPENVDB
53  /// Allows you to find out what this primitive type was named.
54  static GA_PrimitiveTypeId theTypeId() { return theDefinition->getId(); }
55 
56  /// Must be invoked during the factory callback to add us to the
57  /// list of primitives
58  static void registerMyself(GA_PrimitiveFactory *factory);
59 #endif
60 
61  const GA_PrimitiveDefinition &getTypeDef() const override
62  {
63  UT_ASSERT(theDefinition);
64  return *theDefinition;
65  }
66 
67  // Conversion Methods
68 
70  GA_PointGroup *usedpts = 0) override;
71  GEO_Primitive *convertNew(GU_ConvertParms &parms) override;
72 
73  /// Convert all GEO_PrimVolume primitives in geometry to
74  /// GEO_PrimVDB, preserving prim/vertex/point attributes (and prim/point
75  /// groups if requested).
76  static void convertVolumesToVDBs(
77  GU_Detail &dst_geo,
78  const GU_Detail &src_geo,
79  GU_ConvertParms &parms,
80  bool flood_sdf,
81  bool prune,
82  fpreal tolerance,
83  bool keep_original,
84  bool activate_inside = true);
85 
86  /// Convert all GEO_PrimVDB primitives in geometry to parms.toType,
87  /// preserving prim/vertex/point attributes (and prim/point groups if
88  /// requested).
89  /// @{
90  static void convertVDBs(
91  GU_Detail &dst_geo,
92  const GU_Detail &src_geo,
93  GU_ConvertParms &parms,
94  fpreal adaptivity,
95  bool keep_original);
96  static void convertVDBs(
97  GU_Detail &dst_geo,
98  const GU_Detail &src_geo,
99  GU_ConvertParms &parms,
100  fpreal adaptivity,
101  bool keep_original,
102  bool split_disjoint_volumes);
103  /// @}
104 
105  // NOTE: For static member functions please call in the following
106  // manner. <ptrvalue> = GU_PrimVDB::<functname>
107  // i.e. partptr = GU_PrimVDB::build(params...);
108 
109  // Optional Build Method
110 
111  static GU_PrimVDB * build(GU_Detail *gdp, bool append_points = true);
112 
113  /// Store a VDB grid in a new VDB primitive and add the primitive
114  /// to a geometry detail.
115  /// @param gdp the detail to which to add the new primitive
116  /// @param grid a grid to be associated with the new primitive
117  /// @param src if non-null, copy attributes and groups from this primitive
118  /// @param name if non-null, set the new primitive's @c name attribute to
119  /// this string; otherwise, if @a src is non-null, use its name
120  static SYS_FORCE_INLINE
121  GU_PrimVDB* buildFromGrid(GU_Detail& gdp, openvdb::GridBase::Ptr grid,
122  const GEO_PrimVDB* src = NULL, const char* name = NULL)
123  {
124  return GU_PrimVDB::buildFromGridAdapter(gdp, &grid, src, name);
125  }
126 
127  /// Create new VDB primitive from the given native volume primitive
128  static GU_PrimVDB * buildFromPrimVolume(
129  GU_Detail &geo,
130  const GEO_PrimVolume &vol,
131  const char *name,
132  const bool flood_sdf = false,
133  const bool prune = false,
134  const float tolerance = 0.0,
135  const bool activate_inside_sdf = true);
136 
137  /// A fast method for converting a primitive volume to a polysoup via VDB
138  /// into the given gdp. It will _not_ copy attributes because this is a
139  /// special case used for display purposes only.
140  static void convertPrimVolumeToPolySoup(
141  GU_Detail &dst_geo,
142  const GEO_PrimVolume &src_vol);
143 
144  void normal(NormalComp &output) const override;
145 
146  /// @brief Transfer any metadata associated with this primitive's
147  /// VDB grid to primitive attributes.
148  void syncAttrsFromMetadata();
149 
150  /// @brief Transfer any metadata associated with a VDB grid
151  /// to primitive attributes on a VDB primitive.
152  /// @param prim the primitive to be populated with attributes
153  /// @param grid the grid whose metadata should be transferred
154  /// @param gdp the detail to which to transfer attributes
155  static SYS_FORCE_INLINE
157  const GEO_PrimVDB& prim,
158  const openvdb::GridBase& grid,
159  GEO_Detail& gdp)
160  {
161  GU_PrimVDB::createGridAttrsFromMetadataAdapter(prim, &grid, gdp);
162  }
163 
164  /// @brief Transfer any metadata associated with the given MetaMap
165  /// to attributes on the given element specified by owner.
166  /// @param owner the type of element
167  /// @param element the offset of the element
168  /// @param meta_map the metadata that should be transferred
169  /// @param gdp the detail to which to transfer attributes
170  static SYS_FORCE_INLINE
172  GA_AttributeOwner owner,
173  GA_Offset element,
174  const openvdb::MetaMap& meta_map,
175  GEO_Detail& gdp)
176  {
177  GU_PrimVDB::createAttrsFromMetadataAdapter(owner, element, &meta_map, gdp);
178  }
179 
180  /// @brief Transfer a VDB primitive's attributes to a VDB grid as metadata.
181  /// @param grid the grid to be populated with metadata
182  /// @param prim the primitive whose attributes should be transferred
183  /// @param gdp the detail from which to retrieve primitive attributes
184  static SYS_FORCE_INLINE
186  openvdb::GridBase& grid,
187  const GEO_PrimVDB& prim,
188  const GEO_Detail& gdp)
189  {
190  GU_PrimVDB::createMetadataFromGridAttrsAdapter(&grid, prim, gdp);
191  }
192 
193  /// @brief Transfer attributes to VDB metadata.
194  /// @param meta_map the output metadata
195  /// @param owner the type of element
196  /// @param element the offset of the element
197  /// @param geo the detail from which to retrieve primitive attributes
198  static SYS_FORCE_INLINE
200  openvdb::MetaMap& meta_map,
201  GA_AttributeOwner owner,
202  GA_Offset element,
203  const GEO_Detail& geo)
204  {
205  GU_PrimVDB::createMetadataFromAttrsAdapter(&meta_map, owner, element, geo);
206  }
207 
208 private: // METHODS
209 
210  /// Add a border of the given radius by evaluating from the given volume.
211  /// It assumes that the VDB is a float grid and that the voxel array has
212  /// the same index space, so this can really only be safely called after
213  /// buildFromPrimVolume(). This is used to ensure that non-constant borders
214  /// can be converted at the expense of some extra memory.
215  void expandBorderFromPrimVolume(
216  const GEO_PrimVolume &vol,
217  int border_radius);
218 
219  GEO_Primitive * convertToNewPrim(
220  GEO_Detail &dst_geo,
221  GU_ConvertParms &parms,
222  fpreal adaptivity,
223  bool split_disjoint_volumes,
224  bool &success) const;
225  GEO_Primitive * convertToPrimVolume(
226  GEO_Detail &dst_geo,
227  GU_ConvertParms &parms,
228  bool split_disjoint_volumes) const;
229  GEO_Primitive * convertToPoly(
230  GEO_Detail &dst_geo,
231  GU_ConvertParms &parms,
232  fpreal adaptivity,
233  bool buildpolysoup,
234  bool &success) const;
235 
236  static GU_PrimVDB* buildFromGridAdapter(
237  GU_Detail& gdp,
238  void* grid,
239  const GEO_PrimVDB*,
240  const char* name);
241  static void createGridAttrsFromMetadataAdapter(
242  const GEO_PrimVDB& prim,
243  const void* grid,
244  GEO_Detail& gdp);
245  static void createMetadataFromGridAttrsAdapter(
246  void* grid,
247  const GEO_PrimVDB&,
248  const GEO_Detail&);
249 
250  static void createAttrsFromMetadataAdapter(
251  GA_AttributeOwner owner,
252  GA_Offset element,
253  const void* meta_map_ptr,
254  GEO_Detail& geo);
255 
256  static void createMetadataFromAttrsAdapter(
257  void* meta_map_ptr,
258  GA_AttributeOwner owner,
259  GA_Offset element,
260  const GEO_Detail& geo);
261 
262 private: // DATA
263 
264  static GA_PrimitiveDefinition *theDefinition;
265  friend class GU_PrimitiveFactory;
267 };
269 
270 
271 #if 0 //ndef SESI_OPENVDB
272 namespace openvdb_houdini {
273 using ::GU_PrimVDB;
274 } // namespace openvdb_houdini
275 #endif
276 
277 #endif // __GU_PrimVDB__
GU_PrimVDB(GU_Detail *gdp, GA_Offset offset=GA_INVALID_OFFSET)
NOTE: This constructor should only be called via GU_PrimitiveFactory.
Definition: GU_PrimVDB.h:40
Definition of a geometry attribute.
Definition: GA_Attribute.h:196
GLenum src
Definition: glew.h:2410
GLuint const GLchar * name
Definition: glew.h:1814
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
virtual int64 getMemoryUsage() const
Definition: GA_Primitive.h:209
#define GA_INVALID_OFFSET
Definition: GA_Types.h:676
virtual void normal(NormalComp &output) const =0
GA_Size GA_Offset
Definition: GA_Types.h:639
static SYS_FORCE_INLINE void createAttrsFromMetadata(GA_AttributeOwner owner, GA_Offset element, const openvdb::MetaMap &meta_map, GEO_Detail &gdp)
Transfer any metadata associated with the given MetaMap to attributes on the given element specified ...
Definition: GU_PrimVDB.h:171
static SYS_FORCE_INLINE void createMetadataFromGridAttrs(openvdb::GridBase &grid, const GEO_PrimVDB &prim, const GEO_Detail &gdp)
Transfer a VDB primitive's attributes to a VDB grid as metadata.
Definition: GU_PrimVDB.h:185
virtual void countMemory(UT_MemoryCounter &counter) const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
~GU_PrimVDB() override
Definition: GU_PrimVDB.h:36
long long int64
Definition: SYS_Types.h:116
static SYS_FORCE_INLINE void createMetadataFromAttrs(openvdb::MetaMap &meta_map, GA_AttributeOwner owner, GA_Offset element, const GEO_Detail &geo)
Transfer attributes to VDB metadata.
Definition: GU_PrimVDB.h:199
#define GU_API
Definition: GU_API.h:14
GA_AttributeOwner
Definition: GA_Types.h:33
GLuint counter
Definition: glew.h:2740
fpreal64 fpreal
Definition: SYS_Types.h:277
static SYS_FORCE_INLINE GU_PrimVDB * buildFromGrid(GU_Detail &gdp, openvdb::GridBase::Ptr grid, const GEO_PrimVDB *src=NULL, const char *name=NULL)
Definition: GU_PrimVDB.h:121
const GA_PrimitiveDefinition & getTypeDef() const override
Definition: GU_PrimVDB.h:61
virtual GEO_Primitive * convert(GEO_ConvertParms &parms, GA_PointGroup *usedpts=0)=0
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
static SYS_FORCE_INLINE void createGridAttrsFromMetadata(const GEO_PrimVDB &prim, const openvdb::GridBase &grid, GEO_Detail &gdp)
Transfer any metadata associated with a VDB grid to primitive attributes on a VDB primitive...
Definition: GU_PrimVDB.h:156
Definition of a geometric primitive.
void prune(TreeT &tree, typename TreeT::ValueType tolerance=zeroVal< typename TreeT::ValueType >(), bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with tiles any nodes whose values are all the same...
Definition: Prune.h:334
virtual GEO_Primitive * convertNew(GEO_ConvertParms &parms)=0
GEO_ConvertParms GU_ConvertParms
Definition: GU_PrimVDB.h:27
GLintptr offset
Definition: glew.h:1682