Skip to content

Commit ce3da91

Browse files
committed
Exposed TypeLists of the default set of OpenVDB types
Signed-off-by: Nick Avramoussis <[email protected]>
1 parent 8b727c6 commit ce3da91

File tree

4 files changed

+188
-90
lines changed

4 files changed

+188
-90
lines changed

openvdb/openvdb/TypeList.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ template<typename OpT> inline void TSForEachImpl(OpT) {}
351351
template<typename OpT, typename T, typename... Ts>
352352
inline void TSForEachImpl(OpT op) { op(T()); TSForEachImpl<OpT, Ts...>(op); }
353353

354+
template<template <typename> class OpT> inline void TSForEachImpl() {}
355+
template<template <typename> class OpT, typename T, typename... Ts>
356+
inline void TSForEachImpl() { OpT<T>()(); TSForEachImpl<OpT, Ts...>(); }
357+
354358
} // namespace internal
355359

356360
/// @endcond
@@ -499,6 +503,25 @@ struct TypeList
499503
template <size_t First, size_t Last>
500504
using RemoveByIndex = typename typelist_internal::TSRemoveIndicesImpl<Self, First, Last>::type;
501505

506+
/// @brief Invoke a templated class operator on each type in this list. Use
507+
/// this method if you only need access to the type for static methods.
508+
/// @code
509+
/// #include <typeinfo>
510+
///
511+
/// template <typename T>
512+
/// struct PintTypes() {
513+
/// inline void operator()() { std::cout << typeid(T).name() << std::endl; }
514+
/// };
515+
///
516+
/// using MyTypes = openvdb::TypeList<int, float, double>;
517+
/// MyTypes::foreach<PintTypes>(); // "i, f, d" (exact output is compiler-dependent)
518+
/// @endcode
519+
///
520+
/// @note OpT must be a templated class. It is created and invoked for each
521+
/// type in this list.
522+
template<template <typename> class OpT>
523+
static void foreach() { typelist_internal::TSForEachImpl<OpT, Ts...>(); }
524+
502525
/// @brief Invoke a templated, unary functor on a value of each type in this list.
503526
/// @details Example:
504527
/// @code

openvdb/openvdb/openvdb.cc

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
#include "openvdb.h"
55
#include "io/DelayedLoadMetadata.h"
6-
//#ifdef OPENVDB_ENABLE_POINTS
76
#include "points/PointDataGrid.h"
8-
//#endif
97
#include "tools/PointIndexGrid.h"
108
#include "util/logging.h"
119

@@ -54,6 +52,11 @@ std::mutex sInitMutex;
5452
std::atomic<bool> sIsInitialized{false};
5553
}
5654

55+
/// @todo Change registerX() methods to simply be register()...
56+
template <typename GridT> struct RegisterGrid { inline void operator()() { GridT::registerGrid(); } };
57+
template <typename MetaT> struct RegisterMeta { inline void operator()() { MetaT::registerType(); } };
58+
template <typename MapT> struct RegisterMap { inline void operator()() { MapT::registerMap(); } };
59+
5760
void
5861
initialize()
5962
{
@@ -65,65 +68,29 @@ initialize()
6568

6669
// Register metadata.
6770
Metadata::clearRegistry();
68-
BoolMetadata::registerType();
69-
DoubleMetadata::registerType();
70-
FloatMetadata::registerType();
71-
Int32Metadata::registerType();
72-
Int64Metadata::registerType();
73-
StringMetadata::registerType();
74-
Vec2IMetadata::registerType();
75-
Vec2SMetadata::registerType();
76-
Vec2DMetadata::registerType();
77-
Vec3IMetadata::registerType();
78-
Vec3SMetadata::registerType();
79-
Vec3DMetadata::registerType();
80-
Vec4IMetadata::registerType();
81-
Vec4SMetadata::registerType();
82-
Vec4DMetadata::registerType();
83-
Mat4SMetadata::registerType();
84-
Mat4DMetadata::registerType();
71+
NativeMetaTypes::foreach<RegisterMeta>();
8572

8673
// Register maps
8774
math::MapRegistry::clear();
88-
math::AffineMap::registerMap();
89-
math::UnitaryMap::registerMap();
90-
math::ScaleMap::registerMap();
91-
math::UniformScaleMap::registerMap();
92-
math::TranslationMap::registerMap();
93-
math::ScaleTranslateMap::registerMap();
94-
math::UniformScaleTranslateMap::registerMap();
95-
math::NonlinearFrustumMap::registerMap();
75+
NativeMapTypes::foreach<RegisterMap>();
9676

9777
// Register common grid types.
9878
GridBase::clearRegistry();
99-
BoolGrid::registerGrid();
100-
MaskGrid::registerGrid();
101-
FloatGrid::registerGrid();
102-
DoubleGrid::registerGrid();
103-
Int32Grid::registerGrid();
104-
Int64Grid::registerGrid();
79+
NativeGridTypes::foreach<RegisterGrid>();
80+
10581
// @note String grids types are deprecated but we still register them
10682
// as supported serializable types for backward compatibility. This
10783
// will likely be removed in a future major version
10884
OPENVDB_NO_DEPRECATION_WARNING_BEGIN
10985
StringGrid::registerGrid();
11086
OPENVDB_NO_DEPRECATION_WARNING_END
111-
Vec3IGrid::registerGrid();
112-
Vec3SGrid::registerGrid();
113-
Vec3DGrid::registerGrid();
11487

11588
// Register types associated with point index grids.
11689
Metadata::registerType(typeNameAsString<PointIndex32>(), Int32Metadata::createMetadata);
11790
Metadata::registerType(typeNameAsString<PointIndex64>(), Int64Metadata::createMetadata);
118-
tools::PointIndexGrid::registerGrid();
11991

12092
// Register types associated with point data grids.
121-
//#ifdef OPENVDB_ENABLE_POINTS
12293
points::internal::initialize();
123-
//#endif
124-
125-
// Register delay load metadata
126-
io::DelayedLoadMetadata::registerType();
12794

12895
#ifdef OPENVDB_USE_BLOSC
12996
blosc_init();
@@ -166,10 +133,7 @@ __pragma(warning(default:1711))
166133
Metadata::clearRegistry();
167134
GridBase::clearRegistry();
168135
math::MapRegistry::clear();
169-
170-
//#ifdef OPENVDB_ENABLE_POINTS
171136
points::internal::uninitialize();
172-
//#endif
173137

174138
#ifdef OPENVDB_USE_BLOSC
175139
// We don't want to destroy Blosc, because it might have been

openvdb/openvdb/openvdb.h

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,49 @@
66

77
#include "Platform.h"
88
#include "Types.h"
9+
#include "TypeList.h"
910
#include "Metadata.h"
1011
#include "math/Maps.h"
1112
#include "math/Transform.h"
1213
#include "Grid.h"
1314
#include "tree/Tree.h"
15+
#include "points/PointDataGrid.h"
1416
#include "io/File.h"
1517

1618

1719
namespace openvdb {
1820
OPENVDB_USE_VERSION_NAMESPACE
1921
namespace OPENVDB_VERSION_NAME {
2022

23+
/// @brief Global registration of native Grid, Transform, Metadata and Point
24+
/// attribute types. Also initializes blosc (if enabled).
25+
/// @details Calling this methods registers factory callbacks for the set of
26+
/// native grid, transform, metadata and point attribute types that OpenVDB
27+
/// supports by default. For most types, calling openvdb::initialize is only
28+
/// required for serialization support. However, openvdb::initialize must be
29+
/// called for PointDataGrid attribute usage as these callbacks are used in
30+
/// various tools.
31+
/// @note This method is thread safe - it can be concurrently called multiple
32+
/// times, early exiting if it has already been called so long as
33+
/// openvdb::uninitialize() has not been called.
34+
OPENVDB_API void initialize();
35+
36+
/// @brief Global deregistration of native Grid, Transform, Metadata and Point
37+
/// attribute types.
38+
/// @details Clears all registered factory callbacks. This includes everything
39+
/// registered by openvdb::initialize() but will also include any manually
40+
/// registered types.
41+
/// @note This method is thread safe - it can be concurrently called multiple
42+
/// times.
43+
/// @warning This method does *not* uninitialize blosc. This is to avoid
44+
/// changing program states should another library also be using blosc. If
45+
/// blosc is enabled, consider calling blosc_destroy() in your application.
46+
OPENVDB_API void uninitialize();
47+
48+
49+
// foward declare some default types
50+
namespace io { class DelayedLoadMetadata; }
51+
2152
/// Common tree types
2253
using BoolTree = tree::Tree4<bool, 5, 4, 3>::Type;
2354
using DoubleTree = tree::Tree4<double, 5, 4, 3>::Type;
@@ -54,11 +85,127 @@ using Vec3dGrid = Vec3DGrid;
5485
using Vec3fGrid = Vec3SGrid;
5586
using VectorGrid = Vec3fGrid;
5687

57-
/// Global registration of basic types
58-
OPENVDB_API void initialize();
88+
/// @name Lists of native Grid Types
89+
/// @{
90+
/// The floating point Grid types which OpenVDB will register by default.
91+
using NativeFloatGridTypes = TypeList<FloatGrid, DoubleGrid>;
92+
/// The integer Grid types which OpenVDB will register by default.
93+
using NativeIntegerGridTypes = TypeList<Int32Grid, Int64Grid>;
94+
/// The scalar Grid types which OpenVDB will register by default. This is a
95+
/// combination of native floating point and integer grid types. Note that
96+
/// this list does not include Bool or Mask Grids.
97+
using NativeScalarGridTypes = NativeFloatGridTypes::Append<NativeIntegerGridTypes>;
98+
/// The Vec3 Grid types which OpenVDB will register by default.
99+
using NativeVec3GridTypes = TypeList<Vec3IGrid, Vec3SGrid, Vec3DGrid>;
59100

60-
/// Global deregistration of basic types
61-
OPENVDB_API void uninitialize();
101+
/// The Grid types which OpenVDB will register by default.
102+
using NativeGridTypes =
103+
NativeScalarGridTypes::
104+
Append<NativeVec3GridTypes>::
105+
Append<tools::PointIndexGrid>::
106+
// #define unfortunately required for one of the tests that removes this alias
107+
#ifndef OPENVDB_DISABLE_POINT_DATA_TREE_ALIAS
108+
Append<points::PointDataGrid>::
109+
#endif
110+
Append<BoolGrid, MaskGrid>;
111+
/// @}
112+
113+
114+
/// @name Lists of native TypedAttributeArray Types (for PointDataGrids)
115+
/// @{
116+
/// The floating point attribute array types which OpenVDB will register by default.
117+
using NativeFloatAttributeTypes = TypeList<
118+
points::TypedAttributeArray<float>,
119+
points::TypedAttributeArray<double>,
120+
points::TypedAttributeArray<float, points::TruncateCodec>,
121+
points::TypedAttributeArray<float, points::FixedPointCodec<true, points::UnitRange>>,
122+
points::TypedAttributeArray<float, points::FixedPointCodec<false, points::UnitRange>>
123+
>;
124+
/// The integer attribute array types which OpenVDB will register by default.
125+
using NativeIntegerAttributeTypes = TypeList<
126+
points::TypedAttributeArray<int8_t>,
127+
points::TypedAttributeArray<int16_t>,
128+
points::TypedAttributeArray<int32_t>,
129+
points::TypedAttributeArray<int64_t>
130+
>;
131+
/// The scalar attribute array types which OpenVDB will register by default.
132+
/// This is a combination of native floating point and integer array types.
133+
/// Note that this list does not include bool arrays.
134+
using NativeScalarAttributeTypes =
135+
NativeFloatAttributeTypes::Append<NativeIntegerAttributeTypes>;
136+
/// The Vec3 attribute array types which OpenVDB will register by default.
137+
using NativeVec3AttributeTypes = TypeList<
138+
points::TypedAttributeArray<math::Vec3<int32_t>>,
139+
points::TypedAttributeArray<math::Vec3<float>>,
140+
points::TypedAttributeArray<math::Vec3<double>>,
141+
points::TypedAttributeArray<math::Vec3<float>, points::TruncateCodec>,
142+
points::TypedAttributeArray<math::Vec3<float>, points::FixedPointCodec<true, points::PositionRange>>,
143+
points::TypedAttributeArray<math::Vec3<float>, points::FixedPointCodec<false, points::PositionRange>>,
144+
points::TypedAttributeArray<math::Vec3<float>, points::FixedPointCodec<true, points::UnitRange>>,
145+
points::TypedAttributeArray<math::Vec3<float>, points::FixedPointCodec<false, points::UnitRange>>,
146+
points::TypedAttributeArray<math::Vec3<float>, points::UnitVecCodec>
147+
>;
148+
/// The Mat3 attribute array types which OpenVDB will register by default.
149+
using NativeMat3AttributeTypes = TypeList<
150+
points::TypedAttributeArray<math::Mat3<float>>,
151+
points::TypedAttributeArray<math::Mat3<double>>
152+
>;
153+
/// The Mat4 attribute array types which OpenVDB will register by default.
154+
using NativeMat4AttributeTypes = TypeList<
155+
points::TypedAttributeArray<math::Mat4<float>>,
156+
points::TypedAttributeArray<math::Mat4<double>>
157+
>;
158+
/// The Quat attribute array types which OpenVDB will register by default.
159+
using NativeQuatAttributeTypes = TypeList<
160+
points::TypedAttributeArray<math::Quat<float>>,
161+
points::TypedAttributeArray<math::Quat<double>>
162+
>;
163+
164+
/// The attribute array types which OpenVDB will register by default.
165+
using NativeAttributeTypes =
166+
NativeScalarAttributeTypes::
167+
Append<NativeVec3AttributeTypes>::
168+
Append<NativeMat3AttributeTypes>::
169+
Append<NativeMat4AttributeTypes>::
170+
Append<NativeQuatAttributeTypes>::
171+
Append<points::GroupAttributeArray>::
172+
Append<points::StringAttributeArray>::
173+
Append<points::TypedAttributeArray<bool>>;
174+
/// @}
175+
176+
177+
/// The Map types which OpenVDB will register by default.
178+
using NativeMapTypes = TypeList<
179+
math::AffineMap,
180+
math::UnitaryMap,
181+
math::ScaleMap,
182+
math::UniformScaleMap,
183+
math::TranslationMap,
184+
math::ScaleTranslateMap,
185+
math::UniformScaleTranslateMap,
186+
math::NonlinearFrustumMap>;
187+
188+
189+
/// The Metadata types which OpenVDB will register by default.
190+
using NativeMetaTypes = TypeList<
191+
BoolMetadata,
192+
DoubleMetadata,
193+
FloatMetadata,
194+
Int32Metadata,
195+
Int64Metadata,
196+
StringMetadata,
197+
Vec2IMetadata,
198+
Vec2SMetadata,
199+
Vec2DMetadata,
200+
Vec3IMetadata,
201+
Vec3SMetadata,
202+
Vec3DMetadata,
203+
Vec4IMetadata,
204+
Vec4SMetadata,
205+
Vec4DMetadata,
206+
Mat4SMetadata,
207+
Mat4DMetadata,
208+
io::DelayedLoadMetadata>;
62209

63210

64211
// Deprecated types

openvdb/openvdb/points/points.cc

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,25 @@
33

44
/// @file points/points.cc
55

6+
#include "openvdb.h"
67
#include "PointDataGrid.h"
78

89
namespace openvdb {
910
OPENVDB_USE_VERSION_NAMESPACE
1011
namespace OPENVDB_VERSION_NAME {
1112
namespace points {
1213

14+
template <typename ArrayT> struct RegisterArray { inline void operator()() { ArrayT::registerType(); } };
15+
1316
void
1417
internal::initialize()
1518
{
16-
// Register attribute arrays with no compression
17-
TypedAttributeArray<bool>::registerType();
18-
TypedAttributeArray<int8_t>::registerType();
19-
TypedAttributeArray<int16_t>::registerType();
20-
TypedAttributeArray<int32_t>::registerType();
21-
TypedAttributeArray<int64_t>::registerType();
22-
TypedAttributeArray<float>::registerType();
23-
TypedAttributeArray<double>::registerType();
24-
TypedAttributeArray<math::Vec3<int32_t>>::registerType();
25-
TypedAttributeArray<math::Vec3<float>>::registerType();
26-
TypedAttributeArray<math::Vec3<double>>::registerType();
27-
28-
// Register attribute arrays with group and string attribute
29-
GroupAttributeArray::registerType();
30-
StringAttributeArray::registerType();
31-
32-
// Register attribute arrays with matrix and quaternion attributes
33-
TypedAttributeArray<math::Mat3<float>>::registerType();
34-
TypedAttributeArray<math::Mat3<double>>::registerType();
35-
TypedAttributeArray<math::Mat4<float>>::registerType();
36-
TypedAttributeArray<math::Mat4<double>>::registerType();
37-
TypedAttributeArray<math::Quat<float>>::registerType();
38-
TypedAttributeArray<math::Quat<double>>::registerType();
39-
40-
// Register attribute arrays with truncate compression
41-
TypedAttributeArray<float, TruncateCodec>::registerType();
42-
TypedAttributeArray<math::Vec3<float>, TruncateCodec>::registerType();
43-
44-
// Register attribute arrays with fixed point compression
45-
TypedAttributeArray<float, FixedPointCodec<true, UnitRange>>::registerType();
46-
TypedAttributeArray<float, FixedPointCodec<false, UnitRange>>::registerType();
47-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<true>>::registerType();
48-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<false>>::registerType();
49-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<true, PositionRange>>::registerType();
50-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<false, PositionRange>>::registerType();
51-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<true, UnitRange>>::registerType();
52-
TypedAttributeArray<math::Vec3<float>, FixedPointCodec<false, UnitRange>>::registerType();
53-
54-
// Register attribute arrays with unit vector compression
55-
TypedAttributeArray<math::Vec3<float>, UnitVecCodec>::registerType();
19+
NativeAttributeTypes::foreach<RegisterArray>();
5620

5721
// Register types associated with point data grids.
5822
Metadata::registerType(typeNameAsString<PointDataIndex32>(), Int32Metadata::createMetadata);
5923
Metadata::registerType(typeNameAsString<PointDataIndex64>(), Int64Metadata::createMetadata);
60-
PointDataGrid::registerGrid();
24+
if (!PointDataGrid::isRegistered()) PointDataGrid::registerGrid();
6125
}
6226

6327

0 commit comments

Comments
 (0)