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