OpenVDB  5.0.0
PointDelete.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_DELETE_HAS_BEEN_INCLUDED
38 #define OPENVDB_POINTS_POINT_DELETE_HAS_BEEN_INCLUDED
39 
40 #include "PointDataGrid.h"
41 #include "PointGroup.h"
42 #include "IndexIterator.h"
43 #include "IndexFilter.h"
44 
45 #include <openvdb/tree/LeafManager.h>
46 
47 namespace openvdb {
49 namespace OPENVDB_VERSION_NAME {
50 namespace points {
51 
52 
64 
65 template <typename PointDataTree>
66 inline void deleteFromGroups(PointDataTree& pointTree, const std::vector<std::string>& groups,
67  bool invert = false);
68 
80 
81 template <typename PointDataTree>
82 inline void deleteFromGroup(PointDataTree& pointTree, const std::string& group,
83  bool invert = false);
84 
85 
87 
88 
89 namespace point_delete_internal {
90 
91 
92 template <typename PointDataTreeT, typename FilterT>
94 {
97  using LeafNodeT = typename PointDataTreeT::LeafNodeType;
98  using ValueType = typename LeafNodeT::ValueType;
99 
100  DeleteByFilterOp(const FilterT& filter)
101  : mFilter(filter) { }
102 
103  void operator()(const LeafRangeT& range) const
104  {
105  for (auto leaf = range.begin(); leaf != range.end(); ++leaf)
106  {
107  // early-exit if the leaf has no points
108  const size_t size = iterCount(leaf->beginIndexAll());
109  if (size == 0) continue;
110 
111  const size_t newSize =
112  iterCount(leaf->template beginIndexAll<FilterT>(mFilter));
113 
114  // if all points are being deleted, clear the leaf attributes
115  if (newSize == 0) {
116  leaf->clearAttributes();
117  continue;
118  }
119 
120  const AttributeSet& existingAttributeSet = leaf->attributeSet();
121  AttributeSet* newAttributeSet = new AttributeSet(
122  existingAttributeSet, static_cast<Index>(newSize));
123  const size_t attributeSetSize = existingAttributeSet.size();
124 
125  // cache the attribute arrays for efficiency
126 
127  std::vector<AttributeArray*> newAttributeArrays;
128  std::vector<const AttributeArray*> existingAttributeArrays;
129 
130  for (size_t i = 0; i < attributeSetSize; i++) {
131  newAttributeArrays.push_back(newAttributeSet->get(i));
132  existingAttributeArrays.push_back(existingAttributeSet.getConst(i));
133  }
134 
135  typename ValueType::IntType attributeIndex = 0;
136  std::vector<ValueType> endOffsets;
137 
138  endOffsets.reserve(LeafNodeT::NUM_VALUES);
139 
140  // now construct new attribute arrays which exclude data from deleted points
141 
142  for (auto voxel = leaf->cbeginValueAll(); voxel; ++voxel) {
143  for (auto iter = leaf->beginIndexVoxel(voxel.getCoord(), mFilter); iter; ++iter) {
144  for (size_t i = 0; i < attributeSetSize; i++) {
145  newAttributeArrays[i]->set(static_cast<Index>(attributeIndex),
146  *(existingAttributeArrays[i]), *iter);
147  }
148  ++attributeIndex;
149  }
150  endOffsets.push_back(ValueType(attributeIndex));
151  }
152 
153  leaf->replaceAttributeSet(newAttributeSet);
154  leaf->setOffsets(endOffsets);
155  }
156  }
157 
158 private:
159  const FilterT& mFilter;
160 }; // struct DeleteByFilterOp
161 
162 } // namespace point_delete_internal
163 
164 
166 
167 
168 template <typename PointDataTreeT>
169 inline void deleteFromGroups(PointDataTreeT& pointTree, const std::vector<std::string>& groups,
170  bool invert)
171 {
172  const typename PointDataTreeT::LeafCIter leafIter = pointTree.cbeginLeaf();
173 
174  if (!leafIter) return;
175 
176  const openvdb::points::AttributeSet& attributeSet = leafIter->attributeSet();
177  const AttributeSet::Descriptor& descriptor = attributeSet.descriptor();
178  std::vector<std::string> availableGroups;
179 
180  // determine which of the requested groups exist, and early exit
181  // if none are present in the tree
182 
183  for (const auto& groupName : groups) {
184  if (descriptor.hasGroup(groupName)) {
185  availableGroups.push_back(groupName);
186  }
187  }
188 
189  if (availableGroups.empty()) return;
190 
191  std::vector<std::string> empty;
192  std::unique_ptr<MultiGroupFilter> filter;
193  if (invert) {
194  filter.reset(new MultiGroupFilter(groups, empty, leafIter->attributeSet()));
195  }
196  else {
197  filter.reset(new MultiGroupFilter(empty, groups, leafIter->attributeSet()));
198  }
199 
200  tree::LeafManager<PointDataTreeT> leafManager(pointTree);
202  tbb::parallel_for(leafManager.leafRange(), deleteOp);
203 
204  // drop the now-empty groups (unless invert = true)
205 
206  if (!invert) {
207  dropGroups(pointTree, availableGroups);
208  }
209 }
210 
211 template <typename PointDataTreeT>
212 inline void deleteFromGroup(PointDataTreeT& pointTree, const std::string& group, bool invert)
213 {
214  std::vector<std::string> groups(1, group);
215 
216  deleteFromGroups(pointTree, groups, invert);
217 }
218 
219 
220 } // namespace points
221 } // namespace OPENVDB_VERSION_NAME
222 } // namespace openvdb
223 
224 #endif // OPENVDB_POINTS_POINT_DELETE_HAS_BEEN_INCLUDED
225 
226 // Copyright (c) 2012-2017 DreamWorks Animation LLC
227 // All rights reserved. This software is distributed under the
228 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
typename PointDataTreeT::LeafNodeType LeafNodeT
Definition: PointDelete.h:97
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:386
void deleteFromGroups(PointDataTreeT &pointTree, const std::vector< std::string > &groups, bool invert)
Definition: PointDelete.h:169
typename LeafNodeT::ValueType ValueType
Definition: PointDelete.h:98
Definition: LeafManager.h:127
Point group manipulation in a VDB Point Grid.
Index filters primarily designed to be used with a FilterIndexIter.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
const AttributeArray * getConst(const std::string &name) const
Return a pointer to the attribute array whose name is name or a null pointer if no match is found...
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:313
Definition: Exceptions.h:39
Index Iterators.
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
Definition: IndexFilter.h:104
void deleteFromGroup(PointDataTreeT &pointTree, const std::string &group, bool invert)
Definition: PointDelete.h:212
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
void operator()(const LeafRangeT &range) const
Definition: PointDelete.h:103
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
void dropGroups(PointDataTree &tree, const std::vector< Name > &groups)
Drops existing groups from the VDB tree, the tree is compacted after dropping.
Definition: PointGroup.h:568
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
DeleteByFilterOp(const FilterT &filter)
Definition: PointDelete.h:100
typename LeafManagerT::LeafRange LeafRangeT
Definition: PointDelete.h:96
const AttributeArray * get(const std::string &name) const
Return a pointer to the attribute array whose name is name or a null pointer if no match is found...
tree::Tree< tree::RootNode< tree::InternalNode< tree::InternalNode< PointDataLeafNode< PointDataIndex32, 3 >, 4 >, 5 > >> PointDataTree
Point index tree configured to match the default VDB configurations.
Definition: PointDataGrid.h:216