EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Ray.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
30#ifndef EBGEOMETRY_RAY_HPP
31#define EBGEOMETRY_RAY_HPP
32
33// Std includes
34#include <cstdint>
35#include <type_traits>
36
37// Our includes
40#include "EBGeometry_GPU.hpp"
41#include "EBGeometry_Math.hpp"
43#include "EBGeometry_Vec.hpp"
44
45namespace EBGeometry {
46
54template <class T>
55struct RayT
56{
60 using Scalar = T;
61
66
71
77 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
78 inline Vec3T<T>
79 at(T a_t) const noexcept;
80};
81
85enum class HitStatus : uint8_t
86{
87 Miss,
88 Hit,
90};
91
96template <class T>
97struct HitT
98{
103
109
114
120 uint32_t m_primitive = 0;
121
127
131 bool m_entering = false;
132
137 bool m_exact = false;
138
143 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
144 inline bool
145 hit() const noexcept;
146};
147
155template <class T>
157{
162
168
173
177 bool m_entering = false;
178
183 bool m_exact = false;
184
189 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
190 inline bool
191 hit() const noexcept;
192};
193
201template <class T>
203{
211 T m_tolerance = T(1e-4);
212
216 T m_scale = T(0);
217
221 uint32_t m_maxSteps = 512;
222
227 T m_step = T(0.005);
228
233 bool m_march = false;
234};
235
246template <class T>
248{
252 enum Kind : uint8_t
253 {
254 Open = 0,
255 Exact = 1,
256 Marched = 2
257 };
258
263
268 uint32_t m_leaf;
269
274
279 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
280 inline bool
281 crossing() const noexcept;
282};
283
294template <class T>
296{
300 static constexpr uint32_t Capacity = 8;
301
305 struct Span
306 {
311
316 };
317
321 Span m_spans[Capacity];
322
326 uint32_t m_count = 0;
327
332
338 inline void
339 reset(T a_horizon) noexcept;
340
348 inline void
349 add(const SpanEndT<T>& a_in, const SpanEndT<T>& a_out) noexcept;
350
360 inline void
361 addClipped(T a_in, T a_out, T a_t0, T a_t1) noexcept;
362};
363
399template <class S, class = void>
401{
402};
403
419template <class T, class S>
420[[nodiscard]] EBGEOMETRY_HOST_DEVICE
421inline HitT<T>
422firstHit(const S& a_shape,
423 const RayT<T>& a_ray,
424 typename RayT<T>::Scalar a_tMin,
425 typename RayT<T>::Scalar a_tMax,
426 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
427
442template <class T, class S>
443[[nodiscard]] EBGEOMETRY_HOST_DEVICE
444inline HitT<T>
445firstHit(const S& a_shape,
446 const Vec3T<T>& a_from,
447 const Vec3T<T>& a_to,
448 T a_tMin = T(0),
449 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
450
464template <class T, class S>
465[[nodiscard]] EBGEOMETRY_HOST_DEVICE
466inline HitStatus
467intersects(const S& a_shape,
468 const RayT<T>& a_ray,
469 typename RayT<T>::Scalar a_tMin,
470 typename RayT<T>::Scalar a_tMax,
471 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
472
487template <class T, class S>
488[[nodiscard]] EBGEOMETRY_HOST_DEVICE
489inline HitStatus
490intersects(const S& a_shape,
491 const Vec3T<T>& a_from,
492 const Vec3T<T>& a_to,
493 T a_tMin = T(0),
494 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
495
512template <class T, class S>
513[[nodiscard]] EBGEOMETRY_HOST_DEVICE
514inline CrossingT<T>
515firstCrossing(const S& a_shape,
516 const RayT<T>& a_ray,
517 typename RayT<T>::Scalar a_tMin,
518 typename RayT<T>::Scalar a_tMax,
519 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
520
535template <class T, class S>
536[[nodiscard]] EBGEOMETRY_HOST_DEVICE
537inline CrossingT<T>
538firstCrossing(const S& a_shape,
539 const Vec3T<T>& a_from,
540 const Vec3T<T>& a_to,
541 T a_tMin = T(0),
542 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
543
548template <class T>
562
578template <class T, class S>
579[[nodiscard]] EBGEOMETRY_HOST_DEVICE
580inline SurfaceHitT<T>
581firstSurface(const S& a_shape,
582 const RayT<T>& a_ray,
583 typename RayT<T>::Scalar a_tMin,
584 typename RayT<T>::Scalar a_tMax,
585 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
586
599template <class T, class S>
600[[nodiscard]] EBGEOMETRY_HOST_DEVICE
601inline SurfaceHitT<T>
602firstSurface(const S& a_shape,
603 const Vec3T<T>& a_from,
604 const Vec3T<T>& a_to,
605 T a_tMin = T(0),
606 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
607
608} // namespace EBGeometry
609
610#include "EBGeometry_RayImplem.hpp"
612
613#endif
Declarations of bounding volume types used in bounding volume hierarchies.
How far a function's value can be trusted as a distance: DistanceQuality, and the distanceQualityOf t...
GPU portability layer: host/device decoration macros and backend detection.
#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
Scalar helpers and numeric limits that compile for both host and device.
The span arithmetic that traces a composition along a ray: each operand's spans (where the ray is ins...
SurfaceRef: which surface of a shape decides its value at a point, and the container protocol that na...
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
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.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
@ Exact
The value's magnitude is the Euclidean distance to the surface.
HitStatus
The outcome of a ray query.
Definition EBGeometry_Ray.hpp:86
@ Unresolved
The query ran out of steps or passes before it could decide (a ray grazing the surface,...
@ Miss
The ray does not cross the surface in the interval.
@ Hit
The ray crosses the surface; HitT says where.
SurfaceHitT< T > firstSurface(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
A ray's first crossing of a shape's surface, and the surface it crossed: the ray counterpart of close...
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.
Where a ray first crosses a surface, without the normal or the primitive there: what firstCrossing() ...
Definition EBGeometry_Ray.hpp:157
bool hit() const noexcept
Whether the ray crosses the surface.
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
bool hit() const noexcept
Whether the ray crosses the surface.
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
How to ray trace a shape directly, instead of marching its signed distance.
Definition EBGeometry_Ray.hpp:401
How a query marches, where a shape has no ray form.
Definition EBGeometry_Ray.hpp:203
A span: where the ray enters, and where it leaves.
Definition EBGeometry_Ray.hpp:306
SpanEndT< T > m_out
Where it leaves.
Definition EBGeometry_Ray.hpp:315
SpanEndT< T > m_in
Where the ray enters.
Definition EBGeometry_Ray.hpp:310
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
void addClipped(T a_in, T a_out, T a_t0, T a_t1) noexcept
Append the span [a_in, a_out] cut to [a_t0, a_t1]: an end outside it (or infinite) becomes an Open en...
void add(const SpanEndT< T > &a_in, const SpanEndT< T > &a_out) noexcept
Append a span after the last one, or, if the list is full, drop it and lower the horizon to where it ...
void reset(T a_horizon) noexcept
Empty the list, complete up to a horizon.
A ray: the points origin + t * direction.
Definition EBGeometry_Ray.hpp:56
Vec3T< T > m_direction
Unit direction.
Definition EBGeometry_Ray.hpp:70
T Scalar
Floating-point precision.
Definition EBGeometry_Ray.hpp:60
Vec3T< T > at(T a_t) const noexcept
The point at a distance along the ray.
Vec3T< T > m_origin
Where the ray starts.
Definition EBGeometry_Ray.hpp:65
One end of a span: where a ray enters or leaves an object.
Definition EBGeometry_Ray.hpp:248
Kind m_kind
What the end is.
Definition EBGeometry_Ray.hpp:273
T m_t
Distance along the ray.
Definition EBGeometry_Ray.hpp:262
Kind
What an end is.
Definition EBGeometry_Ray.hpp:253
uint32_t m_leaf
Which of a composition's leaves the crossing lies on, numbered by the composition; a shape that is no...
Definition EBGeometry_Ray.hpp:268
bool crossing() const noexcept
Whether the end is a crossing of the surface.
A ray's first crossing of a shape's surface, with the surface it crossed, as firstSurface() reports i...
Definition EBGeometry_Ray.hpp:550
SurfaceRef< T > m_surface
The surface crossed, drilled down as closestSurface() does (its surface, leaf, label,...
Definition EBGeometry_Ray.hpp:560
HitT< T > m_hit
The crossing, as firstHit() reports it.
Definition EBGeometry_Ray.hpp:554
The surface that decides a shape's value at a point, as closestSurface() reports it.
Definition EBGeometry_SurfaceRef.hpp:39