40#ifndef OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
41#define OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
43#include <openvdb/version.h>
62template <
size_t Order,
bool Staggered = false>
65 static_assert(Order < 3,
"Samplers of order higher than 2 are not supported");
79 typename TreeT::ValueType& result);
85 static typename TreeT::ValueType
sample(
const TreeT& inTree,
const Vec3R& inCoord);
98 static const char*
name() {
return "point"; }
108 template<
class TreeT>
109 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
110 typename TreeT::ValueType& result);
114 template<
class TreeT>
115 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
121 static const char*
name() {
return "box"; }
131 template<
class TreeT>
132 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
133 typename TreeT::ValueType& result);
137 template<
class TreeT>
138 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
142 template<
class ValueT,
class TreeT,
size_t N>
143 static inline void getValues(ValueT (&data)[N][N][N],
const TreeT& inTree,
Coord ijk);
148 template<
class ValueT,
class TreeT,
size_t N>
149 static inline bool probeValues(ValueT (&data)[N][N][N],
const TreeT& inTree,
Coord ijk);
153 template<
class ValueT,
size_t N>
154 static inline void extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT& vMax);
157 template<
class ValueT,
size_t N>
158 static inline ValueT trilinearInterpolation(ValueT (&data)[N][N][N],
const Vec3R& uvw);
164 static const char*
name() {
return "quadratic"; }
174 template<
class TreeT>
175 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
176 typename TreeT::ValueType& result);
180 template<
class TreeT>
181 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
183 template<
class ValueT,
size_t N>
184 static inline ValueT triquadraticInterpolation(ValueT (&data)[N][N][N],
const Vec3R& uvw);
198 static const char*
name() {
return "point"; }
208 template<
class TreeT>
209 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
210 typename TreeT::ValueType& result);
214 template<
class TreeT>
215 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
221 static const char*
name() {
return "box"; }
231 template<
class TreeT>
232 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
233 typename TreeT::ValueType& result);
237 template<
class TreeT>
238 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
244 static const char*
name() {
return "quadratic"; }
254 template<
class TreeT>
255 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
256 typename TreeT::ValueType& result);
260 template<
class TreeT>
261 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
282template<
typename Gr
idOrTreeType,
typename SamplerType>
307 template<
typename RealType>
333 SamplerType::sample(*mTree, ispoint, result);
342 SamplerType::sample(*mTree, mTransform->worldToIndex(wspoint), result);
347 const TreeType* mTree;
364template<
typename TreeT,
typename SamplerType>
378 : mAccessor(&acc), mTransform(&
transform) {}
386 template<
typename RealType>
412 SamplerType::sample(*mAccessor, ispoint, result);
421 SamplerType::sample(*mAccessor, mTransform->worldToIndex(wspoint), result);
426 const AccessorType* mAccessor;
443template<
typename GridOrTreeT,
458 : mSourceTree(&(sourceGrid.
tree()))
459 , mSourceXform(&(sourceGrid.transform()))
460 , mTargetXform(&targetXform)
461 , mAligned(targetXform == *mSourceXform)
471 : mSourceTree(&sourceTree)
472 , mSourceXform(&sourceXform)
473 , mTargetXform(&targetXform)
474 , mAligned(targetXform == sourceXform)
481 if (mAligned)
return mSourceTree->getValue(ijk);
482 const Vec3R world = mTargetXform->indexToWorld(ijk);
483 return SamplerT::sample(*mSourceTree, mSourceXform->worldToIndex(world));
488 const TreeType* mSourceTree;
495template<
typename TreeT,
512 : mSourceAcc(&sourceAccessor)
513 , mSourceXform(&sourceXform)
514 , mTargetXform(&targetXform)
515 , mAligned(targetXform == sourceXform)
522 if (mAligned)
return mSourceAcc->getValue(ijk);
523 const Vec3R world = mTargetXform->indexToWorld(ijk);
524 return SamplerT::sample(*mSourceAcc, mSourceXform->worldToIndex(world));
529 const AccessorType* mSourceAcc;
539template <
typename GridT,
542 typename FloatT =
float>
546 static_assert(std::is_floating_point<FloatT>::value,
547 "AlphaMask requires a floating-point value type");
553 AlphaMask(
const GridT& grid,
const MaskT& mask, FloatT min, FloatT max,
bool invert)
572 using AccT =
typename MaskType::ConstAccessor;
586 return Vec3i(
int(std::floor(v(0))),
int(std::floor(v(1))),
int(std::floor(v(2))));
593 return Vec3i(
int(std::ceil(v(0))),
int(std::ceil(v(1))),
int(std::ceil(v(2))));
600 return Vec3i(
int(::round(v(0))),
int(::round(v(1))),
int(::round(v(2))));
612 typename TreeT::ValueType& result)
618inline typename TreeT::ValueType
627template<
class ValueT,
class TreeT,
size_t N>
631 data[0][0][0] = inTree.getValue(ijk);
634 data[0][0][1] = inTree.getValue(ijk);
637 data[0][1][1] = inTree.getValue(ijk);
640 data[0][1][0] = inTree.getValue(ijk);
644 data[1][0][0] = inTree.getValue(ijk);
647 data[1][0][1] = inTree.getValue(ijk);
650 data[1][1][1] = inTree.getValue(ijk);
653 data[1][1][0] = inTree.getValue(ijk);
656template<
class ValueT,
class TreeT,
size_t N>
660 bool hasActiveValues =
false;
661 hasActiveValues |= inTree.probeValue(ijk, data[0][0][0]);
664 hasActiveValues |= inTree.probeValue(ijk, data[0][0][1]);
667 hasActiveValues |= inTree.probeValue(ijk, data[0][1][1]);
670 hasActiveValues |= inTree.probeValue(ijk, data[0][1][0]);
674 hasActiveValues |= inTree.probeValue(ijk, data[1][0][0]);
677 hasActiveValues |= inTree.probeValue(ijk, data[1][0][1]);
680 hasActiveValues |= inTree.probeValue(ijk, data[1][1][1]);
683 hasActiveValues |= inTree.probeValue(ijk, data[1][1][0]);
685 return hasActiveValues;
688template<
class ValueT,
size_t N>
692 vMin = vMax = data[0][0][0];
710template<
class ValueT,
size_t N>
714 auto _interpolate = [](
const ValueT& a,
const ValueT& b,
double weight)
717 const auto temp = (b - a) * weight;
719 return static_cast<ValueT
>(a + ValueT(temp));
730 _interpolate(data[0][0][0], data[0][0][1], uvw[2]),
731 _interpolate(data[0][1][0], data[0][1][1], uvw[2]),
734 _interpolate(data[1][0][0], data[1][0][1], uvw[2]),
735 _interpolate(data[1][1][0], data[1][1][1], uvw[2]),
744 typename TreeT::ValueType& result)
746 using ValueT =
typename TreeT::ValueType;
749 const Vec3R uvw = inCoord - inIdx;
753 ValueT data[2][2][2];
759 return hasActiveValues;
764inline typename TreeT::ValueType
767 using ValueT =
typename TreeT::ValueType;
770 const Vec3R uvw = inCoord - inIdx;
774 ValueT data[2][2][2];
784template<
class ValueT,
size_t N>
788 auto _interpolate = [](
const ValueT* value,
double weight)
792 a =
static_cast<ValueT
>(0.5 * (value[0] + value[2]) - value[1]),
793 b =
static_cast<ValueT
>(0.5 * (value[2] - value[0])),
794 c =
static_cast<ValueT
>(value[1]);
795 const auto temp = weight * (weight * a + b) + c;
797 return static_cast<ValueT
>(temp);
802 for (
int dx = 0; dx < 3; ++dx) {
804 for (
int dy = 0; dy < 3; ++dy) {
815 const ValueT* vz = &data[dx][dy][0];
816 vy[dy] = _interpolate(vz, uvw.
z());
821 vx[dx] = _interpolate(vy, uvw.
y());
825 return _interpolate(vx, uvw.
x());
831 typename TreeT::ValueType& result)
833 using ValueT =
typename TreeT::ValueType;
836 const Vec3R uvw = inCoord - inIdx;
841 ValueT data[3][3][3];
842 for (
int dx = 0, ix = inLoIdx.x(); dx < 3; ++dx, ++ix) {
843 for (
int dy = 0, iy = inLoIdx.y(); dy < 3; ++dy, ++iy) {
844 for (
int dz = 0, iz = inLoIdx.z(); dz < 3; ++dz, ++iz) {
845 if (inTree.probeValue(
Coord(ix, iy, iz), data[dx][dy][dz])) active =
true;
856inline typename TreeT::ValueType
859 using ValueT =
typename TreeT::ValueType;
862 const Vec3R uvw = inCoord - inIdx;
866 ValueT data[3][3][3];
867 for (
int dx = 0, ix = inLoIdx.x(); dx < 3; ++dx, ++ix) {
868 for (
int dy = 0, iy = inLoIdx.y(); dy < 3; ++dy, ++iy) {
869 for (
int dz = 0, iz = inLoIdx.z(); dz < 3; ++dz, ++iz) {
870 data[dx][dy][dz] = inTree.getValue(
Coord(ix, iy, iz));
885 typename TreeT::ValueType& result)
887 using ValueType =
typename TreeT::ValueType;
889 ValueType tempX, tempY, tempZ;
896 result.x() = tempX.x();
897 result.y() = tempY.y();
898 result.z() = tempZ.z();
904inline typename TreeT::ValueType
907 using ValueT =
typename TreeT::ValueType;
913 return ValueT(tempX.x(), tempY.y(), tempZ.z());
923 typename TreeT::ValueType& result)
925 using ValueType =
typename TreeT::ValueType;
927 ValueType tempX, tempY, tempZ;
935 result.x() = tempX.x();
936 result.y() = tempY.y();
937 result.z() = tempZ.z();
943inline typename TreeT::ValueType
946 using ValueT =
typename TreeT::ValueType;
952 return ValueT(tempX.x(), tempY.y(), tempZ.z());
962 typename TreeT::ValueType& result)
964 using ValueType =
typename TreeT::ValueType;
966 ValueType tempX, tempY, tempZ;
973 result.x() = tempX.x();
974 result.y() = tempY.y();
975 result.z() = tempZ.z();
981inline typename TreeT::ValueType
984 using ValueT =
typename TreeT::ValueType;
990 return ValueT(tempX.x(), tempY.y(), tempZ.z());
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
ValueAccessors are designed to help accelerate accesses into the OpenVDB Tree structures by storing c...
Int32 ValueType
Definition Coord.h:32
Container class that associates a tree with a transform and metadata.
Definition Grid.h:571
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:25
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition Vec3.h:85
T & y()
Definition Vec3.h:86
T & z()
Definition Vec3.h:87
Type SmoothUnitStep(Type x)
Return 0 if x < 0, 1 if x > 1 or else (3 − 2 x) x².
Definition Math.h:287
const Type & Max(const Type &a, const Type &b)
Return the maximum of two values.
Definition Math.h:595
Vec3< double > Vec3d
Definition Vec3.h:664
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition Math.h:656
Vec3< int32_t > Vec3i
Definition Vec3.h:661
Definition PointDataGrid.h:169
ValueAccessorImpl< TreeType, IsSafe, MutexType, openvdb::make_index_sequence< CacheLevels > > ValueAccessor
Default alias for a ValueAccessor. This is simply a helper alias for the generic definition but takes...
Definition ValueAccessor.h:86
constexpr T zeroVal()
Return the value of type T that corresponds to zero.
Definition Math.h:70
math::Vec3< Real > Vec3R
Definition Types.h:72
std::shared_ptr< T > SharedPtr
Definition Types.h:114
Definition Exceptions.h:13
Grid< TreeType > GridType
Definition Grid.h:1066
_TreeType TreeType
Definition Grid.h:1061
typename tree::ValueAccessor< TreeType > AccessorType
Definition Grid.h:1072
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212