17#ifndef EBGEOMETRY_MESHRAYFORMS_HPP
18#define EBGEOMETRY_MESHRAYFORMS_HPP
43template <
class T,
size_t K,
size_t W>
114template <
class T,
size_t K>
219template <
class T,
size_t K,
size_t W>
230template <
class T,
size_t K,
size_t W>
240template <
class T,
size_t K>
241struct TracedOnCheaply<
MeshSDF<T, K>> : std::true_type
250template <
class T,
size_t K>
251struct HitNamesPart<
MeshSDF<T, K>> : std::true_type
327tieReach(
const RayT<T>& a_ray,
const T a_nearestT,
const bool a_found,
const T a_tMax)
noexcept
337 return Math::min(a_nearestT + window, a_tMax);
361 const RayT<T>& a_ray,
const T a_t,
const uint32_t a_face,
const T a_nearestT,
const uint32_t a_nearestFace)
noexcept
363 if (a_nearestFace == UINT32_MAX) {
368 const T window = reach - a_nearestT;
370 return a_t < a_nearestT - window || (a_t <= reach && a_face < a_nearestFace);
427 insert(
const T a_t,
const bool a_entering)
noexcept
477 a_spans.add(RayDetail::openAt(a_t0), RayDetail::openAt(
horizon));
485 for (uint32_t i = 0; i <
m_count; i++) {
497template <
class T,
size_t K,
size_t W>
517 const auto bvh = a_shape.getRoot();
518 const auto groups = bvh.getPrimitives();
521 const auto leaf = [&groups, &a_ray, a_tMin, a_tMax](Nearest& a_state,
size_t a_offset,
size_t a_count)
noexcept {
522 for (
size_t i = a_offset; i < a_offset + a_count; i++) {
523 const auto& group = groups[
static_cast<uint32_t
>(i)];
524 const T reach = RayDetail::tieReach(a_ray, a_state.m_t, a_state.m_face != UINT32_MAX, a_tMax);
528 group.rayCrossings(a_ray.m_origin, a_ray.m_direction, a_tMin, reach, crossings);
530 for (uint32_t lane = 0; lane < uint32_t(W); lane++) {
531 const T t = crossings[lane];
532 const uint32_t face = group.getFaceId(lane);
535 RayDetail::replacesNearest(a_ray, t, face, a_state.m_t, a_state.m_face)) {
536 a_state = Nearest{t, face, group.getNormal(lane)};
542 const auto bound = [&a_ray, a_tMax](
const Nearest& a_state)
noexcept -> T {
543 return RayDetail::tieReach(a_ray, a_state.m_t, a_state.m_face != UINT32_MAX, a_tMax);
546 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), nearest, leaf, bound);
550 if (nearest.m_face != UINT32_MAX) {
552 hit.
m_t = nearest.m_t;
553 hit.
m_point = a_ray.at(nearest.m_t);
556 hit.
m_entering =
dot(a_ray.m_direction, nearest.m_normal) < T(0);
563template <
class T,
size_t K,
size_t W>
575 const auto bvh = a_shape.getRoot();
576 const auto groups = bvh.getPrimitives();
578 const auto leaf = [&groups, &a_ray, a_tMin, a_tMax](
bool& a_found,
size_t a_offset,
size_t a_count)
noexcept {
579 for (
size_t i = a_offset; i < a_offset + a_count && !a_found; i++) {
580 uint32_t face = UINT32_MAX;
584 groups[
static_cast<uint32_t
>(i)].rayCrossing(a_ray.m_origin, a_ray.m_direction, a_tMin, a_tMax, face,
normal);
586 a_found = face != UINT32_MAX && t <= a_tMax;
590 const auto bound = [a_tMax](
const bool& a_found)
noexcept -> T {
594 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), found, leaf, bound);
599template <
class T,
size_t K>
616 Nearest nearest{a_tMax, UINT32_MAX};
618 const auto bvh = a_shape.getRoot();
619 const auto mesh = a_shape.getMesh();
620 const auto faceIds = bvh.getPrimitives();
622 const auto leaf = [&faceIds, &mesh, &a_ray, a_tMin, a_tMax](
623 Nearest& a_state,
size_t a_offset,
size_t a_count)
noexcept {
624 for (
size_t i = a_offset; i < a_offset + a_count; i++) {
625 const uint32_t face = faceIds[
static_cast<uint32_t
>(i)];
626 const T reach = RayDetail::tieReach(a_ray, a_state.m_t, a_state.m_face != UINT32_MAX, a_tMax);
627 const T t = mesh.getFace(face).rayCrossing(a_ray.m_origin, a_ray.m_direction, a_tMin, reach, mesh);
631 a_state = Nearest{t, face};
636 const auto bound = [&a_ray, a_tMax](
const Nearest& a_state)
noexcept -> T {
637 return RayDetail::tieReach(a_ray, a_state.m_t, a_state.m_face != UINT32_MAX, a_tMax);
640 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), nearest, leaf, bound);
642 return nearest.m_face == UINT32_MAX
644 : RayDetail::faceHit(a_ray, nearest.m_t, nearest.m_face, mesh.getFace(nearest.m_face).getNormal());
647template <
class T,
size_t K>
659 const auto bvh = a_shape.getRoot();
660 const auto mesh = a_shape.getMesh();
661 const auto faceIds = bvh.getPrimitives();
663 const auto leaf = [&faceIds, &mesh, &a_ray, a_tMin, a_tMax](
bool& a_found,
size_t a_offset,
size_t a_count)
noexcept {
664 for (
size_t i = a_offset; i < a_offset + a_count && !a_found; i++) {
665 const auto& face = mesh.getFace(faceIds[
static_cast<uint32_t
>(i)]);
672 const auto bound = [a_tMax](
const bool& a_found)
noexcept -> T {
676 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), found, leaf, bound);
690 const auto mesh = a_shape.getMesh();
693 uint32_t face = UINT32_MAX;
695 for (uint32_t i = 0; i < mesh.numFaces(); i++) {
696 const T reach = RayDetail::tieReach(a_ray, nearest, face != UINT32_MAX, a_tMax);
697 const T t = mesh.getFace(i).rayCrossing(a_ray.m_origin, a_ray.m_direction, a_tMin, reach, mesh);
705 return face == UINT32_MAX ?
HitT<T>() : RayDetail::faceHit(a_ray, nearest, face, mesh.getFace(face).getNormal());
708template <
class T,
size_t K,
size_t W>
716 const bool a_firstOnly,
720 RayDetail::firstSpan(a_shape, a_ray, a_tMin, a_tMax, a_options, a_spans);
727 crossings.
m_tMax = a_tMax;
729 const auto bvh = a_shape.getRoot();
730 const auto groups = bvh.getPrimitives();
734 for (
size_t i = a_offset; i < a_offset + a_count; i++) {
735 const auto& group = groups[
static_cast<uint32_t
>(i)];
739 group.rayCrossings(a_ray.m_origin, a_ray.m_direction, a_tMin, a_state.horizon(), t);
741 for (
size_t lane = 0; lane < W; lane++) {
743 a_state.insert(t[lane],
dot(a_ray.m_direction, group.getNormal(lane)) < T(0));
751 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), crossings, leaf, bound);
754 a_tMin, crossings.
m_count == 0U && RayDetail::insideUncrossed(a_shape, a_ray, a_tMin, a_tMax, a_options), a_spans);
757template <
class T,
size_t K>
765 const bool a_firstOnly,
769 RayDetail::firstSpan(a_shape, a_ray, a_tMin, a_tMax, a_options, a_spans);
776 crossings.
m_tMax = a_tMax;
778 const auto bvh = a_shape.getRoot();
779 const auto mesh = a_shape.getMesh();
780 const auto faceIds = bvh.getPrimitives();
782 const auto leaf = [&faceIds, &mesh, &a_ray, a_tMin](
784 for (
size_t i = a_offset; i < a_offset + a_count; i++) {
785 const uint32_t face = faceIds[
static_cast<uint32_t
>(i)];
786 const auto& f = mesh.getFace(face);
787 const T t = f.rayCrossing(a_ray.m_origin, a_ray.m_direction, a_tMin, a_state.horizon(), mesh);
790 a_state.insert(t,
dot(a_ray.m_direction, f.getNormal()) < T(0));
797 bvh.traverse(
BVH::RayQuery<T>(a_ray.m_origin, a_ray.m_direction, a_tMin), crossings, leaf, bound);
800 a_tMin, crossings.
m_count == 0U && RayDetail::insideUncrossed(a_shape, a_ray, a_tMin, a_tMax, a_options), a_spans);
811 const bool a_firstOnly,
815 RayDetail::firstSpan(a_shape, a_ray, a_tMin, a_tMax, a_options, a_spans);
822 crossings.
m_tMax = a_tMax;
824 const auto mesh = a_shape.getMesh();
826 for (uint32_t i = 0; i < mesh.numFaces(); i++) {
827 const auto& f = mesh.getFace(i);
828 const T t = f.rayCrossing(a_ray.m_origin, a_ray.m_direction, a_tMin, crossings.
horizon(), mesh);
831 crossings.
insert(t,
dot(a_ray.m_direction, f.getNormal()) < T(0));
836 a_tMin, crossings.
m_count == 0U && RayDetail::insideUncrossed(a_shape, a_ray, a_tMin, a_tMax, a_options), a_spans);
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
Declaration of signed distance functions for DCEL meshes.
Ray queries on any shape: where a ray, or the segment between two points, first crosses its surface (...
Signed distance function for a DCEL mesh. Does not use BVHs.
Definition EBGeometry_MeshDistanceFunctions.hpp:99
A FlatMeshSDF must be trivially copyable: that is what lets a rebasedView() be byte-copied into a dev...
Definition EBGeometry_MeshDistanceFunctions.hpp:315
Signed distance function for a pure triangle mesh using SoA-grouped primitives in a compact (lineariz...
Definition EBGeometry_MeshDistanceFunctions.hpp:627
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
static constexpr Vec3T< T > zeros() noexcept
Return a vector with x = y = z = 0.
constexpr T min(const T &a_a, const T &a_b) noexcept
The smaller of two values.
Definition EBGeometry_Math.hpp:44
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
HitStatus intersects(const S &a_shape, const RayT< T > &a_ray, typename RayT< T >::Scalar a_tMin, typename RayT< T >::Scalar a_tMax, const RayOptions< T > &a_options=RayOptions< T >()) noexcept
Whether a ray crosses a shape's surface in [a_tMin, a_tMax].
T dot(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Dot product of two 2D vectors.
Vec3T< T > normal(const F &a_function, const Vec3T< T > &a_point, const T &a_delta) noexcept
Outward unit normal of a function's level set, by central finite differences.
HitStatus
The outcome of a ray query.
Definition EBGeometry_Ray.hpp:86
@ Miss
The ray does not cross the surface in the interval.
@ Hit
The ray crosses the surface; HitT says where.
HitT< T > firstHit(const S &a_shape, const RayT< T > &a_ray, typename RayT< T >::Scalar a_tMin, typename RayT< T >::Scalar a_tMax, const RayOptions< T > &a_options=RayOptions< T >()) noexcept
The first crossing of a shape's surface along a ray, entering or leaving.
A ray tracked through a BVH: the key of a box is the distance along the ray at which it enters the bo...
Definition EBGeometry_BVHQuery.hpp:286
Where a ray first crosses a surface.
Definition EBGeometry_Ray.hpp:98
bool m_entering
Whether the ray enters the object there (it was outside just before), rather than leaving it.
Definition EBGeometry_Ray.hpp:131
bool m_exact
Whether the crossing is exact (found by a ray form), rather than found to within RayOptions::m_tolera...
Definition EBGeometry_Ray.hpp:137
T m_t
Distance along the ray: of the crossing for a hit, of the last point reached for an unresolved query,...
Definition EBGeometry_Ray.hpp:108
Vec3T< T > m_normal
The outward unit normal at the crossing: from the shape's ray form, or the normalized gradient of its...
Definition EBGeometry_Ray.hpp:126
uint32_t m_primitive
The finest element the shape can name at the crossing: the face of a mesh SDF, the deciding leaf of a...
Definition EBGeometry_Ray.hpp:120
HitStatus m_status
Whether the ray crosses the surface.
Definition EBGeometry_Ray.hpp:102
Vec3T< T > m_point
The point at m_t (for a hit or an unresolved query).
Definition EBGeometry_Ray.hpp:113
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91
Pairs crossings, in order, into spans: a span from each crossing the ray enters at to the next it lea...
Definition EBGeometry_RaySpans.hpp:850
void take(const SpanEndT< T > &a_end, const bool a_entering, const T a_t0, RaySpansT< T > &a_spans) noexcept
Take the next crossing.
Definition EBGeometry_RaySpans.hpp:875
void finish(const T a_t, RaySpansT< T > &a_spans) noexcept
Close the open span, if any, at an Open end.
Definition EBGeometry_RaySpans.hpp:902
A crossing.
Definition EBGeometry_MeshRayForms.hpp:393
bool m_entering
Whether the ray enters there: it runs against the face's outward normal.
Definition EBGeometry_MeshRayForms.hpp:402
T m_t
Distance along the ray.
Definition EBGeometry_MeshRayForms.hpp:397
The nearest crossings of a mesh along a ray, in order, as a BVH walk finds them: at most Capacity,...
Definition EBGeometry_MeshRayForms.hpp:383
uint32_t m_count
How many are held.
Definition EBGeometry_MeshRayForms.hpp:413
Crossing m_crossings[Capacity]
The crossings, nearest first.
Definition EBGeometry_MeshRayForms.hpp:408
T horizon() const noexcept
How far the crossings are known: the end of the interval, or the farthest held once full.
Definition EBGeometry_MeshRayForms.hpp:456
void toSpans(const T a_t0, const bool a_inside, RaySpansT< T > &a_spans) const noexcept
The spans: the crossings paired by the side of the face the ray crosses from.
Definition EBGeometry_MeshRayForms.hpp:469
void insert(const T a_t, const bool a_entering) noexcept
Take a crossing.
Definition EBGeometry_MeshRayForms.hpp:427
static constexpr uint32_t Capacity
Most crossings held.
Definition EBGeometry_MeshRayForms.hpp:387
T m_tMax
End of the interval.
Definition EBGeometry_MeshRayForms.hpp:418
How a query marches, where a shape has no ray form.
Definition EBGeometry_Ray.hpp:203
Where a ray is inside an object over an interval [t0, t1]: its spans, in order, each from where it en...
Definition EBGeometry_Ray.hpp:296
A ray: the points origin + t * direction.
Definition EBGeometry_Ray.hpp:56
One end of a span: where a ray enters or leaves an object.
Definition EBGeometry_Ray.hpp:248