Implement M9.4 revision-tagged microscopic environment rendering
Add SDL-free owned EnvironmentPresentation copied from M6.3 views (no store pointers or validation tokens), a single-slot EnvironmentLayer that rejects foreign/regressed updates and reports none/current/stale against observed authority watermarks, and a bounded camera projection with explicit missing, not-requested, present-zero, undefined-moisture and stale encodings (at most 2313 clipped rectangles, reused buffer). The engineering app renders a local in-memory fixture authority with configurable refresh cadence and layer selection. ADR-0080 records the decision; validation evidence and limits are in VALIDATION.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
#include "evo/client/environment_view.hpp"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
namespace evo::client {
|
||||
namespace {
|
||||
using world::MoistureStatus;
|
||||
constexpr auto water = static_cast<std::size_t>(world::Field::water);
|
||||
bool in_range(std::int64_t value) {
|
||||
return value >= -world::voxel_limit && value < world::voxel_limit;
|
||||
}
|
||||
bool ordered(const EnvironmentCell& a, const EnvironmentCell& b) {
|
||||
return std::pair{a.voxel.x, a.voxel.y} < std::pair{b.voxel.x, b.voxel.y};
|
||||
}
|
||||
bool valid_moisture(const EnvironmentCell& cell, std::uint32_t mask) {
|
||||
if ((mask & (1U << water)) == 0)
|
||||
return cell.moisture_status == MoistureStatus::not_requested;
|
||||
switch (cell.moisture_status) {
|
||||
case MoistureStatus::missing_water:
|
||||
return cell.presence[water] == FieldPresence::missing;
|
||||
case MoistureStatus::zero_capacity:
|
||||
return cell.presence[water] == FieldPresence::present;
|
||||
case MoistureStatus::available:
|
||||
return cell.presence[water] == FieldPresence::present && cell.moisture.denominator != 0 &&
|
||||
cell.moisture.numerator <= cell.moisture.denominator;
|
||||
case MoistureStatus::not_requested:
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool valid(const EnvironmentCell& cell, std::uint32_t mask, std::int64_t z) {
|
||||
if (cell.voxel.z != z || !in_range(cell.voxel.x) || !in_range(cell.voxel.y))
|
||||
return false; // Any u16 fill is within fill_scale.
|
||||
const auto measure = world::measure_habitat(cell.habitat);
|
||||
if (!measure || measure->volume != cell.volume)
|
||||
return false;
|
||||
for (std::size_t k = 0; k < world::field_count; ++k) {
|
||||
const bool requested = (mask & (1U << k)) != 0;
|
||||
if (requested == (cell.presence[k] == FieldPresence::not_requested) ||
|
||||
(cell.presence[k] != FieldPresence::present && cell.quantity[k] != 0))
|
||||
return false;
|
||||
}
|
||||
return valid_moisture(cell, mask);
|
||||
}
|
||||
bool regressed(EnvironmentRevision next, EnvironmentRevision previous) {
|
||||
return next.tick < previous.tick || next.field_revision < previous.field_revision;
|
||||
}
|
||||
// Both sides are strictly ordered by (x, y): a linear merge finds shared voxels.
|
||||
bool chunk_regressed(std::span<const EnvironmentCell> next,
|
||||
std::span<const EnvironmentCell> previous) {
|
||||
std::size_t i = 0, j = 0;
|
||||
while (i < next.size() && j < previous.size()) {
|
||||
if (ordered(next[i], previous[j]))
|
||||
++i;
|
||||
else if (ordered(previous[j], next[i]))
|
||||
++j;
|
||||
else if (next[i++].chunk_revision < previous[j++].chunk_revision)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<EnvironmentPresentation, ViewError>
|
||||
EnvironmentPresentation::create(EnvironmentRevision revision, std::uint32_t requested_mask,
|
||||
std::int64_t slice_z, std::span<const EnvironmentCell> cells) {
|
||||
if (requested_mask == 0 || (requested_mask & ~world::all_environment_fields) != 0 ||
|
||||
!in_range(slice_z))
|
||||
return std::unexpected(ViewError::invalid_view);
|
||||
if (cells.empty())
|
||||
return std::unexpected(ViewError::empty_slice);
|
||||
if (cells.size() > max_environment_cells)
|
||||
return std::unexpected(ViewError::too_many_cells);
|
||||
for (std::size_t i = 0; i < cells.size(); ++i)
|
||||
if (!valid(cells[i], requested_mask, slice_z) ||
|
||||
(i > 0 && !ordered(cells[i - 1], cells[i])))
|
||||
return std::unexpected(ViewError::invalid_view);
|
||||
return EnvironmentPresentation{revision, requested_mask, slice_z, cells};
|
||||
}
|
||||
|
||||
std::expected<EnvironmentPresentation, ViewError>
|
||||
present_environment(const world::EnvironmentView& view, std::int64_t slice_z) {
|
||||
std::vector<EnvironmentCell> cells;
|
||||
for (const auto& sample : view.samples()) {
|
||||
if (sample.voxel.z != slice_z)
|
||||
continue;
|
||||
EnvironmentCell cell;
|
||||
cell.voxel = sample.voxel;
|
||||
cell.chunk_revision = sample.chunk_revision;
|
||||
cell.habitat = sample.habitat.box;
|
||||
cell.volume = sample.measure.volume;
|
||||
cell.substrate = sample.habitat.substrate;
|
||||
cell.terrain_material = sample.terrain.material();
|
||||
cell.terrain_fill = sample.terrain.fill();
|
||||
cell.moisture_status = sample.moisture_status;
|
||||
cell.moisture = sample.moisture.value_or(world::WaterSaturation{});
|
||||
for (std::size_t k = 0; k < world::field_count; ++k) {
|
||||
if ((view.requested_mask() & (1U << k)) == 0)
|
||||
continue;
|
||||
const auto& field = sample.fields[k];
|
||||
cell.presence[k] = field ? FieldPresence::present : FieldPresence::missing;
|
||||
if (field)
|
||||
cell.quantity[k] = std::visit(
|
||||
[](const auto& value) -> std::uint64_t {
|
||||
if constexpr (std::is_same_v<std::decay_t<decltype(value)>,
|
||||
world::EnvironmentMass>)
|
||||
return value.amount.value;
|
||||
else
|
||||
return value.value;
|
||||
},
|
||||
*field);
|
||||
}
|
||||
cells.push_back(cell);
|
||||
}
|
||||
// World views order by (x, y, z); one Z slice therefore keeps (x, y) order.
|
||||
return EnvironmentPresentation::create({view.identity(), view.tick(), view.field_revision()},
|
||||
view.requested_mask(), slice_z, cells);
|
||||
}
|
||||
|
||||
std::expected<void, ViewError> EnvironmentLayer::accept(EnvironmentPresentation next) {
|
||||
const auto revision = next.revision();
|
||||
if (latest_ && revision.identity != latest_->identity)
|
||||
return std::unexpected(ViewError::foreign_world);
|
||||
if (current_ && (regressed(revision, current_->revision()) ||
|
||||
chunk_regressed(next.cells(), current_->cells())))
|
||||
return std::unexpected(ViewError::regressed);
|
||||
if (latest_)
|
||||
latest_ = EnvironmentRevision{revision.identity, std::max(latest_->tick, revision.tick),
|
||||
std::max(latest_->field_revision, revision.field_revision)};
|
||||
else
|
||||
latest_ = revision;
|
||||
current_ = std::move(next);
|
||||
stale_ = *latest_ != revision;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, ViewError> EnvironmentLayer::observe(EnvironmentRevision latest) {
|
||||
if (latest_ && latest.identity != latest_->identity)
|
||||
return std::unexpected(ViewError::foreign_world);
|
||||
if (latest_ && regressed(latest, *latest_))
|
||||
return std::unexpected(ViewError::regressed);
|
||||
latest_ = latest;
|
||||
stale_ = stale_ || (current_ && latest != current_->revision());
|
||||
return {};
|
||||
}
|
||||
|
||||
void EnvironmentLayer::mark_stale() noexcept {
|
||||
stale_ = current_.has_value();
|
||||
}
|
||||
|
||||
void EnvironmentLayer::reset() noexcept {
|
||||
current_.reset();
|
||||
latest_.reset();
|
||||
stale_ = false;
|
||||
}
|
||||
|
||||
Freshness EnvironmentLayer::freshness() const noexcept {
|
||||
if (!current_)
|
||||
return Freshness::none;
|
||||
return stale_ ? Freshness::stale : Freshness::current;
|
||||
}
|
||||
} // namespace evo::client
|
||||
@@ -0,0 +1,186 @@
|
||||
#include "evo/render/environment.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
|
||||
namespace evo::render {
|
||||
namespace {
|
||||
using client::FieldPresence;
|
||||
using world::MoistureStatus;
|
||||
constexpr double pitch = static_cast<double>(world::cell_pitch_mm);
|
||||
constexpr double um_per_mm = 1000;
|
||||
constexpr double detail_units = 4; // Smaller habitats collapse to one encoded rectangle.
|
||||
constexpr double outline_units = 8; // Smaller voxels omit per-cell outlines.
|
||||
constexpr std::array<Color, 5> stops{{{68, 1, 84, 255},
|
||||
{59, 82, 139, 255},
|
||||
{33, 145, 140, 255},
|
||||
{94, 201, 98, 255},
|
||||
{253, 231, 37, 255}}};
|
||||
std::uint8_t mix(std::uint8_t a, std::uint8_t b, double t) {
|
||||
return static_cast<std::uint8_t>(std::lround(a + (b - a) * t));
|
||||
}
|
||||
Color mix(Color a, Color b, double t) {
|
||||
return {mix(a.red, b.red, t), mix(a.green, b.green, t), mix(a.blue, b.blue, t), 255};
|
||||
}
|
||||
Color terrain(world::MaterialId material, std::uint16_t fill) {
|
||||
constexpr Color void_color{20, 24, 32, 255};
|
||||
constexpr std::array<Color, 3> known{{void_color, {96, 96, 104, 255}, {120, 100, 72, 255}}};
|
||||
const Color solid =
|
||||
material.value < known.size() ? known.at(material.value) : Color{0, 200, 220, 255};
|
||||
return mix(void_color, solid, static_cast<double>(fill) / world::fill_scale);
|
||||
}
|
||||
// Camera projection from CAMERA_INPUT.md, without the half-open world-point check:
|
||||
// a voxel's upper edge may lie on the exclusive world bound.
|
||||
struct Projection {
|
||||
double centre_x, centre_y, scale, width, height;
|
||||
[[nodiscard]] double x(double mm) const {
|
||||
return (mm - centre_x) * scale + width / 2;
|
||||
}
|
||||
[[nodiscard]] double y(double mm) const {
|
||||
return (centre_y - mm) * scale + height / 2;
|
||||
}
|
||||
};
|
||||
struct Box {
|
||||
double left, top, right, bottom;
|
||||
};
|
||||
class Emitter {
|
||||
public:
|
||||
Emitter(std::vector<Rectangle>& out, double width, double height, bool stale)
|
||||
: out_{out}, width_{width}, height_{height}, stale_{stale} {}
|
||||
void fill(Box b, Color color, bool tint = true) {
|
||||
const double left = std::max(b.left, 0.0), top = std::max(b.top, 0.0);
|
||||
const double right = std::min(b.right, width_), bottom = std::min(b.bottom, height_);
|
||||
if (!(right > left && bottom > top))
|
||||
return;
|
||||
out_.push_back({static_cast<float>(left), static_cast<float>(top),
|
||||
static_cast<float>(right - left), static_cast<float>(bottom - top),
|
||||
tint && stale_ ? dim(color) : color});
|
||||
}
|
||||
void outline(Box b, Color color, bool tint = true) {
|
||||
fill({b.left, b.top, b.right, b.top + 1}, color, tint);
|
||||
fill({b.left, b.bottom - 1, b.right, b.bottom}, color, tint);
|
||||
fill({b.left, b.top + 1, b.left + 1, b.bottom - 1}, color, tint);
|
||||
fill({b.right - 1, b.top + 1, b.right, b.bottom - 1}, color, tint);
|
||||
}
|
||||
void checker(Box b) {
|
||||
const double mid_x = (b.left + b.right) / 2, mid_y = (b.top + b.bottom) / 2;
|
||||
fill({b.left, b.top, mid_x, mid_y}, palette::missing_a);
|
||||
fill({mid_x, b.top, b.right, mid_y}, palette::missing_b);
|
||||
fill({b.left, mid_y, mid_x, b.bottom}, palette::missing_b);
|
||||
fill({mid_x, mid_y, b.right, b.bottom}, palette::missing_a);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Rectangle>& out_;
|
||||
double width_, height_;
|
||||
bool stale_;
|
||||
};
|
||||
enum class Encoding : std::uint8_t { value, not_requested, missing, undefined };
|
||||
struct Sample {
|
||||
Encoding encoding;
|
||||
Color color;
|
||||
};
|
||||
Sample field_sample(const client::EnvironmentCell& cell, std::size_t k) {
|
||||
switch (cell.presence.at(k)) {
|
||||
case FieldPresence::not_requested:
|
||||
return {Encoding::not_requested, palette::not_requested};
|
||||
case FieldPresence::missing:
|
||||
return {Encoding::missing, palette::missing_a};
|
||||
case FieldPresence::present:
|
||||
break;
|
||||
}
|
||||
if (cell.quantity.at(k) == 0)
|
||||
return {Encoding::value, palette::present_zero};
|
||||
const double density = std::log2(static_cast<double>(cell.quantity.at(k))) -
|
||||
std::log2(static_cast<double>(cell.volume.value));
|
||||
return {Encoding::value, ramp((density - field_log2_min) / (field_log2_max - field_log2_min))};
|
||||
}
|
||||
Sample moisture_sample(const client::EnvironmentCell& cell) {
|
||||
switch (cell.moisture_status) {
|
||||
case MoistureStatus::not_requested:
|
||||
return {Encoding::not_requested, palette::not_requested};
|
||||
case MoistureStatus::missing_water:
|
||||
return {Encoding::missing, palette::missing_a};
|
||||
case MoistureStatus::zero_capacity:
|
||||
return {Encoding::undefined, palette::undefined};
|
||||
case MoistureStatus::available:
|
||||
break;
|
||||
}
|
||||
return {Encoding::value, ramp(static_cast<double>(cell.moisture.numerator) /
|
||||
static_cast<double>(cell.moisture.denominator))};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Color ramp(double t) noexcept {
|
||||
if (!(t > 0)) // Also maps NaN to the low end.
|
||||
return stops.front();
|
||||
if (t >= 1)
|
||||
return stops.back();
|
||||
const double position = t * static_cast<double>(stops.size() - 1);
|
||||
const auto index = static_cast<std::size_t>(position);
|
||||
return mix(stops.at(index), stops.at(index + 1), position - static_cast<double>(index));
|
||||
}
|
||||
|
||||
Color dim(Color color) noexcept {
|
||||
const auto half = [](std::uint8_t c) { return static_cast<std::uint8_t>(c / 2 + 48); };
|
||||
return {half(color.red), half(color.green), half(color.blue), color.alpha};
|
||||
}
|
||||
|
||||
std::expected<void, EnvironmentRenderError>
|
||||
build_environment(std::vector<Rectangle>& out, const client::EnvironmentPresentation* presentation,
|
||||
client::Freshness freshness, const client::Camera& camera,
|
||||
EnvironmentDisplay display) {
|
||||
if ((display.mode != EnvironmentDisplay::Mode::field &&
|
||||
display.mode != EnvironmentDisplay::Mode::moisture) ||
|
||||
static_cast<std::size_t>(display.field) >= world::field_count)
|
||||
return std::unexpected(EnvironmentRenderError::invalid_display);
|
||||
out.clear();
|
||||
out.reserve(max_environment_rectangles);
|
||||
const auto viewport = camera.viewport();
|
||||
const double width = viewport.width, height = viewport.height;
|
||||
if (freshness == client::Freshness::none)
|
||||
presentation = nullptr;
|
||||
const bool stale = presentation != nullptr && freshness == client::Freshness::stale;
|
||||
Emitter emit{out, width, height, stale};
|
||||
const Projection p{camera.centre().x_mm, camera.centre().y_mm, camera.units_per_mm(), width,
|
||||
height};
|
||||
if (presentation != nullptr)
|
||||
for (const auto& cell : presentation->cells()) {
|
||||
const double x0 = static_cast<double>(cell.voxel.x) * pitch;
|
||||
const double y0 = static_cast<double>(cell.voxel.y) * pitch;
|
||||
const Box voxel{p.x(x0), p.y(y0 + pitch), p.x(x0 + pitch), p.y(y0)};
|
||||
if (voxel.right <= 0 || voxel.left >= width || voxel.bottom <= 0 || voxel.top >= height)
|
||||
continue; // Culled before emitting anything for this cell.
|
||||
emit.fill(voxel, terrain(cell.terrain_material, cell.terrain_fill));
|
||||
const auto& h = cell.habitat;
|
||||
const double hx = x0 + static_cast<double>(h.x.value) / um_per_mm;
|
||||
const double hy = y0 + static_cast<double>(h.y.value) / um_per_mm;
|
||||
const Box habitat{p.x(hx), p.y(hy + static_cast<double>(h.height.value) / um_per_mm),
|
||||
p.x(hx + static_cast<double>(h.width.value) / um_per_mm), p.y(hy)};
|
||||
const auto sample = display.mode == EnvironmentDisplay::Mode::field
|
||||
? field_sample(cell, static_cast<std::size_t>(display.field))
|
||||
: moisture_sample(cell);
|
||||
const bool detail = habitat.right - habitat.left >= detail_units &&
|
||||
habitat.bottom - habitat.top >= detail_units;
|
||||
if (detail && sample.encoding == Encoding::missing)
|
||||
emit.checker(habitat);
|
||||
else if (detail && sample.encoding == Encoding::not_requested)
|
||||
emit.outline(habitat, sample.color);
|
||||
else
|
||||
emit.fill(habitat, sample.color);
|
||||
if (voxel.right - voxel.left >= outline_units)
|
||||
emit.outline(voxel, stale ? palette::stale_outline : palette::voxel_outline, false);
|
||||
}
|
||||
const Color status = presentation == nullptr ? palette::status_none
|
||||
: stale ? palette::status_stale
|
||||
: palette::status_current;
|
||||
emit.fill({width - 72, 8, width - 8, 16}, status, false);
|
||||
for (std::size_t i = 0; i < environment_legend_steps; ++i) {
|
||||
const double x = 8 + 12 * static_cast<double>(i);
|
||||
emit.fill({x, height - 16, x + 12, height - 8},
|
||||
ramp(static_cast<double>(i) / static_cast<double>(environment_legend_steps - 1)),
|
||||
false);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace evo::render
|
||||
Reference in New Issue
Block a user