10#ifndef NANOVDB_MATH_RAY_H_HAS_BEEN_INCLUDED
11#define NANOVDB_MATH_RAY_H_HAS_BEEN_INCLUDED
18template<
typename RealT>
70 , mInvDir(1 / mDir[0], 1 / mDir[1], 1 / mDir[2])
72 , mSign{mInvDir[0] < 0, mInvDir[1] < 0, mInvDir[2] < 0}
93 mInvDir[0] = 1.0 / mDir[0];
94 mInvDir[1] = 1.0 / mDir[1];
95 mInvDir[2] = 1.0 / mDir[2];
96 mSign[0] = mInvDir[0] < 0;
97 mSign[1] = mInvDir[1] < 0;
98 mSign[2] = mInvDir[2] < 0;
118 assert(
t0 > 0 &&
t1 > 0);
119 mTimeSpan.set(
t0,
t1);
125 mTimeSpan.scale(scale);
157 return Vec3T(fmaf(time, mDir[0], mEye[0]),
158 fmaf(time, mDir[1], mEye[1]),
159 fmaf(time, mDir[2], mEye[2]));
161 return mEye + mDir * time;
189 template<
typename MapType>
192 const Vec3T eye = map.applyMap(mEye);
193 const Vec3T dir = map.applyJacobian(mDir);
194 const RealT length =
dir.length(), invLength = RealT(1) / length;
195 RealT
t1 = mTimeSpan.t1;
199 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0,
t1);
201 template<
typename MapType>
204 const Vec3T eye = map.applyMapF(mEye);
205 const Vec3T dir = map.applyJacobianF(mDir);
206 const RealT length =
dir.length(), invLength = RealT(1) / length;
207 RealT
t1 = mTimeSpan.t1;
211 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0,
t1);
223 template<
typename MapType>
226 const Vec3T eye = map.applyInverseMap(mEye);
227 const Vec3T dir = map.applyInverseJacobian(mDir);
228 const RealT length =
dir.length(), invLength = RealT(1) / length;
229 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0, length * mTimeSpan.t1);
231 template<
typename MapType>
234 const Vec3T eye = map.applyInverseMapF(mEye);
235 const Vec3T dir = map.applyInverseJacobianF(mDir);
236 const RealT length =
dir.length(), invLength = RealT(1) / length;
237 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0, length * mTimeSpan.t1);
242 template<
typename Gr
idType>
245 const Vec3T eye = grid.indexToWorldF(mEye);
246 const Vec3T dir = grid.indexToWorldDirF(mDir);
247 const RealT length =
dir.length(), invLength = RealT(1) / length;
248 RealT
t1 = mTimeSpan.t1;
252 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0,
t1);
257 template<
typename Gr
idType>
260 const Vec3T eye = grid.worldToIndexF(mEye);
261 const Vec3T dir = grid.worldToIndexDirF(mDir);
262 const RealT length =
dir.length(), invLength = RealT(1) / length;
263 RealT
t1 = mTimeSpan.t1;
267 return Ray(
eye,
dir * invLength, length * mTimeSpan.t0,
t1);
281 const Vec3T origin = mEye - center;
283 const RealT B = 2 * mDir.dot(origin);
284 const RealT C = origin.
lengthSqr() - radius * radius;
285 const RealT D = B * B - 4 * A * C;
290 const RealT Q = RealT(-0.5) * (B < 0 ? (B +
Sqrt(D)) : (B -
Sqrt(D)));
300 if (
t0 < mTimeSpan.t0) {
303 if (
t1 > mTimeSpan.t1) {
330 mTimeSpan.set(
t0,
t1);
343 template<
typename BBoxT>
346 t0 = (bbox[ mSign[0]][0] - mEye[0]) * mInvDir[0];
347 RealT t2 = (bbox[1-mSign[1]][1] - mEye[1]) * mInvDir[1];
348 if (
t0 > t2)
return false;
349 t1 = (bbox[1-mSign[0]][0] - mEye[0]) * mInvDir[0];
350 RealT t3 = (bbox[ mSign[1]][1] - mEye[1]) * mInvDir[1];
351 if (t3 >
t1)
return false;
352 if (t3 >
t0)
t0 = t3;
353 if (t2 <
t1)
t1 = t2;
354 t3 = (bbox[ mSign[2]][2] - mEye[2]) * mInvDir[2];
355 if (t3 >
t1)
return false;
356 t2 = (bbox[1-mSign[2]][2] - mEye[2]) * mInvDir[2];
357 if (
t0 > t2)
return false;
358 if (t3 >
t0)
t0 = t3;
359 if (mTimeSpan.
t1 <
t0)
return false;
360 if (t2 <
t1)
t1 = t2;
361 if (mTimeSpan.
t0 >
t1)
return false;
362 if (mTimeSpan.
t0 >
t0)
t0 = mTimeSpan.
t0;
363 if (mTimeSpan.
t1 <
t1)
t1 = mTimeSpan.
t1;
400 mTimeSpan.get(
t0,
t1);
401 for (
int i = 0; i < 3; ++i) {
402 RealT a = RealT(bbox.min()[i]), b = RealT(bbox.max()[i] + 1);
406 a = (a - mEye[i]) * mInvDir[i];
407 b = (b - mEye[i]) * mInvDir[i];
428 template<
typename OtherVec3T>
432 mTimeSpan.get(
t0,
t1);
433 for (
int i = 0; i < 3; ++i) {
434 RealT a = RealT(bbox.min()[i]), b = RealT(bbox.max()[i]);
438 a = (a - mEye[i]) * mInvDir[i];
439 b = (b - mEye[i]) * mInvDir[i];
467 template<
typename BBoxT>
476 RealT
t0 = (bbox[mSign[0]][0] - mEye[0]) * mInvDir[0];
477 RealT t2 = (bbox[1 - mSign[1]][1] - mEye[1]) * mInvDir[1];
478 if (
t0 > t2)
return false;
479 RealT
t1 = (bbox[1 - mSign[0]][0] - mEye[0]) * mInvDir[0];
480 RealT t3 = (bbox[mSign[1]][1] - mEye[1]) * mInvDir[1];
481 if (t3 >
t1)
return false;
482 if (t3 >
t0)
t0 = t3;
483 if (t2 <
t1)
t1 = t2;
484 t3 = (bbox[mSign[2]][2] - mEye[2]) * mInvDir[2];
485 if (t3 >
t1)
return false;
486 t2 = (bbox[1 - mSign[2]][2] - mEye[2]) * mInvDir[2];
487 if (
t0 > t2)
return false;
506 template<
typename BBoxT>
512 mTimeSpan.set(
t0,
t1);
525 const RealT cosAngle = mDir.dot(normal);
529 t = (distance - mEye.dot(normal)) / cosAngle;
530 return this->
test(t);
545 Vec3T mEye, mDir, mInvDir;
552template<
typename RealT>
553using Ray [[deprecated(
"Use nanovdb::math::Ray instead")]] = math::Ray<RealT>;
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
__hostdev__ bool clip(const Vec3T ¢er, RealT radius)
Return true if this ray intersects the specified sphere.
Definition Ray.h:325
Vec3< RealT > Vec3Type
Definition Ray.h:23
__hostdev__ Ray & setEye(const Vec3Type &eye)
Definition Ray.h:84
__hostdev__ bool intersects(const Vec3T ¢er, RealT radius) const
Return true if this ray intersects the specified sphere.
Definition Ray.h:313
__hostdev__ Ray & setDir(const Vec3Type &dir)
Definition Ray.h:90
__hostdev__ const Vec3T & invDir() const
Definition Ray.h:145
__hostdev__ Vec3T end() const
Return the endpoint of the ray.
Definition Ray.h:169
__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...
Definition Ray.h:429
__hostdev__ bool test(RealT time) const
Return true if time is within t0 and t1, both inclusive.
Definition Ray.h:178
__hostdev__ bool clip(const BBoxT &bbox)
Return true if this ray intersects the specified bounding box.
Definition Ray.h:507
__hostdev__ int sign(int i) const
Definition Ray.h:151
__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())
Definition Ray.h:64
RealT RealType
Definition Ray.h:22
__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.
Definition Ray.h:523
__hostdev__ Ray & setMinTime(RealT t0)
Definition Ray.h:102
__hostdev__ const Vec3T & eye() const
Definition Ray.h:141
__hostdev__ Ray applyInverseMap(const MapType &map) const
Return a new Ray that is transformed with the inverse of the specified map.
Definition Ray.h:224
Vec3Type Vec3T
Definition Ray.h:24
__hostdev__ Ray & reset(const Vec3Type &eye, const Vec3Type &direction, RealT t0=Delta< RealT >::value(), RealT t1=Maximum< RealT >::value())
Definition Ray.h:129
__hostdev__ Ray & offsetEye(RealT offset)
Definition Ray.h:76
__hostdev__ Ray applyMap(const MapType &map) const
Return a new Ray that is transformed with the specified map.
Definition Ray.h:190
__hostdev__ Ray & setMaxTime(RealT t1)
Definition Ray.h:108
__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.
Definition Ray.h:539
__hostdev__ const Vec3T & dir() const
Definition Ray.h:143
__hostdev__ Ray & setTimes(RealT t0=Delta< RealT >::value(), RealT t1=Maximum< RealT >::value())
Definition Ray.h:114
__hostdev__ RealT t1() const
Definition Ray.h:149
__hostdev__ bool intersects(const Vec3T ¢er, RealT radius, RealT &t0, RealT &t1) const
Return true if this ray intersects the specified sphere.
Definition Ray.h:279
__hostdev__ Ray applyInverseMapF(const MapType &map) const
Definition Ray.h:232
__hostdev__ bool valid(RealT eps=Delta< float >::value()) const
Return true if t1 is larger than t0 by at least eps.
Definition Ray.h:175
__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...
Definition Ray.h:243
__hostdev__ RealT t0() const
Definition Ray.h:147
__hostdev__ Ray applyMapF(const MapType &map) const
Definition Ray.h:202
__hostdev__ bool intersects(const BBoxT &bbox) const
Return true if this ray intersects the specified bounding box.
Definition Ray.h:468
__hostdev__ Vec3T start() const
Return the starting point of the ray.
Definition Ray.h:166
__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 ...
Definition Ray.h:398
__hostdev__ Ray & scaleTimes(RealT scale)
Definition Ray.h:123
__hostdev__ Vec3T operator()(RealT time) const
Return the position along the ray at the specified time.
Definition Ray.h:154
__hostdev__ Vec3T mid() const
Return the midpoint of the ray.
Definition Ray.h:172
__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...
Definition Ray.h:258
A simple vector class with three components, similar to openvdb::math::Vec3.
Definition Math.h:1362
__hostdev__ T lengthSqr() const
Definition Math.h:1423
__hostdev__ T dot(const Vec3T &v) const
Definition Math.h:1407
#define __hostdev__
Definition SampleFromVoxels.h:29
Definition DitherLUT.h:19
__hostdev__ bool isApproxZero(const Type &x)
Definition Math.h:127
__hostdev__ float Sqrt(float x)
Return the square root of a floating-point value.
Definition Math.h:277
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
GridType
List of types that are currently supported by NanoVDB.
Definition NanoVDB.h:219
math::BBox< Coord > CoordBBox
Definition Math.h:2241
Delta for small floating-point offsets.
Definition Math.h:73
static T value()
Definition Math.h:121
__hostdev__ void set(RealT _t0, RealT _t1)
Set both times.
Definition Ray.h:38
__hostdev__ bool test(RealT t) const
Return true if time is inclusive.
Definition Ray.h:61
__hostdev__ bool valid(RealT eps=Delta< RealT >::value()) const
Return true if t1 is larger than t0 by at least eps.
Definition Ray.h:50
__hostdev__ TimeSpan()
Default constructor.
Definition Ray.h:30
__hostdev__ RealT mid() const
Return the midpoint of the ray.
Definition Ray.h:52
RealT t1
Definition Ray.h:28
__hostdev__ TimeSpan(RealT _t0, RealT _t1)
Constructor.
Definition Ray.h:32
RealT t0
Definition Ray.h:28
__hostdev__ void get(RealT &_t0, RealT &_t1) const
Get both times.
Definition Ray.h:44
__hostdev__ void scale(RealT s)
Multiplies both times.
Definition Ray.h:54
static constexpr bool value
Definition Util.h:344