OpenVDB  12.0.0
UT_VDBUtils.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  * 123 Front Street West, Suite 1401
10  * Toronto, Ontario
11  * Canada M5J 2M2
12  * 416-366-4607
13  */
14 
15 #include <UT/UT_Version.h>
16 
17 #if !defined(SESI_OPENVDB) && !defined(SESI_OPENVDB_PRIM)
18 
19 #include <UT/UT_VDBUtils.h>
20 
21 #else
22 
23 #ifndef __HDK_UT_VDBUtils__
24 #define __HDK_UT_VDBUtils__
25 
26 enum UT_VDBType
27 {
28  UT_VDB_INVALID,
29  UT_VDB_FLOAT,
30  UT_VDB_DOUBLE,
31  UT_VDB_INT32,
32  UT_VDB_INT64,
33  UT_VDB_BOOL,
34  UT_VDB_VEC3F,
35  UT_VDB_VEC3D,
36  UT_VDB_VEC3I,
37  UT_VDB_POINTINDEX,
38  UT_VDB_POINTDATA,
39 };
40 
41 #include <openvdb/openvdb.h>
44 
45 #include <UT/UT_Assert.h>
46 #include <UT/UT_BoundingBox.h>
47 #include <UT/UT_Matrix4.h>
48 #include <UT/UT_Matrix3.h>
49 #include <UT/UT_Matrix2.h>
50 #include <SYS/SYS_Math.h>
51 
52 
53 /// Calls openvdb::initialize()
54 inline void UTvdbInitialize() { openvdb::initialize(); }
55 
56 /// Find the UT_VDBType from a grid
57 inline UT_VDBType
58 UTvdbGetGridType(const openvdb::GridBase &grid)
59 {
60  using namespace openvdb;
61  using namespace openvdb::tools;
62  using namespace openvdb::points;
63 
64  if (grid.isType<FloatGrid>())
65  return UT_VDB_FLOAT;
66  if (grid.isType<DoubleGrid>())
67  return UT_VDB_DOUBLE;
68  if (grid.isType<Int32Grid>())
69  return UT_VDB_INT32;
70  if (grid.isType<Int64Grid>())
71  return UT_VDB_INT64;
72  if (grid.isType<BoolGrid>())
73  return UT_VDB_BOOL;
74  if (grid.isType<Vec3fGrid>())
75  return UT_VDB_VEC3F;
76  if (grid.isType<Vec3dGrid>())
77  return UT_VDB_VEC3D;
78  if (grid.isType<Vec3IGrid>())
79  return UT_VDB_VEC3I;
80  if (grid.isType<Vec3IGrid>())
81  return UT_VDB_VEC3I;
82  if (grid.isType<PointIndexGrid>())
83  return UT_VDB_POINTINDEX;
84  if (grid.isType<PointDataGrid>())
85  return UT_VDB_POINTDATA;
86 
87  return UT_VDB_INVALID;
88 }
89 
90 /// Return the string representation of a grid's underlying value type
91 inline const char *
92 UTvdbGetGridTypeString(const openvdb::GridBase &grid)
93 {
94  switch(UTvdbGetGridType(grid))
95  {
96  case UT_VDB_FLOAT:
97  return "float";
98  case UT_VDB_DOUBLE:
99  return "double";
100  case UT_VDB_INT32:
101  return "int32";
102  case UT_VDB_INT64:
103  return "int64";
104  case UT_VDB_BOOL:
105  return "bool";
106  case UT_VDB_VEC3F:
107  return "Vec3f";
108  case UT_VDB_VEC3D:
109  return "Vec3d";
110  case UT_VDB_VEC3I:
111  return "Vec3i";
112  case UT_VDB_POINTINDEX:
113  return "PointIndex";
114  case UT_VDB_POINTDATA:
115  return "PointData";
116  default:
117  return "invalid type";
118  }
119 }
120 
121 /// Returns the tuple size of a grid given its value type.
122 inline int
123 UTvdbGetGridTupleSize(UT_VDBType type)
124 {
125  switch(type)
126  {
127  case UT_VDB_FLOAT:
128  case UT_VDB_DOUBLE:
129  case UT_VDB_INT32:
130  case UT_VDB_INT64:
131  case UT_VDB_BOOL:
132  return 1;
133 
134  case UT_VDB_VEC3F:
135  case UT_VDB_VEC3D:
136  case UT_VDB_VEC3I:
137  return 3;
138 
139  case UT_VDB_POINTINDEX:
140  case UT_VDB_POINTDATA:
141  case UT_VDB_INVALID:
142  default:
143  break;
144  }
145 
146  return 0;
147 }
148 
149 /// Returns the tuple size of a grid
150 inline int
151 UTvdbGetGridTupleSize(const openvdb::GridBase &grid)
152 {
153  return UTvdbGetGridTupleSize(UTvdbGetGridType(grid));
154 }
155 
156 /// Special plusEqual class to avoid bool warnings
157 /// @{
158 template <typename T>
159 struct UT_VDBMath
160 {
161  static void plusEqual(T &lhs, const T &rhs)
162  { lhs += rhs; }
163 };
164 template <>
165 struct UT_VDBMath<bool>
166 {
167  static void plusEqual(bool &lhs, const bool &rhs)
168  { lhs = lhs | rhs; }
169 };
170 /// @}
171 
172 /// Helpers for downcasting to a specific grid type
173 /// @{
174 template <typename GridType>
175 inline const GridType *
176 UTvdbGridCast(const openvdb::GridBase *grid)
177  { return UTverify_cast<const GridType *>(grid); }
178 
179 template <typename GridType>
180 inline GridType *
181 UTvdbGridCast(openvdb::GridBase *grid)
182  { return UTverify_cast<GridType *>(grid); }
183 
184 template <typename GridType>
185 inline const GridType &
186 UTvdbGridCast(const openvdb::GridBase &grid)
187  { return *UTverify_cast<const GridType *>(&grid); }
188 
189 template <typename GridType>
190 inline GridType &
191 UTvdbGridCast(openvdb::GridBase &grid)
192  { return *UTverify_cast<GridType *>(&grid); }
193 
194 template <typename GridType>
195 inline typename GridType::ConstPtr
196 UTvdbGridCast(openvdb::GridBase::ConstPtr grid)
197  { return openvdb::gridConstPtrCast<GridType>(grid); }
198 
199 template <typename GridType>
200 inline typename GridType::Ptr
201 UTvdbGridCast(openvdb::GridBase::Ptr grid)
202  { return openvdb::gridPtrCast<GridType>(grid); }
203 /// @}
204 
205 ////////////////////////////////////////
206 
207 namespace UT_VDBUtils {
208 
209 // Helper function used internally by UTvdbProcessTypedGrid()
210 // to instantiate a templated functor for a specific grid type
211 // and then to call the functor with a grid of that type
212 template<typename GridType, typename OpType, typename GridBaseType>
213 inline void
214 callTypedGrid(GridBaseType &grid, OpType& op)
215 {
216  op.template operator()<GridType>(UTvdbGridCast<GridType>(grid));
217 }
218 
219 } // namespace UT_VDBUtils
220 
221 ////////////////////////////////////////
222 
223 
224 /// @brief Utility function that, given a generic grid pointer,
225 /// calls a functor on the fully-resolved grid
226 ///
227 /// @par Example:
228 /// @code
229 /// using openvdb::Coord;
230 /// using openvdb::CoordBBox;
231 ///
232 /// struct FillOp {
233 /// const CoordBBox bbox;
234 ///
235 /// FillOp(const CoordBBox& b): bbox(b) {}
236 ///
237 /// template<typename GridT>
238 /// void operator()(GridT& grid) const {
239 /// using ValueT = typename GridT::ValueType;
240 /// grid.fill(bbox, ValueT(1));
241 /// }
242 /// };
243 ///
244 /// GU_PrimVDB* vdb = ...;
245 /// vdb->makeGridUnique();
246 /// CoordBBox bbox(Coord(0,0,0), Coord(10,10,10));
247 /// UTvdbProcessTypedGrid(vdb->getStorageType(), vdb->getGrid(), FillOp(bbox));
248 /// @endcode
249 ///
250 /// @return @c false if the grid type is unknown or unhandled.
251 /// @{
252 #define UT_VDB_DECL_PROCESS_TYPED_GRID(GRID_BASE_T) \
253 template<typename OpType> \
254 inline bool \
255 UTvdbProcessTypedGrid(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
256 { \
257  using namespace openvdb; \
258  using namespace UT_VDBUtils; \
259  switch (grid_type) \
260  { \
261  case UT_VDB_FLOAT: callTypedGrid<FloatGrid>(grid, op); break; \
262  case UT_VDB_DOUBLE: callTypedGrid<DoubleGrid>(grid, op); break; \
263  case UT_VDB_INT32: callTypedGrid<Int32Grid>(grid, op); break; \
264  case UT_VDB_INT64: callTypedGrid<Int64Grid>(grid, op); break; \
265  case UT_VDB_VEC3F: callTypedGrid<Vec3SGrid>(grid, op); break; \
266  case UT_VDB_VEC3D: callTypedGrid<Vec3DGrid>(grid, op); break; \
267  case UT_VDB_VEC3I: callTypedGrid<Vec3IGrid>(grid, op); break; \
268  default: return false; \
269  } \
270  return true; \
271 } \
272 template<typename OpType> \
273 inline bool \
274 UTvdbProcessTypedGridTopology(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
275 { \
276  using namespace openvdb; \
277  using namespace UT_VDBUtils; \
278  switch (grid_type) \
279  { \
280  case UT_VDB_FLOAT: callTypedGrid<FloatGrid>(grid, op); break; \
281  case UT_VDB_DOUBLE: callTypedGrid<DoubleGrid>(grid, op); break; \
282  case UT_VDB_INT32: callTypedGrid<Int32Grid>(grid, op); break; \
283  case UT_VDB_INT64: callTypedGrid<Int64Grid>(grid, op); break; \
284  case UT_VDB_VEC3F: callTypedGrid<Vec3SGrid>(grid, op); break; \
285  case UT_VDB_VEC3D: callTypedGrid<Vec3DGrid>(grid, op); break; \
286  case UT_VDB_VEC3I: callTypedGrid<Vec3IGrid>(grid, op); break; \
287  case UT_VDB_BOOL: callTypedGrid<BoolGrid>(grid, op); break; \
288  default: return false; \
289  } \
290  return true; \
291 } \
292 template<typename OpType> \
293 inline bool \
294 UTvdbProcessTypedGridVec3(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
295 { \
296  using namespace openvdb; \
297  using namespace UT_VDBUtils; \
298  switch (grid_type) \
299  { \
300  case UT_VDB_VEC3F: callTypedGrid<Vec3SGrid>(grid, op); break; \
301  case UT_VDB_VEC3D: callTypedGrid<Vec3DGrid>(grid, op); break; \
302  case UT_VDB_VEC3I: callTypedGrid<Vec3IGrid>(grid, op); break; \
303  default: return false; \
304  } \
305  return true; \
306 } \
307 template<typename OpType> \
308 inline bool \
309 UTvdbProcessTypedGridScalar(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
310 { \
311  using namespace openvdb; \
312  using namespace UT_VDBUtils; \
313  switch (grid_type) \
314  { \
315  case UT_VDB_FLOAT: callTypedGrid<FloatGrid>(grid, op); break; \
316  case UT_VDB_DOUBLE: callTypedGrid<DoubleGrid>(grid, op); break; \
317  case UT_VDB_INT32: callTypedGrid<Int32Grid>(grid, op); break; \
318  case UT_VDB_INT64: callTypedGrid<Int64Grid>(grid, op); break; \
319  default: return false; \
320  } \
321  return true; \
322 } \
323 template<typename OpType> \
324 inline bool \
325 UTvdbProcessTypedGridReal(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
326 { \
327  using namespace openvdb; \
328  using namespace UT_VDBUtils; \
329  switch (grid_type) \
330  { \
331  case UT_VDB_FLOAT: callTypedGrid<FloatGrid>(grid, op); break; \
332  case UT_VDB_DOUBLE: callTypedGrid<DoubleGrid>(grid, op); break; \
333  default: return false; \
334  } \
335  return true; \
336 } \
337 template<typename OpType> \
338 inline bool \
339 UTvdbProcessTypedGridPoint(UT_VDBType grid_type, GRID_BASE_T grid, OpType& op) \
340 { \
341  using namespace openvdb; \
342  using namespace openvdb::tools; \
343  using namespace openvdb::points; \
344  using namespace UT_VDBUtils; \
345  switch (grid_type) \
346  { \
347  case UT_VDB_POINTINDEX: callTypedGrid<PointIndexGrid>(grid, op); break; \
348  case UT_VDB_POINTDATA: callTypedGrid<PointDataGrid>(grid, op); break; \
349  default: return false; \
350  } \
351  return true; \
352 } \
353 /**/
354 UT_VDB_DECL_PROCESS_TYPED_GRID(const openvdb::GridBase &)
355 UT_VDB_DECL_PROCESS_TYPED_GRID(const openvdb::GridBase *)
356 UT_VDB_DECL_PROCESS_TYPED_GRID(openvdb::GridBase::ConstPtr)
357 UT_VDB_DECL_PROCESS_TYPED_GRID(openvdb::GridBase &)
358 UT_VDB_DECL_PROCESS_TYPED_GRID(openvdb::GridBase *)
359 UT_VDB_DECL_PROCESS_TYPED_GRID(openvdb::GridBase::Ptr)
360 
361 /// @}
362 
363 
364 // Helper macro for UTvdbCall* macros, do not outside of this file!
365 #define UT_VDB_CALL(GRIDT, RETURN, FNAME, GRIDBASE, ...) \
366  { \
367  RETURN FNAME <GRIDT> (UTvdbGridCast<GRIDT>(GRIDBASE), __VA_ARGS__ ); \
368  } \
369  /**/
370 
371 //@{
372 /// Macro to invoke the correct type of grid.
373 /// Use like:
374 /// @code
375 /// UTvdbCallScalarType(grid_type, myfunction, grid, parms)
376 /// @endcode
377 /// to invoke
378 /// @code
379 /// template <typename GridType>
380 /// static void
381 /// myfunction(const GridType &grid, parms)
382 /// { }
383 /// @endcode
384 
385 #define UTvdbCallRealType(TYPE, FNAME, GRIDBASE, ...) \
386  if (TYPE == UT_VDB_FLOAT) \
387  UT_VDB_CALL(openvdb::FloatGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
388  else if (TYPE == UT_VDB_DOUBLE) \
389  UT_VDB_CALL(openvdb::DoubleGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
390  /**/
391 #define UTvdbCallScalarType(TYPE, FNAME, GRIDBASE, ...) \
392  UTvdbCallRealType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
393  else if (TYPE == UT_VDB_INT32) \
394  UT_VDB_CALL(openvdb::Int32Grid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
395  else if (TYPE == UT_VDB_INT64) \
396  UT_VDB_CALL(openvdb::Int64Grid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
397  /**/
398 #define UTvdbCallVec3Type(TYPE, FNAME, GRIDBASE, ...) \
399  if (TYPE == UT_VDB_VEC3F) \
400  UT_VDB_CALL(openvdb::Vec3fGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
401  else if (TYPE == UT_VDB_VEC3D) \
402  UT_VDB_CALL(openvdb::Vec3dGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
403  else if (TYPE == UT_VDB_VEC3I) \
404  UT_VDB_CALL(openvdb::Vec3IGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
405  /**/
406 #define UTvdbCallPointType(TYPE, FNAME, GRIDBASE, ...) \
407  if (TYPE == UT_VDB_POINTINDEX) \
408  UT_VDB_CALL(openvdb::tools::PointIndexGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
409  else if (TYPE == UT_VDB_POINTDATA) \
410  UT_VDB_CALL(openvdb::points::PointDataGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
411  /**/
412 #define UTvdbCallBoolType(TYPE, FNAME, GRIDBASE, ...) \
413  if (TYPE == UT_VDB_BOOL) \
414  UT_VDB_CALL(openvdb::BoolGrid,(void),FNAME,GRIDBASE,__VA_ARGS__) \
415  /**/
416 #define UTvdbCallAllType(TYPE, FNAME, GRIDBASE, ...) \
417  UTvdbCallScalarType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
418  else UTvdbCallVec3Type(TYPE, FNAME, GRIDBASE, __VA_ARGS__); \
419  /**/
420 #define UTvdbCallAllTopology(TYPE, FNAME, GRIDBASE, ...) \
421  UTvdbCallScalarType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
422  else UTvdbCallVec3Type(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
423  else UTvdbCallBoolType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
424  /**/
425 //@}
426 
427 //@{
428 /// Macro to invoke the correct type of grid.
429 /// Use like:
430 /// @code
431 /// UTvdbReturnScalarType(grid_type, myfunction, grid, parms)
432 /// @endcode
433 /// to invoke
434 /// @code
435 /// return myfunction(grid, parms);
436 /// @endcode
437 /// via:
438 /// @code
439 /// template <typename GridType>
440 /// static RESULT
441 /// myfunction(const GridType &grid, parms)
442 /// { }
443 /// @endcode
444 
445 #define UTvdbReturnRealType(TYPE, FNAME, GRIDBASE, ...) \
446  if (TYPE == UT_VDB_FLOAT) \
447  UT_VDB_CALL(openvdb::FloatGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
448  else if (TYPE == UT_VDB_DOUBLE) \
449  UT_VDB_CALL(openvdb::DoubleGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
450  /**/
451 #define UTvdbReturnScalarType(TYPE, FNAME, GRIDBASE, ...) \
452  UTvdbReturnRealType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
453  else if (TYPE == UT_VDB_INT32) \
454  UT_VDB_CALL(openvdb::Int32Grid,return,FNAME,GRIDBASE,__VA_ARGS__) \
455  else if (TYPE == UT_VDB_INT64) \
456  UT_VDB_CALL(openvdb::Int64Grid,return,FNAME,GRIDBASE,__VA_ARGS__) \
457  /**/
458 #define UTvdbReturnVec3Type(TYPE, FNAME, GRIDBASE, ...) \
459  if (TYPE == UT_VDB_VEC3F) \
460  UT_VDB_CALL(openvdb::Vec3fGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
461  else if (TYPE == UT_VDB_VEC3D) \
462  UT_VDB_CALL(openvdb::Vec3dGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
463  else if (TYPE == UT_VDB_VEC3I) \
464  UT_VDB_CALL(openvdb::Vec3IGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
465  /**/
466 #define UTvdbReturnPointType(TYPE, FNAME, GRIDBASE, ...) \
467  if (TYPE == UT_VDB_POINTINDEX) \
468  UT_VDB_CALL(openvdb::tools::PointIndexGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
469  else if (TYPE == UT_VDB_POINTDATA) \
470  UT_VDB_CALL(openvdb::points::PointDataGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
471  /**/
472 #define UTvdbReturnBoolType(TYPE, FNAME, GRIDBASE, ...) \
473  if (TYPE == UT_VDB_BOOL) \
474  UT_VDB_CALL(openvdb::BoolGrid,return,FNAME,GRIDBASE,__VA_ARGS__) \
475  /**/
476 #define UTvdbReturnAllType(TYPE, FNAME, GRIDBASE, ...) \
477  UTvdbReturnScalarType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
478  else UTvdbReturnVec3Type(TYPE, FNAME, GRIDBASE, __VA_ARGS__); \
479  /**/
480 #define UTvdbReturnAllTopology(TYPE, FNAME, GRIDBASE, ...) \
481  UTvdbReturnScalarType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
482  else UTvdbReturnVec3Type(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
483  else UTvdbReturnBoolType(TYPE, FNAME, GRIDBASE, __VA_ARGS__) \
484  /**/
485 //@}
486 
487 
488 ////////////////////////////////////////
489 
490 
491 /// Matrix conversion from openvdb to UT
492 // @{
493 template <typename S>
494 UT_Matrix4T<S>
495 UTvdbConvert(const openvdb::math::Mat4<S> &src)
496 {
497  return UT_Matrix4T<S>(src(0,0), src(0,1), src(0,2), src(0,3),
498  src(1,0), src(1,1), src(1,2), src(1,3),
499  src(2,0), src(2,1), src(2,2), src(2,3),
500  src(3,0), src(3,1), src(3,2), src(3,3));
501 }
502 
503 template <typename S>
504 UT_Matrix3T<S>
505 UTvdbConvert(const openvdb::math::Mat3<S> &src)
506 {
507  return UT_Matrix3T<S>(src(0,0), src(0,1), src(0,2),
508  src(1,0), src(1,1), src(1,2),
509  src(2,0), src(2,1), src(2,2));
510 }
511 
512 template <typename S>
513 UT_Matrix2T<S>
514 UTvdbConvert(const openvdb::math::Mat2<S> &src)
515 {
516  return UT_Matrix2T<S>(src(0,0), src(0,1),
517  src(1,0), src(1,1));
518 }
519 // @}
520 
521 /// Matrix conversion from UT to openvdb
522 // @{
523 template <typename S>
524 openvdb::math::Mat4<S>
525 UTvdbConvert(const UT_Matrix4T<S> &src)
526 {
527  return openvdb::math::Mat4<S>(src(0,0), src(0,1), src(0,2), src(0,3),
528  src(1,0), src(1,1), src(1,2), src(1,3),
529  src(2,0), src(2,1), src(2,2), src(2,3),
530  src(3,0), src(3,1), src(3,2), src(3,3));
531 }
532 template <typename S>
533 openvdb::math::Mat3<S>
534 UTvdbConvert(const UT_Matrix3T<S> &src)
535 {
536  return openvdb::math::Mat3<S>(src(0,0), src(0,1), src(0,2),
537  src(1,0), src(1,1), src(1,2),
538  src(2,0), src(2,1), src(2,2));
539 }
540 template <typename S>
541 openvdb::math::Mat2<S>
542 UTvdbConvert(const UT_Matrix2T<S> &src)
543 {
544  return openvdb::math::Mat2<S>(src(0,0), src(0,1),
545  src(1,0), src(1,1));
546 }
547 // @}
548 
549 /// Vector conversion from openvdb to UT
550 // @{
551 template <typename S>
552 UT_Vector4T<S>
553 UTvdbConvert(const openvdb::math::Vec4<S> &src)
554 {
555  return UT_Vector4T<S>(src.asPointer());
556 }
557 template <typename S>
558 UT_Vector3T<S>
559 UTvdbConvert(const openvdb::math::Vec3<S> &src)
560 {
561  return UT_Vector3T<S>(src.asPointer());
562 }
563 template <typename S>
564 UT_Vector2T<S>
565 UTvdbConvert(const openvdb::math::Vec2<S> &src)
566 {
567  return UT_Vector2T<S>(src.asPointer());
568 }
569 // @}
570 
571 /// Vector conversion from UT to openvdb
572 // @{
573 template <typename S>
574 openvdb::math::Vec4<S>
575 UTvdbConvert(const UT_Vector4T<S> &src)
576 {
577  return openvdb::math::Vec4<S>(src.data());
578 }
579 template <typename S>
580 openvdb::math::Vec3<S>
581 UTvdbConvert(const UT_Vector3T<S> &src)
582 {
583  return openvdb::math::Vec3<S>(src.data());
584 }
585 template <typename S>
586 openvdb::math::Vec2<S>
587 UTvdbConvert(const UT_Vector2T<S> &src)
588 {
589  return openvdb::math::Vec2<S>(src.data());
590 }
591 // @}
592 
593 
594 /// Bounding box conversion from openvdb to UT
595 inline UT_BoundingBoxD
596 UTvdbConvert(const openvdb::CoordBBox &bbox)
597 {
598  return UT_BoundingBoxD(UTvdbConvert(bbox.getStart().asVec3d()),
599  UTvdbConvert(bbox.getEnd().asVec3d()));
600 }
601 
602 /// Bounding box conversion from openvdb to UT
603 inline openvdb::math::CoordBBox
604 UTvdbConvert(const UT_BoundingBoxI &bbox)
605 {
606  return openvdb::math::CoordBBox(
607  openvdb::math::Coord(bbox.xmin(), bbox.ymin(), bbox.zmin()),
608  openvdb::math::Coord(bbox.xmax(), bbox.ymax(), bbox.zmax()));
609 }
610 
611 /// Utility method to construct a Transform that lines up with a
612 /// cell-centered Houdini volume with specified origin and voxel size.
613 inline openvdb::math::Transform::Ptr
614 UTvdbCreateTransform(const UT_Vector3 &orig, const UT_Vector3 &voxsize)
615 {
616  // Transforms only valid for square voxels.
617  UT_ASSERT(SYSalmostEqual(voxsize.minComponent(), voxsize.maxComponent()));
618  fpreal vs = voxsize.maxComponent();
619  openvdb::math::Transform::Ptr xform =
620  openvdb::math::Transform::createLinearTransform(vs);
621  // Ensure voxel centers line up.
622  xform->postTranslate(UTvdbConvert(orig) + vs / 2);
623  return xform;
624 }
625 
626 template <typename T>
627 inline openvdb::math::Vec4<T> SYSabs(const openvdb::math::Vec4<T> &v1)
628 { return openvdb::math::Vec4<T>( SYSabs(v1[0]),
629  SYSabs(v1[1]),
630  SYSabs(v1[2]),
631  SYSabs(v1[3])
632  );
633 }
634 template <typename T>
635 inline openvdb::math::Vec3<T> SYSabs(const openvdb::math::Vec3<T> &v1)
636 { return openvdb::math::Vec3<T>( SYSabs(v1[0]),
637  SYSabs(v1[1]),
638  SYSabs(v1[2])
639  );
640 }
641 template <typename T>
642 inline openvdb::math::Vec2<T> SYSabs(const openvdb::math::Vec2<T> &v1)
643 { return openvdb::math::Vec2<T>( SYSabs(v1[0]),
644  SYSabs(v1[1])
645  );
646 }
647 
648 template <typename T>
649 inline openvdb::math::Vec4<T> SYSmin(const openvdb::math::Vec4<T> &v1, const openvdb::math::Vec4<T> &v2)
650 { return openvdb::math::Vec4<T>( SYSmin(v1[0], v2[0]),
651  SYSmin(v1[1], v2[1]),
652  SYSmin(v1[2], v2[2]),
653  SYSmin(v1[3], v2[3])
654  );
655 }
656 template <typename T>
657 inline openvdb::math::Vec4<T> SYSmax(const openvdb::math::Vec4<T> &v1, const openvdb::math::Vec4<T> &v2)
658 { return openvdb::math::Vec4<T>( SYSmax(v1[0], v2[0]),
659  SYSmax(v1[1], v2[1]),
660  SYSmax(v1[2], v2[2]),
661  SYSmax(v1[3], v2[3])
662  );
663 }
664 template <typename T>
665 inline openvdb::math::Vec4<T> SYSmin(const openvdb::math::Vec4<T> &v1, const openvdb::math::Vec4<T> &v2, const openvdb::math::Vec4<T> &v3)
666 { return openvdb::math::Vec4<T>( SYSmin(v1[0], v2[0], v3[0]),
667  SYSmin(v1[1], v2[1], v3[1]),
668  SYSmin(v1[2], v2[2], v3[2]),
669  SYSmin(v1[3], v2[3], v3[3])
670  );
671 }
672 template <typename T>
673 inline openvdb::math::Vec4<T> SYSmax(const openvdb::math::Vec4<T> &v1, const openvdb::math::Vec4<T> &v2, const openvdb::math::Vec4<T> &v3)
674 { return openvdb::math::Vec4<T>( SYSmax(v1[0], v2[0], v3[0]),
675  SYSmax(v1[1], v2[1], v3[1]),
676  SYSmax(v1[2], v2[2], v3[2]),
677  SYSmax(v1[3], v2[3], v3[3])
678  );
679 }
680 template <typename T>
681 inline openvdb::math::Vec3<T> SYSmin(const openvdb::math::Vec3<T> &v1, const openvdb::math::Vec3<T> &v2)
682 { return openvdb::math::Vec3<T>( SYSmin(v1[0], v2[0]),
683  SYSmin(v1[1], v2[1]),
684  SYSmin(v1[2], v2[2])
685  );
686 }
687 template <typename T>
688 inline openvdb::math::Vec3<T> SYSmax(const openvdb::math::Vec3<T> &v1, const openvdb::math::Vec3<T> &v2)
689 { return openvdb::math::Vec3<T>( SYSmax(v1[0], v2[0]),
690  SYSmax(v1[1], v2[1]),
691  SYSmax(v1[2], v2[2])
692  );
693 }
694 template <typename T>
695 inline openvdb::math::Vec3<T> SYSmin(const openvdb::math::Vec3<T> &v1, const openvdb::math::Vec3<T> &v2, const openvdb::math::Vec3<T> &v3)
696 { return openvdb::math::Vec3<T>( SYSmin(v1[0], v2[0], v3[0]),
697  SYSmin(v1[1], v2[1], v3[1]),
698  SYSmin(v1[2], v2[2], v3[2])
699  );
700 }
701 template <typename T>
702 inline openvdb::math::Vec3<T> SYSmax(const openvdb::math::Vec3<T> &v1, const openvdb::math::Vec3<T> &v2, const openvdb::math::Vec3<T> &v3)
703 { return openvdb::math::Vec3<T>( SYSmax(v1[0], v2[0], v3[0]),
704  SYSmax(v1[1], v2[1], v3[1]),
705  SYSmax(v1[2], v2[2], v3[2])
706  );
707 }
708 template <typename T>
709 inline openvdb::math::Vec2<T> SYSmin(const openvdb::math::Vec2<T> &v1, const openvdb::math::Vec2<T> &v2)
710 { return openvdb::math::Vec2<T>( SYSmin(v1[0], v2[0]),
711  SYSmin(v1[1], v2[1])
712  );
713 }
714 template <typename T>
715 inline openvdb::math::Vec2<T> SYSmax(const openvdb::math::Vec2<T> &v1, const openvdb::math::Vec2<T> &v2)
716 { return openvdb::math::Vec2<T>( SYSmax(v1[0], v2[0]),
717  SYSmax(v1[1], v2[1])
718  );
719 }
720 template <typename T>
721 inline openvdb::math::Vec2<T> SYSmin(const openvdb::math::Vec2<T> &v1, const openvdb::math::Vec2<T> &v2, const openvdb::math::Vec2<T> &v3)
722 { return openvdb::math::Vec2<T>( SYSmin(v1[0], v2[0], v3[0]),
723  SYSmin(v1[1], v2[1], v3[1])
724  );
725 }
726 template <typename T>
727 inline openvdb::math::Vec2<T> SYSmax(const openvdb::math::Vec2<T> &v1, const openvdb::math::Vec2<T> &v2, const openvdb::math::Vec2<T> &v3)
728 { return openvdb::math::Vec2<T>( SYSmax(v1[0], v2[0], v3[0]),
729  SYSmax(v1[1], v2[1], v3[1])
730  );
731 }
732 
733 #endif // __HDK_UT_VDBUtils__
734 
735 #endif // SESI_OPENVDB || SESI_OPENVDB_PRIM
Vec3DGrid Vec3dGrid
Definition: openvdb.h:84
Space-partitioning acceleration structure for points. Partitions the points into voxels to accelerate...
Grid< Int32Tree > Int32Grid
Definition: openvdb.h:76
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
OutGridT XformOp & op
Definition: ValueTransformer.h:139
OPENVDB_IMPORT void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
bool isType() const
Return true if this grid is of the same type as the template parameter.
Definition: Grid.h:146
Grid< DoubleTree > DoubleGrid
Definition: openvdb.h:74
Definition: Exceptions.h:13
SharedPtr< const GridBase > ConstPtr
Definition: Grid.h:81
OutGridT const XformOp bool bool
Definition: ValueTransformer.h:609
Grid< PointIndexTree > PointIndexGrid
Point index grid.
Definition: PointIndexGrid.h:61
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:219
Grid< Vec3ITree > Vec3IGrid
Definition: openvdb.h:80
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Grid< Int64Tree > Int64Grid
Definition: openvdb.h:77
Vec3SGrid Vec3fGrid
Definition: openvdb.h:85
Grid< BoolTree > BoolGrid
Common grid types.
Definition: openvdb.h:73
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
Grid< PointDataTree > PointDataGrid
Point data grid.
Definition: PointDataGrid.h:195
Abstract base class for typed grids.
Definition: Grid.h:77