OpenVDB  12.0.0
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 
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 {
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>
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
87 createPointDataGrid(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
105 createPointDataGrid(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
123 populateAttribute( 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
142 convertPointDataGridPosition( 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
162 convertPointDataGridAttribute( 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
185 convertPointDataGridGroup( 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 
232 
233 #endif // OPENVDB_POINTS_POINT_CONVERSION_HAS_BEEN_INCLUDED
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
Space-partitioning acceleration structure for points. Partitions the points into voxels to accelerate...
uint64_t Index64
Definition: Types.h:53
void getPos(size_t n, ValueType &xyz) const
Definition: PointConversion.h:52
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
PointAttributeVector(const std::vector< value_type > &data, const Index stride=1)
Definition: PointConversion.h:46
size_t size() const
Definition: PointConversion.h:51
Index32 Index
Definition: Types.h:54
Point attribute manipulation in a VDB Point Grid.
Point group manipulation in a VDB Point Grid.
Base class for storing metadata information in a grid.
Definition: Metadata.h:24
Index filters primarily designed to be used with a FilterIndexIter.
ValueType value_type
Definition: PointConversion.h:44
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
Point-partitioner compatible STL vector attribute wrapper for convenience.
Definition: PointConversion.h:41
Definition: Exceptions.h:13
This tool produces a grid where every voxel that contains a point is active. It employs thread-local ...
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
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Definition: Transform.h:39
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
Attribute array storage for string data using Descriptor Metadata.
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
std::string Name
Definition: Name.h:19
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
void void_t
Definition: PointConversion.h:196
ValueType PosType
Definition: PointConversion.h:43
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
Set of Attribute Arrays which tracks metadata about each array.
math::Vec3< Real > Vec3R
Definition: Types.h:72
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218
A no-op filter that can be used when iterating over all indices.
Definition: IndexIterator.h:51
Definition: PointConversion.h:198