67 #ifndef OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
68 #define OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
71 #include <boost/shared_ptr.hpp>
72 #include <openvdb/version.h>
73 #include <openvdb/Platform.h>
74 #include <openvdb/math/Transform.h>
75 #include <openvdb/Grid.h>
76 #include <openvdb/tree/ValueAccessor.h>
91 static const char*
name() {
return "point"; }
93 static bool mipmap() {
return false; }
100 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
101 typename TreeT::ValueType& result);
107 static const char*
name() {
return "box"; }
115 template<
class TreeT>
116 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
117 typename TreeT::ValueType& result);
121 template<
class TreeT>
122 static typename TreeT::ValueType sample(
const TreeT& inTree,
const Vec3R& inCoord);
125 template<
class ValueT,
size_t N>
126 static inline ValueT trilinearInterpolation(ValueT (& data)[N][N][N],
const Vec3R& uvw);
132 static const char*
name() {
return "quadratic"; }
140 template<
class TreeT>
141 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
142 typename TreeT::ValueType& result);
156 static const char*
name() {
return "point"; }
164 template<
class TreeT>
165 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
166 typename TreeT::ValueType& result);
172 static const char*
name() {
return "box"; }
180 template<
class TreeT>
181 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
182 typename TreeT::ValueType& result);
188 static const char*
name() {
return "quadratic"; }
196 template<
class TreeT>
197 static bool sample(
const TreeT& inTree,
const Vec3R& inCoord,
198 typename TreeT::ValueType& result);
219 template<
typename Gr
idOrTreeType,
typename SamplerType>
223 typedef boost::shared_ptr<GridSampler>
Ptr;
231 : mTree(&(grid.tree())), mTransform(&(grid.transform())) {}
236 : mTree(&tree), mTransform(&transform) {}
244 template<
typename RealType>
247 return this->isSample(
Vec3d(x,y,z));
255 typename Coord::ValueType j,
256 typename Coord::ValueType k)
const
258 return this->isSample(Coord(i,j,k));
270 SamplerType::sample(*mTree, ispoint, result);
279 SamplerType::sample(*mTree, mTransform->worldToIndex(wspoint), result);
284 const TreeType* mTree;
301 template<
typename TreeT,
typename SamplerType>
305 typedef boost::shared_ptr<GridSampler>
Ptr;
314 : mAccessor(&acc), mTransform(&transform) {}
322 template<
typename RealType>
325 return this->isSample(
Vec3d(x,y,z));
333 typename Coord::ValueType j,
334 typename Coord::ValueType k)
const
336 return this->isSample(Coord(i,j,k));
348 SamplerType::sample(*mAccessor, ispoint, result);
357 SamplerType::sample(*mAccessor, mTransform->worldToIndex(wspoint), result);
362 const AccessorType* mAccessor;
370 namespace local_util {
375 return Vec3i(
int(std::floor(v(0))),
int(std::floor(v(1))),
int(std::floor(v(2))));
382 return Vec3i(
int(std::ceil(v(0))),
int(std::ceil(v(1))),
int(std::ceil(v(2))));
389 return Vec3i(
int(::round(v(0))),
int(::round(v(1))),
int(::round(v(2))));
398 template<
class TreeT>
400 PointSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
401 typename TreeT::ValueType& result)
404 return inTree.probeValue(Coord(inIdx), result);
411 template<
class ValueT,
size_t N>
413 BoxSampler::trilinearInterpolation(ValueT (& data)[N][N][N],
const Vec3R& uvw)
421 ValueT resultA, resultB;
423 resultA = data[0][0][0] + ValueT((data[0][0][1] - data[0][0][0]) * uvw[2]);
424 resultB = data[0][1][0] + ValueT((data[0][1][1] - data[0][1][0]) * uvw[2]);
425 ValueT result1 = resultA + ValueT((resultB-resultA) * uvw[1]);
427 resultA = data[1][0][0] + ValueT((data[1][0][1] - data[1][0][0]) * uvw[2]);
428 resultB = data[1][1][0] + ValueT((data[1][1][1] - data[1][1][0]) * uvw[2]);
429 ValueT result2 = resultA + ValueT((resultB - resultA) * uvw[1]);
431 return result1 + ValueT(uvw[0] * (result2 - result1));
435 template<
class TreeT>
437 BoxSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
438 typename TreeT::ValueType& result)
440 typedef typename TreeT::ValueType ValueT;
443 Vec3R uvw = inCoord - inIdx;
447 ValueT data[2][2][2];
449 bool hasActiveValues =
false;
451 hasActiveValues |= inTree.probeValue(ijk, data[0][0][0]);
453 hasActiveValues |= inTree.probeValue(ijk, data[0][0][1]);
455 hasActiveValues |= inTree.probeValue(ijk, data[0][1][1]);
457 hasActiveValues |= inTree.probeValue(ijk, data[0][1][0]);
460 hasActiveValues |= inTree.probeValue(ijk, data[1][0][0]);
462 hasActiveValues |= inTree.probeValue(ijk, data[1][0][1]);
464 hasActiveValues |= inTree.probeValue(ijk, data[1][1][1]);
466 hasActiveValues |= inTree.probeValue(ijk, data[1][1][0]);
468 result = trilinearInterpolation(data, uvw);
469 return hasActiveValues;
473 template<
class TreeT>
474 inline typename TreeT::ValueType
475 BoxSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord)
477 typedef typename TreeT::ValueType ValueT;
480 Vec3R uvw = inCoord - inIdx;
484 ValueT data[2][2][2];
487 data[0][0][0] = inTree.getValue(ijk);
489 data[0][0][1] = inTree.getValue(ijk);
491 data[0][1][1] = inTree.getValue(ijk);
493 data[0][1][0] = inTree.getValue(ijk);
496 data[1][0][0] = inTree.getValue(ijk);
498 data[1][0][1] = inTree.getValue(ijk);
500 data[1][1][1] = inTree.getValue(ijk);
502 data[1][1][0] = inTree.getValue(ijk);
504 return trilinearInterpolation(data, uvw);
511 template<
class TreeT>
513 QuadraticSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
514 typename TreeT::ValueType& result)
516 typedef typename TreeT::ValueType ValueT;
520 inLoIdx = inIdx -
Vec3i(1, 1, 1);
521 Vec3R frac = inCoord - inIdx;
527 for (
int dx = 0, ix = inLoIdx.x(); dx < 3; ++dx, ++ix) {
528 for (
int dy = 0, iy = inLoIdx.y(); dy < 3; ++dy, ++iy) {
529 for (
int dz = 0, iz = inLoIdx.z(); dz < 3; ++dz, ++iz) {
530 if (inTree.probeValue(Coord(ix, iy, iz), v[dx][dy][dz])) {
539 for (
int dx = 0; dx < 3; ++dx) {
541 for (
int dy = 0; dy < 3; ++dy) {
552 const ValueT* vz = &v[dx][dy][0];
554 az =
static_cast<ValueT
>(0.5 * (vz[0] + vz[2]) - vz[1]),
555 bz =
static_cast<ValueT
>(0.5 * (vz[2] - vz[0])),
556 cz =
static_cast<ValueT
>(vz[1]);
557 vy[dy] =
static_cast<ValueT
>(frac.
z() * (frac.
z() * az + bz) + cz);
563 ay =
static_cast<ValueT
>(0.5 * (vy[0] + vy[2]) - vy[1]),
564 by =
static_cast<ValueT
>(0.5 * (vy[2] - vy[0])),
565 cy =
static_cast<ValueT
>(vy[1]);
566 vx[dx] =
static_cast<ValueT
>(frac.
y() * (frac.
y() * ay + by) + cy);
571 ax =
static_cast<ValueT
>(0.5 * (vx[0] + vx[2]) - vx[1]),
572 bx =
static_cast<ValueT
>(0.5 * (vx[2] - vx[0])),
573 cx =
static_cast<ValueT
>(vx[1]);
574 result =
static_cast<ValueT
>(frac.
x() * (frac.
x() * ax + bx) + cx);
583 template<
class TreeT>
585 StaggeredPointSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
586 typename TreeT::ValueType& result)
588 typedef typename TreeT::ValueType ValueType;
590 ValueType tempX, tempY, tempZ;
593 active = PointSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0.5, 0, 0), tempX) || active;
594 active = PointSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0.5, 0), tempY) || active;
595 active = PointSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0, 0.5), tempZ) || active;
597 result.
x() = tempX.x();
598 result.y() = tempY.y();
599 result.z() = tempZ.z();
608 template<
class TreeT>
610 StaggeredBoxSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
611 typename TreeT::ValueType& result)
613 typedef typename TreeT::ValueType ValueType;
615 ValueType tempX, tempY, tempZ;
616 tempX = tempY = tempZ = zeroVal<ValueType>();
619 active = BoxSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0.5, 0, 0), tempX) || active;
620 active = BoxSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0.5, 0), tempY) || active;
621 active = BoxSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0, 0.5), tempZ) || active;
623 result.x() = tempX.x();
624 result.y() = tempY.y();
625 result.z() = tempZ.z();
634 template<
class TreeT>
636 StaggeredQuadraticSampler::sample(
const TreeT& inTree,
const Vec3R& inCoord,
637 typename TreeT::ValueType& result)
639 typedef typename TreeT::ValueType ValueType;
641 ValueType tempX, tempY, tempZ;
644 active = QuadraticSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0.5, 0, 0), tempX) || active;
645 active = QuadraticSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0.5, 0), tempY) || active;
646 active = QuadraticSampler::sample<TreeT>(inTree, inCoord +
Vec3R(0, 0, 0.5), tempZ) || active;
648 result.x() = tempX.x();
649 result.y() = tempY.y();
650 result.z() = tempZ.z();
659 #endif // OPENVDB_TOOLS_INTERPOLATION_HAS_BEEN_INCLUDED
T & z()
Definition: Vec3.h:96
Vec3< int32_t > Vec3i
Definition: Vec3.h:602
#define OPENVDB_VERSION_NAME
Definition: version.h:45
math::Vec3< Real > Vec3R
Definition: Types.h:74
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:94
_TreeType TreeType
Definition: Grid.h:825
Vec3< double > Vec3d
Definition: Vec3.h:605
T & y()
Definition: Vec3.h:95
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:54
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56