Bounded 3D grid-ray traversal with a documented tie policy (#12) #28

Merged
faumaray merged 2 commits from issue/12-grid-ray into main 2026-12-03 08:08:29 +00:00
2 changed files with 477 additions and 0 deletions

No files matched your search

+255
View File
@@ -0,0 +1,255 @@
#pragma once
#include <Void/Math/Vec.hpp>
#include <array>
#include <cmath>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <utility>
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<double>::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<double>(
std::numeric_limits<std::int64_t>::min()) ||
coordinate >
static_cast<double>(
std::numeric_limits<std::int64_t>::max())) {
status_ = GridRayStatus::out_of_range;
return;
}
cell_[axis] = static_cast<std::int64_t>(coordinate);
const double direction_value = direction_axis[axis];
if (direction_value == 0.0) {
step_[axis] = 0;
t_max_[axis] =
std::numeric_limits<double>::infinity();
t_delta_[axis] =
std::numeric_limits<double>::infinity();
continue;
}
any_direction = true;
step_[axis] = direction_value > 0.0 ? 1 : -1;
const double boundary =
step_[axis] > 0
? static_cast<double>(cell_[axis]) + 1.0
: static_cast<double>(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<GridRayVisit> 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<std::int64_t>::max()) ||
(step_[axis] < 0 &&
cell_[axis] == std::numeric_limits<std::int64_t>::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<std::int64_t, 3> cell_{};
std::array<int, 3> step_{};
std::array<double, 3> t_max_{};
std::array<double, 3> t_delta_{};
GridRayStatus status_{GridRayStatus::running};
std::size_t visited_{0};
bool started_{false};
bool zero_direction_{false};
};
struct GridRaycastResult {
std::optional<GridRayVisit> 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 <typename Occupancy>
[[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
+222
View File
@@ -0,0 +1,222 @@
#include <Void/Algorithms/GridRay.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <set>
#include <vector>
namespace
{
using Void::Algorithms::GridAxis;
using Void::Algorithms::GridCell;
using Void::Algorithms::GridFace;
using Void::Algorithms::GridRayIterator;
using Void::Algorithms::GridRayOptions;
using Void::Algorithms::GridRayStatus;
using Void::Algorithms::GridRayVisit;
using Void::Math::Vec3d;
[[nodiscard]] std::vector<GridRayVisit> take(GridRayIterator& iterator,
std::size_t count)
{
std::vector<GridRayVisit> visits;
for (std::size_t index = 0; index < count; ++index) {
auto visit = iterator.next();
if (!visit.has_value()) {
break;
}
visits.push_back(*visit);
}
return visits;
}
[[nodiscard]] GridCell cell(std::int64_t x, std::int64_t y, std::int64_t z)
{
return GridCell{.x = x, .y = y, .z = z};
}
} // namespace
Void::Testing::Test grid_ray_forward_corner_tie_order()
{
GridRayIterator iterator{Vec3d{31.5, 31.5, 31.5}, Vec3d{1, 1, 1},
GridRayOptions{.max_steps = 10}};
const auto visits = take(iterator, 5);
Void::Testing::require(visits.size() == 5);
Void::Testing::expect(visits[0].cell == cell(31, 31, 31));
Void::Testing::expect(visits[0].face == GridFace{});
Void::Testing::expect_eq(visits[0].t, 0.0);
// Exact corner tie: x first, then y, then z.
Void::Testing::expect(visits[1].cell == cell(32, 31, 31));
Void::Testing::expect(visits[1].face.axis == GridAxis::x);
Void::Testing::expect_eq(visits[1].face.sign, 1);
Void::Testing::expect_eq(visits[1].t, 0.5);
Void::Testing::expect(visits[2].cell == cell(32, 32, 31));
Void::Testing::expect(visits[2].face.axis == GridAxis::y);
Void::Testing::expect(visits[3].cell == cell(32, 32, 32));
Void::Testing::expect(visits[3].face.axis == GridAxis::z);
Void::Testing::expect(visits[4].cell == cell(33, 32, 32));
Void::Testing::expect(visits[4].face.axis == GridAxis::x);
Void::Testing::expect_eq(visits[4].t, 1.5);
}
Void::Testing::Test grid_ray_reverse_corner()
{
GridRayIterator iterator{Vec3d{32.5, 32.5, 32.5}, Vec3d{-1, -1, -1},
GridRayOptions{.max_steps = 10}};
const auto visits = take(iterator, 4);
Void::Testing::require(visits.size() == 4);
Void::Testing::expect(visits[0].cell == cell(32, 32, 32));
Void::Testing::expect(visits[1].cell == cell(31, 32, 32));
Void::Testing::expect(visits[1].face.axis == GridAxis::x);
Void::Testing::expect_eq(visits[1].face.sign, -1);
Void::Testing::expect(visits[2].cell == cell(31, 31, 32));
Void::Testing::expect(visits[3].cell == cell(31, 31, 31));
}
Void::Testing::Test grid_ray_negative_origin()
{
GridRayIterator iterator{Vec3d{-0.5, -0.5, -0.5}, Vec3d{-1, -1, -1},
GridRayOptions{.max_steps = 10}};
const auto visits = take(iterator, 4);
Void::Testing::require(visits.size() == 4);
Void::Testing::expect(visits[0].cell == cell(-1, -1, -1));
Void::Testing::expect(visits[3].cell == cell(-2, -2, -2));
Void::Testing::expect_eq(visits[3].t, 0.5);
}
Void::Testing::Test grid_ray_axis_parallel()
{
GridRayIterator iterator{Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0},
GridRayOptions{.max_steps = 4}};
const auto visits = take(iterator, 3);
Void::Testing::require(visits.size() == 3);
for (std::size_t index = 0; index < visits.size(); ++index) {
Void::Testing::expect(visits[index].cell ==
cell(static_cast<std::int64_t>(index), 0, 0));
Void::Testing::expect(visits[index].cell.y == 0);
Void::Testing::expect(visits[index].cell.z == 0);
}
}
Void::Testing::Test grid_ray_start_on_boundary()
{
{
GridRayIterator iterator{Vec3d{32.0, 0.5, 0.5}, Vec3d{1, 0, 0},
GridRayOptions{.max_steps = 2}};
const auto visits = take(iterator, 2);
Void::Testing::require(visits.size() == 2);
Void::Testing::expect(visits[0].cell == cell(32, 0, 0));
Void::Testing::expect(visits[1].cell == cell(33, 0, 0));
Void::Testing::expect_eq(visits[1].t, 1.0);
}
{
// Exactly on the boundary moving negative: the point is assigned to
// the positive cell, then leaves it immediately at t == 0.
GridRayIterator iterator{Vec3d{32.0, 0.5, 0.5}, Vec3d{-1, 0, 0},
GridRayOptions{.max_steps = 2}};
const auto visits = take(iterator, 2);
Void::Testing::require(visits.size() == 2);
Void::Testing::expect(visits[0].cell == cell(32, 0, 0));
Void::Testing::expect(visits[1].cell == cell(31, 0, 0));
Void::Testing::expect_eq(visits[1].t, 0.0);
}
}
Void::Testing::Test grid_ray_ending_exactly_on_boundary()
{
GridRayIterator iterator{
Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0},
GridRayOptions{.max_steps = 10, .max_distance = 0.5}};
const auto visits = take(iterator, 2);
Void::Testing::require(visits.size() == 2);
Void::Testing::expect(visits[1].cell == cell(1, 0, 0));
Void::Testing::expect_eq(visits[1].t, 0.5);
Void::Testing::expect(!iterator.next().has_value());
Void::Testing::expect(iterator.status() ==
GridRayStatus::exhausted_distance);
}
Void::Testing::Test grid_ray_zero_direction_yields_start_once()
{
GridRayIterator iterator{Vec3d{2.5, 3.5, 4.5}, Vec3d{0, 0, 0},
GridRayOptions{.max_steps = 4}};
const auto visits = take(iterator, 4);
Void::Testing::require(visits.size() == 1);
Void::Testing::expect(visits[0].cell == cell(2, 3, 4));
Void::Testing::expect(iterator.status() == GridRayStatus::zero_direction);
}
Void::Testing::Test grid_ray_rejects_nonfinite_and_out_of_range()
{
GridRayIterator nan_origin{
Vec3d{std::numeric_limits<double>::quiet_NaN(), 0, 0},
Vec3d{1, 0, 0}};
Void::Testing::expect(!nan_origin.next().has_value());
Void::Testing::expect(nan_origin.status() ==
GridRayStatus::nonfinite_input);
GridRayIterator huge_origin{Vec3d{1e300, 0, 0}, Vec3d{1, 0, 0}};
Void::Testing::expect(!huge_origin.next().has_value());
Void::Testing::expect(huge_origin.status() ==
GridRayStatus::out_of_range);
}
Void::Testing::Test grid_ray_step_exhaustion()
{
GridRayIterator iterator{Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0},
GridRayOptions{.max_steps = 3}};
const auto visits = take(iterator, 10);
Void::Testing::expect_eq(visits.size(), std::size_t{3});
Void::Testing::expect(iterator.status() ==
GridRayStatus::exhausted_steps);
}
Void::Testing::Test grid_raycast_first_occupied_and_chunk_order()
{
const std::set<GridCell> occupied{cell(2, 0, 0), cell(0, 0, 0)};
const auto query = [&occupied](const GridCell& value) {
return occupied.contains(value);
};
auto first = Void::Algorithms::raycast_grid(
Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0}, query);
Void::Testing::require(first.hit.has_value());
// The start cell is occupied and reports no entry face.
Void::Testing::expect(first.hit->cell == cell(0, 0, 0));
Void::Testing::expect(first.hit->face == GridFace{});
const auto query_without_start = [](const GridCell& value) {
return value == cell(2, 0, 0);
};
auto later = Void::Algorithms::raycast_grid(
Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0}, query_without_start);
Void::Testing::require(later.hit.has_value());
Void::Testing::expect(later.hit->cell == cell(2, 0, 0));
Void::Testing::expect(later.hit->face.axis == GridAxis::x);
Void::Testing::expect_eq(later.hit->face.sign, 1);
Void::Testing::expect_eq(later.visited, std::size_t{3});
}
Void::Testing::Test grid_raycast_miss_reports_status()
{
auto result = Void::Algorithms::raycast_grid(
Vec3d{0.5, 0.5, 0.5}, Vec3d{1, 0, 0},
[](const GridCell&) { return false; },
GridRayOptions{.max_steps = 4});
Void::Testing::expect(!result.hit.has_value());
Void::Testing::expect(result.visited == std::size_t{4});
Void::Testing::expect(result.status == GridRayStatus::exhausted_steps);
}