OpenVDB: openvdb/points/PointConversion.h Source File

OpenVDB 10.1.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: 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
15#include <openvdb/tools/PointIndexGrid.h>
16#include <openvdb/tools/PointsToMask.h>
17#include <openvdb/util/NullInterrupter.h>
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
30namespace openvdb {
31 OPENVDB_USE_VERSION_NAMESPACE
32namespace OPENVDB_VERSION_NAME {
33namespace points {
34
35////////////////////////////////////////
36
37
38/// @brief Point-partitioner compatible STL vector attribute wrapper for convenience
39template<typename ValueType>
40 class PointAttributeVector {
41public:
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
55private:
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
80template<
81 typename CompressionT,
82 typename PointDataGridT,
83 typename PositionArrayT,
84 typename PointIndexGridT>
85inline typename PointDataGridT::Ptr
86createPointDataGrid(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
102template <typename CompressionT, typename PointDataGridT, typename ValueT>
103inline typename PointDataGridT::Ptr
104createPointDataGrid(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.
120template <typename PointDataTreeT, typename PointIndexTreeT, typename PointArrayT>
121inline void
122populateAttribute( 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
139template <typename PositionAttribute, typename PointDataGridT, typename FilterT = NullFilter>
140inline void
141convertPointDataGridPosition( 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
159template <typename TypedAttribute, typename PointDataTreeT, typename FilterT = NullFilter>
160inline void
161convertPointDataGridAttribute( 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
182template <typename Group, typename PointDataTreeT, typename FilterT = NullFilter>
183inline void
184convertPointDataGridGroup( 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 {
195template <typename...> using void_t = void;
196template <typename T, typename = void>
197struct ValueTypeTraits { using Type = Vec3R; /* default type if T::value_type is not defined*/ };
198template <typename T>
199struct 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///
216template< typename PositionWrapper,
217 typename InterrupterT = openvdb::util::NullInterrupter,
218 typename VecT = typename internal::ValueTypeTraits<PositionWrapper>::Type>
219inline 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
230#include "impl/PointConversionImpl.h"
231
232#endif // OPENVDB_POINTS_POINT_CONVERSION_HAS_BEEN_INCLUDED
AttributeArrayString.h
Attribute array storage for string data using Descriptor Metadata.
AttributeSet.h
Set of Attribute Arrays which tracks metadata about each array.
value
ValueT value
Definition GridBuilder.h:1290
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::v10_1::Metadata
Base class for storing metadata information in a grid.
Definition Metadata.h:24
openvdb::v10_1::math::Mat4< double >
openvdb::v10_1::math::Transform
Definition Transform.h:40
openvdb::v10_1::math::Vec3< Real >
openvdb::v10_1::points::PointAttributeVector
Point-partitioner compatible STL vector attribute wrapper for convenience.
Definition PointConversion.h:40
openvdb::v10_1::points::PointAttributeVector::size
size_t size() const
Definition PointConversion.h:50
openvdb::v10_1::points::PointAttributeVector::get
void get(ValueType &value, size_t n, openvdb::Index m) const
Definition PointConversion.h:53
openvdb::v10_1::points::PointAttributeVector::get
void get(ValueType &value, size_t n) const
Definition PointConversion.h:52
openvdb::v10_1::points::PointAttributeVector::value_type
ValueType value_type
Definition PointConversion.h:43
openvdb::v10_1::points::PointAttributeVector::PointAttributeVector
PointAttributeVector(const std::vector< value_type > &data, const Index stride=1)
Definition PointConversion.h:45
openvdb::v10_1::points::PointAttributeVector::getPos
void getPos(size_t n, ValueType &xyz) const
Definition PointConversion.h:51
openvdb::v10_1::points::PointAttributeVector::PosType
ValueType PosType
Definition PointConversion.h:42
openvdb::v10_1::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::v10_1::Name
std::string Name
Definition Name.h:19
openvdb::v10_1::Index
Index32 Index
Definition Types.h:54
openvdb::v10_1::Index64
uint64_t Index64
Definition Types.h:53
openvdb
Definition Exceptions.h:13
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:212