54 double window_x,
double window_y,
55 uint32_t
width, uint32_t height,
58 float ndc_x = (
static_cast<float>(window_x) /
static_cast<float>(
width)) * 2.0F - 1.0F;
59 float ndc_y = 1.0F - (
static_cast<float>(window_y) /
static_cast<float>(height)) * 2.0F;
63 glm::vec4 near_h = inv_vp * glm::vec4(ndc_x, ndc_y, 0.0F, 1.0F);
64 glm::vec4 far_h = inv_vp * glm::vec4(ndc_x, ndc_y, 1.0F, 1.0F);
66 glm::vec3 near_w = glm::vec3(near_h) / near_h.w;
67 glm::vec3 far_w = glm::vec3(far_h) / far_h.w;
69 return { .origin = near_w, .direction = glm::normalize(far_w - near_w) };
86 const glm::vec3& point,
87 float* out_t =
nullptr)
89 glm::vec3 op = point - ray.
origin;
92 glm::vec3 diff = point - closest;
96 return glm::dot(diff, diff);
115[[nodiscard]]
inline std::optional<HitResult>
ray_cast(
120 float tol_sq = tolerance * tolerance;
121 std::optional<HitResult> best;
123 for (
const auto& [
id, pos] : index.
all()) {
127 if (t < 0.0F || d_sq > tol_sq) {
131 float d = std::sqrt(d_sq);
133 if (!best || d < best->
distance || (d == best->distance && t < best->t)) {
153 float tol_sq = tolerance * tolerance;
154 std::vector<HitResult> results;
156 for (
const auto& [
id, pos] : index.
all()) {
160 if (t < 0.0F || d_sq > tol_sq) {
166 .distance = std::sqrt(d_sq),
171 return a.distance <
b.distance;
std::vector< std::pair< uint32_t, PointT > > all() const
Return all entity ids and positions from the published snapshot.
Lock-free spatial acceleration structure with atomic snapshot publication.
std::vector< HitResult > ray_cast_all(const SpatialIndex3D &index, const Ray &ray, float tolerance)
Find all entities within tolerance of a ray, sorted by distance.
Ray screen_to_ray(double window_x, double window_y, uint32_t width, uint32_t height, const ViewTransform &vt)
Construct a world-space ray from window pixel coordinates.
std::optional< HitResult > ray_cast(const SpatialIndex3D &index, const Ray &ray, float tolerance)
Find the closest entity to a ray within a tolerance radius.
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.
SpatialField distance(const glm::vec3 &anchor, float radius, DistanceMetric metric=DistanceMetric::EUCLIDEAN)
Normalized distance from an anchor point using the specified metric.
Entity id and perpendicular distance from the ray axis.
Origin and normalized direction in world space.