HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PointCount.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
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 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @file points/PointCount.h
32 ///
33 /// @author Dan Bailey
34 ///
35 /// @brief Various point counting methods using a VDB Point Grid.
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 {
50 namespace OPENVDB_VERSION_NAME {
51 namespace points {
52 
53 
54 /// @brief Total points in the PointDataTree
55 /// @param tree PointDataTree.
56 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
57 template <typename PointDataTreeT>
58 Index64 pointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
59 
60 
61 /// @brief Total active points in the PointDataTree
62 /// @param tree PointDataTree.
63 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
64 template <typename PointDataTreeT>
65 Index64 activePointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
66 
67 
68 /// @brief Total inactive points in the PointDataTree
69 /// @param tree PointDataTree.
70 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
71 template <typename PointDataTreeT>
72 Index64 inactivePointCount(const PointDataTreeT& tree, const bool inCoreOnly = false);
73 
74 
75 /// @brief Populate an array of cumulative point offsets per leaf node.
76 /// @param pointOffsets array of offsets to be populated.
77 /// @param tree PointDataTree from which to populate the offsets.
78 /// @param includeGroups the group of names to include.
79 /// @param excludeGroups the group of names to exclude.
80 /// @param inCoreOnly if true, points in out-of-core leaf nodes are ignored
81 /// @note returns the final cumulative point offset.
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 
89 /// @brief Total points in the group in the PointDataTree
90 /// @param tree PointDataTree.
91 /// @param name group name.
92 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
93 template <typename PointDataTreeT>
94 Index64 groupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
95 
96 
97 /// @brief Total active points in the group in the PointDataTree
98 /// @param tree PointDataTree.
99 /// @param name group name.
100 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
101 template <typename PointDataTreeT>
102 Index64 activeGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
103 
104 
105 /// @brief Total inactive points in the group in the PointDataTree
106 /// @param tree PointDataTree.
107 /// @param name group name.
108 /// @param inCoreOnly if true, points in out-of-core leaf nodes are not counted
109 template <typename PointDataTreeT>
110 Index64 inactiveGroupPointCount(const PointDataTreeT& tree, const Name& name, const bool inCoreOnly = false);
111 
112 
113 ////////////////////////////////////////
114 
115 
116 namespace point_count_internal {
117 
118 template < typename PointDataTreeT,
119  typename ValueIterT,
120  typename FilterT>
122 {
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 {
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);
290  pointOffset += iterCount(filterIndexIter);
291  }
292  else {
293  pointOffset += leaf.onPointCount();
294  }
295  pointOffsets.push_back(pointOffset);
296  }
297  return pointOffset;
298 }
299 
300 
301 ////////////////////////////////////////
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/ )
Definition: ImfName.h:53
Index64 filterInactivePointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:185
GLenum GLint * range
Definition: glcorearb.h:1924
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:391
PointCountOp(const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:125
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
Index64 groupPointCount(const PointDataTreeT &tree, const Name &name, const bool inCoreOnly=false)
Total points in the group in the PointDataTree.
Definition: PointCount.h:237
Index64 filterPointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:165
typename tree::LeafManager< const PointDataTreeT > LeafManagerT
Definition: PointCount.h:123
Index filters primarily designed to be used with a FilterIndexIter.
Index64 pointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total points in the PointDataTree.
Definition: PointCount.h:198
GLsizeiptr size
Definition: glcorearb.h:663
size_t leafCount() const
Return the number of leaf nodes.
Definition: LeafManager.h:333
Index64 threadedFilterPointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:152
GLdouble n
Definition: glcorearb.h:2007
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Index64 inactivePointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total inactive points in the PointDataTree.
Definition: PointCount.h:224
Index64 operator()(const typename LeafManagerT::LeafRange &range, Index64 size) const
Definition: PointCount.h:130
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
This class manages a linear array of pointers to a given tree's leaf nodes, as well as optional auxil...
Definition: LeafManager.h:115
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
A forward iterator over array indices with filtering IteratorT can be either IndexIter or ValueIndexI...
GLuint const GLchar * name
Definition: glcorearb.h:785
Index64 filterActivePointCount(const PointDataTreeT &tree, const FilterT &filter, const bool inCoreOnly=false)
Definition: PointCount.h:175
Index filtering on group membership.
Point attribute manipulation in a VDB Point Grid.
Set of Attribute Arrays which tracks metadata about each array.
LeafType & leaf(size_t leafIdx) const
Return a pointer to the leaf node at index leafIdx in the array.
Definition: LeafManager.h:364
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
Index64 activePointCount(const PointDataTreeT &tree, const bool inCoreOnly=false)
Total active points in the PointDataTree.
Definition: PointCount.h:211
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
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1296