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: GPL-3.0-or-later
4
29#ifndef EBGEOMETRY_RAY_HPP
30#define EBGEOMETRY_RAY_HPP
31
32// Std includes
33#include <cstdint>
34#include <type_traits>
35
36// Our includes
39#include "EBGeometry_GPU.hpp"
40#include "EBGeometry_Math.hpp"
41#include "EBGeometry_Vec.hpp"
42
43namespace EBGeometry {
44
52template <class T>
53struct RayT
54{
58 using Scalar = T;
59
64
69
75 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
76 inline Vec3T<T>
77 at(T a_t) const noexcept;
78};
79
83enum class HitStatus : uint8_t
84{
85 Miss,
86 Hit,
88};
89
94template <class T>
95struct HitT
96{
101
107
112
118 uint32_t m_primitive = 0;
119
125
129 bool m_entering = false;
130
135 bool m_exact = false;
136
141 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
142 inline bool
143 hit() const noexcept;
144};
145
153template <class T>
155{
163 T m_tolerance = T(1e-4);
164
168 T m_scale = T(0);
169
173 uint32_t m_maxSteps = 512;
174
179 T m_step = T(0.005);
180
185 bool m_march = false;
186};
187
198template <class T>
200{
204 enum Kind : uint8_t
205 {
206 Open = 0,
207 Exact = 1,
208 Marched = 2
209 };
210
215
220 uint32_t m_leaf;
221
226
231 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
232 inline bool
233 crossing() const noexcept;
234};
235
246template <class T>
248{
252 static constexpr uint32_t Capacity = 8;
253
257 struct Span
258 {
263
268 };
269
273 Span m_spans[Capacity];
274
278 uint32_t m_count = 0;
279
284
290 inline void
291 reset(T a_horizon) noexcept;
292
300 inline void
301 add(const SpanEndT<T>& a_in, const SpanEndT<T>& a_out) noexcept;
302
312 inline void
313 addClipped(T a_in, T a_out, T a_t0, T a_t1) noexcept;
314};
315
345template <class S, class = void>
347{
348};
349
365template <class T, class S>
366[[nodiscard]] EBGEOMETRY_HOST_DEVICE
367inline HitT<T>
368firstHit(const S& a_shape,
369 const RayT<T>& a_ray,
370 typename RayT<T>::Scalar a_tMin,
371 typename RayT<T>::Scalar a_tMax,
372 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
373
388template <class T, class S>
389[[nodiscard]] EBGEOMETRY_HOST_DEVICE
390inline HitT<T>
391firstHit(const S& a_shape,
392 const Vec3T<T>& a_from,
393 const Vec3T<T>& a_to,
394 T a_tMin = T(0),
395 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
396
410template <class T, class S>
411[[nodiscard]] EBGEOMETRY_HOST_DEVICE
412inline HitStatus
413intersects(const S& a_shape,
414 const RayT<T>& a_ray,
415 typename RayT<T>::Scalar a_tMin,
416 typename RayT<T>::Scalar a_tMax,
417 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
418
433template <class T, class S>
434[[nodiscard]] EBGEOMETRY_HOST_DEVICE
435inline HitStatus
436intersects(const S& a_shape,
437 const Vec3T<T>& a_from,
438 const Vec3T<T>& a_to,
439 T a_tMin = T(0),
440 const RayOptions<T>& a_options = RayOptions<T>()) noexcept;
441
442} // namespace EBGeometry
443
444#include "EBGeometry_RayImplem.hpp"
446
447#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...
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:84
@ 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.
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.
Definition EBGeometry_Ray.hpp:96
bool m_entering
Whether the ray enters the object there (it was outside just before), rather than leaving it.
Definition EBGeometry_Ray.hpp:129
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:135
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:106
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:124
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:118
HitStatus m_status
Whether the ray crosses the surface.
Definition EBGeometry_Ray.hpp:100
Vec3T< T > m_point
The point at m_t (for a hit or an unresolved query).
Definition EBGeometry_Ray.hpp:111
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:347
How a query marches, where a shape has no ray form.
Definition EBGeometry_Ray.hpp:155
A span: where the ray enters, and where it leaves.
Definition EBGeometry_Ray.hpp:258
SpanEndT< T > m_out
Where it leaves.
Definition EBGeometry_Ray.hpp:267
SpanEndT< T > m_in
Where the ray enters.
Definition EBGeometry_Ray.hpp:262
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:248
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:54
Vec3T< T > m_direction
Unit direction.
Definition EBGeometry_Ray.hpp:68
T Scalar
Floating-point precision.
Definition EBGeometry_Ray.hpp:58
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:63
One end of a span: where a ray enters or leaves an object.
Definition EBGeometry_Ray.hpp:200
Kind m_kind
What the end is.
Definition EBGeometry_Ray.hpp:225
T m_t
Distance along the ray.
Definition EBGeometry_Ray.hpp:214
Kind
What an end is.
Definition EBGeometry_Ray.hpp:205
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:220
bool crossing() const noexcept
Whether the end is a crossing of the surface.