7 #ifndef OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED
8 #define OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED
19 #include <tbb/blocked_range.h>
20 #include <tbb/parallel_reduce.h>
52 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
54 resampleToMatch(
const GridType& inGrid, GridType& outGrid, Interrupter& interrupter);
77 template<
typename Sampler,
typename Gr
idType>
90 template<
typename Sampler,
typename TreeT>
94 using ValueT =
typename TreeT::ValueType;
100 mBBox(b.
min().asVec3d(), b.
max().asVec3d()), mVal(tileVal), mActive(on), mEmpty(false)
102 mBBox.expand(-this->radius());
103 mEmpty = mBBox.empty();
108 if (!mEmpty && mBBox.isInside(inCoord)) { result = mVal;
return mActive; }
109 return Sampler::sample(inTree, inCoord, result);
121 template<
typename TreeT>
129 template<
typename TreeT>
182 template<
typename InterrupterType>
void setInterrupter(InterrupterType&);
184 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
185 void transformGrid(
const Transformer&,
186 const GridT& inGrid, GridT& outGrid)
const;
189 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
190 void applyTransform(
const Transformer&,
const GridT& inGrid, GridT& outGrid)
const;
192 bool interrupt()
const {
return mInterrupt && mInterrupt(); }
195 template<
typename Sampler,
typename InTreeT,
typename OutTreeT,
typename Transformer>
196 static void transformBBox(
const Transformer&,
const CoordBBox& inBBox,
197 const InTreeT& inTree, OutTreeT& outTree,
const InterruptFunc&,
200 template<
typename Sampler,
typename TreeT,
typename Transformer>
201 class RangeProcessor;
203 bool mThreaded, mTransformTiles;
204 InterruptFunc mInterrupt;
239 const Vec3R& translate,
240 const std::string& xformOrder =
"tsr",
241 const std::string& rotationOrder =
"zyx");
249 template<
class Sampler,
class Gr
idT>
250 void transformGrid(
const GridT& inGrid, GridT& outGrid)
const;
253 struct MatrixTransform;
257 const std::string& xformOrder,
const std::string& rotOrder);
261 Mat4R mTransform, mPreScaleTransform, mPostScaleTransform;
268 namespace local_util {
294 const bool hasUniformScale = unsignedScale.
eq(
math::Vec3<T>(unsignedScale[0]));
296 bool hasRotation =
false;
297 bool validDecomposition =
false;
303 for (
size_t n = 0; n < 8; ++n) {
305 n & 0x1 ? -unsignedScale.
x() : unsignedScale.
x(),
306 n & 0x2 ? -unsignedScale.
y() : unsignedScale.
y(),
307 n & 0x4 ? -unsignedScale.
z() : unsignedScale.
z());
310 const math::Mat3<T> mat = xform * math::scale<math::Mat3<T> >(signedScale).inverse();
311 if (mat.
det() < T(0.0))
continue;
316 math::rotation<math::Mat3<T> >(
math::Vec3<T>(0, 0, 1), tmpAngle.
z()) *
321 if (xform.
eq(rebuild)) {
323 const T maxAngle =
std::max(std::abs(tmpAngle[0]),
324 std::max(std::abs(tmpAngle[1]), std::abs(tmpAngle[2])));
326 if (!(minAngle < maxAngle)) {
333 validDecomposition =
true;
335 if (hasUniformScale || !hasRotation) {
343 if (!validDecomposition) {
347 if (hasRotation && !hasUniformScale) {
393 mIsAffine(mAXform.isLinear() && mBXform.isLinear()),
394 mIsIdentity(mIsAffine && mAXform == mBXform)
403 return mBXform.worldToIndex(mAXform.indexToWorld(pos));
408 return mAXform.worldToIndex(mBXform.indexToWorld(pos));
416 const bool mIsAffine;
417 const bool mIsIdentity;
427 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
431 ABTransform xform(inGrid.transform(), outGrid.transform());
433 if (Sampler::consistent() && xform.
isIdentity()) {
436 outGrid.setTree(inGrid.tree().copy());
442 ( xform.
getB().
baseMap()->getAffineMap()->getMat4().inverse() );
460 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
467 if (inGrid.constTransform() == outGrid.constTransform()) {
470 outGrid.setTree(inGrid.tree().copy());
476 using ValueT =
typename GridType::ValueType;
480 const ValueT halfWidth = outIsLevelSet
481 ? ValueT(outGrid.background() * (1.0 / outGrid.voxelSize()[0]))
482 : ValueT(inGrid.background() * (1.0 / inGrid.voxelSize()[0]));
485 typename GridType::Ptr tempGrid;
487 tempGrid = doLevelSetRebuild(inGrid, zeroVal<ValueT>(),
488 halfWidth, halfWidth,
489 &outGrid.constTransform(), &interrupter);
497 outGrid.setTree(tempGrid->treePtr());
503 doResampleToMatch<Sampler>(inGrid, outGrid, interrupter);
507 template<
typename Sampler,
typename Gr
idType>
512 resampleToMatch<Sampler>(inGrid, outGrid, interrupter);
520 GridTransformer::GridTransformer(
const Mat4R& xform):
524 mPreScaleTransform(
Mat4R::identity()),
525 mPostScaleTransform(
Mat4R::identity())
531 init(mPivot,
scale, rotate, translate,
"srt",
"zyx");
540 const std::string& xformOrder,
const std::string& rotOrder):
543 mPreScaleTransform(
Mat4R::identity()),
544 mPostScaleTransform(
Mat4R::identity())
546 init(
pivot,
scale, rotate, translate, xformOrder, rotOrder);
554 GridTransformer::init(
557 const std::string& xformOrder,
const std::string& rotOrder)
559 if (xformOrder.size() != 3) {
562 if (rotOrder.size() != 3) {
572 for (
int i = 0; i < 3; ++i) {
573 double s = std::fabs(
scale(i));
575 mMipLevels(i) = int(std::floor(-std::log(s)/std::log(2.0)));
576 scaleRemainder(i) =
scale(i) * (1 << mMipLevels(i));
585 mTransform = mPreScaleTransform = mPostScaleTransform =
Mat4R::identity();
586 Mat4R* remainder = &mPostScaleTransform;
587 int rpos, spos, tpos;
588 rpos = spos = tpos = 3;
589 for (
int ix = 2; ix >= 0; --ix) {
590 switch (xformOrder[ix]) {
595 remainder->preTranslate(
pivot);
597 int xpos, ypos, zpos;
598 xpos = ypos = zpos = 3;
599 for (
int ir = 2; ir >= 0; --ir) {
600 switch (rotOrder[ir]) {
620 if (xpos > 2 || ypos > 2 || zpos > 2) {
621 OPENVDB_THROW(ValueError,
"invalid rotation order (" + rotOrder +
")");
625 remainder->preTranslate(-
pivot);
634 remainder->preTranslate(
pivot);
635 remainder->preScale(scaleRemainder);
636 remainder->preTranslate(-
pivot);
637 remainder = &mPreScaleTransform;
643 remainder->preTranslate(translate);
649 if (tpos > 2 || rpos > 2 || spos > 2) {
650 OPENVDB_THROW(ValueError,
"invalid transform order (" + xformOrder +
")");
658 template<
typename InterrupterType>
667 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
670 const GridT& inGrid, GridT& outGrid)
const
673 applyTransform<Sampler>(xform, inGrid, outGrid);
677 template<
class Sampler,
class Gr
idT>
686 applyTransform<Sampler>(xform, inGrid, outGrid);
689 bool firstPass =
true;
690 const typename GridT::ValueType background = inGrid.background();
691 typename GridT::Ptr tempGrid = GridT::create(background);
698 applyTransform<Sampler>(xform, inGrid, *tempGrid);
703 Vec3i count = mMipLevels;
704 while (count != Vec3i::zero()) {
708 count.x() ? .5 : 1, count.y() ? .5 : 1, count.z() ? .5 : 1));
715 applyTransform<Sampler>(xform, inGrid, *tempGrid);
719 typename GridT::Ptr destGrid = GridT::create(background);
720 applyTransform<Sampler>(xform, *tempGrid, *destGrid);
721 tempGrid.swap(destGrid);
730 applyTransform<Sampler>(xform, *tempGrid, outGrid);
732 outGrid.setTree(tempGrid->treePtr());
741 template<
class Sampler,
class TreeT,
typename Transformer>
742 class GridResampler::RangeProcessor
745 using LeafIterT =
typename TreeT::LeafCIter;
746 using TileIterT =
typename TreeT::ValueAllCIter;
752 RangeProcessor(
const Transformer& xform,
const CoordBBox& b,
const TreeT& inT, TreeT& outT):
753 mIsRoot(true), mXform(xform), mBBox(b),
754 mInTree(inT), mOutTree(&outT), mInAcc(mInTree), mOutAcc(*mOutTree)
757 RangeProcessor(
const Transformer& xform,
const CoordBBox& b,
const TreeT& inTree):
758 mIsRoot(false), mXform(xform), mBBox(b),
759 mInTree(inTree), mOutTree(new TreeT(inTree.background())),
760 mInAcc(mInTree), mOutAcc(*mOutTree)
763 ~RangeProcessor() {
if (!mIsRoot)
delete mOutTree; }
766 RangeProcessor(RangeProcessor& other, tbb::split):
768 mXform(other.mXform),
770 mInTree(other.mInTree),
771 mOutTree(new TreeT(mInTree.background())),
774 mInterrupt(other.mInterrupt)
777 void setInterrupt(
const InterruptFunc& f) { mInterrupt = f; }
780 void operator()(LeafRange& r)
784 LeafIterT i = r.iterator();
785 CoordBBox bbox(i->origin(), i->origin() + Coord(i->dim()));
786 if (!mBBox.empty()) {
793 transformBBox<Sampler>(mXform, bbox, mInAcc, mOutAcc, mInterrupt);
799 void operator()(TileRange& r)
804 TileIterT i = r.iterator();
806 if (!i.isTileValue())
continue;
810 i.getBoundingBox(bbox);
811 if (!mBBox.empty()) {
822 internal::TileSampler<Sampler, InTreeAccessor>
823 sampler(bbox, i.getValue(), i.isValueOn());
824 transformBBox(mXform, bbox, mInAcc, mOutAcc, mInterrupt, sampler);
830 void join(RangeProcessor& other)
832 if (!
interrupt()) mOutTree->merge(*other.mOutTree);
836 bool interrupt()
const {
return mInterrupt && mInterrupt(); }
841 const TreeT& mInTree;
843 InTreeAccessor mInAcc;
844 OutTreeAccessor mOutAcc;
852 template<
class Sampler,
class Gr
idT,
typename Transformer>
855 const GridT& inGrid, GridT& outGrid)
const
857 using TreeT =
typename GridT::TreeType;
858 const TreeT& inTree = inGrid.tree();
859 TreeT& outTree = outGrid.tree();
861 using RangeProc = RangeProcessor<Sampler, TreeT, Transformer>;
863 const GridClass gridClass = inGrid.getGridClass();
870 RangeProc proc(xform,
CoordBBox(), inTree, outTree);
871 proc.setInterrupt(mInterrupt);
873 typename RangeProc::TileIterT tileIter = inTree.cbeginValueAll();
874 tileIter.setMaxDepth(tileIter.getLeafDepth() - 1);
875 typename RangeProc::TileRange tileRange(tileIter);
878 tbb::parallel_reduce(tileRange, proc);
888 clipBBox = inGrid.evalActiveVoxelBoundingBox();
893 RangeProc proc(xform, clipBBox, inTree, outTree);
894 proc.setInterrupt(mInterrupt);
896 typename RangeProc::LeafRange leafRange(inTree.cbeginLeaf());
899 tbb::parallel_reduce(leafRange, proc);
916 template<
class Sampler,
class InTreeT,
class OutTreeT,
class Transformer>
918 GridResampler::transformBBox(
919 const Transformer& xform,
921 const InTreeT& inTree,
923 const InterruptFunc& interrupt,
926 using ValueT =
typename OutTreeT::ValueType;
932 inRMax(bbox.
max().
x()+1, bbox.
max().
y()+1, bbox.
max().
z()+1),
935 for (
int i = 0; i < 8; ++i) {
937 i & 1 ? inRMax.x() : inRMin.x(),
938 i & 2 ? inRMax.y() : inRMin.y(),
939 i & 4 ? inRMax.z() : inRMin.z());
947 if (!xform.isAffine()) {
952 int &x = outXYZ.
x(), &y = outXYZ.
y(), &z = outXYZ.
z();
953 for (x = outMin.x(); x <= outMax.x(); ++x) {
956 for (y = outMin.y(); y <= outMax.y(); ++y) {
959 for (z = outMin.z(); z <= outMax.z(); ++z) {
961 inXYZ = xform.invTransform(xyz);
963 if (sampler.
sample(inTree, inXYZ, result)) {
964 outTree.setValueOn(outXYZ, result);
967 if (!outTree.isValueOn(outXYZ)) {
968 outTree.setValueOff(outXYZ, result);
978 translation = xform.invTransform(
Vec3R(0, 0, 0)),
979 deltaX = xform.invTransform(
Vec3R(1, 0, 0)) - translation,
980 deltaY = xform.invTransform(
Vec3R(0, 1, 0)) - translation,
981 deltaZ = xform.invTransform(
Vec3R(0, 0, 1)) - translation;
984 const Vec3R dummy = deltaX;
992 Vec3R inStartX = xform.invTransform(
Vec3R(outMin));
994 int &x = outXYZ.
x(), &y = outXYZ.y(), &z = outXYZ.z();
995 for (x = outMin.x(); x <= outMax.x(); ++x, inStartX += deltaX) {
997 Vec3R inStartY = inStartX;
998 for (y = outMin.y(); y <= outMax.y(); ++y, inStartY += deltaY) {
1000 Vec3R inXYZ = inStartY;
1001 for (z = outMin.z(); z <= outMax.z(); ++z, inXYZ += deltaZ) {
1003 if (sampler.
sample(inTree, inXYZ, result)) {
1004 outTree.setValueOn(outXYZ, result);
1007 if (!outTree.isValueOn(outXYZ)) {
1008 outTree.setValueOff(outXYZ, result);
1021 #endif // OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED