10 #ifndef NANOVDB_RAY_H_HAS_BEEN_INCLUDED
11 #define NANOVDB_RAY_H_HAS_BEEN_INCLUDED
17 template<
typename RealT>
69 , mInvDir(1 / mDir[0], 1 / mDir[1], 1 / mDir[2])
71 , mSign{mInvDir[0] < 0, mInvDir[1] < 0, mInvDir[2] < 0}
92 mInvDir[0] = 1.0 / mDir[0];
93 mInvDir[1] = 1.0 / mDir[1];
94 mInvDir[2] = 1.0 / mDir[2];
95 mSign[0] = mInvDir[0] < 0;
96 mSign[1] = mInvDir[1] < 0;
97 mSign[2] = mInvDir[2] < 0;
117 assert(
t0 > 0 &&
t1 > 0);
124 mTimeSpan.
scale(scale);
156 return Vec3T(fmaf(time, mDir[0], mEye[0]),
157 fmaf(time, mDir[1], mEye[1]),
158 fmaf(time, mDir[2], mEye[2]));
160 return mEye + mDir *
time;
188 template<
typename MapType>
191 const Vec3T eye = map.applyMap(mEye);
192 const Vec3T dir = map.applyJacobian(mDir);
194 RealT
t1 = mTimeSpan.
t1;
198 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, t1);
200 template<
typename MapType>
203 const Vec3T eye = map.applyMapF(mEye);
204 const Vec3T dir = map.applyJacobianF(mDir);
206 RealT
t1 = mTimeSpan.
t1;
210 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, t1);
222 template<
typename MapType>
225 const Vec3T eye = map.applyInverseMap(mEye);
226 const Vec3T dir = map.applyInverseJacobian(mDir);
228 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, length * mTimeSpan.
t1);
230 template<
typename MapType>
233 const Vec3T eye = map.applyInverseMapF(mEye);
234 const Vec3T dir = map.applyInverseJacobianF(mDir);
236 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, length * mTimeSpan.
t1);
241 template<
typename Gr
idType>
244 const Vec3T eye = grid.indexToWorldF(mEye);
245 const Vec3T dir = grid.indexToWorldDirF(mDir);
247 RealT
t1 = mTimeSpan.
t1;
251 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, t1);
256 template<
typename Gr
idType>
259 const Vec3T eye = grid.worldToIndexF(mEye);
260 const Vec3T dir = grid.worldToIndexDirF(mDir);
262 RealT
t1 = mTimeSpan.
t1;
266 return Ray(eye, dir * invLength, length * mTimeSpan.
t0, t1);
282 const RealT
B = 2 * mDir.
dot(origin);
283 const RealT C = origin.
lengthSqr() - radius * radius;
284 const RealT D = B * B - 4 * A * C;
289 const RealT Q = RealT(-0.5) * (B < 0 ? (B +
Sqrt(D)) : (B -
Sqrt(D)));
299 if (t0 < mTimeSpan.
t0) {
302 if (t1 > mTimeSpan.
t1) {
315 return this->
intersects(center, radius, t0, t1) > 0;
327 const bool hit = this->
intersects(center, radius, t0, t1);
329 mTimeSpan.
set(t0, t1);
342 template<
typename BBoxT>
345 t0 = (bbox[ mSign[0]][0] - mEye[0]) * mInvDir[0];
346 RealT t2 = (bbox[1-mSign[1]][1] - mEye[1]) * mInvDir[1];
347 if (t0 > t2)
return false;
348 t1 = (bbox[1-mSign[0]][0] - mEye[0]) * mInvDir[0];
349 RealT t3 = (bbox[ mSign[1]][1] - mEye[1]) * mInvDir[1];
350 if (t3 > t1)
return false;
351 if (t3 > t0) t0 = t3;
352 if (t2 < t1) t1 = t2;
353 t3 = (bbox[ mSign[2]][2] - mEye[2]) * mInvDir[2];
354 if (t3 > t1)
return false;
355 t2 = (bbox[1-mSign[2]][2] - mEye[2]) * mInvDir[2];
356 if (t0 > t2)
return false;
357 if (t3 > t0) t0 = t3;
358 if (mTimeSpan.
t1 < t0)
return false;
359 if (t2 < t1) t1 = t2;
360 if (mTimeSpan.
t0 > t1)
return false;
361 if (mTimeSpan.
t0 > t0) t0 = mTimeSpan.
t0;
362 if (mTimeSpan.
t1 < t1) t1 = mTimeSpan.
t1;
399 mTimeSpan.
get(t0, t1);
400 for (
int i = 0; i < 3; ++i) {
401 RealT
a = RealT(bbox.min()[i]),
b = RealT(bbox.max()[i] + 1);
405 a = (a - mEye[i]) * mInvDir[i];
406 b = (
b - mEye[i]) * mInvDir[i];
427 template<
typename OtherVec3T>
431 mTimeSpan.
get(t0, t1);
432 for (
int i = 0; i < 3; ++i) {
433 RealT
a = RealT(bbox.min()[i]),
b = RealT(bbox.max()[i]);
437 a = (a - mEye[i]) * mInvDir[i];
438 b = (
b - mEye[i]) * mInvDir[i];
466 template<
typename BBoxT>
475 RealT t0 = (bbox[mSign[0]][0] - mEye[0]) * mInvDir[0];
476 RealT t2 = (bbox[1 - mSign[1]][1] - mEye[1]) * mInvDir[1];
477 if (t0 > t2)
return false;
478 RealT t1 = (bbox[1 - mSign[0]][0] - mEye[0]) * mInvDir[0];
479 RealT t3 = (bbox[mSign[1]][1] - mEye[1]) * mInvDir[1];
480 if (t3 > t1)
return false;
481 if (t3 > t0) t0 = t3;
482 if (t2 < t1) t1 = t2;
483 t3 = (bbox[mSign[2]][2] - mEye[2]) * mInvDir[2];
484 if (t3 > t1)
return false;
485 t2 = (bbox[1 - mSign[2]][2] - mEye[2]) * mInvDir[2];
486 if (t0 > t2)
return false;
505 template<
typename BBoxT>
509 const bool hit = this->
intersects(bbox, t0, t1);
511 mTimeSpan.
set(t0, t1);
524 const RealT cosAngle = mDir.
dot(normal);
528 t = (distance - mEye.
dot(normal)) / cosAngle;
529 return this->
test(t);
544 Vec3T mEye, mDir, mInvDir;
551 #endif // NANOVDB_RAY_HAS_BEEN_INCLUDED
__hostdev__ void scale(RealT s)
Multiplies both times.
__hostdev__ Ray(const Vec3Type &eye=Vec3Type(0, 0, 0), const Vec3Type &direction=Vec3Type(1, 0, 0), RealT t0=Delta< RealT >::value(), RealT t1=Maximum< RealT >::value())
A simple vector class with three components, similar to openvdb::math::Vec3.
__hostdev__ RealT t0() const
__hostdev__ Ray & setMaxTime(RealT t1)
__hostdev__ Ray indexToWorldF(const GridType &grid) const
Return a new ray in world space, assuming the existing ray is represented in the index space of the s...
__hostdev__ Ray applyMapF(const MapType &map) const
__hostdev__ 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...
GridType
List of types that are currently supported by NanoVDB.
__hostdev__ RealT t1() const
GT_API const UT_StringHolder time
IMF_EXPORT IMATH_NAMESPACE::V3f direction(const IMATH_NAMESPACE::Box2i &dataWindow, const IMATH_NAMESPACE::V2f &pixelPosition)
__hostdev__ 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.
__hostdev__ Ray & setDir(const Vec3Type &dir)
__hostdev__ bool clip(const Vec3T ¢er, RealT radius)
Return true if this ray intersects the specified sphere.
__hostdev__ bool test(RealT time) const
Return true if time is within t0 and t1, both inclusive.
__hostdev__ Ray & offsetEye(RealT offset)
__hostdev__ RealT mid() const
Return the midpoint of the ray.
__hostdev__ T length() const
__hostdev__ Vec3T operator()(RealT time) const
Return the position along the ray at the specified time.
GLboolean GLboolean GLboolean GLboolean a
GLuint GLsizei GLsizei * length
__hostdev__ Ray applyInverseMapF(const MapType &map) const
__hostdev__ const Vec3T & eye() const
__hostdev__ Ray & setEye(const Vec3Type &eye)
__hostdev__ Ray & setTimes(RealT t0=Delta< RealT >::value(), RealT t1=Maximum< RealT >::value())
__hostdev__ Vec3T mid() const
Return the midpoint of the ray.
GA_API const UT_StringHolder scale
__hostdev__ bool clip(const BBoxT &bbox)
Return true if this ray intersects the specified bounding box.
__hostdev__ bool intersects(const BBox< OtherVec3T > &bbox, RealT &t0, RealT &t1) const
Returns true if this ray intersects a floating-point bounding box. If the return value is true t0 and...
__hostdev__ bool intersects(const BBoxT &bbox) const
Return true if this ray intersects the specified bounding box.
__hostdev__ uint64_t offset() const
__hostdev__ void set(RealT _t0, RealT _t1)
Set both times.
__hostdev__ Ray & reset(const Vec3Type &eye, const Vec3Type &direction, RealT t0=Delta< RealT >::value(), RealT t1=Maximum< RealT >::value())
__hostdev__ int sign(int i) const
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
__hostdev__ Vec3T start() const
Return the starting point of the ray.
__hostdev__ Ray applyMap(const MapType &map) const
Return a new Ray that is transformed with the specified map.
__hostdev__ Ray & scaleTimes(RealT scale)
__hostdev__ bool valid(RealT eps=Delta< float >::value()) const
Return true if t1 is larger than t0 by at least eps.
__hostdev__ const Vec3T & invDir() const
__hostdev__ bool isApproxZero(const Type &x)
Maximum floating-point values.
__hostdev__ bool test(RealT t) const
Return true if time is inclusive.
GLboolean GLboolean GLboolean b
__hostdev__ void get(RealT &_t0, RealT &_t1) const
Get both times.
__hostdev__ Vec3T end() const
Return the endpoint of the ray.
__hostdev__ Ray applyInverseMap(const MapType &map) const
Return a new Ray that is transformed with the inverse of the specified map.
__hostdev__ T dot(const Vec3T &v) const
__hostdev__ Ray worldToIndexF(const GridType &grid) const
Return a new ray in index space, assuming the existing ray is represented in the world space of the s...
__hostdev__ bool intersects(const Vec3T ¢er, RealT radius) const
Return true if this ray intersects the specified sphere.
__hostdev__ Ray & setMinTime(RealT t0)
Delta for small floating-point offsets.
__hostdev__ bool valid(RealT eps=Delta< RealT >::value()) const
Return true if t1 is larger than t0 by at least eps.
__hostdev__ const Vec3T & dir() const
__hostdev__ T lengthSqr() const
SIM_API const UT_StringHolder distance
__hostdev__ float Sqrt(float x)
Return the square root of a floating-point value.
__hostdev__ bool intersects(const Vec3T ¢er, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
C++11 implementation of std::is_floating_point.
__hostdev__ TimeSpan(RealT _t0, RealT _t1)
Constructor.
__hostdev__ bool intersects(const CoordBBox &bbox, RealT &t0, RealT &t1) const
Returns true if this ray intersects an index bounding box. If the return value is true t0 and t1 are ...
__hostdev__ TimeSpan()
Default constructor.