OpenVDB: LevelSetFracture.h Source File

OpenVDB 5.1.0

LevelSetFracture.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_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
38 #define OPENVDB_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
39
40 #include <openvdb/Grid.h>
41 #include <openvdb/math/Quat.h>
42 #include <openvdb/util/NullInterrupter.h>
43
44 #include "Composite.h" // for csgIntersectionCopy() and csgDifferenceCopy()
45 #include "GridTransformer.h" // for resampleToMatch()
46 #include "LevelSetUtil.h" // for sdfSegmentation()
47
48 #include <limits>
49 #include <list>
50
51 #include <tbb/blocked_range.h>
52 #include <tbb/parallel_reduce.h>
53
54
55 namespace openvdb {
56 OPENVDB_USE_VERSION_NAMESPACE
57 namespace OPENVDB_VERSION_NAME {
58 namespace tools {
59
61 template<class GridType, class InterruptType = util::NullInterrupter>
62 class LevelSetFracture
63 {
64 public:
65 typedef std::vector<Vec3s> Vec3sList;
66 typedef std::vector<math::Quats> QuatsList;
67 typedef std::list<typename GridType::Ptr> GridPtrList;
68 typedef typename GridPtrList::iterator GridPtrListIter;
69
70
74 explicit LevelSetFracture(InterruptType* interrupter = NULL);
75
94 void fracture(GridPtrList& grids, const GridType& cutter, bool segment = false,
95 const Vec3sList* points = NULL, const QuatsList* rotations = NULL,
96 bool cutterOverlap = true);
97
99 GridPtrList& fragments() { return mFragments; }
100
102 void clear() { mFragments.clear(); }
103
104 private:
105 // disallow copy by assignment
106 void operator=(const LevelSetFracture&) {}
107
108 bool wasInterrupted(int percent = -1) const {
109 return mInterrupter && mInterrupter->wasInterrupted(percent);
110 }
111
112 bool isValidFragment(GridType&) const;
113 void segmentFragments(GridPtrList&) const;
114 void process(GridPtrList&, const GridType& cutter);
115
116 InterruptType* mInterrupter;
117 GridPtrList mFragments;
118 };
119
120
122
123
124 // Internal utility objects and implementation details
125
126 namespace level_set_fracture_internal {
127
128
129 template<typename LeafNodeType>
130 struct FindMinMaxVoxelValue {
131
132 typedef typename LeafNodeType::ValueType ValueType;
133
134 FindMinMaxVoxelValue(const std::vector<const LeafNodeType*>& nodes)
135 : minValue(std::numeric_limits<ValueType>::max())
136 , maxValue(-minValue)
137 , mNodes(nodes.empty() ? NULL : &nodes.front())
138 {
139 }
140
141 FindMinMaxVoxelValue(FindMinMaxVoxelValue& rhs, tbb::split)
142 : minValue(std::numeric_limits<ValueType>::max())
143 , maxValue(-minValue)
144 , mNodes(rhs.mNodes)
145 {
146 }
147
148 void operator()(const tbb::blocked_range<size_t>& range) {
149 for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
150 const ValueType* data = mNodes[n]->buffer().data();
151 for (Index i = 0; i < LeafNodeType::SIZE; ++i) {
152 minValue = std::min(minValue, data[i]);
153 maxValue = std::max(maxValue, data[i]);
154 }
155 }
156 }
157
158 void join(FindMinMaxVoxelValue& rhs) {
159 minValue = std::min(minValue, rhs.minValue);
160 maxValue = std::max(maxValue, rhs.maxValue);
161 }
162
163 ValueType minValue, maxValue;
164
165 LeafNodeType const * const * const mNodes;
166 }; // struct FindMinMaxVoxelValue
167
168
169 } // namespace level_set_fracture_internal
170
171
173
174
175 template<class GridType, class InterruptType>
176 LevelSetFracture<GridType, InterruptType>::LevelSetFracture(InterruptType* interrupter)
177 : mInterrupter(interrupter)
178 , mFragments()
179 {
180 }
181
182
183 template<class GridType, class InterruptType>
184 void
185 LevelSetFracture<GridType, InterruptType>::fracture(GridPtrList& grids, const GridType& cutter,
186 bool segmentation, const Vec3sList* points, const QuatsList* rotations, bool cutterOverlap)
187 {
188 // We can process all incoming grids with the same cutter instance,
189 // this optimization is enabled by the requirement of having matching
190 // transforms between all incoming grids and the cutter object.
191 if (points && points->size() != 0) {
192
193
194 math::Transform::Ptr originalCutterTransform = cutter.transform().copy();
195 GridType cutterGrid(*const_cast<GridType*>(&cutter), ShallowCopy());
196
197 const bool hasInstanceRotations =
198 points && rotations && points->size() == rotations->size();
199
200 // for each instance point..
201 for (size_t p = 0, P = points->size(); p < P; ++p) {
202 int percent = int((float(p) / float(P)) * 100.0);
203 if (wasInterrupted(percent)) break;
204
205 GridType instCutterGrid;
206 instCutterGrid.setTransform(originalCutterTransform->copy());
207 math::Transform::Ptr xform = originalCutterTransform->copy();
208
209 if (hasInstanceRotations) {
210 const Vec3s& rot = (*rotations)[p].eulerAngles(math::XYZ_ROTATION);
211 xform->preRotate(rot[0], math::X_AXIS);
212 xform->preRotate(rot[1], math::Y_AXIS);
213 xform->preRotate(rot[2], math::Z_AXIS);
214 xform->postTranslate((*points)[p]);
215 } else {
216 xform->postTranslate((*points)[p]);
217 }
218
219 cutterGrid.setTransform(xform);
220
221 // Since there is no scaling, use the generic resampler instead of
222 // the more expensive level set rebuild tool.
223 if (mInterrupter != NULL) {
224
225 if (hasInstanceRotations) {
226 doResampleToMatch<BoxSampler>(cutterGrid, instCutterGrid, *mInterrupter);
227 } else {
228 doResampleToMatch<PointSampler>(cutterGrid, instCutterGrid, *mInterrupter);
229 }
230 } else {
231 util::NullInterrupter interrupter;
232 if (hasInstanceRotations) {
233 doResampleToMatch<BoxSampler>(cutterGrid, instCutterGrid, interrupter);
234 } else {
235 doResampleToMatch<PointSampler>(cutterGrid, instCutterGrid, interrupter);
236 }
237 }
238
239 if (wasInterrupted(percent)) break;
240
241 if (cutterOverlap && !mFragments.empty()) process(mFragments, instCutterGrid);
242 process(grids, instCutterGrid);
243 }
244
245 } else {
246 // use cutter in place
247 if (cutterOverlap && !mFragments.empty()) process(mFragments, cutter);
248 process(grids, cutter);
249 }
250
251 if (segmentation) {
252 segmentFragments(mFragments);
253 segmentFragments(grids);
254 }
255 }
256
257
258 template<class GridType, class InterruptType>
259 bool
260 LevelSetFracture<GridType, InterruptType>::isValidFragment(GridType& grid) const
261 {
262 typedef typename GridType::TreeType::LeafNodeType LeafNodeType;
263
264 if (grid.tree().leafCount() < 9) {
265
266 std::vector<const LeafNodeType*> nodes;
267 grid.tree().getNodes(nodes);
268
269 Index64 activeVoxelCount = 0;
270
271 for (size_t n = 0, N = nodes.size(); n < N; ++n) {
272 activeVoxelCount += nodes[n]->onVoxelCount();
273 }
274
275 if (activeVoxelCount < 27) return false;
276
277 level_set_fracture_internal::FindMinMaxVoxelValue<LeafNodeType> op(nodes);
278 tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), op);
279
280 if ((op.minValue < 0) == (op.maxValue < 0)) return false;
281 }
282
283 return true;
284 }
285
286
287 template<class GridType, class InterruptType>
288 void
289 LevelSetFracture<GridType, InterruptType>::segmentFragments(GridPtrList& grids) const
290 {
291 GridPtrList newFragments;
292
293 for (GridPtrListIter it = grids.begin(); it != grids.end(); ++it) {
294
295 std::vector<typename GridType::Ptr> segments;
296 segmentSDF(*(*it), segments);
297
298 for (size_t n = 0, N = segments.size(); n < N; ++n) {
299 newFragments.push_back(segments[n]);
300 }
301 }
302
303 grids.swap(newFragments);
304 }
305
306
307 template<class GridType, class InterruptType>
308 void
309 LevelSetFracture<GridType, InterruptType>::process(
310 GridPtrList& grids, const GridType& cutter)
311 {
312 typedef typename GridType::Ptr GridPtr;
313 GridPtrList newFragments;
314
315 for (GridPtrListIter it = grids.begin(); it != grids.end(); ++it) {
316
317 if (wasInterrupted()) break;
318
319 GridPtr& grid = *it;
320
321 GridPtr fragment = csgIntersectionCopy(*grid, cutter);
322 if (!isValidFragment(*fragment)) continue;
323
324 GridPtr residual = csgDifferenceCopy(*grid, cutter);
325 if (!isValidFragment(*residual)) continue;
326
327 newFragments.push_back(fragment);
328
329 grid->tree().clear();
330 grid->tree().merge(residual->tree());
331 }
332
333 if (!newFragments.empty()) {
334 mFragments.splice(mFragments.end(), newFragments);
335 }
336 }
337
338 } // namespace tools
339 } // namespace OPENVDB_VERSION_NAME
340 } // namespace openvdb
341
342 #endif // OPENVDB_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
343
344 // Copyright (c) 2012-2018 DreamWorks Animation LLC
345 // All rights reserved. This software is distributed under the
346 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::operator()
void operator()(const tbb::blocked_range< size_t > &range)
Definition: LevelSetFracture.h:148
openvdb::v5_1::tools::LevelSetFracture::clear
void clear()
Remove all elements from the fragment list.
Definition: LevelSetFracture.h:102
NullInterrupter.h
Composite.h
Functions to efficiently perform various compositing operations on grids.
openvdb::v5_1::math::XYZ_ROTATION
Definition: Math.h:864
openvdb::v5_1::tools::csgDifferenceCopy
GridOrTreeT::Ptr csgDifferenceCopy(const GridOrTreeT &a, const GridOrTreeT &b)
Threaded CSG difference operation that produces a new grid or tree from immutable inputs...
Definition: Composite.h:1210
GridTransformer.h
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::minValue
ValueType minValue
Definition: LevelSetFracture.h:163
openvdb::v5_1::tools::LevelSetFracture::QuatsList
std::vector< math::Quats > QuatsList
Definition: LevelSetFracture.h:66
Quat.h
openvdb::v5_1::tools::LevelSetFracture::fracture
void fracture(GridPtrList &grids, const GridType &cutter, bool segment=false, const Vec3sList *points=NULL, const QuatsList *rotations=NULL, bool cutterOverlap=true)
Divide volumes represented by level set grids into multiple, disjoint pieces by intersecting them wit...
Definition: LevelSetFracture.h:185
openvdb::v5_1::tools::LevelSetFracture::GridPtrList
std::list< typename GridType::Ptr > GridPtrList
Definition: LevelSetFracture.h:67
openvdb::v5_1::tools::segmentSDF
void segmentSDF(const GridOrTreeType &volume, std::vector< typename GridOrTreeType::Ptr > &segments)
Separates disjoint SDF surfaces into distinct grids or trees.
Definition: LevelSetUtil.h:2566
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::tools::LevelSetFracture::GridPtrListIter
GridPtrList::iterator GridPtrListIter
Definition: LevelSetFracture.h:68
openvdb::v5_1::ShallowCopy
Tag dispatch class that distinguishes shallow copy constructors from deep copy constructors.
Definition: Types.h:512
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::mNodes
LeafNodeType const *const *const mNodes
Definition: LevelSetFracture.h:165
openvdb::v5_1::Index64
uint64_t Index64
Definition: Types.h:60
openvdb::v5_1::util::wasInterrupted
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
openvdb
Definition: Exceptions.h:40
openvdb::v5_1::math::Z_AXIS
Definition: Math.h:859
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::FindMinMaxVoxelValue
FindMinMaxVoxelValue(FindMinMaxVoxelValue &rhs, tbb::split)
Definition: LevelSetFracture.h:141
openvdb::v5_1::math::Y_AXIS
Definition: Math.h:858
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::join
void join(FindMinMaxVoxelValue &rhs)
Definition: LevelSetFracture.h:158
openvdb::v5_1::tools::LevelSetFracture::fragments
GridPtrList & fragments()
Return a list of new fragments, not including the residuals from the input grids. ...
Definition: LevelSetFracture.h:99
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::maxValue
ValueType maxValue
Definition: LevelSetFracture.h:163
openvdb::v5_1::math::Vec3s
Vec3< float > Vec3s
Definition: Vec3.h:678
openvdb::v5_1::tools::composite::min
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
openvdb::v5_1::tools::LevelSetFracture
Level set fracturing.
Definition: LevelSetFracture.h:62
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::FindMinMaxVoxelValue
FindMinMaxVoxelValue(const std::vector< const LeafNodeType *> &nodes)
Definition: LevelSetFracture.h:134
Grid.h
openvdb::v5_1::tools::LevelSetFracture::Vec3sList
std::vector< Vec3s > Vec3sList
Definition: LevelSetFracture.h:65
LevelSetUtil.h
Miscellaneous utility methods that operate primarily or exclusively on level set grids.
openvdb::v5_1::math::Transform::Ptr
SharedPtr< Transform > Ptr
Definition: Transform.h:69
openvdb::v5_1::math::X_AXIS
Definition: Math.h:857
openvdb::v5_1::Index
Index32 Index
Definition: Types.h:61
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::tools::csgIntersectionCopy
GridOrTreeT::Ptr csgIntersectionCopy(const GridOrTreeT &a, const GridOrTreeT &b)
Threaded CSG intersection operation that produces a new grid or tree from immutable inputs...
Definition: Composite.h:1196
openvdb::v5_1::tools::composite::max
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue
Definition: LevelSetFracture.h:130
openvdb::v5_1::tools::level_set_fracture_internal::FindMinMaxVoxelValue::ValueType
LeafNodeType::ValueType ValueType
Definition: LevelSetFracture.h:132
openvdb::v5_1::util::NullInterrupter
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52