40#ifndef OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
41#define OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
43#include <openvdb/version.h>
63template <
size_t Order,
bool Staggered = false>
66 static_assert(Order < 3,
"Samplers of order higher than 2 are not supported");
80 typename TreeT::ValueType& result);
86 static typename TreeT::ValueType
sample(
const TreeT& inTree,
const Vec3R& inCoord);
99 static const char*
name() {
return "point"; }
109 template<
class TreeT>
110 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
111 typename TreeT::ValueType& result);
115 template<
class TreeT>
116 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
122 static const char*
name() {
return "box"; }
132 template<
class TreeT>
133 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
134 typename TreeT::ValueType& result);
138 template<
class TreeT>
139 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
143 template<
class ValueT,
class TreeT,
size_t N>
144 static inline void getValues(ValueT (&data)[N][N][N],
const TreeT& inTree,
Coord ijk);
149 template<
class ValueT,
class TreeT,
size_t N>
150 static inline bool probeValues(ValueT (&data)[N][N][N],
const TreeT& inTree,
Coord ijk);
155 template<
class ValueT,
size_t N>
156 static inline void extrema(ValueT (&data)[N][N][N], ValueT& vMin, ValueT& vMax);
159 template<
class ValueT,
size_t N>
160 static inline ValueT trilinearInterpolation(ValueT (&data)[N][N][N],
const Vec3R& uvw);
166 static const char*
name() {
return "quadratic"; }
176 template<
class TreeT>
177 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
178 typename TreeT::ValueType& result);
182 template<
class TreeT>
183 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
185 template<
class ValueT,
size_t N>
186 static inline ValueT triquadraticInterpolation(ValueT (&data)[N][N][N],
const Vec3R& uvw);
200 static const char*
name() {
return "point"; }
210 template<
class TreeT>
211 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
212 typename TreeT::ValueType& result);
216 template<
class TreeT>
217 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
223 static const char*
name() {
return "box"; }
233 template<
class TreeT>
234 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
235 typename TreeT::ValueType& result);
239 template<
class TreeT>
240 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
246 static const char*
name() {
return "quadratic"; }
256 template<
class TreeT>
257 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
258 typename TreeT::ValueType& result);
262 template<
class TreeT>
263 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
284template<
typename Gr
idOrTreeType,
typename SamplerType>
309 template<
typename RealType>
335 SamplerType::sample(*mTree, ispoint, result);
344 SamplerType::sample(*mTree, mTransform->worldToIndex(wspoint), result);
349 const TreeType* mTree;
366template<
typename TreeT,
typename SamplerType>
380 : mAccessor(&acc), mTransform(&
transform) {}
388 template<
typename RealType>
414 SamplerType::sample(*mAccessor, ispoint, result);
423 SamplerType::sample(*mAccessor, mTransform->worldToIndex(wspoint), result);
428 const AccessorType* mAccessor;
445template<
typename GridOrTreeT,
460 : mSourceTree(&(sourceGrid.
tree()))
461 , mSourceXform(&(sourceGrid.transform()))
462 , mTargetXform(&targetXform)
463 , mAligned(targetXform == *mSourceXform)
473 : mSourceTree(&sourceTree)
474 , mSourceXform(&sourceXform)
475 , mTargetXform(&targetXform)
476 , mAligned(targetXform == sourceXform)
483 if (mAligned)
return mSourceTree->getValue(ijk);
484 const Vec3R world = mTargetXform->indexToWorld(ijk);
485 return SamplerT::sample(*mSourceTree, mSourceXform->worldToIndex(world));
490 const TreeType* mSourceTree;
497template<
typename TreeT,
514 : mSourceAcc(&sourceAccessor)
515 , mSourceXform(&sourceXform)
516 , mTargetXform(&targetXform)
517 , mAligned(targetXform == sourceXform)
524 if (mAligned)
return mSourceAcc->getValue(ijk);
525 const Vec3R world = mTargetXform->indexToWorld(ijk);
526 return SamplerT::sample(*mSourceAcc, mSourceXform->worldToIndex(world));
531 const AccessorType* mSourceAcc;
541template <
typename GridT,
544 typename FloatT =
float>
548 static_assert(std::is_floating_point<FloatT>::value,
549 "AlphaMask requires a floating-point value type");
555 AlphaMask(
const GridT& grid,
const MaskT& mask, FloatT min, FloatT max,
bool invert)
574 using AccT =
typename MaskType::ConstAccessor;
588 return Vec3i(
int(std::floor(v(0))),
int(std::floor(v(1))),
int(std::floor(v(2))));
595 return Vec3i(
int(std::ceil(v(0))),
int(std::ceil(v(1))),
int(std::ceil(v(2))));
602 return Vec3i(
int(::round(v(0))),
int(::round(v(1))),
int(::round(v(2))));
614 typename TreeT::ValueType& result)
620inline typename TreeT::ValueType
629template<
class ValueT,
class TreeT,
size_t N>
633 data[0][0][0] = inTree.getValue(ijk);
636 data[0][0][1] = inTree.getValue(ijk);
639 data[0][1][1] = inTree.getValue(ijk);
642 data[0][1][0] = inTree.getValue(ijk);
646 data[1][0][0] = inTree.getValue(ijk);
649 data[1][0][1] = inTree.getValue(ijk);
652 data[1][1][1] = inTree.getValue(ijk);
655 data[1][1][0] = inTree.getValue(ijk);
658template<
class ValueT,
class TreeT,
size_t N>
662 bool hasActiveValues =
false;
663 hasActiveValues |= inTree.probeValue(ijk, data[0][0][0]);
666 hasActiveValues |= inTree.probeValue(ijk, data[0][0][1]);
669 hasActiveValues |= inTree.probeValue(ijk, data[0][1][1]);
672 hasActiveValues |= inTree.probeValue(ijk, data[0][1][0]);
676 hasActiveValues |= inTree.probeValue(ijk, data[1][0][0]);
679 hasActiveValues |= inTree.probeValue(ijk, data[1][0][1]);
682 hasActiveValues |= inTree.probeValue(ijk, data[1][1][1]);
685 hasActiveValues |= inTree.probeValue(ijk, data[1][1][0]);
687 return hasActiveValues;
690template<
class ValueT,
size_t N>
694 vMin = vMax = data[0][0][0];
712template<
class ValueT,
size_t N>
716 auto _interpolate = [](
const ValueT& a,
const ValueT& b,
double weight)
719 const auto temp = (b - a) * weight;
721 return static_cast<ValueT
>(a + ValueT(temp));
732 _interpolate(data[0][0][0], data[0][0][1], uvw[2]),
733 _interpolate(data[0][1][0], data[0][1][1], uvw[2]),
736 _interpolate(data[1][0][0], data[1][0][1], uvw[2]),
737 _interpolate(data[1][1][0], data[1][1][1], uvw[2]),
746 typename TreeT::ValueType& result)
748 using ValueT =
typename TreeT::ValueType;
751 const Vec3R uvw = inCoord - inIdx;
755 ValueT data[2][2][2];
761 return hasActiveValues;
766inline typename TreeT::ValueType
769 using ValueT =
typename TreeT::ValueType;
772 const Vec3R uvw = inCoord - inIdx;
776 ValueT data[2][2][2];
786template<
class ValueT,
size_t N>
790 auto _interpolate = [](
const ValueT* value,
double weight)
794 a =
static_cast<ValueT
>(0.5 * (value[0] + value[2]) - value[1]),
795 b =
static_cast<ValueT
>(0.5 * (value[2] - value[0])),
796 c =
static_cast<ValueT
>(value[1]);
797 const auto temp = weight * (weight * a + b) + c;
799 return static_cast<ValueT
>(temp);
804 for (
int dx = 0; dx < 3; ++dx) {
806 for (
int dy = 0; dy < 3; ++dy) {
817 const ValueT* vz = &data[dx][dy][0];
818 vy[dy] = _interpolate(vz, uvw.
z());
823 vx[dx] = _interpolate(vy, uvw.
y());
827 return _interpolate(vx, uvw.
x());
833 typename TreeT::ValueType& result)
835 using ValueT =
typename TreeT::ValueType;
838 const Vec3R uvw = inCoord - inIdx;
843 ValueT data[3][3][3];
844 for (
int dx = 0, ix = inLoIdx.x(); dx < 3; ++dx, ++ix) {
845 for (
int dy = 0, iy = inLoIdx.y(); dy < 3; ++dy, ++iy) {
846 for (
int dz = 0, iz = inLoIdx.z(); dz < 3; ++dz, ++iz) {
847 if (inTree.probeValue(
Coord(ix, iy, iz), data[dx][dy][dz])) active =
true;
858inline typename TreeT::ValueType
861 using ValueT =
typename TreeT::ValueType;
864 const Vec3R uvw = inCoord - inIdx;
868 ValueT data[3][3][3];
869 for (
int dx = 0, ix = inLoIdx.x(); dx < 3; ++dx, ++ix) {
870 for (
int dy = 0, iy = inLoIdx.y(); dy < 3; ++dy, ++iy) {
871 for (
int dz = 0, iz = inLoIdx.z(); dz < 3; ++dz, ++iz) {
872 data[dx][dy][dz] = inTree.getValue(
Coord(ix, iy, iz));
887 typename TreeT::ValueType& result)
889 using ValueType =
typename TreeT::ValueType;
891 ValueType tempX, tempY, tempZ;
898 result.x() = tempX.x();
899 result.y() = tempY.y();
900 result.z() = tempZ.z();
906inline typename TreeT::ValueType
909 using ValueT =
typename TreeT::ValueType;
915 return ValueT(tempX.x(), tempY.y(), tempZ.z());
925 typename TreeT::ValueType& result)
927 using ValueType =
typename TreeT::ValueType;
929 ValueType tempX, tempY, tempZ;
937 result.x() = tempX.x();
938 result.y() = tempY.y();
939 result.z() = tempZ.z();
945inline typename TreeT::ValueType
948 using ValueT =
typename TreeT::ValueType;
954 return ValueT(tempX.x(), tempY.y(), tempZ.z());
964 typename TreeT::ValueType& result)
966 using ValueType =
typename TreeT::ValueType;
968 ValueType tempX, tempY, tempZ;
975 result.x() = tempX.x();
976 result.y() = tempY.y();
977 result.z() = tempZ.z();
983inline typename TreeT::ValueType
986 using ValueT =
typename TreeT::ValueType;
992 return ValueT(tempX.x(), tempY.y(), tempZ.z());
#define OPENVDB_ASSERT(X)
Definition Assert.h:41
ValueAccessors are designed to help accelerate accesses into the OpenVDB Tree structures by storing c...
Int32 ValueType
Definition Coord.h:33
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:26
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition Vec3.h:86
T & y()
Definition Vec3.h:87
T & z()
Definition Vec3.h:88
Type SmoothUnitStep(Type x)
Return 0 if x < 0, 1 if x > 1 or else (3 − 2 x) x².
Definition Math.h:300
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition Mat.h:1015
Vec3< double > Vec3d
Definition Vec3.h:708
Vec3< int32_t > Vec3i
Definition Vec3.h:705
Definition PointDataGrid.h:170
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:71
math::Vec3< Real > Vec3R
Definition Types.h:53
std::shared_ptr< T > SharedPtr
Definition Types.h:95
Definition Exceptions.h:13
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
_TreeType TreeType
Definition Grid.h:1059
Grid< NonConstTreeType > GridType
Definition Grid.h:1064
typename tree::ValueAccessor< TreeType > AccessorType
Definition Grid.h:1070
#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:291