10 #ifndef OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
11 #define OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
19 #include <boost/mpl/at.hpp>
34 template<
typename RayT, Index Log2Dim = 0>
40 using Vec3Type =
typename RayT::Vec3Type;
46 DDA(
const RayT& ray) { this->init(ray); }
48 DDA(
const RayT& ray,
RealT startTime) { this->init(ray, startTime); }
50 DDA(
const RayT& ray,
RealT startTime,
RealT maxTime) { this->init(ray, startTime, maxTime); }
54 assert(startTime <= maxTime);
55 static const int DIM = 1 << Log2Dim;
58 const Vec3T &pos = ray(mT0), &dir = ray.dir(), &inv = ray.invDir();
60 for (
int axis = 0; axis < 3; ++axis) {
65 }
else if (inv[axis] > 0) {
67 mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
68 mDelta[axis] = mStep[axis] * inv[axis];
71 mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
72 mDelta[axis] = mStep[axis] * inv[axis];
77 inline void init(
const RayT& ray) { this->
init(ray, ray.t0(), ray.t1()); }
79 inline void init(
const RayT& ray,
RealT startTime) { this->
init(ray, startTime, ray.t1()); }
86 mT0 = mNext[stepAxis];
87 mNext[stepAxis] += mDelta[stepAxis];
88 mVoxel[stepAxis] += mStep[stepAxis];
116 void print(std::ostream& os = std::cout)
const
118 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << mT0 <<
" next()="
119 << this->next() <<
" voxel=" << mVoxel <<
" next=" << mNext
120 <<
" delta=" << mDelta <<
" step=" << mStep << std::endl;
131 template<
typename RayT, Index Log2Dim>
134 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << dda.
time()
135 <<
" next()=" << dda.
next() <<
" voxel=" << dda.
voxel();
144 template<
typename TreeT,
int NodeLevel>
147 using ChainT =
typename TreeT::RootNodeType::NodeChainType;
148 using NodeT =
typename boost::mpl::at<ChainT, boost::mpl::int_<NodeLevel> >::type;
150 template <
typename TesterT>
151 static bool test(TesterT& tester)
155 if (tester.template hasNode<NodeT>(dda.
voxel())) {
156 tester.setRange(dda.
time(), dda.
next());
166 template<
typename TreeT>
169 template <
typename TesterT>
170 static bool test(TesterT& tester)
173 tester.init(dda.
time());
174 do {
if (tester(dda.
voxel(), dda.
next()))
return true; }
while(dda.
step());
187 template <
typename TreeT,
typename RayT,
int ChildNodeLevel>
192 using ChainT =
typename TreeT::RootNodeType::NodeChainType;
193 using NodeT =
typename boost::mpl::at<ChainT, boost::mpl::int_<ChildNodeLevel> >::type;
198 template <
typename AccessorT>
202 if (ray.valid()) this->march(ray, acc, t);
210 template <
typename AccessorT,
typename ListT>
211 void hits(RayT& ray, AccessorT &acc, ListT& times)
215 this->hits(ray, acc, times, t);
216 if (t.valid()) times.push_back(t);
221 friend class VolumeHDDA<TreeT, RayT, ChildNodeLevel+1>;
223 template <
typename AccessorT>
224 bool march(RayT& ray, AccessorT &acc, TimeSpanT& t)
228 if (acc.template probeConstNode<NodeT>(mDDA.voxel()) !=
nullptr) {
229 ray.setTimes(mDDA.time(), mDDA.next());
230 if (mHDDA.march(ray, acc, t))
return true;
231 }
else if (acc.isValueOn(mDDA.voxel())) {
232 if (t.t0<0) t.t0 = mDDA.time();
233 }
else if (t.t0>=0) {
235 if (t.valid())
return true;
238 }
while (mDDA.step());
239 if (t.t0>=0) t.t1 = mDDA.maxTime();
247 template <
typename AccessorT,
typename ListT>
248 void hits(RayT& ray, AccessorT &acc, ListT& times, TimeSpanT& t)
252 if (acc.template probeConstNode<NodeT>(mDDA.voxel()) !=
nullptr) {
253 ray.setTimes(mDDA.time(), mDDA.next());
254 mHDDA.
hits(ray, acc, times, t);
255 }
else if (acc.isValueOn(mDDA.voxel())) {
256 if (t.t0<0) t.t0 = mDDA.time();
257 }
else if (t.t0>=0) {
259 if (t.valid()) times.push_back(t);
262 }
while (mDDA.step());
263 if (t.t0>=0) t.t1 = mDDA.maxTime();
266 math::DDA<RayT, NodeT::TOTAL> mDDA;
267 VolumeHDDA<TreeT, RayT, ChildNodeLevel-1> mHDDA;
272 template <
typename TreeT,
typename RayT>
277 using LeafT =
typename TreeT::LeafNodeType;
282 template <
typename AccessorT>
286 if (ray.valid()) this->march(ray, acc, t);
290 template <
typename AccessorT,
typename ListT>
291 void hits(RayT& ray, AccessorT &acc, ListT& times)
295 this->hits(ray, acc, times, t);
296 if (t.valid()) times.push_back(t);
303 template <
typename AccessorT>
304 bool march(RayT& ray, AccessorT &acc, TimeSpanT& t)
308 if (acc.template probeConstNode<LeafT>(mDDA.voxel()) ||
309 acc.isValueOn(mDDA.voxel())) {
310 if (t.t0<0) t.t0 = mDDA.time();
311 }
else if (t.t0>=0) {
313 if (t.valid())
return true;
316 }
while (mDDA.step());
317 if (t.t0>=0) t.t1 = mDDA.maxTime();
321 template <
typename AccessorT,
typename ListT>
322 void hits(RayT& ray, AccessorT &acc, ListT& times, TimeSpanT& t)
326 if (acc.template probeConstNode<LeafT>(mDDA.voxel()) ||
327 acc.isValueOn(mDDA.voxel())) {
328 if (t.t0<0) t.t0 = mDDA.time();
329 }
else if (t.t0>=0) {
331 if (t.valid()) times.push_back(t);
334 }
while (mDDA.step());
335 if (t.t0>=0) t.t1 = mDDA.maxTime();
337 math::DDA<RayT, LeafT::TOTAL> mDDA;
344 #endif // OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED