34 #ifndef OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED 35 #define OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED 46 #include <tbb/blocked_range.h> 47 #include <tbb/parallel_reduce.h> 79 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
81 resampleToMatch(
const GridType& inGrid, GridType& outGrid, Interrupter& interrupter);
104 template<
typename Sampler,
typename Gr
idType>
117 template<
typename Sampler,
typename TreeT>
121 using ValueT =
typename TreeT::ValueType;
127 mBBox(b.
min().asVec3d(), b.
max().asVec3d()), mVal(tileVal), mActive(on), mEmpty(false)
129 mBBox.expand(-this->radius());
130 mEmpty = mBBox.empty();
135 if (!mEmpty && mBBox.isInside(inCoord)) { result = mVal;
return mActive; }
136 return Sampler::sample(inTree, inCoord, result);
148 template<
typename TreeT>
151 TileSampler(
const CoordBBox&,
const typename TreeT::ValueType&,
bool) {}
156 template<
typename TreeT>
159 TileSampler(
const CoordBBox&,
const typename TreeT::ValueType&,
bool) {}
206 template<
typename InterrupterType>
void setInterrupter(InterrupterType&);
208 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
209 void transformGrid(
const Transformer&,
210 const GridT& inGrid, GridT& outGrid)
const;
213 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
214 void applyTransform(
const Transformer&,
const GridT& inGrid, GridT& outGrid)
const;
216 bool interrupt()
const {
return mInterrupt && mInterrupt(); }
219 template<
typename Sampler,
typename InTreeT,
typename OutTreeT,
typename Transformer>
220 static void transformBBox(
const Transformer&,
const CoordBBox& inBBox,
221 const InTreeT& inTree, OutTreeT& outTree,
const InterruptFunc&,
224 template<
typename Sampler,
typename TreeT,
typename Transformer>
225 class RangeProcessor;
227 bool mThreaded, mTransformTiles;
263 const Vec3R& translate,
264 const std::string& xformOrder =
"tsr",
265 const std::string& rotationOrder =
"zyx");
270 template<
class Sampler,
class Gr
idT>
271 void transformGrid(
const GridT& inGrid, GridT& outGrid)
const;
278 const std::string& xformOrder,
const std::string& rotOrder);
282 Mat4R mTransform, mPreScaleTransform, mPostScaleTransform;
289 namespace local_util {
310 const bool hasUniformScale = unsignedScale.
eq(
math::Vec3<T>(unsignedScale[0]));
312 bool hasRotation =
false;
313 bool validDecomposition =
false;
320 for (
size_t n = 0; n < 8; ++n) {
323 n & 0x1 ? -unsignedScale.
x() : unsignedScale.
x(),
324 n & 0x2 ? -unsignedScale.
y() : unsignedScale.
y(),
325 n & 0x4 ? -unsignedScale.
z() : unsignedScale.
z());
328 const math::Mat3<T> mat = xform * math::scale<math::Mat3<T> >(signedScale).inverse();
329 if (mat.det() < T(0.0))
continue;
334 math::rotation<math::Mat3<T> >(
math::Vec3<T>(1, 0, 0), tmpAngle.
x()) *
339 if (xform.
eq(rebuild)) {
341 const T maxAngle =
std::max(std::abs(tmpAngle[0]),
342 std::max(std::abs(tmpAngle[1]), std::abs(tmpAngle[2])));
344 if (!(minAngle < maxAngle)) {
351 validDecomposition =
true;
353 if (hasUniformScale || !hasRotation) {
361 if (!validDecomposition || (hasRotation && !hasUniformScale)) {
409 mIsAffine(mAXform.isLinear() && mBXform.isLinear()),
410 mIsIdentity(mIsAffine && mAXform == mBXform)
419 return mBXform.worldToIndex(mAXform.indexToWorld(pos));
424 return mAXform.worldToIndex(mBXform.indexToWorld(pos));
432 const bool mIsAffine;
433 const bool mIsIdentity;
443 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
447 ABTransform xform(inGrid.transform(), outGrid.transform());
449 if (Sampler::consistent() && xform.isIdentity()) {
452 outGrid.setTree(inGrid.tree().copy());
453 }
else if (xform.isAffine()) {
457 Mat4R mat = xform.getA().baseMap()->getAffineMap()->getMat4() *
458 ( xform.getB().baseMap()->getAffineMap()->getMat4().inverse() );
476 template<
typename Sampler,
typename Interrupter,
typename Gr
idType>
483 if (inGrid.constTransform() == outGrid.constTransform()) {
486 outGrid.setTree(inGrid.tree().copy());
492 using ValueT =
typename GridType::ValueType;
494 ? ValueT(outGrid.background() * (1.0 / outGrid.voxelSize()[0]))
495 : ValueT(inGrid.background() * (1.0 / inGrid.voxelSize()[0])));
497 typename GridType::Ptr tempGrid;
500 halfWidth, halfWidth,
501 &outGrid.constTransform(), &interrupter);
509 outGrid.setTree(tempGrid->treePtr());
515 doResampleToMatch<Sampler>(inGrid, outGrid, interrupter);
519 template<
typename Sampler,
typename Gr
idType>
524 resampleToMatch<Sampler>(inGrid, outGrid, interrupter);
532 GridTransformer::GridTransformer(
const Mat4R& xform):
536 mPreScaleTransform(
Mat4R::identity()),
537 mPostScaleTransform(
Mat4R::identity())
543 init(mPivot, scale, rotate, translate,
"srt",
"zyx");
552 const std::string& xformOrder,
const std::string& rotOrder):
555 mPreScaleTransform(
Mat4R::identity()),
556 mPostScaleTransform(
Mat4R::identity())
558 init(pivot, scale, rotate, translate, xformOrder, rotOrder);
566 GridTransformer::init(
569 const std::string& xformOrder,
const std::string& rotOrder)
571 if (xformOrder.size() != 3) {
574 if (rotOrder.size() != 3) {
584 for (
int i = 0; i < 3; ++i) {
585 double s = std::fabs(
scale(i));
587 mMipLevels(i) = int(std::floor(-std::log(s)/std::log(2.0)));
588 scaleRemainder(i) =
scale(i) * (1 << mMipLevels(i));
597 mTransform = mPreScaleTransform = mPostScaleTransform =
Mat4R::identity();
598 Mat4R* remainder = &mPostScaleTransform;
599 int rpos, spos, tpos;
600 rpos = spos = tpos = 3;
601 for (
int ix = 2; ix >= 0; --ix) {
602 switch (xformOrder[ix]) {
609 int xpos, ypos, zpos;
610 xpos = ypos = zpos = 3;
611 for (
int ir = 2; ir >= 0; --ir) {
612 switch (rotOrder[ir]) {
632 if (xpos > 2 || ypos > 2 || zpos > 2) {
647 remainder->
preScale(scaleRemainder);
649 remainder = &mPreScaleTransform;
661 if (tpos > 2 || rpos > 2 || spos > 2) {
670 template<
typename InterrupterType>
679 template<
typename Sampler,
typename Gr
idT,
typename Transformer>
682 const GridT& inGrid, GridT& outGrid)
const 685 applyTransform<Sampler>(xform, inGrid, outGrid);
689 template<
class Sampler,
class Gr
idT>
698 applyTransform<Sampler>(xform, inGrid, outGrid);
701 bool firstPass =
true;
702 const typename GridT::ValueType background = inGrid.background();
703 typename GridT::Ptr tempGrid = GridT::create(background);
710 applyTransform<Sampler>(xform, inGrid, *tempGrid);
715 Vec3i count = mMipLevels;
716 while (count != Vec3i::zero()) {
720 count.x() ? .5 : 1, count.y() ? .5 : 1, count.z() ? .5 : 1));
727 applyTransform<Sampler>(xform, inGrid, *tempGrid);
731 typename GridT::Ptr destGrid = GridT::create(background);
732 applyTransform<Sampler>(xform, *tempGrid, *destGrid);
733 tempGrid.swap(destGrid);
742 applyTransform<Sampler>(xform, *tempGrid, outGrid);
744 outGrid.setTree(tempGrid->treePtr());
753 template<
class Sampler,
class TreeT,
typename Transformer>
754 class GridResampler::RangeProcessor
757 using LeafIterT =
typename TreeT::LeafCIter;
758 using TileIterT =
typename TreeT::ValueAllCIter;
764 RangeProcessor(
const Transformer& xform,
const CoordBBox& b,
const TreeT& inT, TreeT& outT):
765 mIsRoot(
true), mXform(xform), mBBox(b),
766 mInTree(inT), mOutTree(&outT), mInAcc(mInTree), mOutAcc(*mOutTree)
769 RangeProcessor(
const Transformer& xform,
const CoordBBox& b,
const TreeT& inTree):
770 mIsRoot(
false), mXform(xform), mBBox(b),
771 mInTree(inTree), mOutTree(
new TreeT(inTree.background())),
772 mInAcc(mInTree), mOutAcc(*mOutTree)
775 ~RangeProcessor() {
if (!mIsRoot)
delete mOutTree; }
778 RangeProcessor(RangeProcessor& other, tbb::split):
780 mXform(other.mXform),
782 mInTree(other.mInTree),
783 mOutTree(
new TreeT(mInTree.background())),
786 mInterrupt(other.mInterrupt)
789 void setInterrupt(
const InterruptFunc& f) { mInterrupt = f; }
792 void operator()(LeafRange& r)
796 LeafIterT i = r.iterator();
797 CoordBBox bbox(i->origin(), i->origin() + Coord(i->dim()));
798 if (!mBBox.empty()) {
805 transformBBox<Sampler>(mXform, bbox, mInAcc, mOutAcc, mInterrupt);
811 void operator()(TileRange& r)
816 TileIterT i = r.iterator();
818 if (!i.isTileValue())
continue;
822 i.getBoundingBox(bbox);
823 if (!mBBox.empty()) {
835 sampler(bbox, i.getValue(), i.isValueOn());
836 transformBBox(mXform, bbox, mInAcc, mOutAcc, mInterrupt, sampler);
842 void join(RangeProcessor& other)
844 if (!
interrupt()) mOutTree->merge(*other.mOutTree);
848 bool interrupt()
const {
return mInterrupt && mInterrupt(); }
853 const TreeT& mInTree;
855 InTreeAccessor mInAcc;
856 OutTreeAccessor mOutAcc;
864 template<
class Sampler,
class Gr
idT,
typename Transformer>
867 const GridT& inGrid, GridT& outGrid)
const 869 using TreeT =
typename GridT::TreeType;
870 const TreeT& inTree = inGrid.tree();
871 TreeT& outTree = outGrid.tree();
873 using RangeProc = RangeProcessor<Sampler, TreeT, Transformer>;
875 const GridClass gridClass = inGrid.getGridClass();
882 RangeProc proc(xform, CoordBBox(), inTree, outTree);
883 proc.setInterrupt(mInterrupt);
885 typename RangeProc::TileIterT tileIter = inTree.cbeginValueAll();
886 tileIter.setMaxDepth(tileIter.getLeafDepth() - 1);
887 typename RangeProc::TileRange tileRange(tileIter);
890 tbb::parallel_reduce(tileRange, proc);
900 clipBBox = inGrid.evalActiveVoxelBoundingBox();
905 RangeProc proc(xform, clipBBox, inTree, outTree);
906 proc.setInterrupt(mInterrupt);
908 typename RangeProc::LeafRange leafRange(inTree.cbeginLeaf());
911 tbb::parallel_reduce(leafRange, proc);
928 template<
class Sampler,
class InTreeT,
class OutTreeT,
class Transformer>
930 GridResampler::transformBBox(
931 const Transformer& xform,
932 const CoordBBox& bbox,
933 const InTreeT& inTree,
938 using ValueT =
typename OutTreeT::ValueType;
943 inRMin(bbox.min().x(), bbox.min().y(), bbox.min().z()),
944 inRMax(bbox.max().x(), bbox.max().y(), bbox.max().z()),
947 for (
int i = 0; i < 8; ++i) {
949 i & 1 ? inRMax.x() : inRMin.x(),
950 i & 2 ? inRMax.y() : inRMin.y(),
951 i & 4 ? inRMax.z() : inRMin.z());
959 if (!xform.isAffine()) {
964 int &x = outXYZ.
x(), &y = outXYZ.y(), &z = outXYZ.z();
965 for (x = outMin.x(); x <= outMax.x(); ++x) {
968 for (y = outMin.y(); y <= outMax.y(); ++y) {
971 for (z = outMin.z(); z <= outMax.z(); ++z) {
973 inXYZ = xform.invTransform(xyz);
975 if (sampler.
sample(inTree, inXYZ, result)) {
976 outTree.setValueOn(outXYZ, result);
979 if (!outTree.isValueOn(outXYZ)) {
980 outTree.setValueOff(outXYZ, result);
990 translation = xform.invTransform(
Vec3R(0, 0, 0)),
991 deltaX = xform.invTransform(
Vec3R(1, 0, 0)) - translation,
992 deltaY = xform.invTransform(
Vec3R(0, 1, 0)) - translation,
993 deltaZ = xform.invTransform(
Vec3R(0, 0, 1)) - translation;
996 const Vec3R dummy = deltaX;
1004 Vec3R inStartX = xform.invTransform(
Vec3R(outMin));
1006 int &x = outXYZ.
x(), &y = outXYZ.y(), &z = outXYZ.z();
1007 for (x = outMin.x(); x <= outMax.x(); ++x, inStartX += deltaX) {
1009 Vec3R inStartY = inStartX;
1010 for (y = outMin.y(); y <= outMax.y(); ++y, inStartY += deltaY) {
1012 Vec3R inXYZ = inStartY;
1013 for (z = outMin.z(); z <= outMax.z(); ++z, inXYZ += deltaZ) {
1015 if (sampler.
sample(inTree, inXYZ, result)) {
1016 outTree.setValueOn(outXYZ, result);
1019 if (!outTree.isValueOn(outXYZ)) {
1020 outTree.setValueOff(outXYZ, result);
1033 #endif // OPENVDB_TOOLS_GRIDTRANSFORMER_HAS_BEEN_INCLUDED
bool isAffine(const Mat4< T > &m)
Definition: Mat4.h:1363
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:370
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Vec3< int32_t > Vec3i
Definition: Vec3.h:705
T & z()
Definition: Vec3.h:111
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the rotation matrix specified by the given quaternion.
Definition: Mat.h:169
Efficient multi-threaded replacement of the background values in tree.
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:610
Defined various multi-threaded utility functions for trees.
GridClass
Definition: Types.h:262
bool eq(const Vec3< T > &v, T eps=static_cast< T >(1.0e-7)) const
Test if "this" vector is equivalent to vector v with tolerance of eps.
Definition: Vec3.h:157
void preScale(const Vec3< T0 > &v)
Definition: Mat4.h:790
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:562
bool eq(const Mat4 &m, T eps=1.0e-8) const
Test if "this" is equivalent to m with tolerance of eps value.
Definition: Mat4.h:387
#define OPENVDB_VERSION_NAME
Definition: version.h:43
void preRotate(Axis axis, T angle)
Left multiplies by a rotation clock-wiseabout the given axis into this matrix.
Definition: Mat4.h:852
3x3 matrix class.
Definition: Mat3.h:54
Definition: Exceptions.h:91
Propagates the sign of distance values from the active voxels in the narrow band to the inactive valu...
Mat3< T > getMat3() const
Definition: Mat4.h:351
Definition: Exceptions.h:39
bool wasInterrupted(T *i, int percent=-1)
Definition: NullInterrupter.h:76
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:109
T & y()
Definition: Vec3.h:110
Vec3< typename MatType::value_type > eulerAngles(const MatType &mat, RotationOrder rotationOrder, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the Euler angles composing the given rotation matrix.
Definition: Mat.h:330
Definition: TreeIterator.h:1339
void preTranslate(const Vec3< T0 > &tr)
Left multiples by the specified translation, i.e. Trans * (*this)
Definition: Mat4.h:757
Definition: Exceptions.h:92
math::Vec3< Real > Vec3R
Definition: Types.h:75
Mat4 inverse(T tolerance=0) const
Definition: Mat4.h:539
std::shared_ptr< T > SharedPtr
Definition: Types.h:130
Vec3< T > getTranslation() const
Return the translation component.
Definition: Mat4.h:363
Dummy NOOP interrupter class defining interface.
Definition: NullInterrupter.h:52
static const Mat4< Real > & identity()
Predefined constant for identity matrix.
Definition: Mat4.h:152
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:553
void setTranslation(const Vec3< T > &t)
Definition: Mat4.h:368
bool eq(const Mat3 &m, T eps=1.0e-8) const
Test if "this" is equivalent to m with tolerance of eps value.
Definition: Mat3.h:355