223 lines
7.5 KiB
C++
223 lines
7.5 KiB
C++
#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);
|
|
}
|