OpenVDB 13.0.0

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