OpenVDB  9.0.1
GU_PrimVDB.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /*
5  * Copyright (c) Side Effects Software Inc.
6  *
7  * Produced by:
8  * Side Effects Software Inc
9  * 477 Richmond Street West
10  * Toronto, Ontario
11  * Canada M5V 3E7
12  * 416-504-9876
13  *
14  * NAME: GU_PrimVDB.h ( GU Library, C++)
15  *
16  * COMMENTS: Custom VDB primitive.
17  */
18 
19 #include <UT/UT_Version.h>
20 
21 // Using the native OpenVDB Primitive shipped with Houdini is strongly recommended,
22 // as there is no guarantee that this code will be kept in sync with Houdini.
23 // However, for debugging it can be useful, so supply -DSESI_OPENVDB_PRIM to
24 // the compiler to build this custom primitive.
25 
26 #if !defined(SESI_OPENVDB) && !defined(SESI_OPENVDB_PRIM)
27 
28 #include <GU/GU_PrimVDB.h>
29 
30 namespace openvdb_houdini {
31 using ::GU_PrimVDB;
32 }
33 
34 #else // SESI_OPENVDB || SESI_OPENVDB_PRIM
35 
36 #ifndef __HDK_GU_PrimVDB__
37 #define __HDK_GU_PrimVDB__
38 
39 #include <GA/GA_PrimitiveDefinition.h>
40 #include "GEO_PrimVDB.h"
41 #include <GU/GU_Detail.h>
42 #include <UT/UT_Matrix4.h>
43 #include <UT/UT_VoxelArray.h>
44 #include <openvdb/Platform.h>
45 #include <stddef.h>
46 
47 
48 class GA_Attribute;
49 class GEO_PrimVolume;
50 class UT_MemoryCounter;
51 class GEO_ConvertParms;
52 typedef GEO_ConvertParms GU_ConvertParms;
53 
54 
55 class OPENVDB_HOUDINI_API GU_PrimVDB : public GEO_PrimVDB
56 {
57 protected:
58  /// NOTE: Primitives should not be deleted directly. They are managed
59  /// by the GA_PrimitiveList and the stash.
60  ~GU_PrimVDB() override {}
61 
62 public:
63  /// NOTE: This constructor should only be called via GU_PrimitiveFactory.
64  GU_PrimVDB(GU_Detail *gdp, GA_Offset offset=GA_INVALID_OFFSET)
65  : GEO_PrimVDB(gdp, offset)
66  {}
67 
68  /// Report approximate memory usage.
69  int64 getMemoryUsage() const override;
70 
71  /// Count memory usage using a UT_MemoryCounter in order to count
72  /// shared memory correctly.
73  /// NOTE: This should always include sizeof(*this).
74  void countMemory(UT_MemoryCounter &counter) const override;
75 
76 #ifndef SESI_OPENVDB
77  /// Allows you to find out what this primitive type was named.
78  static GA_PrimitiveTypeId theTypeId() { return theDefinition->getId(); }
79 
80  /// Must be invoked during the factory callback to add us to the
81  /// list of primitives
82  static void registerMyself(GA_PrimitiveFactory *factory);
83 #endif
84 
85  const GA_PrimitiveDefinition &getTypeDef() const override
86  {
87  UT_ASSERT(theDefinition);
88  return *theDefinition;
89  }
90 
91  // Conversion Methods
92 
93  GEO_Primitive *convert(GU_ConvertParms &parms,
94  GA_PointGroup *usedpts = 0) override;
95  GEO_Primitive *convertNew(GU_ConvertParms &parms) override;
96 
97  /// Convert all GEO_PrimVolume primitives in geometry to
98  /// GEO_PrimVDB, preserving prim/vertex/point attributes (and prim/point
99  /// groups if requested).
100  static void convertVolumesToVDBs(
101  GU_Detail &dst_geo,
102  const GU_Detail &src_geo,
103  GU_ConvertParms &parms,
104  bool flood_sdf,
105  bool prune,
106  fpreal tolerance,
107  bool keep_original,
108  bool activate_inside = true);
109 
110  /// Convert all GEO_PrimVDB primitives in geometry to parms.toType,
111  /// preserving prim/vertex/point attributes (and prim/point groups if
112  /// requested).
113  /// @{
114  static void convertVDBs(
115  GU_Detail &dst_geo,
116  const GU_Detail &src_geo,
117  GU_ConvertParms &parms,
118  fpreal adaptivity,
119  bool keep_original);
120  static void convertVDBs(
121  GU_Detail &dst_geo,
122  const GU_Detail &src_geo,
123  GU_ConvertParms &parms,
124  fpreal adaptivity,
125  bool keep_original,
126  bool split_disjoint_volumes);
127  /// @}
128 
129  // NOTE: For static member functions please call in the following
130  // manner. <ptrvalue> = GU_PrimVDB::<functname>
131  // i.e. partptr = GU_PrimVDB::build(params...);
132 
133  // Optional Build Method
134 
135  static GU_PrimVDB * build(GU_Detail *gdp, bool append_points = true);
136 
137  /// Store a VDB grid in a new VDB primitive and add the primitive
138  /// to a geometry detail.
139  /// @param gdp the detail to which to add the new primitive
140  /// @param grid a grid to be associated with the new primitive
141  /// @param src if non-null, copy attributes and groups from this primitive
142  /// @param name if non-null, set the new primitive's @c name attribute to
143  /// this string; otherwise, if @a src is non-null, use its name
144  static SYS_FORCE_INLINE
145  GU_PrimVDB* buildFromGrid(GU_Detail& gdp, openvdb::GridBase::Ptr grid,
146  const GEO_PrimVDB* src = NULL, const char* name = NULL)
147  {
148  return GU_PrimVDB::buildFromGridAdapter(gdp, &grid, src, name);
149  }
150 
151  /// Create new VDB primitive from the given native volume primitive
152  static GU_PrimVDB * buildFromPrimVolume(
153  GU_Detail &geo,
154  const GEO_PrimVolume &vol,
155  const char *name,
156  const bool flood_sdf = false,
157  const bool prune = false,
158  const float tolerance = 0.0,
159  const bool activate_inside_sdf = true);
160 
161  /// A fast method for converting a primitive volume to a polysoup via VDB
162  /// into the given gdp. It will _not_ copy attributes because this is a
163  /// special case used for display purposes only.
164  static void convertPrimVolumeToPolySoup(
165  GU_Detail &dst_geo,
166  const GEO_PrimVolume &src_vol);
167 
168  void normal(NormalComp &output) const override;
169 
170  /// @brief Transfer any metadata associated with this primitive's
171  /// VDB grid to primitive attributes.
172  void syncAttrsFromMetadata();
173 
174  /// @brief Transfer any metadata associated with a VDB grid
175  /// to primitive attributes on a VDB primitive.
176  /// @param prim the primitive to be populated with attributes
177  /// @param grid the grid whose metadata should be transferred
178  /// @param gdp the detail to which to transfer attributes
179  static SYS_FORCE_INLINE
180  void createGridAttrsFromMetadata(
181  const GEO_PrimVDB& prim,
182  const openvdb::GridBase& grid,
183  GEO_Detail& gdp)
184  {
185  GU_PrimVDB::createGridAttrsFromMetadataAdapter(prim, &grid, gdp);
186  }
187 
188  /// @brief Transfer any metadata associated with the given MetaMap
189  /// to attributes on the given element specified by owner.
190  /// @param owner the type of element
191  /// @param element the offset of the element
192  /// @param meta_map the metadata that should be transferred
193  /// @param gdp the detail to which to transfer attributes
194  static SYS_FORCE_INLINE
195  void createAttrsFromMetadata(
196  GA_AttributeOwner owner,
197  GA_Offset element,
198  const openvdb::MetaMap& meta_map,
199  GEO_Detail& gdp)
200  {
201  GU_PrimVDB::createAttrsFromMetadataAdapter(owner, element, &meta_map, gdp);
202  }
203 
204  /// @brief Transfer a VDB primitive's attributes to a VDB grid as metadata.
205  /// @param grid the grid to be populated with metadata
206  /// @param prim the primitive whose attributes should be transferred
207  /// @param gdp the detail from which to retrieve primitive attributes
208  static SYS_FORCE_INLINE
209  void createMetadataFromGridAttrs(
210  openvdb::GridBase& grid,
211  const GEO_PrimVDB& prim,
212  const GEO_Detail& gdp)
213  {
214  GU_PrimVDB::createMetadataFromGridAttrsAdapter(&grid, prim, gdp);
215  }
216 
217  /// @brief Transfer attributes to VDB metadata.
218  /// @param meta_map the output metadata
219  /// @param owner the type of element
220  /// @param element the offset of the element
221  /// @param geo the detail from which to retrieve primitive attributes
222  static SYS_FORCE_INLINE
223  void createMetadataFromAttrs(
224  openvdb::MetaMap& meta_map,
225  GA_AttributeOwner owner,
226  GA_Offset element,
227  const GEO_Detail& geo)
228  {
229  GU_PrimVDB::createMetadataFromAttrsAdapter(&meta_map, owner, element, geo);
230  }
231 
232 private: // METHODS
233 
234  /// Add a border of the given radius by evaluating from the given volume.
235  /// It assumes that the VDB is a float grid and that the voxel array has
236  /// the same index space, so this can really only be safely called after
237  /// buildFromPrimVolume(). This is used to ensure that non-constant borders
238  /// can be converted at the expense of some extra memory.
239  void expandBorderFromPrimVolume(
240  const GEO_PrimVolume &vol,
241  int border_radius);
242 
243  GEO_Primitive * convertToNewPrim(
244  GEO_Detail &dst_geo,
245  GU_ConvertParms &parms,
246  fpreal adaptivity,
247  bool split_disjoint_volumes,
248  bool &success) const;
249  GEO_Primitive * convertToPrimVolume(
250  GEO_Detail &dst_geo,
251  GU_ConvertParms &parms,
252  bool split_disjoint_volumes) const;
253  GEO_Primitive * convertToPoly(
254  GEO_Detail &dst_geo,
255  GU_ConvertParms &parms,
256  fpreal adaptivity,
257  bool buildpolysoup,
258  bool &success) const;
259 
260  static GU_PrimVDB* buildFromGridAdapter(
261  GU_Detail& gdp,
262  void* grid,
263  const GEO_PrimVDB*,
264  const char* name);
265  static void createGridAttrsFromMetadataAdapter(
266  const GEO_PrimVDB& prim,
267  const void* grid,
268  GEO_Detail& gdp);
269  static void createMetadataFromGridAttrsAdapter(
270  void* grid,
271  const GEO_PrimVDB&,
272  const GEO_Detail&);
273 
274  static void createAttrsFromMetadataAdapter(
275  GA_AttributeOwner owner,
276  GA_Offset element,
277  const void* meta_map_ptr,
278  GEO_Detail& geo);
279 
280  static void createMetadataFromAttrsAdapter(
281  void* meta_map_ptr,
282  GA_AttributeOwner owner,
283  GA_Offset element,
284  const GEO_Detail& geo);
285 
286 private: // DATA
287 
288  static GA_PrimitiveDefinition *theDefinition;
289  friend class GU_PrimitiveFactory;
290  SYS_DEPRECATED_PUSH_DISABLE()
291 };
292  SYS_DEPRECATED_POP_DISABLE()
293 
294 
295 #ifndef SESI_OPENVDB
296 namespace openvdb_houdini {
297 using ::GU_PrimVDB;
298 } // namespace openvdb_houdini
299 #endif
300 
301 #endif // __HDK_GU_PrimVDB__
302 
303 #endif // SESI_OPENVDB || SESI_OPENVDB_PRIM
Abstract base class for typed grids.
Definition: Grid.h:77
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
#define OPENVDB_HOUDINI_API
Definition: Platform.h:262
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:335
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
Definition: AttributeTransferUtil.h:34