MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ ray_cast()

std::optional< HitResult > MayaFlux::Kinesis::ray_cast ( const SpatialIndex3D index,
const Ray ray,
float  tolerance 
)
inline

Find the closest entity to a ray within a tolerance radius.

Parameters
indexPublished SpatialIndex3D to query.
rayWorld-space ray (from screen_to_ray or constructed manually).
toleranceMaximum perpendicular distance from the ray axis for a hit.
Returns
Closest entity within tolerance, or std::nullopt if nothing was hit.

Iterates all entities in the published snapshot. Entities behind the ray origin (t < 0) are rejected. Among forward candidates within tolerance, returns the one with the smallest perpendicular distance. Ties broken by smaller t (closer to camera).

Definition at line 115 of file HitTest.hpp.

119{
120 float tol_sq = tolerance * tolerance;
121 std::optional<HitResult> best;
122
123 for (const auto& [id, pos] : index.all()) {
124 float t = 0.0F;
125 float d_sq = point_ray_distance_sq(ray, pos, &t);
126
127 if (t < 0.0F || d_sq > tol_sq) {
128 continue;
129 }
130
131 float d = std::sqrt(d_sq);
132
133 if (!best || d < best->distance || (d == best->distance && t < best->t)) {
134 best = HitResult { .id = id, .distance = d, .t = t };
135 }
136 }
137
138 return best;
139}
uint32_t id
float point_ray_distance_sq(const Ray &ray, const glm::vec3 &point, float *out_t=nullptr)
Squared perpendicular distance from a point to an infinite ray.
Definition HitTest.hpp:84

References MayaFlux::Kinesis::SpatialIndex< PointT >::all(), distance(), MayaFlux::Kinesis::HitResult::id, id, and point_ray_distance_sq().

+ Here is the call graph for this function: