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,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