Define M5.1 local voxel contract and expand M5-M9 gates

This commit is contained in:
dingo
2026-09-26 10:06:22 -04:00
parent c237e4da97
commit 69bbf364a9
14 changed files with 778 additions and 12 deletions
+101
View File
@@ -0,0 +1,101 @@
#include "evo/world/coordinates.hpp"
namespace evo::world {
namespace {
bool valid_edge(std::uint32_t edge) noexcept {
return edge == 8 || edge == 16 || edge == 32 || edge == 64;
}
bool inside(std::int64_t value, std::int64_t limit) noexcept {
return value >= -limit && value < limit;
}
bool valid_voxel(VoxelAddress voxel) noexcept {
return inside(voxel.x, voxel_limit) && inside(voxel.y, voxel_limit) &&
inside(voxel.z, voxel_limit);
}
bool valid_local(LocalCell local, std::uint32_t edge) noexcept {
return local.x < edge && local.y < edge && local.z < edge;
}
struct Axis {
std::int64_t chunk;
std::uint32_t local;
};
Axis split_axis(std::int64_t voxel, std::int64_t edge) noexcept {
auto chunk = voxel / edge;
auto local = voxel % edge;
if (local < 0) {
--chunk;
local += edge;
}
return {chunk, static_cast<std::uint32_t>(local)};
}
} // namespace
std::expected<ChunkLocation, WorldError> split(VoxelAddress voxel, std::uint32_t edge) noexcept {
if (!valid_edge(edge)) {
return std::unexpected{WorldError::invalid_edge};
}
if (!valid_voxel(voxel)) {
return std::unexpected{WorldError::voxel_out_of_range};
}
const auto x = split_axis(voxel.x, edge);
const auto y = split_axis(voxel.y, edge);
const auto z = split_axis(voxel.z, edge);
return ChunkLocation{{x.chunk, y.chunk, z.chunk}, {x.local, y.local, z.local}};
}
std::expected<VoxelAddress, WorldError> join(ChunkLocation location, std::uint32_t edge) noexcept {
if (!valid_edge(edge)) {
return std::unexpected{WorldError::invalid_edge};
}
const auto limit = voxel_limit / edge;
const auto chunk = location.chunk;
if (!inside(chunk.x, limit) || !inside(chunk.y, limit) || !inside(chunk.z, limit)) {
return std::unexpected{WorldError::chunk_out_of_range};
}
const auto local = location.local;
if (!valid_local(local, edge)) {
return std::unexpected{WorldError::local_out_of_range};
}
// Both operands bounded above; multiplication cannot overflow or escape the
// voxel envelope. Domain divisibility means no partial outer chunks exist.
return VoxelAddress{chunk.x * edge + local.x, chunk.y * edge + local.y,
chunk.z * edge + local.z};
}
std::expected<std::uint32_t, WorldError> ordinal(LocalCell local, std::uint32_t edge) noexcept {
if (!valid_edge(edge)) {
return std::unexpected{WorldError::invalid_edge};
}
if (!valid_local(local, edge)) {
return std::unexpected{WorldError::local_out_of_range};
}
return local.x + edge * (local.y + edge * local.z);
}
std::expected<LocalCell, WorldError> local_from_ordinal(std::uint32_t index,
std::uint32_t edge) noexcept {
if (!valid_edge(edge)) {
return std::unexpected{WorldError::invalid_edge};
}
if (index >= edge * edge * edge) {
return std::unexpected{WorldError::ordinal_out_of_range};
}
return LocalCell{index % edge, (index / edge) % edge, index / (edge * edge)};
}
std::expected<MetricCorner, WorldError> lower_corner_mm(VoxelAddress voxel) noexcept {
if (!valid_voxel(voxel)) {
return std::unexpected{WorldError::voxel_out_of_range};
}
return MetricCorner{
{voxel.x * cell_pitch_mm}, {voxel.y * cell_pitch_mm}, {voxel.z * cell_pitch_mm}};
}
std::expected<CellSample, WorldError> CellSample::create(MaterialId material,
std::uint32_t fill) noexcept {
if (fill > fill_scale || ((material.value == 0) != (fill == 0))) {
return std::unexpected{WorldError::invalid_cell};
}
return CellSample{material, static_cast<std::uint16_t>(fill)};
}
} // namespace evo::world