HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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;
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
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>
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,
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
This tool produces a grid where every voxel that contains a point is active. It employs thread-local ...
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glad.h:2676
math::Vec3< Real > Vec3R
Definition: Types.h:72
void
Definition: png.h:1083
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Point group manipulation in a VDB Point Grid.
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:239
Index filters primarily designed to be used with a FilterIndexIter.
uint64 value_type
Definition: GA_PrimCompat.h:29
PointDataGridT::Ptr createPointDataGrid(const PointIndexGridT &pointIndexGrid, const PositionArrayT &positions, const math::Transform &xform, const Metadata *positionDefaultValue)
Localises points with position into a PointDataGrid into two stages: allocation of the leaf attribute...
void populateAttribute(PointDataTreeT &tree, const PointIndexTreeT &pointIndexTree, const openvdb::Name &attributeName, const PointArrayT &data, const Index stride, const bool insertMetadata)
Stores point attribute data in an existing PointDataGrid attribute.
GLdouble n
Definition: glcorearb.h:2008
Attribute array storage for string data using Descriptor Metadata.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
GA_API const UT_StringHolder transform
Point attribute manipulation in a VDB Point Grid.
Set of Attribute Arrays which tracks metadata about each array.
Space-partitioning acceleration structure for points. Partitions the points into voxels to accelerate...
GLuint index
Definition: glcorearb.h:786
void convertPointDataGridAttribute(TypedAttribute &attribute, const PointDataTreeT &tree, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const unsigned arrayIndex, const Index stride, const FilterT &filter, const bool inCoreOnly)
Convert the attribute from a PointDataGrid.
Base class for storing metadata information in a grid.
Definition: Metadata.h:23
static const Mat4< double > & identity()
Predefined constant for identity matrix.
Definition: Mat4.h:117
Index64 pointOffsets(std::vector< Index64 > &pointOffsets, const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly, const bool threaded)
Populate an array of cumulative point offsets per leaf node.
OIIO_API bool attribute(string_view name, TypeDesc type, const void *val)
Point-partitioner compatible STL vector attribute wrapper for convenience.
Definition: core.h:1131
void convertPointDataGridGroup(Group &group, const PointDataTreeT &tree, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const AttributeSet::Descriptor::GroupIndex index, const FilterT &filter, const bool inCoreOnly)
Convert the group from a PointDataGrid.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:119
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
bool ValueType
Definition: NanoVDB.h:5729
void convertPointDataGridPosition(PositionAttribute &positionAttribute, const PointDataGridT &grid, const std::vector< Index64 > &pointOffsets, const Index64 startOffset, const FilterT &filter, const bool inCoreOnly)
Convert the position attribute from a Point Data Grid.
Definition: format.h:895
float computeVoxelSize(const PositionWrapper &positions, const uint32_t pointsPerVoxel, const math::Mat4d transform, const Index decimalPlaces, InterrupterT *const interrupter)
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297
PointAttributeVector(const std::vector< value_type > &data, const Index stride=1)