37 #ifndef OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
38 #define OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
51 template<
typename RealT =
double>
63 : mEye(eye), mDir(direction), mInvDir(1/mDir), mT0(t0), mT1(t1)
75 inline void setMinTime(RealT t0) { assert(t0>0); mT0 = t0; }
77 inline void setMaxTime(RealT t1) { assert(t1>0); mT1 = t1; }
79 inline void setTimes(RealT t0, RealT t1) { assert(t0>0 && t1>0);mT0 = t0; mT1 = t1; }
87 this->setDir(direction);
88 this->setTimes(t0, t1);
97 inline RealT
t0()
const {
return mT0;}
99 inline RealT
t1()
const {
return mT1;}
111 inline Vec3R mid()
const {
return (*
this)(0.5*(mT0+mT1)); }
114 inline bool test()
const {
return (mT0 < mT1); }
117 inline bool test(RealT time)
const {
return (time>=mT0 && time<=mT1); }
125 template<
typename MapType>
128 assert(map.isLinear());
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);
142 template<
typename MapType>
145 assert(map.isLinear());
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);
155 template<
typename Gr
idType>
158 return this->applyMap(*(grid.transform().baseMap()));
163 template<
typename Gr
idType>
166 return this->applyInverseMap(*(grid.transform().baseMap()));
178 const Vec3T origin = mEye - center;
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;
184 if (D < 0)
return false;
186 const RealT Q = RealT(-0.5)*(B<0 ? (B +
Sqrt(D)) : (B -
Sqrt(D)));
191 if (t0 > t1) std::swap(t0, t1);
192 if (t0 < mT0) t0 = mT0;
193 if (t1 > mT1) t1 = mT1;
203 return this->intersects(center, radius, t0, t1)>0;
213 const bool hit = this->intersects(center, radius, t0, t1);
228 template<
typename BBoxT>
229 inline bool intersects(
const BBoxT& bbox, RealT& t0, RealT& t1)
const
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);
239 if (t0 > t1)
return false;
246 template<
typename BBoxT>
250 return this->intersects(bbox, t0, t1);
256 template<
typename BBoxT>
257 inline bool clip(
const BBoxT& bbox)
260 const bool hit = this->intersects(bbox, t0, t1);
275 const RealT cosAngle = mDir.dot(normal);
277 t = (distance - mEye.dot(normal))/cosAngle;
278 return this->test(t);
288 return this->intersects(normal, point.
dot(normal), t);
292 Vec3T mEye, mDir, mInvDir;
298 template<
typename RealT>
299 inline std::ostream& operator<<(std::ostream& os, const Ray<RealT>& r)
301 os <<
"eye=" << r.eye() <<
" dir=" << r.dir() <<
" 1/dir="<<r.invDir()
302 <<
" t0=" << r.t0() <<
" t1=" << r.t1();
318 template<
typename RayT, Index Log2Dim = 0>
327 DDA(
const RayT& ray) { this->init(ray, ray.t0(), ray.t1()); }
329 DDA(
const RayT& ray,
RealT startTime) { this->init(ray, startTime, ray.t1()); }
331 DDA(
const RayT& ray,
RealT startTime,
RealT maxTime) { this->init(ray, startTime, maxTime); }
335 assert(startTime <= maxTime);
336 static const int DIM = 1 << Log2Dim;
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) {
346 }
else if (inv[axis] > 0) {
348 mNext[axis] = mT0 + (mVoxel[axis] + DIM - pos[axis]) * inv[axis];
349 mDelta[axis] = mStep[axis] * inv[axis];
352 mNext[axis] = mT0 + (mVoxel[axis] - pos[axis]) * inv[axis];
353 mDelta[axis] = mStep[axis] * inv[axis];
363 mT0 = mNext[stepAxis];
364 mNext[stepAxis] += mDelta[stepAxis];
365 mVoxel[stepAxis] += mStep[stepAxis];
390 void print(std::ostream& os = std::cout)
const
392 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << mT0 <<
" next()="
393 << this->next() <<
" voxel=" << mVoxel <<
" next=" << mNext
394 <<
" delta=" << mDelta <<
" step=" << mStep << std::endl;
405 template<
typename RayT, Index Log2Dim>
406 inline std::ostream& operator<<(std::ostream& os, const DDA<RayT, Log2Dim>& dda)
408 os <<
"Dim=" << (1<<Log2Dim) <<
" time=" << dda.time()
409 <<
" next()=" << dda.next() <<
" voxel=" << dda.voxel();
417 #endif // OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED
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
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 ¢er, 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 ¢er, 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 ¢er, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:176