OpenVDB  11.0.0
PointConversion.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @author Dan Bailey, Nick Avramoussis
5 ///
6 /// @file points/PointConversion.h
7 ///
8 /// @brief Convert points and attributes to and from VDB Point Data grids.
9 
10 #ifndef OPENVDB_POINTS_POINT_CONVERSION_HAS_BEEN_INCLUDED
11 #define OPENVDB_POINTS_POINT_CONVERSION_HAS_BEEN_INCLUDED
12 
13 #include <openvdb/math/Transform.h>
14 
18 
19 #include "AttributeArrayString.h"
20 #include "AttributeSet.h"
21 #include "IndexFilter.h"
22 #include "PointAttribute.h"
23 #include "PointDataGrid.h"
24 #include "PointGroup.h"
25 
26 #include <tbb/parallel_reduce.h>
27 
28 #include <type_traits>
29 
30 namespace openvdb {
32 namespace OPENVDB_VERSION_NAME {
33 namespace points {
34 
35 ////////////////////////////////////////
36 
37 
38 /// @brief Point-partitioner compatible STL vector attribute wrapper for convenience
39 template<typename ValueType>
41 public:
42  using PosType = ValueType;
43  using value_type= ValueType;
44 
45  PointAttributeVector(const std::vector<value_type>& data,
46  const Index stride = 1)
47  : mData(data)
48  , mStride(stride) { }
49 
50  size_t size() const { return mData.size(); }
51  void getPos(size_t n, ValueType& xyz) const { xyz = mData[n]; }
52  void get(ValueType& value, size_t n) const { value = mData[n]; }
53  void get(ValueType& value, size_t n, openvdb::Index m) const { value = mData[n * mStride + m]; }
54 
55 private:
56  const std::vector<value_type>& mData;
57  const Index mStride;
58 }; // PointAttributeVector
59 
60 
61 ////////////////////////////////////////
62 
63 /// @brief Localises points with position into a @c PointDataGrid into two stages:
64 /// allocation of the leaf attribute data and population of the positions.
65 ///
66 /// @param pointIndexGrid a PointIndexGrid into the points.
67 /// @param positions list of world space point positions.
68 /// @param xform world to index space transform.
69 /// @param positionDefaultValue metadata default position value
70 ///
71 /// @note The position data must be supplied in a Point-Partitioner compatible
72 /// data structure. A convenience PointAttributeVector class is offered.
73 ///
74 /// @note The position data is populated separately to perform world space to
75 /// voxel space conversion and apply quantisation.
76 ///
77 /// @note A @c PointIndexGrid to the points must be supplied to perform this
78 /// operation. Typically this is built implicitly by the PointDataGrid constructor.
79 
80 template<
81  typename CompressionT,
82  typename PointDataGridT,
83  typename PositionArrayT,
84  typename PointIndexGridT>
85 inline typename PointDataGridT::Ptr
86 createPointDataGrid(const PointIndexGridT& pointIndexGrid,
87  const PositionArrayT& positions,
88  const math::Transform& xform,
89  const Metadata* positionDefaultValue = nullptr);
90 
91 
92 /// @brief Convenience method to create a @c PointDataGrid from a std::vector of
93 /// point positions.
94 ///
95 /// @param positions list of world space point positions.
96 /// @param xform world to index space transform.
97 /// @param positionDefaultValue metadata default position value
98 ///
99 /// @note This method implicitly wraps the std::vector for a Point-Partitioner compatible
100 /// data structure and creates the required @c PointIndexGrid to the points.
101 
102 template <typename CompressionT, typename PointDataGridT, typename ValueT>
103 inline typename PointDataGridT::Ptr
104 createPointDataGrid(const std::vector<ValueT>& positions,
105  const math::Transform& xform,
106  const Metadata* positionDefaultValue = nullptr);
107 
108 
109 /// @brief Stores point attribute data in an existing @c PointDataGrid attribute.
110 ///
111 /// @param tree the PointDataGrid to be populated.
112 /// @param pointIndexTree a PointIndexTree into the points.
113 /// @param attributeName the name of the VDB Points attribute to be populated.
114 /// @param data a wrapper to the attribute data.
115 /// @param stride the stride of the attribute
116 /// @param insertMetadata true if strings are to be automatically inserted as metadata.
117 ///
118 /// @note A @c PointIndexGrid to the points must be supplied to perform this
119 /// operation. This is required to ensure the same point index ordering.
120 template <typename PointDataTreeT, typename PointIndexTreeT, typename PointArrayT>
121 inline void
122 populateAttribute( PointDataTreeT& tree,
123  const PointIndexTreeT& pointIndexTree,
124  const openvdb::Name& attributeName,
125  const PointArrayT& data,
126  const Index stride = 1,
127  const bool insertMetadata = true);
128 
129 /// @brief Convert the position attribute from a Point Data Grid
130 ///
131 /// @param positionAttribute the position attribute to be populated.
132 /// @param grid the PointDataGrid to be converted.
133 /// @param pointOffsets a vector of cumulative point offsets for each leaf
134 /// @param startOffset a value to shift all the point offsets by
135 /// @param filter an index filter
136 /// @param inCoreOnly true if out-of-core leaf nodes are to be ignored
137 ///
138 
139 template <typename PositionAttribute, typename PointDataGridT, typename FilterT = NullFilter>
140 inline void
141 convertPointDataGridPosition( PositionAttribute& positionAttribute,
142  const PointDataGridT& grid,
143  const std::vector<Index64>& pointOffsets,
144  const Index64 startOffset,
145  const FilterT& filter = NullFilter(),
146  const bool inCoreOnly = false);
147 
148 
149 /// @brief Convert the attribute from a PointDataGrid
150 ///
151 /// @param attribute the attribute to be populated.
152 /// @param tree the PointDataTree to be converted.
153 /// @param pointOffsets a vector of cumulative point offsets for each leaf.
154 /// @param startOffset a value to shift all the point offsets by
155 /// @param arrayIndex the index in the Descriptor of the array to be converted.
156 /// @param stride the stride of the attribute
157 /// @param filter an index filter
158 /// @param inCoreOnly true if out-of-core leaf nodes are to be ignored
159 template <typename TypedAttribute, typename PointDataTreeT, typename FilterT = NullFilter>
160 inline void
161 convertPointDataGridAttribute( TypedAttribute& attribute,
162  const PointDataTreeT& tree,
163  const std::vector<Index64>& pointOffsets,
164  const Index64 startOffset,
165  const unsigned arrayIndex,
166  const Index stride = 1,
167  const FilterT& filter = NullFilter(),
168  const bool inCoreOnly = false);
169 
170 
171 /// @brief Convert the group from a PointDataGrid
172 ///
173 /// @param group the group to be populated.
174 /// @param tree the PointDataTree to be converted.
175 /// @param pointOffsets a vector of cumulative point offsets for each leaf
176 /// @param startOffset a value to shift all the point offsets by
177 /// @param index the group index to be converted.
178 /// @param filter an index filter
179 /// @param inCoreOnly true if out-of-core leaf nodes are to be ignored
180 ///
181 
182 template <typename Group, typename PointDataTreeT, typename FilterT = NullFilter>
183 inline void
184 convertPointDataGridGroup( Group& group,
185  const PointDataTreeT& tree,
186  const std::vector<Index64>& pointOffsets,
187  const Index64 startOffset,
188  const AttributeSet::Descriptor::GroupIndex index,
189  const FilterT& filter = NullFilter(),
190  const bool inCoreOnly = false);
191 
192 // for internal use only - this traits class extracts T::value_type if defined,
193 // otherwise falls back to using Vec3R
194 namespace internal {
195 template <typename...> using void_t = void;
196 template <typename T, typename = void>
197 struct ValueTypeTraits { using Type = Vec3R; /* default type if T::value_type is not defined*/ };
198 template <typename T>
199 struct ValueTypeTraits <T, void_t<typename T::value_type>> { using Type = typename T::value_type; };
200 } // namespace internal
201 
202 /// @ brief Given a container of world space positions and a target points per voxel,
203 /// compute a uniform voxel size that would best represent the storage of the points in a grid.
204 /// This voxel size is typically used for conversion of the points into a PointDataGrid.
205 ///
206 /// @param positions array of world space positions
207 /// @param pointsPerVoxel the target number of points per voxel, must be positive and non-zero
208 /// @param transform voxel size will be computed using this optional transform if provided
209 /// @param decimalPlaces for readability, truncate voxel size to this number of decimals
210 /// @param interrupter an optional interrupter
211 ///
212 /// @note VecT will be PositionWrapper::value_type or Vec3R (if there is no value_type defined)
213 ///
214 /// @note if none or one point provided in positions, the default voxel size of 0.1 will be returned
215 ///
216 template< typename PositionWrapper,
217  typename InterrupterT = openvdb::util::NullInterrupter,
218  typename VecT = typename internal::ValueTypeTraits<PositionWrapper>::Type>
219 inline float
220 computeVoxelSize( const PositionWrapper& positions,
221  const uint32_t pointsPerVoxel,
222  const math::Mat4d transform = math::Mat4d::identity(),
223  const Index decimalPlaces = 5,
224  InterrupterT* const interrupter = nullptr);
225 
226 } // namespace points
227 } // namespace OPENVDB_VERSION_NAME
228 } // namespace openvdb
229 
231 
232 #endif // OPENVDB_POINTS_POINT_CONVERSION_HAS_BEEN_INCLUDED
ValueType value_type
Definition: PointConversion.h:43
float computeVoxelSize(const PositionWrapper &positions, const uint32_t pointsPerVoxel, const math::Mat4d transform=math::Mat4d::identity(), const Index decimalPlaces=5, InterrupterT *const interrupter=nullptr)
Definition: PointConversionImpl.h:675
Space-partitioning acceleration structure for points. Partitions the points into voxels to accelerate...
void populateAttribute(PointDataTreeT &tree, const PointIndexTreeT &pointIndexTree, const openvdb::Name &attributeName, const PointArrayT &data, const Index stride=1, const bool insertMetadata=true)
Stores point attribute data in an existing PointDataGrid attribute.
Definition: PointConversionImpl.h:544
math::Vec3< Real > Vec3R
Definition: Types.h:72
void convertPointDataGridGroup(Group &group, const PointDataTreeT &tree, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const AttributeSet::Descriptor::GroupIndex index, const FilterT &filter=NullFilter(), const bool inCoreOnly=false)
Convert the group from a PointDataGrid.
Definition: PointConversionImpl.h:647
ValueType PosType
Definition: PointConversion.h:42
Point attribute manipulation in a VDB Point Grid.
Definition: PointConversion.h:197
Point group manipulation in a VDB Point Grid.
PointDataGridT::Ptr createPointDataGrid(const std::vector< ValueT > &positions, const math::Transform &xform, const Metadata *positionDefaultValue=nullptr)
Convenience method to create a PointDataGrid from a std::vector of point positions.
Definition: PointConversionImpl.h:526
Index filters primarily designed to be used with a FilterIndexIter.
void void_t
Definition: PointConversion.h:195
uint64_t Index64
Definition: Types.h:53
void convertPointDataGridPosition(PositionAttribute &positionAttribute, const PointDataGridT &grid, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const FilterT &filter=NullFilter(), const bool inCoreOnly=false)
Convert the position attribute from a Point Data Grid.
Definition: PointConversionImpl.h:581
Base class for storing metadata information in a grid.
Definition: Metadata.h:23
Index64 pointOffsets(std::vector< Index64 > &pointOffsets, const PointDataTreeT &tree, const FilterT &filter=NullFilter(), const bool inCoreOnly=false, const bool threaded=true)
Populate an array of cumulative point offsets per leaf node.
Definition: PointCountImpl.h:52
Definition: Exceptions.h:13
Definition: Transform.h:39
PointAttributeVector(const std::vector< value_type > &data, const Index stride=1)
Definition: PointConversion.h:45
This tool produces a grid where every voxel that contains a point is active. It employs thread-local ...
std::string Name
Definition: Name.h:19
size_t size() const
Definition: PointConversion.h:50
void convertPointDataGridAttribute(TypedAttribute &attribute, const PointDataTreeT &tree, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const unsigned arrayIndex, const Index stride=1, const FilterT &filter=NullFilter(), const bool inCoreOnly=false)
Convert the attribute from a PointDataGrid.
Definition: PointConversionImpl.h:615
Index32 Index
Definition: Types.h:54
void getPos(size_t n, ValueType &xyz) const
Definition: PointConversion.h:51
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Attribute array storage for string data using Descriptor Metadata.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:50
Set of Attribute Arrays which tracks metadata about each array.
Point-partitioner compatible STL vector attribute wrapper for convenience.
Definition: PointConversion.h:40
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212