10 #ifndef OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
11 #define OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
33 template<
typename RayT, Index Log2Dim = 0>
47 DDA(
const RayT& ray,
RealT startTime) { this->
init(ray, startTime); }
53 assert(startTime <= maxTime);
54 static const int DIM = 1 << Log2Dim;
57 const Vec3T &pos = ray(mT0), &dir = ray.dir(), &inv = ray.invDir();
59 for (
int axis = 0; axis < 3; ++axis) {
64 }
else if (inv[axis] > 0) {
66 mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
67 mDelta[axis] = mStep[axis] * inv[axis];
70 mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
71 mDelta[axis] = mStep[axis] * inv[axis];
76 inline void init(
const RayT& ray) { this->
init(ray, ray.t0(), ray.t1()); }
78 inline void init(
const RayT& ray,
RealT startTime) { this->
init(ray, startTime, ray.t1()); }
85 mT0 = mNext[stepAxis];
86 mNext[stepAxis] += mDelta[stepAxis];
87 mVoxel[stepAxis] += mStep[stepAxis];
115 void print(std::ostream& os = std::cout)
const
117 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << mT0 <<
" next()="
118 << this->
next() <<
" voxel=" << mVoxel <<
" next=" << mNext
119 <<
" delta=" << mDelta <<
" step=" << mStep << std::endl;
130 template<
typename RayT, Index Log2Dim>
131 inline std::ostream& operator<<(std::ostream& os, const DDA<RayT, Log2Dim>& dda)
133 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << dda.time()
134 <<
" next()=" << dda.next() <<
" voxel=" << dda.voxel();
143 template<
typename TreeT,
int NodeLevel>
146 using ChainT =
typename TreeT::RootNodeType::NodeChainType;
147 using NodeT =
typename ChainT::template Get<NodeLevel>;
149 template <
typename TesterT>
150 static bool test(TesterT& tester)
154 if (tester.template hasNode<NodeT>(dda.voxel())) {
155 tester.setRange(dda.time(), dda.next());
165 template<
typename TreeT>
168 template <
typename TesterT>
169 static bool test(TesterT& tester)
172 tester.
init(dda.time());
173 do {
if (tester(dda.voxel(), dda.next()))
return true; }
while(dda.step());
186 template <
typename TreeT,
typename RayT,
int ChildNodeLevel>
191 using ChainT =
typename TreeT::RootNodeType::NodeChainType;
192 using NodeT =
typename ChainT::template Get<ChildNodeLevel>;
197 template <
typename AccessorT>
201 if (ray.valid()) this->
march(ray, acc, t);
209 template <
typename AccessorT,
typename ListT>
210 void hits(RayT& ray, AccessorT &acc, ListT& times)
214 this->
hits(ray, acc, times, t);
215 if (t.valid()) times.push_back(t);
222 template <
typename AccessorT>
227 if (acc.template probeConstNode<NodeT>(mDDA.
voxel()) !=
nullptr) {
228 ray.setTimes(mDDA.
time(), mDDA.
next());
229 if (mHDDA.march(ray, acc, t))
return true;
230 }
else if (acc.isValueOn(mDDA.
voxel())) {
231 if (t.t0<0) t.t0 = mDDA.
time();
232 }
else if (t.t0>=0) {
234 if (t.valid())
return true;
237 }
while (mDDA.
step());
238 if (t.t0>=0) t.t1 = mDDA.
maxTime();
246 template <
typename AccessorT,
typename ListT>
247 void hits(RayT& ray, AccessorT &acc, ListT& times,
TimeSpanT& t)
251 if (acc.template probeConstNode<NodeT>(mDDA.
voxel()) !=
nullptr) {
252 ray.setTimes(mDDA.
time(), mDDA.
next());
253 mHDDA.hits(ray, acc, times, t);
254 }
else if (acc.isValueOn(mDDA.
voxel())) {
255 if (t.t0<0) t.t0 = mDDA.
time();
256 }
else if (t.t0>=0) {
258 if (t.valid()) times.push_back(t);
261 }
while (mDDA.
step());
262 if (t.t0>=0) t.t1 = mDDA.
maxTime();
265 math::DDA<RayT, NodeT::TOTAL> mDDA;
266 VolumeHDDA<TreeT, RayT, ChildNodeLevel-1> mHDDA;
271 template <
typename TreeT,
typename RayT>
276 using LeafT =
typename TreeT::LeafNodeType;
281 template <
typename AccessorT>
285 if (ray.valid()) this->
march(ray, acc, t);
289 template <
typename AccessorT,
typename ListT>
290 void hits(RayT& ray, AccessorT &acc, ListT& times)
294 this->
hits(ray, acc, times, t);
295 if (t.valid()) times.push_back(t);
302 template <
typename AccessorT>
307 if (acc.template probeConstNode<LeafT>(mDDA.
voxel()) ||
308 acc.isValueOn(mDDA.
voxel())) {
309 if (t.t0<0) t.t0 = mDDA.
time();
310 }
else if (t.t0>=0) {
312 if (t.valid())
return true;
315 }
while (mDDA.
step());
316 if (t.t0>=0) t.t1 = mDDA.
maxTime();
320 template <
typename AccessorT,
typename ListT>
321 void hits(RayT& ray, AccessorT &acc, ListT& times,
TimeSpanT& t)
325 if (acc.template probeConstNode<LeafT>(mDDA.
voxel()) ||
326 acc.isValueOn(mDDA.
voxel())) {
327 if (t.t0<0) t.t0 = mDDA.
time();
328 }
else if (t.t0>=0) {
330 if (t.valid()) times.push_back(t);
333 }
while (mDDA.
step());
334 if (t.t0>=0) t.t1 = mDDA.
maxTime();
336 math::DDA<RayT, LeafT::TOTAL> mDDA;
343 #endif // OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
void hits(RayT &ray, AccessorT &acc, ListT ×)
void init(const RayT &ray)
TimeSpanT march(RayT &ray, AccessorT &acc)
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
DDA()
uninitialized constructor
typename RayT::RealType RealType
static bool test(TesterT &tester)
RealType next() const
Return the time (parameterized along the Ray) of the second (i.e. next) hit of a tree node of size 2^...
#define OPENVDB_USE_VERSION_NAMESPACE
typename RayT::Vec3Type Vec3Type
A Digital Differential Analyzer specialized for OpenVDB grids.
typename ChainT::template Get< ChildNodeLevel > NodeT
void init(const RayT &ray, RealT startTime, RealT maxTime)
Signed (x, y, z) 32-bit integer coordinates.
typename RayType::TimeSpan TimeSpanT
typename openvdb::OPENVDB_VERSION_NAME::tree::Tree::RootNodeType::NodeChainType ChainT
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
void print(std::ostream &os=std::cout) const
Print information about this DDA for debugging.
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
DDA(const RayT &ray, RealT startTime)
typename ChainT::template Get< NodeLevel > NodeT
bool step()
Increment the voxel index to next intersected voxel or node and returns true if the step in time does...
DDA(const RayT &ray, RealT startTime, RealT maxTime)
typename TreeT::LeafNodeType LeafT
TimeSpanT march(RayT &ray, AccessorT &acc)
void init(const RayT &ray, RealT startTime)
RealType time() const
Return the time (parameterized along the Ray) of the first hit of a tree node of size 2^Log2Dim...
typename RayT::TimeSpan TimeSpanT
Helper class that implements Hierarchical Digital Differential Analyzers and is specialized for ray i...
RealType maxTime() const
Return the maximum time (parameterized along the Ray).
void hits(RayT &ray, AccessorT &acc, ListT ×)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
typename TreeT::RootNodeType::NodeChainType ChainT
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
const Coord & voxel() const
Return the index coordinates of the next node or voxel intersected by the ray. If Log2Dim = 0 the ret...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Helper class that implements Hierarchical Digital Differential Analyzers for ray intersections agains...
static bool test(TesterT &tester)