OpenVDB: PointDelete.h Source File

OpenVDB 4.0.2

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 {
48 OPENVDB_USE_VERSION_NAMESPACE
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>
93 struct DeleteGroupsOp
94 {
95 using LeafManagerT = tree::LeafManager<PointDataTreeT>;
96 using LeafRangeT = typename LeafManagerT::LeafRange;
97 using LeafNodeT = typename PointDataTreeT::LeafNodeType;
98 using ValueType = typename LeafNodeT::ValueType;
99
100 DeleteGroupsOp(const std::vector<std::string>& groupNames, bool invert)
101 : mGroupNames(groupNames)
102 , mInvert(invert) { }
103
104 void operator()(const LeafRangeT& range) const
105 {
106 // based on the invert parameter reverse the include and exclude arguments
107
108 std::unique_ptr<MultiGroupFilter> filter;
109 if (mInvert) {
110 filter.reset(new MultiGroupFilter(mGroupNames, std::vector<std::string>()));
111 }
112 else {
113 filter.reset(new MultiGroupFilter(std::vector<std::string>(), mGroupNames));
114 }
115
116 for (auto leaf = range.begin(); leaf != range.end(); ++leaf)
117 {
118 // early-exit if the leaf has no points
119 const size_t size = iterCount(leaf->beginIndexAll());
120 if (size == 0) continue;
121
122 const size_t newSize =
123 iterCount(leaf->template beginIndexAll<MultiGroupFilter>(*filter));
124
125 // if all points are being deleted, clear the leaf attributes
126 if (newSize == 0) {
127 leaf->clearAttributes();
128 continue;
129 }
130
131 const AttributeSet& existingAttributeSet = leaf->attributeSet();
132 AttributeSet* newAttributeSet = new AttributeSet(
133 existingAttributeSet, static_cast<Index>(newSize));
134 const size_t attributeSetSize = existingAttributeSet.size();
135
136 // cache the attribute arrays for efficiency
137
138 std::vector<AttributeArray*> newAttributeArrays;
139 std::vector<const AttributeArray*> existingAttributeArrays;
140
141 for (size_t i = 0; i < attributeSetSize; i++) {
142 newAttributeArrays.push_back(newAttributeSet->get(i));
143 existingAttributeArrays.push_back(existingAttributeSet.getConst(i));
144 }
145
146 typename ValueType::IntType attributeIndex = 0;
147 std::vector<ValueType> endOffsets;
148
149 endOffsets.reserve(LeafNodeT::NUM_VALUES);
150
151 // now construct new attribute arrays which exclude data from deleted points
152
153 for (auto voxel = leaf->cbeginValueAll(); voxel; ++voxel) {
154 for (auto iter = leaf->beginIndexVoxel(voxel.getCoord(), *filter); iter; ++iter) {
155 for (size_t i = 0; i < attributeSetSize; i++) {
156 newAttributeArrays[i]->set(static_cast<Index>(attributeIndex),
157 *(existingAttributeArrays[i]), *iter);
158 }
159 ++attributeIndex;
160 }
161 endOffsets.push_back(ValueType(attributeIndex));
162 }
163
164 leaf->replaceAttributeSet(newAttributeSet);
165 leaf->setOffsets(endOffsets);
166 }
167 }
168
169 private:
170 const std::vector<std::string>& mGroupNames;
171 bool mInvert;
172 }; // struct DeleteGroupsOp
173
174 } // namespace point_delete_internal
175
176
178
179
180 template <typename PointDataTreeT>
181 inline void deleteFromGroups(PointDataTreeT& pointTree, const std::vector<std::string>& groups,
182 bool invert)
183 {
184 const typename PointDataTreeT::LeafCIter leafIter = pointTree.cbeginLeaf();
185
186 if (!leafIter) return;
187
188 const openvdb::points::AttributeSet& attributeSet = leafIter->attributeSet();
189 const AttributeSet::Descriptor& descriptor = attributeSet.descriptor();
190 std::vector<std::string> availableGroups;
191
192 // determine which of the requested groups exist, and early exit
193 // if none are present in the tree
194
195 for (const auto& groupName : groups) {
196 if (descriptor.hasGroup(groupName)) {
197 availableGroups.push_back(groupName);
198 }
199 }
200
201 if (availableGroups.empty()) return;
202
203 tree::LeafManager<PointDataTreeT> leafManager(pointTree);
204 point_delete_internal::DeleteGroupsOp<PointDataTreeT> deleteOp(availableGroups, invert);
205 tbb::parallel_for(leafManager.leafRange(), deleteOp);
206
207 // drop the now-empty groups (unless invert = true)
208
209 if (!invert) {
210 dropGroups(pointTree, availableGroups);
211 }
212 }
213
214 template <typename PointDataTreeT>
215 inline void deleteFromGroup(PointDataTreeT& pointTree, const std::string& group, bool invert)
216 {
217 std::vector<std::string> groups(1, group);
218
219 deleteFromGroups(pointTree, groups, invert);
220 }
221
222
223 } // namespace points
224 } // namespace OPENVDB_VERSION_NAME
225 } // namespace openvdb
226
227 #endif // OPENVDB_POINTS_POINT_DELETE_HAS_BEEN_INCLUDED
228
229 // Copyright (c) 2012-2017 DreamWorks Animation LLC
230 // All rights reserved. This software is distributed under the
231 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v4_0_2::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::v4_0_2::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::v4_0_2::points::deleteFromGroups
void deleteFromGroups(PointDataTreeT &pointTree, const std::vector< std::string > &groups, bool invert)
Definition: PointDelete.h:181
i
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
openvdb::v4_0_2::tree::LeafManager::LeafRange
Definition: LeafManager.h:127
PointGroup.h
Point group 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_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp::ValueType
typename LeafNodeT::ValueType ValueType
Definition: PointDelete.h:98
openvdb
Definition: Exceptions.h:39
openvdb::v4_0_2::points::AttributeSet::size
size_t size() const
Return the number of attributes in this set.
Definition: AttributeSet.h:130
openvdb::v4_0_2::points::AttributeSet
Ordered collection of uniquely-named attribute arrays.
Definition: AttributeSet.h:62
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp::LeafNodeT
typename PointDataTreeT::LeafNodeType LeafNodeT
Definition: PointDelete.h:97
IndexIterator.h
Index Iterators.
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::deleteFromGroup
void deleteFromGroup(PointDataTreeT &pointTree, const std::string &group, bool invert)
Definition: PointDelete.h:215
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
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp
Definition: PointDelete.h:93
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp::operator()
void operator()(const LeafRangeT &range) const
Definition: PointDelete.h:104
PointDataGrid.h
Attribute-owned data structure for points. Point attributes are stored in leaf nodes and ordered by v...
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp::DeleteGroupsOp
DeleteGroupsOp(const std::vector< std::string > &groupNames, bool invert)
Definition: PointDelete.h:100
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
LeafManager.h
A LeafManager manages a linear array of pointers to a given tree&#39;s leaf nodes, as well as optional au...
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::PointDataTree
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:207
openvdb::v4_0_2::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:562
openvdb::v4_0_2::points::point_delete_internal::DeleteGroupsOp::LeafRangeT
typename LeafManagerT::LeafRange LeafRangeT
Definition: PointDelete.h:96