OpenVDB: PointDelete.h Source File

OpenVDB 5.1.0

PointDelete.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 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/tools/Prune.h>
46 #include <openvdb/tree/LeafManager.h>
47
48 #include <memory>
49 #include <string>
50 #include <vector>
51
52
53 namespace openvdb {
54 OPENVDB_USE_VERSION_NAMESPACE
55 namespace OPENVDB_VERSION_NAME {
56 namespace points {
57
58
72
73 template <typename PointDataTreeT>
74 inline void deleteFromGroups(PointDataTreeT& pointTree,
75 const std::vector<std::string>& groups,
76 bool invert = false,
77 bool drop = true);
78
92
93 template <typename PointDataTreeT>
94 inline void deleteFromGroup(PointDataTreeT& pointTree,
95 const std::string& group,
96 bool invert = false,
97 bool drop = true);
98
99
101
102
103 namespace point_delete_internal {
104
105
106 template <typename PointDataTreeT, typename FilterT>
107 struct DeleteByFilterOp
108 {
109 using LeafManagerT = tree::LeafManager<PointDataTreeT>;
110 using LeafRangeT = typename LeafManagerT::LeafRange;
111 using LeafNodeT = typename PointDataTreeT::LeafNodeType;
112 using ValueType = typename LeafNodeT::ValueType;
113
114 DeleteByFilterOp(const FilterT& filter)
115 : mFilter(filter) { }
116
117 void operator()(const LeafRangeT& range) const
118 {
119 for (auto leaf = range.begin(); leaf != range.end(); ++leaf) {
120
121 const size_t newSize =
122 iterCount(leaf->template beginIndexAll<FilterT>(mFilter));
123
124 // if all points are being deleted, clear the leaf attributes
125 if (newSize == 0) {
126 leaf->clearAttributes();
127 continue;
128 }
129
130 // early exit if no points are being deleted
131
132 const size_t currentSize = leaf->getLastValue();
133 if (newSize == currentSize) continue;
134
135 const AttributeSet& existingAttributeSet = leaf->attributeSet();
136 AttributeSet* newAttributeSet = new AttributeSet(
137 existingAttributeSet, static_cast<Index>(newSize));
138 const size_t attributeSetSize = existingAttributeSet.size();
139
140 // cache the attribute arrays for efficiency
141
142 std::vector<AttributeArray*> newAttributeArrays;
143 std::vector<const AttributeArray*> existingAttributeArrays;
144
145 for (size_t i = 0; i < attributeSetSize; i++) {
146 AttributeArray* newArray = newAttributeSet->get(i);
147 const AttributeArray* existingArray = existingAttributeSet.getConst(i);
148
149 if (!newArray->hasConstantStride() || !existingArray->hasConstantStride()) {
150 OPENVDB_THROW(openvdb::NotImplementedError,
151 "Transfer of attribute values for dynamic arrays not currently supported.");
152 }
153
154 if (newArray->stride() != existingArray->stride()) {
155 OPENVDB_THROW(openvdb::LookupError,
156 "Cannot transfer attribute values with mis-matching strides.");
157 }
158
159 newAttributeArrays.push_back(newArray);
160 existingAttributeArrays.push_back(existingArray);
161 }
162
163 Index attributeIndex = 0;
164 std::vector<ValueType> endOffsets;
165
166 endOffsets.reserve(LeafNodeT::NUM_VALUES);
167
168 // now construct new attribute arrays which exclude data from deleted points
169
170 for (auto voxel = leaf->cbeginValueAll(); voxel; ++voxel) {
171 for (auto iter = leaf->beginIndexVoxel(voxel.getCoord(), mFilter);
172 iter; ++iter) {
173 for (size_t i = 0; i < attributeSetSize; i++) {
174 newAttributeArrays[i]->set(attributeIndex, *(existingAttributeArrays[i]),
175 *iter);
176 }
177 ++attributeIndex;
178 }
179 endOffsets.push_back(static_cast<ValueType>(attributeIndex));
180 }
181
182 leaf->replaceAttributeSet(newAttributeSet);
183 leaf->setOffsets(endOffsets);
184 }
185 }
186
187 private:
188 const FilterT& mFilter;
189 }; // struct DeleteByFilterOp
190
191 } // namespace point_delete_internal
192
193
195
196
197 template <typename PointDataTreeT>
198 inline void deleteFromGroups(PointDataTreeT& pointTree,
199 const std::vector<std::string>& groups,
200 bool invert,
201 bool drop)
202 {
203 const typename PointDataTreeT::LeafCIter leafIter = pointTree.cbeginLeaf();
204
205 if (!leafIter) return;
206
207 const openvdb::points::AttributeSet& attributeSet = leafIter->attributeSet();
208 const AttributeSet::Descriptor& descriptor = attributeSet.descriptor();
209 std::vector<std::string> availableGroups;
210
211 // determine which of the requested groups exist, and early exit
212 // if none are present in the tree
213
214 for (const auto& groupName : groups) {
215 if (descriptor.hasGroup(groupName)) {
216 availableGroups.push_back(groupName);
217 }
218 }
219
220 if (availableGroups.empty()) return;
221
222 std::vector<std::string> empty;
223 std::unique_ptr<MultiGroupFilter> filter;
224 if (invert) {
225 filter.reset(new MultiGroupFilter(groups, empty, leafIter->attributeSet()));
226 }
227 else {
228 filter.reset(new MultiGroupFilter(empty, groups, leafIter->attributeSet()));
229 }
230
231 tree::LeafManager<PointDataTreeT> leafManager(pointTree);
232 point_delete_internal::DeleteByFilterOp<PointDataTreeT, MultiGroupFilter> deleteOp(*filter);
233 tbb::parallel_for(leafManager.leafRange(), deleteOp);
234
235 // remove empty leaf nodes
236
237 tools::pruneInactive(pointTree);
238
239 // drop the now-empty groups if requested (unless invert = true)
240
241 if (drop && !invert) {
242 dropGroups(pointTree, availableGroups);
243 }
244 }
245
246 template <typename PointDataTreeT>
247 inline void deleteFromGroup(PointDataTreeT& pointTree,
248 const std::string& group,
249 bool invert,
250 bool drop)
251 {
252 std::vector<std::string> groups(1, group);
253
254 deleteFromGroups(pointTree, groups, invert, drop);
255 }
256
257
258 } // namespace points
259 } // namespace OPENVDB_VERSION_NAME
260 } // namespace openvdb
261
262 #endif // OPENVDB_POINTS_POINT_DELETE_HAS_BEEN_INCLUDED
263
264 // Copyright (c) 2012-2018 DreamWorks Animation LLC
265 // All rights reserved. This software is distributed under the
266 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_1::points::MultiGroupFilter
Definition: IndexFilter.h:104
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp::LeafRangeT
typename LeafManagerT::LeafRange LeafRangeT
Definition: PointDelete.h:110
openvdb::v5_1::points::AttributeArray
Base class for storing attribute data.
Definition: AttributeArray.h:118
openvdb::v5_1::points::AttributeArray::stride
virtual Index stride() const =0
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp::ValueType
typename LeafNodeT::ValueType ValueType
Definition: PointDelete.h:112
openvdb::v5_1::points::AttributeSet::size
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
openvdb::v5_1::NotImplementedError
Definition: Exceptions.h:88
Prune.h
Defined various multi-threaded utility functions for trees.
PointGroup.h
Point group manipulation in a VDB Point Grid.
openvdb::v5_1::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
IndexFilter.h
Index filters primarily designed to be used with a FilterIndexIter.
openvdb::v5_1::points::AttributeSet::getConst
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...
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::LookupError
Definition: Exceptions.h:87
openvdb::v5_1::points::iterCount
Index64 iterCount(const IterT &iter)
Count up the number of times the iterator can iterate.
Definition: IndexIterator.h:313
openvdb::v5_1::points::deleteFromGroups
void deleteFromGroups(PointDataTreeT &pointTree, const std::vector< std::string > &groups, bool invert=false, bool drop=true)
Delete points that are members of specific groups.
Definition: PointDelete.h:198
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::tree::LeafManager::LeafRange
Definition: LeafManager.h:127
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp::operator()
void operator()(const LeafRangeT &range) const
Definition: PointDelete.h:117
IndexIterator.h
Index Iterators.
openvdb::v5_1::points::AttributeSet::get
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...
openvdb::v5_1::tools::pruneInactive
void pruneInactive(TreeT &tree, bool threaded=true, size_t grainSize=1)
Reduce the memory footprint of a tree by replacing with background tiles any nodes whose values are a...
Definition: Prune.h:381
openvdb::v5_1::points::deleteFromGroup
void deleteFromGroup(PointDataTreeT &pointTree, const std::string &group, bool invert=false, bool drop=true)
Delete points that are members of a group.
Definition: PointDelete.h:247
openvdb::v5_1::points::AttributeArray::hasConstantStride
bool hasConstantStride() const
Return true if this attribute has a constant stride.
Definition: AttributeArray.h:236
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
openvdb::v5_1::tree::LeafManager::leafRange
LeafRange leafRange(size_t grainsize=1) const
Return a TBB-compatible LeafRange.
Definition: LeafManager.h:386
openvdb::v5_1::Index
Index32 Index
Definition: Types.h:61
openvdb::v5_1::points::AttributeSet
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
openvdb::v5_1::points::dropGroups
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:578
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp
Definition: PointDelete.h:107
LeafManager.h
A LeafManager manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional au...
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp::DeleteByFilterOp
DeleteByFilterOp(const FilterT &filter)
Definition: PointDelete.h:114
openvdb::v5_1::points::point_delete_internal::DeleteByFilterOp::LeafNodeT
typename PointDataTreeT::LeafNodeType LeafNodeT
Definition: PointDelete.h:111