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 TimeSpan(RealT _t0, RealT _t1) : t0(_t0), t1(_t1) {}
67 inline void set(RealT _t0, RealT _t1) { t0=_t0; t1=_t1; }
69 inline void get(RealT& _t0, RealT& _t1)
const { _t0=t0; _t1=t1; }
73 inline RealT
mid()
const {
return 0.5*(t0 + t1); }
75 inline void scale(RealT s) {assert(s>0); t0*=s; t1*=s; }
77 inline bool test(RealT t)
const {
return (t>=t0 && t<=t1); }
80 Ray(
const Vec3Type& eye = Vec3Type(0,0,0),
81 const Vec3Type& direction = Vec3Type(1,0,0),
84 : mEye(eye), mDir(direction), mInvDir(1/mDir), mTimeSpan(t0, t1)
88 inline void setEye(
const Vec3Type& eye) { mEye = eye; }
90 inline void setDir(
const Vec3Type& dir)
96 inline void setMinTime(RealT t0) { assert(t0>0); mTimeSpan.t0 = t0; }
98 inline void setMaxTime(RealT t1) { assert(t1>0); mTimeSpan.t1 = t1; }
103 assert(t0>0 && t1>0);
104 mTimeSpan.set(t0, t1);
109 inline void reset(
const Vec3Type& eye,
110 const Vec3Type& direction,
115 this->setDir(direction);
116 this->setTimes(t0, t1);
119 inline const Vec3T&
eye()
const {
return mEye;}
121 inline const Vec3T&
dir()
const {
return mDir;}
123 inline const Vec3T&
invDir()
const {
return mInvDir;}
125 inline RealT
t0()
const {
return mTimeSpan.t0;}
127 inline RealT
t1()
const {
return mTimeSpan.t1;}
136 inline Vec3R end()
const {
return (*
this)(mTimeSpan.t1); }
139 inline Vec3R mid()
const {
return (*
this)(mTimeSpan.mid()); }
147 return mTimeSpan.valid(eps);
151 inline bool test(RealT time)
const {
return mTimeSpan.test(time); }
159 template<
typename MapType>
162 assert(map.isLinear());
164 const Vec3T eye = map.applyMap(mEye);
165 const Vec3T dir = map.applyJacobian(mDir);
166 const RealT length = dir.
length();
167 return Ray(eye, dir/length, length*mTimeSpan.t0, length*mTimeSpan.t1);
176 template<
typename MapType>
179 assert(map.isLinear());
181 const Vec3T eye = map.applyInverseMap(mEye);
182 const Vec3T dir = map.applyInverseJacobian(mDir);
183 const RealT length = dir.
length();
184 return Ray(eye, dir/length, length*mTimeSpan.t0, length*mTimeSpan.t1);
189 template<
typename Gr
idType>
192 return this->applyMap(*(grid.transform().baseMap()));
197 template<
typename Gr
idType>
200 return this->applyInverseMap(*(grid.transform().baseMap()));
210 inline bool intersects(
const Vec3T& center, RealT radius, RealT& t0, RealT& t1)
const 212 const Vec3T origin = mEye - center;
214 const RealT B = 2 * mDir.dot(origin);
215 const RealT C = origin.
lengthSqr() - radius * radius;
216 const RealT D = B * B - 4 * A * C;
218 if (D < 0)
return false;
220 const RealT Q = RealT(-0.5)*(B<0 ? (B +
Sqrt(D)) : (B -
Sqrt(D)));
225 if (t0 > t1) std::swap(t0, t1);
226 if (t0 < mTimeSpan.t0) t0 = mTimeSpan.t0;
227 if (t1 > mTimeSpan.t1) t1 = mTimeSpan.t1;
234 inline bool intersects(
const Vec3T& center, RealT radius)
const 237 return this->intersects(center, radius, t0, t1)>0;
244 inline bool clip(
const Vec3T& center, RealT radius)
247 const bool hit = this->intersects(center, radius, t0, t1);
248 if (hit) mTimeSpan.set(t0, t1);
259 template<
typename BBoxT>
260 inline bool intersects(
const BBoxT& bbox, RealT& t0, RealT& t1)
const 262 mTimeSpan.get(t0, t1);
263 for (
int i = 0; i < 3; ++i) {
264 RealT a = (bbox.min()[i] - mEye[i]) * mInvDir[i];
265 RealT b = (bbox.max()[i] - mEye[i]) * mInvDir[i];
266 if (a > b) std::swap(a, b);
269 if (t0 > t1)
return false;
276 template<
typename BBoxT>
280 return this->intersects(bbox, t0, t1);
286 template<
typename BBoxT>
287 inline bool clip(
const BBoxT& bbox)
290 const bool hit = this->intersects(bbox, t0, t1);
291 if (hit) mTimeSpan.set(t0, t1);
300 inline bool intersects(
const Vec3T& normal, RealT distance, RealT& t)
const 302 const RealT cosAngle = mDir.dot(normal);
304 t = (distance - mEye.dot(normal))/cosAngle;
305 return this->test(t);
313 inline bool intersects(
const Vec3T& normal,
const Vec3T& point, RealT& t)
const 315 return this->intersects(normal, point.
dot(normal), t);
319 Vec3T mEye, mDir, mInvDir;
325 template<
typename RealT>
326 inline std::ostream& operator<<(std::ostream& os, const Ray<RealT>& r)
328 os <<
"eye=" << r.eye() <<
" dir=" << r.dir() <<
" 1/dir="<<r.invDir()
329 <<
" t0=" << r.t0() <<
" t1=" << r.t1();
338 #endif // OPENVDB_MATH_RAY_HAS_BEEN_INCLUDED OPENVDB_DEPRECATED bool test() const
Return true if t0 is strictly less than t1.
Definition: Ray.h:142
Vec3R start() const
Return the starting point of the ray.
Definition: Ray.h:133
RealT RealType
Definition: Ray.h:57
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Ray applyInverseMap(const MapType &map) const
Return a new Ray that is transformed with the inverse of the specified map.
Definition: Ray.h:177
Delta for small floating-point offsets.
Definition: Math.h:132
void reset(const Vec3Type &eye, const Vec3Type &direction, RealT t0=math::Delta< RealT >::value(), RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:109
Vec3< RealT > Vec3Type
Definition: Ray.h:58
Vec3R end() const
Return the endpoint of the ray.
Definition: Ray.h:136
T length() const
Length of the vector.
Definition: Vec3.h:224
RealT t1
Definition: Ray.h:61
void setMinTime(RealT t0)
Definition: Ray.h:96
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:313
bool test(RealT time) const
Return true if time is within t0 and t1, both inclusive.
Definition: Ray.h:151
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:190
Vec3R operator()(RealT time) const
Return the position along the ray at the specified time.
Definition: Ray.h:130
bool clip(const BBoxT &bbox)
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:287
bool intersects(const Vec3T ¢er, RealT radius) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:234
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:610
const Vec3T & dir() const
Definition: Ray.h:121
void setEye(const Vec3Type &eye)
Definition: Ray.h:88
void scaleTimes(RealT scale)
Definition: Ray.h:107
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:198
TimeSpan()
Default constructor.
Definition: Ray.h:63
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:80
const Vec3T & invDir() const
Definition: Ray.h:123
float Sqrt(float x)
Return the square root of a floating-point value.
Definition: Math.h:727
Vec3Type Vec3T
Definition: Ray.h:59
#define OPENVDB_VERSION_NAME
Definition: version.h:43
TimeSpan(RealT _t0, RealT _t1)
Constructor.
Definition: Ray.h:65
void setMaxTime(RealT t1)
Definition: Ray.h:98
Vec3R mid() const
Return the midpoint of the ray.
Definition: Ray.h:139
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:300
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:215
bool intersects(const Vec3T ¢er, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
Definition: Ray.h:210
bool valid(RealT eps=math::Delta< RealT >::value()) const
Return true if t1 is larger than t0 by at least eps.
Definition: Ray.h:71
Definition: Exceptions.h:39
void setTimes(RealT t0=math::Delta< RealT >::value(), RealT t1=std::numeric_limits< RealT >::max())
Definition: Ray.h:100
void scale(RealT s)
Multiplies both times.
Definition: Ray.h:75
RealT t0() const
Definition: Ray.h:125
void setDir(const Vec3Type &dir)
Definition: Ray.h:90
RealT mid() const
Return the midpoint of the ray.
Definition: Ray.h:73
const Vec3T & eye() const
Definition: Ray.h:119
bool test(RealT t) const
Return true if time is inclusive.
Definition: Ray.h:77
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:417
bool isApproxZero(const Type &x)
Return true if x is equal to zero to within the default floating-point comparison tolerance...
Definition: Math.h:336
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
bool intersects(const BBoxT &bbox, RealT &t0, RealT &t1) const
Return true if the Ray intersects the specified axisaligned bounding box.
Definition: Ray.h:260
bool intersects(const BBoxT &bbox) const
Return true if this ray intersects the specified bounding box.
Definition: Ray.h:277
Ray applyMap(const MapType &map) const
Return a new Ray that is transformed with the specified map.
Definition: Ray.h:160
RealT t1() const
Definition: Ray.h:127
T lengthSqr() const
Definition: Vec3.h:235
bool valid(RealT eps=math::Delta< float >::value()) const
Return true if t1 is larger than t0 by at least eps.
Definition: Ray.h:145
bool clip(const Vec3T ¢er, RealT radius)
Return true if this ray intersects the specified sphere.
Definition: Ray.h:244
Tolerance for floating-point comparison.
Definition: Math.h:125