OpenVDB  11.0.0
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  void normal(NormalCompD &output) const override;
170 
171  /// @brief Transfer any metadata associated with this primitive's
172  /// VDB grid to primitive attributes.
173  void syncAttrsFromMetadata();
174 
175  /// @brief Transfer any metadata associated with a VDB grid
176  /// to primitive attributes on a VDB primitive.
177  /// @param prim the primitive to be populated with attributes
178  /// @param grid the grid whose metadata should be transferred
179  /// @param gdp the detail to which to transfer attributes
180  static SYS_FORCE_INLINE
181  void createGridAttrsFromMetadata(
182  const GEO_PrimVDB& prim,
183  const openvdb::GridBase& grid,
184  GEO_Detail& gdp)
185  {
186  GU_PrimVDB::createGridAttrsFromMetadataAdapter(prim, &grid, gdp);
187  }
188 
189  /// @brief Transfer any metadata associated with the given MetaMap
190  /// to attributes on the given element specified by owner.
191  /// @param owner the type of element
192  /// @param element the offset of the element
193  /// @param meta_map the metadata that should be transferred
194  /// @param gdp the detail to which to transfer attributes
195  static SYS_FORCE_INLINE
196  void createAttrsFromMetadata(
197  GA_AttributeOwner owner,
198  GA_Offset element,
199  const openvdb::MetaMap& meta_map,
200  GEO_Detail& gdp)
201  {
202  GU_PrimVDB::createAttrsFromMetadataAdapter(owner, element, &meta_map, gdp);
203  }
204 
205  /// @brief Transfer a VDB primitive's attributes to a VDB grid as metadata.
206  /// @param grid the grid to be populated with metadata
207  /// @param prim the primitive whose attributes should be transferred
208  /// @param gdp the detail from which to retrieve primitive attributes
209  static SYS_FORCE_INLINE
210  void createMetadataFromGridAttrs(
211  openvdb::GridBase& grid,
212  const GEO_PrimVDB& prim,
213  const GEO_Detail& gdp)
214  {
215  GU_PrimVDB::createMetadataFromGridAttrsAdapter(&grid, prim, gdp);
216  }
217 
218  /// @brief Transfer attributes to VDB metadata.
219  /// @param meta_map the output metadata
220  /// @param owner the type of element
221  /// @param element the offset of the element
222  /// @param geo the detail from which to retrieve primitive attributes
223  static SYS_FORCE_INLINE
224  void createMetadataFromAttrs(
225  openvdb::MetaMap& meta_map,
226  GA_AttributeOwner owner,
227  GA_Offset element,
228  const GEO_Detail& geo)
229  {
230  GU_PrimVDB::createMetadataFromAttrsAdapter(&meta_map, owner, element, geo);
231  }
232 
233 private: // METHODS
234 
235  /// Add a border of the given radius by evaluating from the given volume.
236  /// It assumes that the VDB is a float grid and that the voxel array has
237  /// the same index space, so this can really only be safely called after
238  /// buildFromPrimVolume(). This is used to ensure that non-constant borders
239  /// can be converted at the expense of some extra memory.
240  void expandBorderFromPrimVolume(
241  const GEO_PrimVolume &vol,
242  int border_radius);
243 
244  GEO_Primitive * convertToNewPrim(
245  GEO_Detail &dst_geo,
246  GU_ConvertParms &parms,
247  fpreal adaptivity,
248  bool split_disjoint_volumes,
249  bool &success) const;
250  GEO_Primitive * convertToPrimVolume(
251  GEO_Detail &dst_geo,
252  GU_ConvertParms &parms,
253  bool split_disjoint_volumes) const;
254  GEO_Primitive * convertToPoly(
255  GEO_Detail &dst_geo,
256  GU_ConvertParms &parms,
257  fpreal adaptivity,
258  bool buildpolysoup,
259  bool &success) const;
260 
261  static GU_PrimVDB* buildFromGridAdapter(
262  GU_Detail& gdp,
263  void* grid,
264  const GEO_PrimVDB*,
265  const char* name);
266  static void createGridAttrsFromMetadataAdapter(
267  const GEO_PrimVDB& prim,
268  const void* grid,
269  GEO_Detail& gdp);
270  static void createMetadataFromGridAttrsAdapter(
271  void* grid,
272  const GEO_PrimVDB&,
273  const GEO_Detail&);
274 
275  static void createAttrsFromMetadataAdapter(
276  GA_AttributeOwner owner,
277  GA_Offset element,
278  const void* meta_map_ptr,
279  GEO_Detail& geo);
280 
281  static void createMetadataFromAttrsAdapter(
282  void* meta_map_ptr,
283  GA_AttributeOwner owner,
284  GA_Offset element,
285  const GEO_Detail& geo);
286 
287 private: // DATA
288 
289  static GA_PrimitiveDefinition *theDefinition;
290  friend class GU_PrimitiveFactory;
291  SYS_DEPRECATED_PUSH_DISABLE()
292 };
293  SYS_DEPRECATED_POP_DISABLE()
294 
295 
296 #ifndef SESI_OPENVDB
297 namespace openvdb_houdini {
298 using ::GU_PrimVDB;
299 } // namespace openvdb_houdini
300 #endif
301 
302 #endif // __HDK_GU_PrimVDB__
303 
304 #endif // SESI_OPENVDB || SESI_OPENVDB_PRIM
#define OPENVDB_HOUDINI_API
Definition: Platform.h:282
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
Definition: AttributeTransferUtil.h:34
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
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
Abstract base class for typed grids.
Definition: Grid.h:77