From acd4ff40f0c404c33e4a45f07ddbce4a3d6d90cf Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 06:53:27 +0000 Subject: [PATCH] feat(Algorithms): bounded 3D grid-ray traversal with a documented tie policy (#12) --- include/Void/Algorithms/GridRay.hpp | 255 ++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 include/Void/Algorithms/GridRay.hpp diff --git a/include/Void/Algorithms/GridRay.hpp b/include/Void/Algorithms/GridRay.hpp new file mode 100644 index 0000000..d68b563 --- /dev/null +++ b/include/Void/Algorithms/GridRay.hpp @@ -0,0 +1,255 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Void::Algorithms +{ + +/// Integer cell index in the voxel grid. +struct GridCell { + std::int64_t x{0}; + std::int64_t y{0}; + std::int64_t z{0}; + + [[nodiscard]] auto operator<=>(const GridCell&) const noexcept = default; + [[nodiscard]] bool operator==(const GridCell&) const noexcept = default; +}; + +enum class GridAxis : std::uint8_t { + none = 0, + x, + y, + z, +}; + +/// Face crossed when entering a cell. +/// +/// `sign` is the direction of travel along `axis`: `+1` means the ray crossed +/// the lower boundary (entered through the cell's negative face), `-1` means +/// it crossed the upper boundary. `axis == none` is the starting cell. +struct GridFace { + GridAxis axis{GridAxis::none}; + int sign{0}; + + [[nodiscard]] bool operator==(const GridFace&) const noexcept = default; +}; + +struct GridRayOptions { + /// Maximum number of cells yielded, including the start cell. + std::size_t max_steps{1024}; + /// Maximum ray parameter for entering a cell. The start cell (t = 0) is + /// always yielded; a later cell is yielded only when its entry t is + /// `<= max_distance`. + double max_distance{std::numeric_limits::infinity()}; +}; + +struct GridRayVisit { + GridCell cell; + GridFace face; + /// Ray parameter at which the cell was entered (0 for the start cell). + double t{0}; +}; + +enum class GridRayStatus : std::uint8_t { + /// More cells may be produced. + running = 0, + exhausted_steps, + exhausted_distance, + /// The direction was exactly (0, 0, 0); the start cell was still yielded. + zero_direction, + nonfinite_input, + /// A cell index would leave the int64 range. + out_of_range, +}; + +/// Bounded 3D grid traversal (Amanatides-Woo DDA) over integer cells. +/// +/// Policy: +/// - The start cell is `floor(origin)` per axis, so a coordinate exactly on a +/// boundary belongs to the cell on the positive side. +/// - Zero direction components never cross that axis (axis-parallel rays work). +/// - At simultaneous axis crossings (edge/corner ties) the axis with the lowest +/// index is stepped first: x, then y, then z. The traversal may therefore +/// visit the intermediate edge/corner cell; each visit reports the face it +/// crossed to enter. +/// - Nonfinite origin/direction is rejected before any visit. An all-zero +/// direction yields the start cell once and then reports `zero_direction`. +/// - The work is bounded by `max_steps` and `max_distance`. +class GridRayIterator +{ + public: + + GridRayIterator(Void::Math::Vec3d origin, + Void::Math::Vec3d direction, + GridRayOptions options = {}) + : options_(options) + { + if (!std::isfinite(origin.x) || !std::isfinite(origin.y) || + !std::isfinite(origin.z) || !std::isfinite(direction.x) || + !std::isfinite(direction.y) || + !std::isfinite(direction.z) || + std::isnan(options.max_distance)) { + status_ = GridRayStatus::nonfinite_input; + return; + } + + const double origin_axis[3] = {origin.x, origin.y, origin.z}; + const double direction_axis[3] = {direction.x, direction.y, + direction.z}; + bool any_direction = false; + for (int axis = 0; axis < 3; ++axis) { + const double coordinate = std::floor(origin_axis[axis]); + if (coordinate < + static_cast( + std::numeric_limits::min()) || + coordinate > + static_cast( + std::numeric_limits::max())) { + status_ = GridRayStatus::out_of_range; + return; + } + cell_[axis] = static_cast(coordinate); + + const double direction_value = direction_axis[axis]; + if (direction_value == 0.0) { + step_[axis] = 0; + t_max_[axis] = + std::numeric_limits::infinity(); + t_delta_[axis] = + std::numeric_limits::infinity(); + continue; + } + any_direction = true; + step_[axis] = direction_value > 0.0 ? 1 : -1; + const double boundary = + step_[axis] > 0 + ? static_cast(cell_[axis]) + 1.0 + : static_cast(cell_[axis]); + t_max_[axis] = + (boundary - origin_axis[axis]) / direction_value; + t_delta_[axis] = + 1.0 / std::abs(direction_value); + } + zero_direction_ = !any_direction; + } + + [[nodiscard]] std::optional next() + { + if (status_ != GridRayStatus::running) { + return std::nullopt; + } + if (!started_) { + if (options_.max_steps == 0) { + status_ = GridRayStatus::exhausted_steps; + return std::nullopt; + } + started_ = true; + visited_ = 1; + const GridRayVisit visit{.cell = GridCell{cell_[0], cell_[1], + cell_[2]}, + .face = {}, + .t = 0.0}; + if (zero_direction_) { + status_ = GridRayStatus::zero_direction; + } else if (visited_ >= options_.max_steps) { + status_ = GridRayStatus::exhausted_steps; + } + return visit; + } + + int axis = 0; + if (t_max_[1] < t_max_[axis]) { + axis = 1; + } + if (t_max_[2] < t_max_[axis]) { + axis = 2; + } + const double t = t_max_[axis]; + if (t > options_.max_distance) { + status_ = GridRayStatus::exhausted_distance; + return std::nullopt; + } + if ((step_[axis] > 0 && + cell_[axis] == std::numeric_limits::max()) || + (step_[axis] < 0 && + cell_[axis] == std::numeric_limits::min())) { + status_ = GridRayStatus::out_of_range; + return std::nullopt; + } + cell_[axis] += step_[axis]; + t_max_[axis] += t_delta_[axis]; + ++visited_; + const GridRayVisit visit{ + .cell = GridCell{cell_[0], cell_[1], cell_[2]}, + .face = GridFace{ + .axis = axis == 0 ? GridAxis::x + : axis == 1 ? GridAxis::y : GridAxis::z, + .sign = step_[axis]}, + .t = t}; + if (visited_ >= options_.max_steps) { + status_ = GridRayStatus::exhausted_steps; + } + return visit; + } + + [[nodiscard]] GridRayStatus status() const noexcept + { + return status_; + } + + [[nodiscard]] GridCell cell() const noexcept + { + return GridCell{cell_[0], cell_[1], cell_[2]}; + } + + private: + + GridRayOptions options_; + std::array cell_{}; + std::array step_{}; + std::array t_max_{}; + std::array t_delta_{}; + GridRayStatus status_{GridRayStatus::running}; + std::size_t visited_{0}; + bool started_{false}; + bool zero_direction_{false}; +}; + +struct GridRaycastResult { + std::optional hit; + GridRayStatus status{GridRayStatus::running}; + std::size_t visited{0}; +}; + +/// Traverse until `occupied(cell)` is true, then return that cell and the face +/// it was entered through (the start cell reports `GridFace{}`). +template +[[nodiscard]] GridRaycastResult + raycast_grid(Void::Math::Vec3d origin, + Void::Math::Vec3d direction, + Occupancy&& occupied, + GridRayOptions options = {}) +{ + GridRayIterator iterator{origin, direction, options}; + GridRaycastResult result; + while (const auto visit = iterator.next()) { + ++result.visited; + if (occupied(visit->cell)) { + result.hit = *visit; + return result; + } + } + result.status = iterator.status(); + return result; +} + +} // namespace Void::Algorithms