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 #if (UT_VERSION_INT < 0x0d050000) // Earlier than 13.5
19 #include "GU_Prim.h"
20 #endif
21 #include <UT/UT_Matrix4.h>
22 #include <UT/UT_VoxelArray.h>
23 #include <openvdb/Platform.h>
24 #include <stddef.h>
25 
26 
27 class GA_Attribute;
28 class GEO_PrimVolume;
29 class UT_MemoryCounter;
30 #if (UT_VERSION_INT >= 0x0d050000) // 13.5 or later
31 class GEO_ConvertParms;
33 #endif
34 
35 
37 #if (UT_VERSION_INT < 0x0d050000) // Earlier than 13.5
38  , public GU_Primitive
39 #endif
40 {
41 protected:
42  /// NOTE: Primitives should not be deleted directly. They are managed
43  /// by the GA_PrimitiveList and the stash.
44  virtual ~GU_PrimVDB() {}
45 
46 public:
47  /// NOTE: This constructor should only be called via GU_PrimitiveFactory.
49  : GEO_PrimVDB(gdp, offset)
50  {}
51 
52 #if UT_VERSION_INT < 0x1000011F // earlier than 16.0.287
53  /// NOTE: This constructor should only be called via GU_PrimitiveFactory.
54  GU_PrimVDB(const GA_MergeMap &map, GA_Detail &detail,
55  GA_Offset offset, const GU_PrimVDB &src_prim)
56  : GEO_PrimVDB(map, detail, offset, src_prim)
57  {}
58 #endif
59 
60  /// Report approximate memory usage.
61  virtual int64 getMemoryUsage() const;
62 
63  /// Count memory usage using a UT_MemoryCounter in order to count
64  /// shared memory correctly.
65  /// NOTE: This should always include sizeof(*this).
66  virtual void countMemory(UT_MemoryCounter &counter) const;
67 
68 #if 0 //ndef SESI_OPENVDB
69  /// Allows you to find out what this primitive type was named.
70  static GA_PrimitiveTypeId theTypeId() { return theDefinition->getId(); }
71 
72  /// Must be invoked during the factory callback to add us to the
73  /// list of primitives
74  static void registerMyself(GA_PrimitiveFactory *factory);
75 #endif
76 
77  virtual const GA_PrimitiveDefinition &getTypeDef() const
78  {
79  UT_ASSERT(theDefinition);
80  return *theDefinition;
81  }
82 
83  // Conversion Methods
84 
85  virtual GEO_Primitive *convert(GU_ConvertParms &parms,
86  GA_PointGroup *usedpts = 0);
87  virtual GEO_Primitive *convertNew(GU_ConvertParms &parms);
88 
89  /// Convert all GEO_PrimVolume primitives in geometry to
90  /// GEO_PrimVDB, preserving prim/vertex/point attributes (and prim/point
91  /// groups if requested).
92  static void convertVolumesToVDBs(
93  GU_Detail &dst_geo,
94  const GU_Detail &src_geo,
95  GU_ConvertParms &parms,
96  bool flood_sdf,
97  bool prune,
98  fpreal tolerance,
99  bool keep_original,
100  bool activate_inside = true);
101 
102  /// Convert all GEO_PrimVDB primitives in geometry to parms.toType,
103  /// preserving prim/vertex/point attributes (and prim/point groups if
104  /// requested).
105  /// @{
106  static void convertVDBs(
107  GU_Detail &dst_geo,
108  const GU_Detail &src_geo,
109  GU_ConvertParms &parms,
110  fpreal adaptivity,
111  bool keep_original);
112  static void convertVDBs(
113  GU_Detail &dst_geo,
114  const GU_Detail &src_geo,
115  GU_ConvertParms &parms,
116  fpreal adaptivity,
117  bool keep_original,
118  bool split_disjoint_volumes);
119  /// @}
120 
121 #if (UT_VERSION_INT < 0x0d050000) // Earlier than 13.5
122  virtual void *castTo (void) const;
123  virtual const GEO_Primitive *castToGeo(void) const;
124 #endif
125 
126  // NOTE: For static member functions please call in the following
127  // manner. <ptrvalue> = GU_PrimVDB::<functname>
128  // i.e. partptr = GU_PrimVDB::build(params...);
129 
130  // Optional Build Method
131 
132  static GU_PrimVDB * build(GU_Detail *gdp, bool append_points = true);
133 
134  /// Store a VDB grid in a new VDB primitive and add the primitive
135  /// to a geometry detail.
136  /// @param gdp the detail to which to add the new primitive
137  /// @param grid a grid to be associated with the new primitive
138  /// @param src if non-null, copy attributes and groups from this primitive
139  /// @param name if non-null, set the new primitive's @c name attribute to
140  /// this string; otherwise, if @a src is non-null, use its name
141  static GU_PrimVDB* buildFromGrid(GU_Detail& gdp, openvdb::GridBase::Ptr grid,
142  const GEO_PrimVDB* src = NULL, const char* name = NULL)
143  {
144  return GU_PrimVDB::buildFromGridAdapter(gdp, &grid, src, name);
145  }
146 
147  /// Create new VDB primitive from the given native volume primitive
148  static GU_PrimVDB * buildFromPrimVolume(
149  GU_Detail &geo,
150  const GEO_PrimVolume &vol,
151  const char *name,
152  const bool flood_sdf = false,
153  const bool prune = false,
154  const float tolerance = 0.0,
155  const bool activate_inside_sdf = true);
156 
157  /// A fast method for converting a primitive volume to a polysoup via VDB
158  /// into the given gdp. It will _not_ copy attributes because this is a
159  /// special case used for display purposes only.
160  static void convertPrimVolumeToPolySoup(
161  GU_Detail &dst_geo,
162  const GEO_PrimVolume &src_vol);
163 
164  virtual void normal(NormalComp &output) const;
165 
166 #if (UT_VERSION_INT < 0x0d050000) // Earlier than 13.5
167  virtual int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d,
168  float tmax = 1E17F, float tol = 1E-12F,
169  float *distance = 0, UT_Vector3 *pos = 0,
170  UT_Vector3 *nml = 0, int accurate = 0,
171  float *u = 0, float *v = 0,
172  int ignoretrim = 1) const;
173 #endif
174 
175  // callermustdelete is true if the returned cache is to be deleted by
176  // the caller.
177 #if (UT_VERSION_INT < 0x0d050000) // Earlier than 13.5
178 
179 #if (UT_VERSION_INT >= 0x0d000000) // 13.0 or later
180  SYS_DEPRECATED_HDK(13.0)
181 #endif
182  virtual GU_RayIntersect *createRayCache(int &callermustdelete);
183 #endif
184 
185  /// @brief Transfer any metadata associated with this primitive's
186  /// VDB grid to primitive attributes.
187  void syncAttrsFromMetadata();
188 
189  /// @brief Transfer any metadata associated with a VDB grid
190  /// to primitive attributes on a VDB primitive.
191  /// @param prim the primitive to be populated with attributes
192  /// @param grid the grid whose metadata should be transferred
193  /// @param gdp the detail to which to transfer attributes
195  const GEO_PrimVDB& prim,
196  const openvdb::GridBase& grid,
197  GEO_Detail& gdp)
198  {
199  GU_PrimVDB::createGridAttrsFromMetadataAdapter(prim, &grid, gdp);
200  }
201 
202  /// @brief Transfer any metadata associated with the given MetaMap
203  /// to attributes on the given element specified by owner.
204  /// @param owner the type of element
205  /// @param element the offset of the element
206  /// @param meta_map the metadata that should be transferred
207  /// @param gdp the detail to which to transfer attributes
209  GA_AttributeOwner owner,
210  GA_Offset element,
211  const openvdb::MetaMap& meta_map,
212  GEO_Detail& gdp)
213  {
214  GU_PrimVDB::createAttrsFromMetadataAdapter(owner, element, &meta_map, gdp);
215  }
216 
217  /// @brief Transfer a VDB primitive's attributes to a VDB grid as metadata.
218  /// @param grid the grid to be populated with metadata
219  /// @param prim the primitive whose attributes should be transferred
220  /// @param gdp the detail from which to retrieve primitive attributes
222  openvdb::GridBase& grid,
223  const GEO_PrimVDB& prim,
224  const GEO_Detail& gdp)
225  {
226  GU_PrimVDB::createMetadataFromGridAttrsAdapter(&grid, prim, gdp);
227  }
228 
229  /// @brief Transfer attributes to VDB metadata.
230  /// @param meta_map the output metadata
231  /// @param owner the type of element
232  /// @param element the offset of the element
233  /// @param geo the detail from which to retrieve primitive attributes
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 
243 private: // 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 
297 private: // DATA
298 
299  static GA_PrimitiveDefinition *theDefinition;
300  friend class GU_PrimitiveFactory;
301 #if (UT_VERSION_INT >= 0x0d000000) // 13.0 or later
303 #endif
304 };
305 #if (UT_VERSION_INT >= 0x0d000000) // 13.0 or later
307 #endif
308 
309 
310 #if 0 //ndef SESI_OPENVDB
311 namespace openvdb_houdini {
312 using ::GU_PrimVDB;
313 } // namespace openvdb_houdini
314 #endif
315 
316 #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:48
Definition of a geometry attribute.
Definition: GA_Attribute.h:190
static 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:208
#define SYS_DEPRECATED_PUSH_DISABLE()
const GLdouble * v
Definition: glcorearb.h:836
#define SYS_DEPRECATED_POP_DISABLE()
The merge map keeps track of information when merging details.
Definition: GA_MergeMap.h:53
virtual int64 getMemoryUsage() const
Definition: GA_Primitive.h:201
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
virtual void normal(NormalComp &output) const =0
GA_Size GA_Offset
Definition: GA_Types.h:617
long long int64
Definition: SYS_Types.h:107
static 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:234
T distance(const UT_Vector4T< T > &v1, const UT_Vector4T< T > &v2)
Definition: UT_Vector4.h:698
virtual void countMemory(UT_MemoryCounter &counter) const
GLintptr offset
Definition: glcorearb.h:664
static 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:194
GU_PrimVDB(const GA_MergeMap &map, GA_Detail &detail, GA_Offset offset, const GU_PrimVDB &src_prim)
NOTE: This constructor should only be called via GU_PrimitiveFactory.
Definition: GU_PrimVDB.h:54
#define GU_API
Definition: GU_API.h:12
#define SYS_DEPRECATED_HDK(__V__)
GLuint const GLchar * name
Definition: glcorearb.h:785
static 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:221
virtual ~GU_PrimVDB()
Definition: GU_PrimVDB.h:44
static GU_PrimVDB * buildFromGrid(GU_Detail &gdp, openvdb::GridBase::Ptr grid, const GEO_PrimVDB *src=NULL, const char *name=NULL)
Definition: GU_PrimVDB.h:141
GA_AttributeOwner
Definition: GA_Types.h:33
double fpreal
Definition: SYS_Types.h:270
virtual int intersectRay(const UT_Vector3 &o, const UT_Vector3 &d, float tmax=1E17F, float tol=1E-12F, float *distance=0, UT_Vector3 *pos=0, UT_Vector3 *nml=0, int accurate=0, float *u=0, float *v=0, int ignoretrim=1) const
GEO_ConvertParms GU_ConvertParms
Container class for all geometry.
Definition: GA_Detail.h:95
virtual GEO_Primitive * convert(GEO_ConvertParms &parms, GA_PointGroup *usedpts=0)=0
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
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:361
virtual const GA_PrimitiveDefinition & getTypeDef() const
Definition: GU_PrimVDB.h:77
virtual GEO_Primitive * convertNew(GEO_ConvertParms &parms)=0
GLenum src
Definition: glcorearb.h:1792