HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DDA.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 //
31 /// @file DDA.h
32 ///
33 /// @author Ken Museth
34 ///
35 /// @brief Digital Differential Analyzers specialized for VDB.
36 
37 #ifndef OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
39 
40 #include "Coord.h"
41 #include "Math.h"
42 #include "Vec3.h"
43 #include <iostream>// for std::ostream
44 #include <limits>// for std::numeric_limits<Type>::max()
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace math {
50 
51 /// @brief A Digital Differential Analyzer specialized for OpenVDB grids
52 /// @note Conceptually similar to Bresenham's line algorithm applied
53 /// to a 3D Ray intersecting OpenVDB nodes or voxels. Log2Dim = 0
54 /// corresponds to a voxel and Log2Dim a tree node of size 2^Log2Dim.
55 ///
56 /// @note The Ray template class is expected to have the following
57 /// methods: test(time), t0(), t1(), invDir(), and operator()(time).
58 /// See the example Ray class above for their definition.
59 template<typename RayT, Index Log2Dim = 0>
60 class DDA
61 {
62 public:
63  typedef typename RayT::RealType RealType;
64  typedef RealType RealT;
65  typedef typename RayT::Vec3Type Vec3Type;
66  typedef Vec3Type Vec3T;
67 
68  /// @brief uninitialized constructor
69  DDA() {}
70 
71  DDA(const RayT& ray) { this->init(ray); }
72 
73  DDA(const RayT& ray, RealT startTime) { this->init(ray, startTime); }
74 
75  DDA(const RayT& ray, RealT startTime, RealT maxTime) { this->init(ray, startTime, maxTime); }
76 
77  inline void init(const RayT& ray, RealT startTime, RealT maxTime)
78  {
79  assert(startTime <= maxTime);
80  static const int DIM = 1 << Log2Dim;
81  mT0 = startTime;
82  mT1 = maxTime;
83  const Vec3T &pos = ray(mT0), &dir = ray.dir(), &inv = ray.invDir();
84  mVoxel = Coord::floor(pos) & (~(DIM-1));
85  for (int axis = 0; axis < 3; ++axis) {
86  if (math::isZero(dir[axis])) {//handles dir = +/- 0
87  mStep[axis] = 0;//dummy value
88  mNext[axis] = std::numeric_limits<RealT>::max();//i.e. disabled!
89  mDelta[axis] = std::numeric_limits<RealT>::max();//dummy value
90  } else if (inv[axis] > 0) {
91  mStep[axis] = DIM;
92  mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
93  mDelta[axis] = mStep[axis] * inv[axis];
94  } else {
95  mStep[axis] = -DIM;
96  mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
97  mDelta[axis] = mStep[axis] * inv[axis];
98  }
99  }
100  }
101 
102  inline void init(const RayT& ray) { this->init(ray, ray.t0(), ray.t1()); }
103 
104  inline void init(const RayT& ray, RealT startTime) { this->init(ray, startTime, ray.t1()); }
105 
106  /// @brief Increment the voxel index to next intersected voxel or node
107  /// and returns true if the step in time does not exceed maxTime.
108  inline bool step()
109  {
110  const int stepAxis = static_cast<int>(math::MinIndex(mNext));
111  mT0 = mNext[stepAxis];
112  mNext[stepAxis] += mDelta[stepAxis];
113  mVoxel[stepAxis] += mStep[stepAxis];
114  return mT0 <= mT1;
115  }
116 
117  /// @brief Return the index coordinates of the next node or voxel
118  /// intersected by the ray. If Log2Dim = 0 the return value is the
119  /// actual signed coordinate of the voxel, else it is the origin
120  /// of the corresponding VDB tree node or tile.
121  /// @note Incurs no computational overhead.
122  inline const Coord& voxel() const { return mVoxel; }
123 
124  /// @brief Return the time (parameterized along the Ray) of the
125  /// first hit of a tree node of size 2^Log2Dim.
126  /// @details This value is initialized to startTime or ray.t0()
127  /// depending on the constructor used.
128  /// @note Incurs no computational overhead.
129  inline RealType time() const { return mT0; }
130 
131  /// @brief Return the maximum time (parameterized along the Ray).
132  inline RealType maxTime() const { return mT1; }
133 
134  /// @brief Return the time (parameterized along the Ray) of the
135  /// second (i.e. next) hit of a tree node of size 2^Log2Dim.
136  /// @note Incurs a (small) computational overhead.
137  inline RealType next() const { return math::Min(mT1, mNext[0], mNext[1], mNext[2]); }
138 
139  /// @brief Print information about this DDA for debugging.
140  /// @param os a stream to which to write textual information.
141  void print(std::ostream& os = std::cout) const
142  {
143  os << "Dim=" << (1<<Log2Dim) << " time=" << mT0 << " next()="
144  << this->next() << " voxel=" << mVoxel << " next=" << mNext
145  << " delta=" << mDelta << " step=" << mStep << std::endl;
146  }
147 
148 private:
149  RealT mT0, mT1;
150  Coord mVoxel, mStep;
151  Vec3T mDelta, mNext;
152 }; // class DDA
153 
154 /// @brief Output streaming of the Ray class.
155 /// @note Primarily intended for debugging.
156 template<typename RayT, Index Log2Dim>
157 inline std::ostream& operator<<(std::ostream& os, const DDA<RayT, Log2Dim>& dda)
158 {
159  os << "Dim=" << (1<<Log2Dim) << " time=" << dda.time()
160  << " next()=" << dda.next() << " voxel=" << dda.voxel();
161  return os;
162 }
163 
164 /////////////////////////////////////////// LevelSetHDDA ////////////////////////////////////////////
165 
166 
167 /// @brief Helper class that implements Hierarchical Digital Differential Analyzers
168 /// and is specialized for ray intersections with level sets
169 template<typename TreeT, int NodeLevel>
171 {
172  typedef typename TreeT::RootNodeType::NodeChainType ChainT;
173  typedef typename hboost::mpl::at<ChainT, hboost::mpl::int_<NodeLevel> >::type NodeT;
174 
175  template <typename TesterT>
176  static bool test(TesterT& tester)
177  {
179  do {
180  if (tester.template hasNode<NodeT>(dda.voxel())) {
181  tester.setRange(dda.time(), dda.next());
182  if (LevelSetHDDA<TreeT, NodeLevel-1>::test(tester)) return true;
183  }
184  } while(dda.step());
185  return false;
186  }
187 };
188 
189 /// @brief Specialization of Hierarchical Digital Differential Analyzer
190 /// class that intersects a ray against the voxels of a level set
191 template<typename TreeT>
192 struct LevelSetHDDA<TreeT, -1>
193 {
194  template <typename TesterT>
195  static bool test(TesterT& tester)
196  {
197  math::DDA<typename TesterT::RayT, 0> dda(tester.ray());
198  tester.init(dda.time());
199  do { if (tester(dda.voxel(), dda.next())) return true; } while(dda.step());
200  return false;
201  }
202 };
203 
204 //////////////////////////////////////////// VolumeHDDA /////////////////////////////////////////////
205 
206 /// @brief Helper class that implements Hierarchical Digital Differential Analyzers
207 /// for ray intersections against a generic volume.
208 ///
209 /// @details The template argument ChildNodeLevel specifies the entry
210 /// upper node level used for the hierarchical ray-marching. The final
211 /// lowest level is always the leaf node level, i.e. not the voxel level!
212 template <typename TreeT, typename RayT, int ChildNodeLevel>
214 {
215 public:
216 
217  typedef typename TreeT::RootNodeType::NodeChainType ChainT;
218  typedef typename hboost::mpl::at<ChainT, hboost::mpl::int_<ChildNodeLevel> >::type NodeT;
219  typedef typename RayT::TimeSpan TimeSpanT;
220 
222 
223  template <typename AccessorT>
224  TimeSpanT march(RayT& ray, AccessorT &acc)
225  {
226  TimeSpanT t(-1, -1);
227  if (ray.valid()) this->march(ray, acc, t);
228  return t;
229  }
230 
231  /// ListType is a list of RayType::TimeSpan and is required to
232  /// have the two methods: clear() and push_back(). Thus, it could
233  /// be std::vector<typename RayType::TimeSpan> or
234  /// std::deque<typename RayType::TimeSpan>.
235  template <typename AccessorT, typename ListT>
236  void hits(RayT& ray, AccessorT &acc, ListT& times)
237  {
238  TimeSpanT t(-1,-1);
239  times.clear();
240  this->hits(ray, acc, times, t);
241  if (t.valid()) times.push_back(t);
242  }
243 
244 private:
245 
246  friend class VolumeHDDA<TreeT, RayT, ChildNodeLevel+1>;
247 
248  template <typename AccessorT>
249  bool march(RayT& ray, AccessorT &acc, TimeSpanT& t)
250  {
251  mDDA.init(ray);
252  do {
253  if (acc.template probeConstNode<NodeT>(mDDA.voxel()) != NULL) {//child node
254  ray.setTimes(mDDA.time(), mDDA.next());
255  if (mHDDA.march(ray, acc, t)) return true;//terminate
256  } else if (acc.isValueOn(mDDA.voxel())) {//hit an active tile
257  if (t.t0<0) t.t0 = mDDA.time();//this is the first hit so set t0
258  } else if (t.t0>=0) {//hit an inactive tile after hitting active values
259  t.t1 = mDDA.time();//set end of active ray segment
260  if (t.valid()) return true;//terminate
261  t.set(-1, -1);//reset to an empty and invalid time-span
262  }
263  } while (mDDA.step());
264  if (t.t0>=0) t.t1 = mDDA.maxTime();
265  return false;
266  }
267 
268  /// ListType is a list of RayType::TimeSpan and is required to
269  /// have the two methods: clear() and push_back(). Thus, it could
270  /// be std::vector<typename RayType::TimeSpan> or
271  /// std::deque<typename RayType::TimeSpan>.
272  template <typename AccessorT, typename ListT>
273  void hits(RayT& ray, AccessorT &acc, ListT& times, TimeSpanT& t)
274  {
275  mDDA.init(ray);
276  do {
277  if (acc.template probeConstNode<NodeT>(mDDA.voxel()) != NULL) {//child node
278  ray.setTimes(mDDA.time(), mDDA.next());
279  mHDDA.hits(ray, acc, times, t);
280  } else if (acc.isValueOn(mDDA.voxel())) {//hit an active tile
281  if (t.t0<0) t.t0 = mDDA.time();//this is the first hit so set t0
282  } else if (t.t0>=0) {//hit an inactive tile after hitting active values
283  t.t1 = mDDA.time();//set end of active ray segment
284  if (t.valid()) times.push_back(t);
285  t.set(-1,-1);//reset to an empty and invalid time-span
286  }
287  } while (mDDA.step());
288  if (t.t0>=0) t.t1 = mDDA.maxTime();
289  }
290 
291  math::DDA<RayT, NodeT::TOTAL> mDDA;
292  VolumeHDDA<TreeT, RayT, ChildNodeLevel-1> mHDDA;
293 };
294 
295 /// @brief Specialization of Hierarchical Digital Differential Analyzer
296 /// class that intersects against the leafs or tiles of a generic volume.
297 template <typename TreeT, typename RayT>
298 class VolumeHDDA<TreeT, RayT, 0>
299 {
300 public:
301 
302  typedef typename TreeT::LeafNodeType LeafT;
303  typedef typename RayT::TimeSpan TimeSpanT;
304 
306 
307  template <typename AccessorT>
308  TimeSpanT march(RayT& ray, AccessorT &acc)
309  {
310  TimeSpanT t(-1, -1);
311  if (ray.valid()) this->march(ray, acc, t);
312  return t;
313  }
314 
315  template <typename AccessorT, typename ListT>
316  void hits(RayT& ray, AccessorT &acc, ListT& times)
317  {
318  TimeSpanT t(-1,-1);
319  times.clear();
320  this->hits(ray, acc, times, t);
321  if (t.valid()) times.push_back(t);
322  }
323 
324 private:
325 
326  friend class VolumeHDDA<TreeT, RayT, 1>;
327 
328  template <typename AccessorT>
329  bool march(RayT& ray, AccessorT &acc, TimeSpanT& t)
330  {
331  mDDA.init(ray);
332  do {
333  if (acc.template probeConstNode<LeafT>(mDDA.voxel()) ||
334  acc.isValueOn(mDDA.voxel())) {//hit a leaf or an active tile
335  if (t.t0<0) t.t0 = mDDA.time();//this is the first hit
336  } else if (t.t0>=0) {//hit an inactive tile after hitting active values
337  t.t1 = mDDA.time();//set end of active ray segment
338  if (t.valid()) return true;//terminate
339  t.set(-1, -1);//reset to an empty and invalid time-span
340  }
341  } while (mDDA.step());
342  if (t.t0>=0) t.t1 = mDDA.maxTime();
343  return false;
344  }
345 
346  template <typename AccessorT, typename ListT>
347  void hits(RayT& ray, AccessorT &acc, ListT& times, TimeSpanT& t)
348  {
349  mDDA.init(ray);
350  do {
351  if (acc.template probeConstNode<LeafT>(mDDA.voxel()) ||
352  acc.isValueOn(mDDA.voxel())) {//hit a leaf or an active tile
353  if (t.t0<0) t.t0 = mDDA.time();//this is the first hit
354  } else if (t.t0>=0) {//hit an inactive tile after hitting active values
355  t.t1 = mDDA.time();//set end of active ray segment
356  if (t.valid()) times.push_back(t);
357  t.set(-1, -1);//reset to an empty and invalid time-span
358  }
359  } while (mDDA.step());
360  if (t.t0>=0) t.t1 = mDDA.maxTime();
361  }
362  math::DDA<RayT, LeafT::TOTAL> mDDA;
363 };
364 
365 } // namespace math
366 } // namespace OPENVDB_VERSION_NAME
367 } // namespace openvdb
368 
369 #endif // OPENVDB_MATH_DDA_HAS_BEEN_INCLUDED
370 
371 // Copyright (c) 2012-2017 DreamWorks Animation LLC
372 // All rights reserved. This software is distributed under the
373 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
void hits(RayT &ray, AccessorT &acc, ListT &times)
Definition: DDA.h:236
void init(const RayT &ray)
Definition: DDA.h:102
TimeSpanT march(RayT &ray, AccessorT &acc)
Definition: DDA.h:224
DDA()
uninitialized constructor
Definition: DDA.h:69
static bool test(TesterT &tester)
Definition: DDA.h:176
RealType next() const
Return the time (parameterized along the Ray) of the second (i.e. next) hit of a tree node of size 2^...
Definition: DDA.h:137
A Digital Differential Analyzer specialized for OpenVDB grids.
Definition: DDA.h:60
void init(const RayT &ray, RealT startTime, RealT maxTime)
Definition: DDA.h:77
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:48
TreeT::RootNodeType::NodeChainType ChainT
Definition: DDA.h:217
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
hboost::mpl::at< ChainT, hboost::mpl::int_< NodeLevel > >::type NodeT
Definition: DDA.h:173
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:890
hboost::mpl::at< ChainT, hboost::mpl::int_< ChildNodeLevel > >::type NodeT
Definition: DDA.h:218
TreeT::RootNodeType::NodeChainType ChainT
Definition: DDA.h:172
void print(std::ostream &os=std::cout) const
Print information about this DDA for debugging.
Definition: DDA.h:141
#define OPENVDB_VERSION_NAME
Definition: version.h:43
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
DDA(const RayT &ray, RealT startTime)
Definition: DDA.h:73
bool step()
Increment the voxel index to next intersected voxel or node and returns true if the step in time does...
Definition: DDA.h:108
DDA(const RayT &ray, RealT startTime, RealT maxTime)
Definition: DDA.h:75
TimeSpanT march(RayT &ray, AccessorT &acc)
Definition: DDA.h:308
void init(const RayT &ray, RealT startTime)
Definition: DDA.h:104
RealType time() const
Return the time (parameterized along the Ray) of the first hit of a tree node of size 2^Log2Dim...
Definition: DDA.h:129
int floor(T x)
Definition: ImathFun.h:150
Helper class that implements Hierarchical Digital Differential Analyzers and is specialized for ray i...
Definition: DDA.h:170
RealType maxTime() const
Return the maximum time (parameterized along the Ray).
Definition: DDA.h:132
void hits(RayT &ray, AccessorT &acc, ListT &times)
Definition: DDA.h:316
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:622
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
const Coord & voxel() const
Return the index coordinates of the next node or voxel intersected by the ray. If Log2Dim = 0 the ret...
Definition: DDA.h:122
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
Helper class that implements Hierarchical Digital Differential Analyzers for ray intersections agains...
Definition: DDA.h:213