37 #ifndef OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
44 #include <boost/type_traits/is_floating_point.hpp>
52 template<
typename RealT =
double>
56 BOOST_STATIC_ASSERT(boost::is_floating_point<RealT>::value);
65 : mEye(eye), mDir(direction), mInvDir(1/mDir), mT0(t0), mT1(t1)
77 inline void setMinTime(RealT t0) { assert(t0>0); mT0 = t0; }
79 inline void setMaxTime(RealT t1) { assert(t1>0); mT1 = t1; }
81 inline void setTimes(RealT t0, RealT t1) { assert(t0>0 && t1>0);mT0 = t0; mT1 = t1; }
89 this->setDir(direction);
90 this->setTimes(t0, t1);
99 inline RealT
t0()
const {
return mT0;}
101 inline RealT
t1()
const {
return mT1;}
113 inline Vec3R mid()
const {
return (*
this)(0.5*(mT0+mT1)); }
116 inline bool test()
const {
return (mT0 < mT1); }
119 inline bool test(RealT time)
const {
return (time>=mT0 && time<=mT1); }
127 template<
typename MapType>
130 assert(map.isLinear());
132 const Vec3T eye = map.applyMap(mEye);
133 const Vec3T dir = map.applyJacobian(mDir);
134 const RealT length = dir.
length();
135 return Ray(eye, dir/length, length*mT0, length*mT1);
144 template<
typename MapType>
147 assert(map.isLinear());
149 const Vec3T eye = map.applyInverseMap(mEye);
150 const Vec3T dir = map.applyInverseJacobian(mDir);
151 const RealT length = dir.
length();
152 return Ray(eye, dir/length, length*mT0, length*mT1);
157 template<
typename Gr
idType>
160 return this->applyMap(*(grid.transform().baseMap()));
165 template<
typename Gr
idType>
168 return this->applyInverseMap(*(grid.transform().baseMap()));
180 const Vec3T origin = mEye - center;
182 const RealT B = 2 * mDir.dot(origin);
183 const RealT C = origin.
lengthSqr() - radius * radius;
184 const RealT D = B * B - 4 * A * C;
186 if (D < 0)
return false;
188 const RealT Q = RealT(-0.5)*(B<0 ? (B +
Sqrt(D)) : (B -
Sqrt(D)));
193 if (t0 > t1) std::swap(t0, t1);
194 if (t0 < mT0) t0 = mT0;
195 if (t1 > mT1) t1 = mT1;
205 return this->intersects(center, radius, t0, t1)>0;
215 const bool hit = this->intersects(center, radius, t0, t1);
230 template<
typename BBoxT>
231 inline bool intersects(
const BBoxT& bbox, RealT& t0, RealT& t1)
const
235 for (
size_t i = 0; i < 3; ++i) {
236 RealT a = (bbox.min()[i] - mEye[i]) * mInvDir[i];
237 RealT b = (bbox.max()[i] - mEye[i]) * mInvDir[i];
238 if (a > b) std::swap(a, b);
241 if (t0 > t1)
return false;
248 template<
typename BBoxT>
252 return this->intersects(bbox, t0, t1);
258 template<
typename BBoxT>
259 inline bool clip(
const BBoxT& bbox)
262 const bool hit = this->intersects(bbox, t0, t1);
277 const RealT cosAngle = mDir.dot(normal);
279 t = (distance - mEye.dot(normal))/cosAngle;
280 return this->test(t);
290 return this->intersects(normal, point.
dot(normal), t);
294 Vec3T mEye, mDir, mInvDir;
300 template<
typename RealT>
301 inline std::ostream& operator<<(std::ostream& os, const Ray<RealT>& r)
303 os <<
"eye=" << r.eye() <<
" dir=" << r.dir() <<
" 1/dir="<<r.invDir()
304 <<
" t0=" << r.t0() <<
" t1=" << r.t1();
320 template<
typename RayT, Index Log2Dim = 0>
329 DDA(
const RayT& ray) { this->init(ray, ray.t0(), ray.t1()); }
331 DDA(
const RayT& ray,
RealT startTime) { this->init(ray, startTime, ray.t1()); }
333 DDA(
const RayT& ray,
RealT startTime,
RealT maxTime) { this->init(ray, startTime, maxTime); }
337 assert(startTime <= maxTime);
338 static const int DIM = 1 << Log2Dim;
341 const Vec3T &pos = ray(mT0), &dir = ray.dir(), &inv = ray.invDir();
342 mVoxel = Coord::floor(pos) & (~(DIM-1));
343 for (
size_t axis = 0; axis < 3; ++axis) {
348 }
else if (inv[axis] > 0) {
350 mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
351 mDelta[axis] = mStep[axis] * inv[axis];
354 mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
355 mDelta[axis] = mStep[axis] * inv[axis];
365 mT0 = mNext[stepAxis];
366 mNext[stepAxis] += mDelta[stepAxis];
367 mVoxel[stepAxis] += mStep[stepAxis];
392 void print(std::ostream& os = std::cout)
const
394 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << mT0 <<
" next()="
395 << this->next() <<
" voxel=" << mVoxel <<
" next=" << mNext
396 <<
" delta=" << mDelta <<
" step=" << mStep << std::endl;
407 template<
typename RayT, Index Log2Dim>
408 inline std::ostream& operator<<(std::ostream& os, const DDA<RayT, Log2Dim>& dda)
410 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << dda.time()
411 <<
" next()=" << dda.next() <<
" voxel=" << dda.voxel();
419 #endif // OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
const Vec3T & dir() const
Definition: Ray.h:95
Vec3R end() const
Return the endpoint of the ray.
Definition: Ray.h:110
bool intersects(const BBoxT &bbox) const
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:249
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:166
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:295
const Vec3T & eye() const
Definition: Ray.h:93
RealT t1() const
Definition: Ray.h:101
bool test(RealT time) const
Return true if time is within t0 and t1, both inclusive.
Definition: Ray.h:119
RayT::Vec3Type Vec3Type
Definition: Ray.h:326
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Vec3Type Vec3T
Definition: Ray.h:59
Ray(const Vec3Type &eye=Vec3Type(0, 0, 0), const Vec3Type &direction=Vec3Type(1, 0, 0), RealT t0=math::Delta< RealT >::value(), RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:61
bool intersects(const Vec3T ¢er, RealT radius) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:202
void scaleTimes(RealT scale)
Definition: Ray.h:83
Delta for small floating-point offsets.
Definition: Math.h:123
DDA(const RayT &ray, RealT startTime)
Definition: Ray.h:331
bool clip(const BBoxT &bbox)
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:259
MatType scale(const Vec3< typename MatType::value_type > &scaling)
Definition: Mat.h:595
void setMaxTime(RealT t1)
Definition: Ray.h:79
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:655
Vec3R start() const
Return the starting point of the ray.
Definition: Ray.h:107
void setEye(const Vec3Type &eye)
Definition: Ray.h:69
RayT::RealType RealType
Definition: Ray.h:324
bool intersects(const Vec3T ¢er, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:178
RealT RealType
Definition: Ray.h:57
Vec3< Real > Vec3Type
Definition: Ray.h:58
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:388
#define OPENVDB_VERSION_NAME
Definition: version.h:45
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:158
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:47
const Vec3T & invDir() const
Definition: Ray.h:97
bool test() const
Return true if t0 is strictly less then t1.
Definition: Ray.h:116
Vec3R operator()(RealT time) const
Return the position along the ray at the specified time.
Definition: Ray.h:104
DDA(const RayT &ray, RealT startTime, RealT maxTime)
Definition: Ray.h:333
DDA(const RayT &ray)
Definition: Ray.h:329
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:376
void reset(const Vec3Type &eye, const Vec3Type &direction, RealT t0=0, RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:85
bool step()
Increment the voxel index to next intersected voxel or node and returns true if the step in time does...
Definition: Ray.h:362
T length() const
Length of the vector.
Definition: Vec3.h:208
void setDir(const Vec3Type &dir)
Definition: Ray.h:71
T lengthSqr() const
Definition: Vec3.h:219
size_t MinIndex(const Vec3T &v)
Return the index [0,1,2] of the smallest value in a 3D vector.
Definition: Math.h:803
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
bool intersects(const BBoxT &bbox, RealT &t0, RealT &t1) const
Return true if the Ray intersects the specified axisaligned bounding box.
Definition: Ray.h:231
A Digital Differential Analyzer specialized for OpenVDB grids.
Definition: Ray.h:321
Ray applyMap(const MapType &map) const
Return a new Ray that is transformed with the specified map.
Definition: Ray.h:128
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:275
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:383
void init(const RayT &ray, RealT startTime, RealT maxTime)
Definition: Ray.h:335
Vec3R mid() const
Return the midpoint of the ray.
Definition: Ray.h:113
const Type & Min(const Type &a, const Type &b)
Return the minimum of two values.
Definition: Math.h:575
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:283
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
RealT t0() const
Definition: Ray.h:99
Ray applyInverseMap(const MapType &map) const
Return a new Ray that is transformed with the inverse of the specified map.
Definition: Ray.h:145
void setTimes(RealT t0, RealT t1)
Definition: Ray.h:81
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
RealType RealT
Definition: Ray.h:325
void print(std::ostream &os=std::cout) const
Print information about this DDA for debugging.
Definition: Ray.h:392
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:199
Vec3Type Vec3T
Definition: Ray.h:327
bool clip(const Vec3T ¢er, RealT radius)
Return true if this ray intersects the specified sphere.
Definition: Ray.h:212
void setMinTime(RealT t0)
Definition: Ray.h:77
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:288