OpenVDB  2.0.0
Ray.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 //
30 //
36 
37 #ifndef OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
39 
40 #include "Math.h"
41 #include "Vec3.h"
42 #include "Transform.h"
43 #include <iostream> // for std::ostream
44 
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace math {
50 
51 template<typename RealT = double>
52 class Ray
53 {
54 public:
55  typedef RealT RealType;
57  typedef Vec3Type Vec3T;
58 
59  Ray(const Vec3Type& eye = Vec3Type(0,0,0),
60  const Vec3Type& direction = Vec3Type(1,0,0),
61  RealT t0 = 1e-3,
63  : mEye(eye), mDir(direction), mInvDir(1/mDir), mT0(t0), mT1(t1)
64  {
65  }
66 
67  inline void setEye(const Vec3Type& eye) { mEye = eye; }
68 
69  inline void setDir(const Vec3Type& dir)
70  {
71  mDir = dir;
72  mInvDir = 1/mDir;
73  }
74 
75  inline void setMinTime(RealT t0) { assert(t0>0); mT0 = t0; }
76 
77  inline void setMaxTime(RealT t1) { assert(t1>0); mT1 = t1; }
78 
79  inline void setTimes(RealT t0, RealT t1) { assert(t0>0 && t1>0);mT0 = t0; mT1 = t1; }
80 
81  inline void scaleTimes(RealT scale) { assert(scale>0); mT0 *= scale; mT1 *= scale; }
82 
83  inline void reset(const Vec3Type& eye, const Vec3Type& direction,
84  RealT t0 = 0, RealT t1 = std::numeric_limits<RealT>::max())
85  {
86  this->setEye(eye);
87  this->setDir(direction);
88  this->setTimes(t0, t1);
89  }
90 
91  inline const Vec3T& eye() const {return mEye;}
92 
93  inline const Vec3T& dir() const {return mDir;}
94 
95  inline const Vec3T& invDir() const {return mInvDir;}
96 
97  inline RealT t0() const {return mT0;}
98 
99  inline RealT t1() const {return mT1;}
100 
102  inline Vec3R operator()(RealT time) const { return mEye + mDir * time; }
103 
105  inline Vec3R start() const { return (*this)(mT0); }
106 
108  inline Vec3R end() const { return (*this)(mT1); }
109 
111  inline Vec3R mid() const { return (*this)(0.5*(mT0+mT1)); }
112 
114  inline bool test() const { return (mT0 < mT1); }
115 
117  inline bool test(RealT time) const { return (time>=mT0 && time<=mT1); }
118 
125  template<typename MapType>
126  inline Ray applyMap(const MapType& map) const
127  {
128  assert(map.isLinear());
129  assert(math::isApproxEqual(mDir.length(), RealT(1)));
130  const Vec3T eye = map.applyMap(mEye);
131  const Vec3T dir = map.applyJacobian(mDir);
132  const RealT length = dir.length();
133  return Ray(eye, dir/length, length*mT0, length*mT1);
134  }
135 
142  template<typename MapType>
143  inline Ray applyInverseMap(const MapType& map) const
144  {
145  assert(map.isLinear());
146  assert(math::isApproxEqual(mDir.length(), RealT(1)));
147  const Vec3T eye = map.applyInverseMap(mEye);
148  const Vec3T dir = map.applyInverseJacobian(mDir);
149  const RealT length = dir.length();
150  return Ray(eye, dir/length, length*mT0, length*mT1);
151  }
152 
155  template<typename GridType>
156  inline Ray indexToWorld(const GridType& grid) const
157  {
158  return this->applyMap(*(grid.transform().baseMap()));
159  }
160 
163  template<typename GridType>
164  inline Ray worldToIndex(const GridType& grid) const
165  {
166  return this->applyInverseMap(*(grid.transform().baseMap()));
167  }
168 
176  inline bool intersects(const Vec3T& center, RealT radius, RealT& t0, RealT& t1) const
177  {
178  const Vec3T origin = mEye - center;
179  const RealT A = mDir.lengthSqr();
180  const RealT B = 2 * mDir.dot(origin);
181  const RealT C = origin.lengthSqr() - radius * radius;
182  const RealT D = B * B - 4 * A * C;
183 
184  if (D < 0) return false;
185 
186  const RealT Q = RealT(-0.5)*(B<0 ? (B + Sqrt(D)) : (B - Sqrt(D)));
187 
188  t0 = Q / A;
189  t1 = C / Q;
190 
191  if (t0 > t1) std::swap(t0, t1);
192  if (t0 < mT0) t0 = mT0;
193  if (t1 > mT1) t1 = mT1;
194  return t0 <= t1;
195  }
196 
200  inline bool intersects(const Vec3T& center, RealT radius) const
201  {
202  RealT t0, t1;
203  return this->intersects(center, radius, t0, t1)>0;
204  }
205 
210  inline bool clip(const Vec3T& center, RealT radius)
211  {
212  RealT t0, t1;
213  const bool hit = this->intersects(center, radius, t0, t1);
214  if (hit) {
215  mT0 = t0;
216  mT1 = t1;
217  }
218  return hit;
219  }
220 
228  template<typename BBoxT>
229  inline bool intersects(const BBoxT& bbox, RealT& t0, RealT& t1) const
230  {
231  t0 = mT0;
232  t1 = mT1;
233  for (size_t i = 0; i < 3; ++i) {
234  RealT a = (bbox.min()[i] - mEye[i]) * mInvDir[i];
235  RealT b = (bbox.max()[i] - mEye[i]) * mInvDir[i];
236  if (a > b) std::swap(a, b);
237  if (a > t0) t0 = a;
238  if (b < t1) t1 = b;
239  if (t0 > t1) return false;
240  }
241  return true;
242  }
243 
246  template<typename BBoxT>
247  inline bool intersects(const BBoxT& bbox) const
248  {
249  RealT t0, t1;
250  return this->intersects(bbox, t0, t1);
251  }
252 
256  template<typename BBoxT>
257  inline bool clip(const BBoxT& bbox)
258  {
259  RealT t0, t1;
260  const bool hit = this->intersects(bbox, t0, t1);
261  if (hit) {
262  mT0 = t0;
263  mT1 = t1;
264  }
265  return hit;
266  }
267 
273  inline bool intersects(const Vec3T& normal, RealT distance, RealT& t) const
274  {
275  const RealT cosAngle = mDir.dot(normal);
276  if (math::isApproxZero(cosAngle)) return false;//parallel
277  t = (distance - mEye.dot(normal))/cosAngle;
278  return this->test(t);
279  }
280 
286  inline bool intersects(const Vec3T& normal, const Vec3T& point, RealT& t) const
287  {
288  return this->intersects(normal, point.dot(normal), t);
289  }
290 
291 private:
292  Vec3T mEye, mDir, mInvDir;
293  RealT mT0, mT1;
294 }; // end of Ray class
295 
298 template<typename RealT>
299 inline std::ostream& operator<<(std::ostream& os, const Ray<RealT>& r)
300 {
301  os << "eye=" << r.eye() << " dir=" << r.dir() << " 1/dir="<<r.invDir()
302  << " t0=" << r.t0() << " t1=" << r.t1();
303  return os;
304 }
305 
306 
308 
309 
318 template<typename RayT, Index Log2Dim = 0>
319 class DDA
320 {
321 public:
322  typedef typename RayT::RealType RealType;
323  typedef RealType RealT;
324  typedef typename RayT::Vec3Type Vec3Type;
325  typedef Vec3Type Vec3T;
326 
327  DDA(const RayT& ray) { this->init(ray, ray.t0(), ray.t1()); }
328 
329  DDA(const RayT& ray, RealT startTime) { this->init(ray, startTime, ray.t1()); }
330 
331  DDA(const RayT& ray, RealT startTime, RealT maxTime) { this->init(ray, startTime, maxTime); }
332 
333  inline void init(const RayT& ray, RealT startTime, RealT maxTime)
334  {
335  assert(startTime <= maxTime);
336  static const int DIM = 1 << Log2Dim;
337  mT0 = startTime;
338  mT1 = maxTime;
339  const Vec3T &pos = ray(mT0), &dir = ray.dir(), &inv = ray.invDir();
340  mVoxel = Coord::floor(pos) & (~(DIM-1));
341  for (size_t axis = 0; axis < 3; ++axis) {
342  if (math::isZero(dir[axis])) {//handles dir = +/- 0
343  mStep[axis] = 0;//dummy value
344  mNext[axis] = std::numeric_limits<RealT>::max();//i.e. disabled!
345  mDelta[axis] = std::numeric_limits<RealT>::max();//dummy value
346  } else if (inv[axis] > 0) {
347  mStep[axis] = DIM;
348  mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
349  mDelta[axis] = mStep[axis] * inv[axis];
350  } else {
351  mStep[axis] = -DIM;
352  mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
353  mDelta[axis] = mStep[axis] * inv[axis];
354  }
355  }
356  }
357 
360  inline bool step()
361  {
362  const size_t stepAxis = math::MinIndex(mNext);
363  mT0 = mNext[stepAxis];
364  mNext[stepAxis] += mDelta[stepAxis];
365  mVoxel[stepAxis] += mStep[stepAxis];
366  return mT0 <= mT1;
367  }
368 
374  inline const Coord& voxel() const { return mVoxel; }
375 
381  inline RealType time() const { return mT0; }
382 
386  inline RealType next() const { return math::Min(mT1, mNext[0], mNext[1], mNext[2]); }
387 
390  void print(std::ostream& os = std::cout) const
391  {
392  os << "Dim=" << (1<<Log2Dim) << " time=" << mT0 << " next()="
393  << this->next() << " voxel=" << mVoxel << " next=" << mNext
394  << " delta=" << mDelta << " step=" << mStep << std::endl;
395  }
396 
397 private:
398  RealT mT0, mT1;
399  Coord mVoxel, mStep;
400  Vec3T mDelta, mNext;
401 }; // class DDA
402 
405 template<typename RayT, Index Log2Dim>
406 inline std::ostream& operator<<(std::ostream& os, const DDA<RayT, Log2Dim>& dda)
407 {
408  os << "Dim=" << (1<<Log2Dim) << " time=" << dda.time()
409  << " next()=" << dda.next() << " voxel=" << dda.voxel();
410  return os;
411 }
412 
413 } // namespace math
414 } // namespace OPENVDB_VERSION_NAME
415 } // namespace openvdb
416 
417 #endif // OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
418 
419 // Copyright (c) 2012-2013 DreamWorks Animation LLC
420 // All rights reserved. This software is distributed under the
421 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Vec3< Real > Vec3Type
Definition: Ray.h:56
bool test() const
Return true if t0 is strictly less then t1.
Definition: Ray.h:114
bool intersects(const Vec3T &normal, RealT distance, RealT &t) const
Return true if the Ray intersects the plane specified by a normal and distance from the origin...
Definition: Ray.h:273
RealType time() const
Return the time (parameterized along the Ray) of the first hit of a tree node of size 2^Log2Dim...
Definition: Ray.h:381
DDA(const RayT &ray, RealT startTime, RealT maxTime)
Definition: Ray.h:331
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:288
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
RealType RealT
Definition: Ray.h:323
void init(const RayT &ray, RealT startTime, RealT maxTime)
Definition: Ray.h:333
T length() const
Length of the vector.
Definition: Vec3.h:208
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:648
RealT t0() const
Definition: Ray.h:97
Definition: Ray.h:52
void setDir(const Vec3Type &dir)
Definition: Ray.h:69
const Vec3T & eye() const
Definition: Ray.h:91
DDA(const RayT &ray)
Definition: Ray.h:327
const Coord & voxel() const
Return the index coordinates of the next node or voxel intersected by the ray. If Log2Dim = 0 the ret...
Definition: Ray.h:374
RayT::Vec3Type Vec3Type
Definition: Ray.h:324
bool step()
Increment the voxel index to next intersected voxel or node and returns true if the step in time does...
Definition: Ray.h:360
void setTimes(RealT t0, RealT t1)
Definition: Ray.h:79
bool intersects(const BBoxT &bbox, RealT &t0, RealT &t1) const
Return true if the Ray intersects the specified axisaligned bounding box.
Definition: Ray.h:229
bool intersects(const BBoxT &bbox) const
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:247
A Digital Differential Analyzer specialized for OpenVDB grids.
Definition: Ray.h:319
Ray applyMap(const MapType &map) const
Return a new Ray that is transformed with the specified map.
Definition: Ray.h:126
#define OPENVDB_VERSION_NAME
Definition: version.h:45
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
RealT t1() const
Definition: Ray.h:99
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:276
void print(std::ostream &os=std::cout) const
Print information about this DDA for debugging.
Definition: Ray.h:390
bool clip(const Vec3T &center, RealT radius)
Return true if this ray intersects the specified sphere.
Definition: Ray.h:210
Ray applyInverseMap(const MapType &map) const
Return a new Ray that is transformed with the inverse of the specified map.
Definition: Ray.h:143
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:199
Vec3R start() const
Return the starting point of the ray.
Definition: Ray.h:105
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:796
RayT::RealType RealType
Definition: Ray.h:322
Vec3R end() const
Return the endpoint of the ray.
Definition: Ray.h:108
RealT RealType
Definition: Ray.h:55
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: Ray.h:386
bool test(RealT time) const
Return true if time is within t0 and t1, both inclusive.
Definition: Ray.h:117
Ray indexToWorld(const GridType &grid) const
Return a new ray in world space, assuming the existing ray is represented in the index space of the s...
Definition: Ray.h:156
Vec3R operator()(RealT time) const
Return the position along the ray at the specified time.
Definition: Ray.h:102
Vec3Type Vec3T
Definition: Ray.h:325
bool clip(const BBoxT &bbox)
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:257
MatType scale(const Vec3< typename MatType::value_type > &scaling)
Definition: Mat.h:595
void setMinTime(RealT t0)
Definition: Ray.h:75
bool intersects(const Vec3T &center, RealT radius) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:200
bool intersects(const Vec3T &normal, const Vec3T &point, RealT &t) const
Return true if the Ray intersects the plane specified by a normal and point.
Definition: Ray.h:286
const Vec3T & dir() const
Definition: Ray.h:93
void setEye(const Vec3Type &eye)
Definition: Ray.h:67
void scaleTimes(RealT scale)
Definition: Ray.h:81
Ray worldToIndex(const GridType &grid) const
Return a new ray in the index space of the specified grid, assuming the existing ray is represented i...
Definition: Ray.h:164
Ray(const Vec3Type &eye=Vec3Type(0, 0, 0), const Vec3Type &direction=Vec3Type(1, 0, 0), RealT t0=1e-3, RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:59
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:568
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
const Vec3T & invDir() const
Definition: Ray.h:95
DDA(const RayT &ray, RealT startTime)
Definition: Ray.h:329
Vec3Type Vec3T
Definition: Ray.h:57
void setMaxTime(RealT t1)
Definition: Ray.h:77
Vec3R mid() const
Return the midpoint of the ray.
Definition: Ray.h:111
T lengthSqr() const
Definition: Vec3.h:219
void reset(const Vec3Type &eye, const Vec3Type &direction, RealT t0=0, RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:83
bool intersects(const Vec3T &center, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:176