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 // Builds a vdb directly from an IMX_VDB
155 static GEO_PrimVDB *buildFromVDB(GU_Detail *gdp,
156 UT_SharedPtr<IMX_VDB> vdb);
157 static GEO_PrimVDB *buildFromVDB(GU_Detail *gdp,
158 UT_SharedPtr<const IMX_VDB> vdb);
159
160
161 /// Create new VDB primitive from the given native volume primitive
162 static GU_PrimVDB * buildFromPrimVolume(
163 GU_Detail &geo,
164 const GEO_PrimVolume &vol,
165 const char *name,
166 const bool flood_sdf = false,
167 const bool prune = false,
168 const float tolerance = 0.0,
169 const bool activate_inside_sdf = true);
170
171 /// A fast method for converting a primitive volume to a polysoup via VDB
172 /// into the given gdp. It will _not_ copy attributes because this is a
173 /// special case used for display purposes only.
174 static void convertPrimVolumeToPolySoup(
175 GU_Detail &dst_geo,
176 const GEO_PrimVolume &src_vol);
177
178 void normal(NormalComp &output) const override;
179 void normal(NormalCompD &output) const override;
180
181 /// @brief Transfer any metadata associated with this primitive's
182 /// VDB grid to primitive attributes.
183 void syncAttrsFromMetadata();
184
185 /// @brief Transfer any metadata associated with a VDB grid
186 /// to primitive attributes on a VDB primitive.
187 /// @param prim the primitive to be populated with attributes
188 /// @param grid the grid whose metadata should be transferred
189 /// @param gdp the detail to which to transfer attributes
190 static SYS_FORCE_INLINE
191 void createGridAttrsFromMetadata(
192 const GEO_PrimVDB& prim,
193 const openvdb::GridBase& grid,
194 GEO_Detail& gdp)
195 {
196 GU_PrimVDB::createGridAttrsFromMetadataAdapter(prim, &grid, gdp);
197 }
198
199 /// @brief Transfer any metadata associated with the given MetaMap
200 /// to attributes on the given element specified by owner.
201 /// @param owner the type of element
202 /// @param element the offset of the element
203 /// @param meta_map the metadata that should be transferred
204 /// @param gdp the detail to which to transfer attributes
205 static SYS_FORCE_INLINE
206 void createAttrsFromMetadata(
207 GA_AttributeOwner owner,
208 GA_Offset element,
209 const openvdb::MetaMap& meta_map,
210 GEO_Detail& gdp)
211 {
212 GU_PrimVDB::createAttrsFromMetadataAdapter(owner, element, &meta_map, gdp);
213 }
214
215 /// @brief Transfer a VDB primitive's attributes to a VDB grid as metadata.
216 /// @param grid the grid to be populated with metadata
217 /// @param prim the primitive whose attributes should be transferred
218 /// @param gdp the detail from which to retrieve primitive attributes
219 static SYS_FORCE_INLINE
220 void createMetadataFromGridAttrs(
221 openvdb::GridBase& grid,
222 const GEO_PrimVDB& prim,
223 const GEO_Detail& gdp)
224 {
225 GU_PrimVDB::createMetadataFromGridAttrsAdapter(&grid, prim, gdp);
226 }
227
228 /// @brief Transfer attributes to VDB metadata.
229 /// @param meta_map the output metadata
230 /// @param owner the type of element
231 /// @param element the offset of the element
232 /// @param geo the detail from which to retrieve primitive attributes
233 static SYS_FORCE_INLINE
234 void createMetadataFromAttrs(
235 openvdb::MetaMap& meta_map,
236 GA_AttributeOwner owner,
237 GA_Offset element,
238 const GEO_Detail& geo)
239 {
240 GU_PrimVDB::createMetadataFromAttrsAdapter(&meta_map, owner, element, geo);
241 }
242
243private: // METHODS
244
245 /// Add a border of the given radius by evaluating from the given volume.
246 /// It assumes that the VDB is a float grid and that the voxel array has
247 /// the same index space, so this can really only be safely called after
248 /// buildFromPrimVolume(). This is used to ensure that non-constant borders
249 /// can be converted at the expense of some extra memory.
250 void expandBorderFromPrimVolume(
251 const GEO_PrimVolume &vol,
252 int border_radius);
253
254 GEO_Primitive * convertToNewPrim(
255 GEO_Detail &dst_geo,
256 GU_ConvertParms &parms,
257 fpreal adaptivity,
258 bool split_disjoint_volumes,
259 bool &success) const;
260 GEO_Primitive * convertToPrimVolume(
261 GEO_Detail &dst_geo,
262 GU_ConvertParms &parms,
263 bool split_disjoint_volumes) const;
264 GEO_Primitive * convertToPoly(
265 GEO_Detail &dst_geo,
266 GU_ConvertParms &parms,
267 fpreal adaptivity,
268 bool buildpolysoup,
269 bool &success) const;
270
271 static GU_PrimVDB* buildFromGridAdapter(
272 GU_Detail& gdp,
273 void* grid,
274 const GEO_PrimVDB*,
275 const char* name);
276 static void createGridAttrsFromMetadataAdapter(
277 const GEO_PrimVDB& prim,
278 const void* grid,
279 GEO_Detail& gdp);
280 static void createMetadataFromGridAttrsAdapter(
281 void* grid,
282 const GEO_PrimVDB&,
283 const GEO_Detail&);
284
285 static void createAttrsFromMetadataAdapter(
286 GA_AttributeOwner owner,
287 GA_Offset element,
288 const void* meta_map_ptr,
289 GEO_Detail& geo);
290
291 static void createMetadataFromAttrsAdapter(
292 void* meta_map_ptr,
293 GA_AttributeOwner owner,
294 GA_Offset element,
295 const GEO_Detail& geo);
296
297private: // DATA
298
299 static GA_PrimitiveDefinition *theDefinition;
300 friend class GU_PrimitiveFactory;
301 SYS_DEPRECATED_PUSH_DISABLE()
302};
303 SYS_DEPRECATED_POP_DISABLE()
304
305
306#ifndef SESI_OPENVDB
307namespace openvdb_houdini {
308using ::GU_PrimVDB;
309} // namespace openvdb_houdini
310#endif
311
312#endif // __HDK_GU_PrimVDB__
313
314#endif // SESI_OPENVDB || SESI_OPENVDB_PRIM
#define OPENVDB_HOUDINI_API
Definition Platform.h:286
Definition AttributeTransferUtil.h:34