/usr/include/openvdb/tools/LevelSetFracture.h is in libopenvdb-dev 3.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | ///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2015 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
//
/// @file tools/LevelSetFracture.h
///
/// @brief Divide volumes represented by level set grids into multiple,
/// disjoint pieces by intersecting them with one or more "cutter" volumes,
/// also represented by level sets.
#ifndef OPENVDB_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
#include <openvdb/Grid.h>
#include <openvdb/math/Quat.h>
#include <openvdb/tree/LeafManager.h>
#include <openvdb/tools/Prune.h>
#include <openvdb/tools/SignedFloodFill.h>
#include <openvdb/util/NullInterrupter.h>
#include "Composite.h" // for csgIntersection() and csgDifference()
#include "GridTransformer.h" // for resampleToMatch()
#include <limits>
#include <list>
#include <deque>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {
/// @brief Level set fracturing
template<class GridType, class InterruptType = util::NullInterrupter>
class LevelSetFracture
{
public:
typedef std::vector<Vec3s> Vec3sList;
typedef std::vector<math::Quats> QuatsList;
typedef std::list<typename GridType::Ptr> GridPtrList;
typedef typename GridPtrList::iterator GridPtrListIter;
/// @brief Default constructor
///
/// @param interrupter optional interrupter object
explicit LevelSetFracture(InterruptType* interrupter = NULL);
/// @brief Divide volumes represented by level set grids into multiple,
/// disjoint pieces by intersecting them with one or more "cutter" volumes,
/// also represented by level sets.
/// @details If desired, the process can be applied iteratively, so that
/// fragments created with one cutter are subdivided by other cutters.
///
/// @note The incoming @a grids and the @a cutter are required to have matching
/// transforms and narrow band widths!
///
/// @param grids list of grids to fracture. The residuals of the
/// fractured grids will remain in this list
/// @param cutter a level set grid to use as the cutter object
/// @param segment toggle to split disjoint fragments into their own grids
/// @param points optional list of world space points at which to instance the
/// cutter object (if null, use the cutter's current position only)
/// @param rotations optional list of custom rotations for each cutter instance
/// @param cutterOverlap toggle to allow consecutive cutter instances to fracture
/// previously generated fragments
void fracture(GridPtrList& grids, const GridType& cutter, bool segment = false,
const Vec3sList* points = NULL, const QuatsList* rotations = NULL,
bool cutterOverlap = true);
/// Return a list of new fragments, not including the residuals from the input grids.
GridPtrList& fragments() { return mFragments; }
/// Remove all elements from the fragment list.
void clear() { mFragments.clear(); }
private:
// disallow copy by assignment
void operator=(const LevelSetFracture&) {}
bool wasInterrupted(int percent = -1) const {
return mInterrupter && mInterrupter->wasInterrupted(percent);
}
bool isValidFragment(GridType&) const;
void segmentFragments(GridPtrList&) const;
void process(GridPtrList&, const GridType& cutter);
InterruptType* mInterrupter;
GridPtrList mFragments;
};
////////////////////////////////////////
// Internal utility objects and implementation details
namespace internal {
/// @brief Segmentation scheme, splits disjoint fragments into separate grids.
/// @note This is a temporary solution and it will be replaced soon.
template<typename GridType, typename InterruptType>
inline std::vector<typename GridType::Ptr>
segment(GridType& grid, InterruptType* interrupter = NULL)
{
typedef typename GridType::Ptr GridPtr;
typedef typename GridType::TreeType TreeType;
typedef typename TreeType::Ptr TreePtr;
typedef typename TreeType::ValueType ValueType;
std::vector<GridPtr> segments;
while (grid.activeVoxelCount() > 0) {
if (interrupter && interrupter->wasInterrupted()) break;
// Deep copy the grid's metadata (tree and transform are shared)
GridPtr segment(new GridType(grid, ShallowCopy()));
// Make the transform unique and insert an empty tree
segment->setTransform(grid.transform().copy());
TreePtr tree(new TreeType(grid.background()));
segment->setTree(tree);
std::deque<Coord> coordList;
coordList.push_back(grid.tree().beginLeaf()->beginValueOn().getCoord());
Coord ijk, n_ijk;
ValueType value;
typename tree::ValueAccessor<TreeType> sourceAcc(grid.tree());
typename tree::ValueAccessor<TreeType> targetAcc(segment->tree());
while (!coordList.empty()) {
if (interrupter && interrupter->wasInterrupted()) break;
ijk = coordList.back();
coordList.pop_back();
if (!sourceAcc.probeValue(ijk, value)) continue;
if (targetAcc.isValueOn(ijk)) continue;
targetAcc.setValue(ijk, value);
sourceAcc.setValueOff(ijk);
for (int n = 0; n < 6; n++) {
n_ijk = ijk + util::COORD_OFFSETS[n];
if (!targetAcc.isValueOn(n_ijk) && sourceAcc.isValueOn(n_ijk)) {
coordList.push_back(n_ijk);
}
}
}
tools::pruneInactive(grid.tree());
tools::signedFloodFill(segment->tree());
segments.push_back(segment);
}
return segments;
}
template<class TreeType>
class MinMaxVoxel
{
public:
typedef tree::LeafManager<TreeType> LeafArray;
typedef typename TreeType::ValueType ValueType;
// LeafArray = openvdb::tree::LeafManager<TreeType> leafs(myTree)
MinMaxVoxel(LeafArray&);
void runParallel();
void runSerial();
const ValueType& minVoxel() const { return mMin; }
const ValueType& maxVoxel() const { return mMax; }
inline MinMaxVoxel(const MinMaxVoxel<TreeType>&, tbb::split);
inline void operator()(const tbb::blocked_range<size_t>&);
inline void join(const MinMaxVoxel<TreeType>&);
private:
LeafArray& mLeafArray;
ValueType mMin, mMax;
};
template <class TreeType>
MinMaxVoxel<TreeType>::MinMaxVoxel(LeafArray& leafs)
: mLeafArray(leafs)
, mMin(std::numeric_limits<ValueType>::max())
, mMax(-mMin)
{
}
template <class TreeType>
inline
MinMaxVoxel<TreeType>::MinMaxVoxel(const MinMaxVoxel<TreeType>& rhs, tbb::split)
: mLeafArray(rhs.mLeafArray)
, mMin(std::numeric_limits<ValueType>::max())
, mMax(-mMin)
{
}
template <class TreeType>
void
MinMaxVoxel<TreeType>::runParallel()
{
tbb::parallel_reduce(mLeafArray.getRange(), *this);
}
template <class TreeType>
void
MinMaxVoxel<TreeType>::runSerial()
{
(*this)(mLeafArray.getRange());
}
template <class TreeType>
inline void
MinMaxVoxel<TreeType>::operator()(const tbb::blocked_range<size_t>& range)
{
typename TreeType::LeafNodeType::ValueOnCIter iter;
for (size_t n = range.begin(); n < range.end(); ++n) {
iter = mLeafArray.leaf(n).cbeginValueOn();
for (; iter; ++iter) {
const ValueType value = iter.getValue();
mMin = std::min(mMin, value);
mMax = std::max(mMax, value);
}
}
}
template <class TreeType>
inline void
MinMaxVoxel<TreeType>::join(const MinMaxVoxel<TreeType>& rhs)
{
mMin = std::min(mMin, rhs.mMin);
mMax = std::max(mMax, rhs.mMax);
}
} // namespace internal
////////////////////////////////////////
template<class GridType, class InterruptType>
LevelSetFracture<GridType, InterruptType>::LevelSetFracture(InterruptType* interrupter)
: mInterrupter(interrupter)
, mFragments()
{
}
template<class GridType, class InterruptType>
void
LevelSetFracture<GridType, InterruptType>::fracture(GridPtrList& grids, const GridType& cutter,
bool segmentation, const Vec3sList* points, const QuatsList* rotations, bool cutterOverlap)
{
// We can process all incoming grids with the same cutter instance,
// this optimization is enabled by the requirement of having matching
// transforms between all incoming grids and the cutter object.
if (points && points->size() != 0) {
math::Transform::Ptr originalCutterTransform = cutter.transform().copy();
GridType cutterGrid(cutter, ShallowCopy());
const bool hasInstanceRotations =
points && rotations && points->size() == rotations->size();
// for each instance point..
for (size_t p = 0, P = points->size(); p < P; ++p) {
int percent = int((float(p) / float(P)) * 100.0);
if (wasInterrupted(percent)) break;
GridType instCutterGrid;
instCutterGrid.setTransform(originalCutterTransform->copy());
math::Transform::Ptr xform = originalCutterTransform->copy();
if (hasInstanceRotations) {
const Vec3s& rot = (*rotations)[p].eulerAngles(math::XYZ_ROTATION);
xform->preRotate(rot[0], math::X_AXIS);
xform->preRotate(rot[1], math::Y_AXIS);
xform->preRotate(rot[2], math::Z_AXIS);
xform->postTranslate((*points)[p]);
} else {
xform->postTranslate((*points)[p]);
}
cutterGrid.setTransform(xform);
if (wasInterrupted()) break;
// Since there is no scaling, use the generic resampler instead of
// the more expensive level set rebuild tool.
if (mInterrupter != NULL) {
doResampleToMatch<BoxSampler>(cutterGrid, instCutterGrid, *mInterrupter);
} else {
util::NullInterrupter interrupter;
doResampleToMatch<BoxSampler>(cutterGrid, instCutterGrid, interrupter);
}
if (cutterOverlap && !mFragments.empty()) process(mFragments, instCutterGrid);
process(grids, instCutterGrid);
}
} else {
// use cutter in place
if (cutterOverlap && !mFragments.empty()) process(mFragments, cutter);
process(grids, cutter);
}
if (segmentation) {
segmentFragments(mFragments);
segmentFragments(grids);
}
}
template<class GridType, class InterruptType>
bool
LevelSetFracture<GridType, InterruptType>::isValidFragment(GridType& grid) const
{
typedef typename GridType::TreeType TreeType;
if (grid.activeVoxelCount() < 27) return false;
// Check if valid level-set
{
tree::LeafManager<TreeType> leafs(grid.tree());
internal::MinMaxVoxel<TreeType> minmax(leafs);
minmax.runParallel();
if ((minmax.minVoxel() < 0) == (minmax.maxVoxel() < 0)) return false;
}
return true;
}
template<class GridType, class InterruptType>
void
LevelSetFracture<GridType, InterruptType>::segmentFragments(GridPtrList& grids) const
{
GridPtrList newFragments;
for (GridPtrListIter it = grids.begin(); it != grids.end(); ++it) {
if (wasInterrupted()) break;
std::vector<typename GridType::Ptr> segments = internal::segment(*(*it), mInterrupter);
for (size_t n = 0, N = segments.size(); n < N; ++n) {
if (wasInterrupted()) break;
if (isValidFragment(*segments[n])) {
newFragments.push_back(segments[n]);
}
}
}
grids.swap(newFragments);
}
template<class GridType, class InterruptType>
void
LevelSetFracture<GridType, InterruptType>::process(
GridPtrList& grids, const GridType& cutter)
{
typedef typename GridType::Ptr GridPtr;
GridPtrList newFragments;
for (GridPtrListIter it = grids.begin(); it != grids.end(); ++it) {
if (wasInterrupted()) break;
GridPtr grid = *it;
// gen new fragment
GridPtr fragment = grid->deepCopy();
csgIntersection(*fragment, *cutter.deepCopy());
if (wasInterrupted()) break;
if (!isValidFragment(*fragment)) continue;
// update residual
GridPtr residual = grid->deepCopy();
csgDifference(*residual, *cutter.deepCopy());
if (wasInterrupted()) break;
if (!isValidFragment(*residual)) continue;
newFragments.push_back(fragment);
grid->tree().clear();
grid->tree().merge(residual->tree());
}
if (!newFragments.empty()) {
mFragments.splice(mFragments.end(), newFragments);
}
}
} // namespace tools
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_TOOLS_LEVELSETFRACTURE_HAS_BEEN_INCLUDED
// Copyright (c) 2012-2015 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|