mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 10:59:20 +02:00
The track layout library came in as a subproject of its own -- its own CMakeLists, its own install and export rules, its own include tree -- which only made the build harder to follow now that everything else is a module. It is part of the editor, so it lives where the editor lives. - editor/plan_*.cppm / plan_*.cpp: the fifteen interface units and twelve implementation units, flat in editor/ beside editorTerrain and orthophoto, with the plan_ prefix keeping them clear of what was already there (editor/turnouts is the preset catalogue, so the geometry is plan_turnout_geometry). - modules are named eu07.editor.plan_*, like every other module in the tree. - namespaces move under the editor's own: editor::plan, editor::plan::geometry, ::io, ::render. The library's old name is gone from the sources and the docs. - every moved file carries the MPL header the rest of the tree carries; it had none. version.hpp went with the subproject: macros only, and unused. - the umbrella header is dropped -- nothing imported it. - the unit tests survive the fold: editor/tests, behind EU07_EDITOR_PLAN_TESTS (off by default), compiles the interfaces a second time into a small library of its own, because a test can only import modules from a target it links and linking the game would drag the whole simulator in. The code under test needs nothing but a compiler. 7/7 pass. - a switch may now be written with two dashes as well: --editor beside -editor. Builds with clang 22 (-DWITH_BETTER_RENDERER=OFF); -editor and --editor both come up in the editor on empty ground.
72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
module;
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <utility>
|
|
|
|
module eu07.editor.plan_rail_renderer;
|
|
|
|
namespace editor::plan::render {
|
|
|
|
namespace {
|
|
|
|
// Offsets a centreline polyline sideways by @p dist metres (signed) along the
|
|
// per-vertex normal. The vertex tangent uses a central difference of the
|
|
// neighbours (forward/backward at the ends); the left normal of (dx, dy) is
|
|
// (-dy, dx), matching the project's (east=x, north=y) convention.
|
|
std::vector<Point> offset(const std::vector<Point>& pts, double dist) {
|
|
const std::size_t m = pts.size();
|
|
std::vector<Point> out(m);
|
|
for (std::size_t i = 0; i < m; ++i) {
|
|
double dx = 1.0, dy = 0.0;
|
|
if (m >= 2) {
|
|
if (i == 0) {
|
|
dx = pts[1].x - pts[0].x;
|
|
dy = pts[1].y - pts[0].y;
|
|
} else if (i + 1 == m) {
|
|
dx = pts[i].x - pts[i - 1].x;
|
|
dy = pts[i].y - pts[i - 1].y;
|
|
} else {
|
|
dx = pts[i + 1].x - pts[i - 1].x;
|
|
dy = pts[i + 1].y - pts[i - 1].y;
|
|
}
|
|
}
|
|
const double len = std::hypot(dx, dy);
|
|
const double nx = len > 0.0 ? -dy / len : 0.0;
|
|
const double ny = len > 0.0 ? dx / len : 0.0;
|
|
out[i] = Point{pts[i].x + nx * dist, pts[i].y + ny * dist};
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
RailRenderer::RailRenderer(double gauge_metres) noexcept
|
|
: half_gauge_{gauge_metres * 0.5} {}
|
|
|
|
void RailRenderer::add(const CentrelineElement& element) {
|
|
for (const double side : {half_gauge_, -half_gauge_}) {
|
|
RailPolyline rail;
|
|
rail.kind = element.kind;
|
|
rail.element_id = element.element_id;
|
|
rail.owner_id = element.owner_id;
|
|
rail.from_turnout = element.from_turnout;
|
|
rail.length = element.length;
|
|
rail.radius_start = element.radius_start;
|
|
rail.radius_end = element.radius_end;
|
|
rail.points = offset(element.points, side);
|
|
rails_.push_back(std::move(rail));
|
|
}
|
|
}
|
|
|
|
} // namespace editor::plan::render
|