OpenVDB: PointCount.h Source File

OpenVDB 4.0.2

PointCount.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30
36
37 #ifndef OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
39
40 #include <openvdb/openvdb.h>
41 #include "AttributeSet.h"
42 #include "PointDataGrid.h"
43 #include "PointAttribute.h"
44 #include "IndexFilter.h"
45 #include <tbb/parallel_reduce.h>
46
47
48 namespace openvdb {
49 OPENVDB_USE_VERSION_NAMESPACE
50 namespace OPENVDB_VERSION_NAME {
51 namespace points {
52
53
57 template <typename PointDataTreeT>
58 Index64 pointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
59
60
64 template <typename PointDataTreeT>
65 Index64 activePointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
66
67
71 template <typename PointDataTreeT>
72 Index64 inactivePointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
73
74
82 template <typename PointDataTreeT>
83 Index64 getPointOffsets(std::vector<Index64>& pointOffsets, const PointDataTreeT& tree,
84 const std::vector<Name>& includeGroups = std::vector<Name>(),
85 const std::vector<Name>& excludeGroups = std::vector<Name>(),
86 const bool inCoreOnly = false);
87
88
93 template <typename PointDataTreeT>
94 Index64 groupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
95
96
101 template <typename PointDataTreeT>
102 Index64 activeGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
103
104
109 template <typename PointDataTreeT>
110 Index64 inactiveGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
111
112
114
115
116 namespace point_count_internal {
117
118 template < typename PointDataTreeT,
119 typename ValueIterT,
120 typename FilterT>
121 struct PointCountOp
122 {
123 using LeafManagerT = typename tree::LeafManager<const PointDataTreeT>;
124
125 PointCountOp(const FilterT& filter,
126 const bool inCoreOnly = false)
127 : mFilter(filter)
128 , mInCoreOnly(inCoreOnly) { }
129
130 Index64 operator()(const typename LeafManagerT::LeafRange& range, Index64 size) const {
131
132 for (auto leaf = range.begin(); leaf; ++leaf) {
133 if (mInCoreOnly && leaf->buffer().isOutOfCore()) continue;
134 auto iter = leaf->template beginIndex<ValueIterT, FilterT>(mFilter);
135 size += iterCount(iter);
136 }
137
138 return size;
139 }
140
141 static Index64 join(Index64 size1, Index64 size2) {
142 return size1 + size2;
143 }
144
145 private:
146 const FilterT& mFilter;
147 const bool mInCoreOnly;
148 }; // struct PointCountOp
149
150
151 template <typename PointDataTreeT, typename FilterT, typename ValueIterT>
152 Index64 threadedFilterPointCount( const PointDataTreeT& tree,
153 const FilterT& filter,
154 const bool inCoreOnly = false)
155 {
156 using PointCountOp = point_count_internal::PointCountOp< PointDataTreeT, ValueIterT, FilterT>;
157
158 typename tree::LeafManager<const PointDataTreeT> leafManager(tree);
159 const PointCountOp pointCountOp(filter, inCoreOnly);
160 return tbb::parallel_reduce(leafManager.leafRange(), Index64(0), pointCountOp, PointCountOp::join);
161 }
162
163
164 template <typename PointDataTreeT, typename FilterT>
165 Index64 filterPointCount(const PointDataTreeT& tree,
166 const FilterT& filter,
167 const bool inCoreOnly = false)
168 {
169 using ValueIterT = typename PointDataTreeT::LeafNodeType::ValueAllCIter;
170 return threadedFilterPointCount< PointDataTreeT, FilterT, ValueIterT>(tree, filter, inCoreOnly);
171 }
172
173
174 template <typename PointDataTreeT, typename FilterT>
175 Index64 filterActivePointCount( const PointDataTreeT& tree,
176 const FilterT& filter,
177 const bool inCoreOnly = false)
178 {
179 using ValueIterT = typename PointDataTreeT::LeafNodeType::ValueOnCIter;
180 return threadedFilterPointCount< PointDataTreeT, FilterT, ValueIterT>(tree, filter, inCoreOnly);
181 }
182
183
184 template <typename PointDataTreeT, typename FilterT>
185 Index64 filterInactivePointCount( const PointDataTreeT& tree,
186 const FilterT& filter,
187 const bool inCoreOnly = false)
188 {
189 using ValueIterT = typename PointDataTreeT::LeafNodeType::ValueOffCIter;
190 return threadedFilterPointCount< PointDataTreeT, FilterT, ValueIterT>(tree, filter, inCoreOnly);
191 }
192
193
194 } // namespace point_count_internal
195
196
197 template <typename PointDataTreeT>
198 Index64 pointCount(const PointDataTreeT& tree, const bool inCoreOnly)
199 {
200 (void) inCoreOnly;
201 Index64 size = 0;
202 for (auto iter = tree.cbeginLeaf(); iter; ++iter) {
203 if (inCoreOnly && iter->buffer().isOutOfCore()) continue;
204 size += iter->pointCount();
205 }
206 return size;
207 }
208
209
210 template <typename PointDataTreeT>
211 Index64 activePointCount(const PointDataTreeT& tree, const bool inCoreOnly)
212 {
213 (void) inCoreOnly;
214 Index64 size = 0;
215 for (auto iter = tree.cbeginLeaf(); iter; ++iter) {
216 if (inCoreOnly && iter->buffer().isOutOfCore()) continue;
217 size += iter->onPointCount();
218 }
219 return size;
220 }
221
222
223 template <typename PointDataTreeT>
224 Index64 inactivePointCount(const PointDataTreeT& tree, const bool inCoreOnly)
225 {
226 (void) inCoreOnly;
227 Index64 size = 0;
228 for (auto iter = tree.cbeginLeaf(); iter; ++iter) {
229 if (inCoreOnly && iter->buffer().isOutOfCore()) continue;
230 size += iter->offPointCount();
231 }
232 return size;
233 }
234
235
236 template <typename PointDataTreeT>
237 Index64 groupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly)
238 {
239 GroupFilter groupFilter(name);
240 return point_count_internal::filterPointCount<PointDataTreeT, GroupFilter>(tree, groupFilter, inCoreOnly);
241 }
242
243
244 template <typename PointDataTreeT>
245 Index64 activeGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly)
246 {
247 GroupFilter groupFilter(name);
248 return point_count_internal::filterActivePointCount<PointDataTreeT, GroupFilter>(tree, groupFilter, inCoreOnly);
249 }
250
251
252 template <typename PointDataTreeT>
253 Index64 inactiveGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly)
254 {
255 GroupFilter groupFilter(name);
256 return point_count_internal::filterInactivePointCount<PointDataTreeT, GroupFilter>(tree, groupFilter, inCoreOnly);
257 }
258
259
260 template <typename PointDataTreeT>
261 Index64 getPointOffsets(std::vector<Index64>& pointOffsets, const PointDataTreeT& tree,
262 const std::vector<Name>& includeGroups, const std::vector<Name>& excludeGroups,
263 const bool inCoreOnly)
264 {
265 using LeafNode = typename PointDataTreeT::LeafNodeType;
266
267 const bool useGroup = includeGroups.size() > 0 || excludeGroups.size() > 0;
268
269 tree::LeafManager<const PointDataTreeT> leafManager(tree);
270 const size_t leafCount = leafManager.leafCount();
271
272 pointOffsets.reserve(leafCount);
273
274 Index64 pointOffset = 0;
275 for (size_t n = 0; n < leafCount; n++)
276 {
277 const LeafNode& leaf = leafManager.leaf(n);
278
279 // skip out-of-core leafs
280 if (inCoreOnly && leaf.buffer().isOutOfCore()) {
281 pointOffsets.push_back(pointOffset);
282 continue;
283 }
284
285 if (useGroup) {
286 auto iter = leaf.beginValueOn();
287 MultiGroupFilter filter(includeGroups, excludeGroups);
288 filter.reset(leaf);
289 IndexIter<typename LeafNode::ValueOnCIter, MultiGroupFilter> filterIndexIter(iter, filter);
290 pointOffset += iterCount(filterIndexIter);
291 }
292 else {
293 pointOffset += leaf.onPointCount();
294 }
295 pointOffsets.push_back(pointOffset);
296 }
297 return pointOffset;
298 }
299
300
302
303
304 } // namespace points
305 } // namespace OPENVDB_VERSION_NAME
306 } // namespace openvdb
307
308 #endif // OPENVDB_POINTS_POINT_COUNT_HAS_BEEN_INCLUDED
309
310 // Copyright (c) 2012-2017 DreamWorks Animation LLC
311 // All rights reserved. This software is distributed under the
312 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v4_0_2::tree::LeafManager::leafCount
size_t leafCount() const
Return the number of leaf nodes.
Definition: LeafManager.h:328
openvdb::v4_0_2::points::IndexIter
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
Definition: IndexIterator.h:144
openvdb::v4_0_2::points::point_count_internal::filterInactivePointCount
Index64 filterInactivePointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:185
openvdb.h
openvdb::v4_0_2::points::point_count_internal::PointCountOp::operator()
Index64 operator()(const typename LeafManagerT::LeafRange &range, Index64 size) const
Definition: PointCount.h:130
openvdb::v4_0_2::tree::LeafManager::leaf
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:359
openvdb::v4_0_2::points::GroupFilter
Index filtering on group membership.
Definition: AttributeGroup.h:157
openvdb::v4_0_2::points::activePointCount
Index64 activePointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total active points in the PointDataTree.
Definition: PointCount.h:211
openvdb::v4_0_2::points::activeGroupPointCount
Index64 activeGroupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=false)
Total active points in the group in the PointDataTree.
Definition: PointCount.h:245
openvdb::v4_0_2::points::point_count_internal::filterPointCount
Index64 filterPointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:165
PointAttribute.h
Point attribute manipulation in a VDB Point Grid.
openvdb::v4_0_2::points::MultiGroupFilter
Definition: IndexFilter.h:103
IndexFilter.h
Index filters primarily designed to be used with a FilterIndexIter.
openvdb::v4_0_2::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:313
openvdb::v4_0_2::Index64
uint64_t Index64
Definition: Types.h:56
openvdb::v4_0_2::Name
std::string Name
Definition: Name.h:44
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::points::getPointOffsets
Index64 getPointOffsets(std::vector< Index64 > &pointOffsets, const PointDataTreeT &tree, const std::vector< Name > &includeGroups=std::vector< Name >(), const std::vector< Name > &excludeGroups=std::vector< Name >(), const bool inCoreOnly=false)
Populate an array of cumulative point offsets per leaf node.
Definition: PointCount.h:261
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::points::point_count_internal::PointCountOp
Definition: PointCount.h:121
openvdb::v4_0_2::tree::LeafManager::leafRange
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:386
openvdb::v4_0_2::points::groupPointCount
Index64 groupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=false)
Total points in the group in the PointDataTree.
Definition: PointCount.h:237
openvdb::v4_0_2::tree::LeafManager
This class manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional auxil...
Definition: LeafManager.h:110
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
openvdb::v4_0_2::points::inactiveGroupPointCount
Index64 inactiveGroupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=false)
Total inactive points in the group in the PointDataTree.
Definition: PointCount.h:253
openvdb::v4_0_2::points::point_count_internal::PointCountOp::LeafManagerT
typename tree::LeafManager< const PointDataTreeT > LeafManagerT
Definition: PointCount.h:123
openvdb::v4_0_2::points::point_count_internal::threadedFilterPointCount
Index64 threadedFilterPointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:152
openvdb::v4_0_2::points::point_count_internal::PointCountOp::PointCountOp
PointCountOp(const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:125
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v4_0_2::points::inactivePointCount
Index64 inactivePointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total inactive points in the PointDataTree.
Definition: PointCount.h:224
openvdb::v4_0_2::points::MultiGroupFilter::reset
void reset(const LeafT &leaf)
Definition: IndexFilter.h:124
size
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
openvdb::v4_0_2::points::point_count_internal::filterActivePointCount
Index64 filterActivePointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:175
openvdb::v4_0_2::points::pointCount
Index64 pointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total points in the PointDataTree.
Definition: PointCount.h:198
AttributeSet.h
Set of Attribute Arrays which tracks metadata about each array.
openvdb::v4_0_2::points::point_count_internal::PointCountOp::join
static Index64 join(Index64 size1, Index64 size2)
Definition: PointCount.h:141