18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 09:27:10 +01:00
Files
maszyna/tests/unit/layout_fixture.h
T
maj00r 4a3b36c204 feat(editor): the turnouts of the track geometry, laid into a line, moved, changed and taken out, their legs nodes
E2b (editor-tracks-domain.md §2, §4 "Rozjazd", §7, §8): a turnout is an aggregate of the domain model, laid from the catalogue the
editor lays its switches from, its rules invariants which refuse.

- layoutTurnout: the type of the catalogue (switch_design::catalogue(), Id-4) as the model lays it: R, 1:n, a, b, the main length,
  the lead and the parts of the diverging track, the movable nose, the blades; the ordinary switches whose diverging track leaves
  tangentially, the double, symmetric and secant ones refused with the reason (E5). The diverging track laid off the start along the
  turnout: on a straight the curve of the type; bent with the main track the curve of the basic switch of the radius of table 11.8
  (switch_curve::bent_radius), over the length which keeps its turn off the main track, so the angle of the frog stays 1:n, the lead
  and the straight through the frog bent along with the main track; the cant of the main track all along it, the grade of its start.
  The speeds: the diverging one by switch_curve::branch_speed (the rule the editor writes), the main one the line's, bent no faster
  than the deficiency of the frog lets it; the velocity written by the same rule as the editor's.
- The turnout: its type, hand, direction along the line, its start, the ends of its two tracks (nodes), its main and diverging track
  (lines it owns), derived: the kilometrage of its start, the bending, the cant, the speeds. The network keeps the turnouts with the
  lines and the nodes, in the snapshots of undo and in the transactions; settle() has a turnout whose main track or start changed
  lay its diverging track again, move the node at its end and lay again the lines going on from there.
- The nodes: three ends at the start of a turnout, its two tracks on one side of it; the start of a diverging track tells its node
  nothing (the node keeps the curvature of the main track) and runs on into no line, the end of one sets its node.
- check(): the turnouts whose tracks or legs are touched, their invariants (the legs, the tracks, the main track as long as the type)
  and the rules of Q2 and 11.4 through switch_curve::check (the curvature of each track of one sign, KZW II refused; the cant the same
  over it; the ends of the ramps at its ends or 6 m off it), a switch of 1:9.403 or steeper than 1:9 bent in a main track refused (the
  model knows a main track from a side one, where the editor only warns), table 11.9 and a transition under it as warnings. The norms
  of the E1 module with the switch context: the elements of a turnout track are a connection of tracks of their frog (fixed or movable
  nose: the deficiency of tab. 9.7), bent or not (the cant of tab. 9.6), at its speeds; the chain of the line runs through the main
  track, the element cut by a turnout open. BLOCK findings are kept apart from the broken invariants (verdict::blocks).
- The commands: insert a turnout at a chainage of a line (cut at its legs, the part between them its main track), move it along its
  line (the lines on both sides and its main track one line again, cut anew, the ids kept, a line going on from the end of its
  diverging track carried with it), change its type or hand, remove it (the line one again where it can be: the merge laid and kept
  only where it's the same track within a micrometre). The tracks of a turnout aren't split, deleted or edited as lines; a line is joined
  to the free side of a turnout's start, or to the end of its diverging track from a straight (the curvature stepping there).

Tests: U: the types laid and refused; the legs on a straight against the catalogue for four types both hands both ways (the main
track a + b, the tangent of the diverging end meeting the main track at R tan(a/2), Id-4's a within half a millimetre, the angle 1:n,
the speeds of table 11.2); Rz 60E1-500-1:12 bent outward and inward into R 1200 m with 60 mm (r" 857.8 m and 352.7 m, 60 km/h, the
angle of the frog kept, the cant over it); refusals: KZW II, a ramp under it and 3 m off it, a 1:7.5 bent in a main track (laid in a
side track), no type, no hand, no room; moved, changed, turned, taken out with the line laid again within a micrometre, each a step
to undo; a line from the diverging end carried along. P: 250 random edits with turnouts each, seeds 20261124, 31 and 977 (laid,
moved, changed, taken out, with vertices moved and set, splits, joins, deletions, undo, redo): the invariants after each (the legs
and the nodes whole, three ends only at a start, the velocity by the rule), no BLOCK, a refusal changing nothing, an undo exact.
2026-11-23 12:41:49 +01:00

167 lines
7.0 KiB
C++

#pragma once
// the lines of the track geometry the tests of editor/layout edit and make tracks of: a line of three curves between free ends, the
// norms of a line of 80 km/h of mixed traffic, and the invariants of the lines and the nodes checked on their own, apart from the
// checks of the model
#include "doctest.h"
#include "editor/editorGeometry.hpp"
#include "editor/editorSwitchCurve.hpp"
#include "editor/layout/layoutCommands.hpp"
#include "editor/layout/layoutTurnout.hpp"
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
namespace test::layout_fixture {
inline norms::line_class line_class() {
norms::line_class result;
result.vmax = 80.0;
result.kind = norms::traffic::mixed;
result.track = norms::track_kind::main;
return result;
}
// a vertex with the cant and the transitions the norms prefer for the radius
inline layout::vertex curve(glm::dvec2 const &Point, double const Radius) {
auto const preferred{layout::preferred_curve(norms::profile_id::plk, line_class(), Radius)};
layout::vertex result;
result.point = Point;
result.radius = Radius;
result.cant = preferred.cant;
result.transition_in = result.transition_out = preferred.transition;
return result;
}
// three curves, R 800, 600 and 900, with straights between them, a break of the grade line with a vertical curve
inline layout::create_line three_curves(layout::transition_shape const Shape = layout::transition_shape::clothoid, glm::dvec3 const &Offset = glm::dvec3{0.0}) {
layout::create_line result;
result.start = glm::dvec3{0.0, 100.0, 0.0} + Offset;
result.end = glm::dvec3{2200.0, 104.0, 600.0} + Offset;
glm::dvec2 const shift{Offset.x, Offset.z};
result.vertices = {curve(glm::dvec2{600.0, 0.0} + shift, 800.0), curve(glm::dvec2{1100.0, 300.0} + shift, 600.0), curve(glm::dvec2{1600.0, 300.0} + shift, 900.0)};
result.grades = {{1000.0, 103.0 + Offset.y, 10000.0}};
result.transition = Shape;
result.ramp = Shape == layout::transition_shape::bloss ? layout::ramp_shape::bloss : layout::ramp_shape::linear;
result.category = line_class();
return result;
}
inline std::string reasons_of(layout::outcome const &Outcome) {
std::string result;
for (auto const &reason : Outcome.reasons) {
result += reason + "; ";
}
return result;
}
inline std::string reasons_of(layout::verdict const &Verdict) {
std::string result;
for (auto const &reason : Verdict.refusals()) {
result += reason + "; ";
}
return result;
}
// the invariants, checked here on their own: each line laid and running on from element to element, the cant running on, the ends at
// their nodes and running on through them, the topology of the ends (two at a node, three at the start of a turnout: its two tracks on one
// side of it); the turnouts whole, their main tracks as long as their types, their tracks from their start, their velocity by the speed rule;
// and the model's own check, the norms along the chains and the rules of the turnouts with it (check_invariants; check_structure leaves it out)
inline void check_structure(layout::network const &Network) {
for (auto const &[id, line] : Network.lines()) {
CAPTURE(layout::number(id));
auto const &solved{line.solved};
REQUIRE(solved.valid());
auto const *start{Network.find(line.start)};
auto const *end{Network.find(line.end)};
REQUIRE(start != nullptr);
REQUIRE(end != nullptr);
CHECK(line.start != line.end);
// the line begins at its node, to the bit, and ends there within the sum of the rounding along it
CHECK(solved.elements.front().origin == geometry::plan_of(start->position));
auto const &last{solved.elements.back()};
CHECK(glm::distance(last.position(last.length), geometry::plan_of(end->position)) < 1e-6);
CHECK(solved.start.position == start->position);
CHECK(solved.end.position == end->position);
for (std::size_t i = 1; i < solved.elements.size(); ++i) {
auto const &a{solved.elements[i - 1]};
auto const &b{solved.elements[i]};
CHECK(glm::distance(a.position(a.length), b.origin) < 1e-6);
CHECK(std::abs(geometry::signed_angle(a.tangent(a.length), b.direction)) < 1e-9);
CHECK(a.end_chainage() == doctest::Approx(b.chainage).epsilon(1e-12));
}
for (std::size_t i = 1; i < solved.cant.size(); ++i) {
CHECK(std::abs(solved.cant[i - 1].end - solved.cant[i].start) <= 0.5);
CHECK(solved.cant[i - 1].to == doctest::Approx(solved.cant[i].from).epsilon(1e-12));
}
CHECK(std::abs(solved.grade(0.0) - solved.start.grade) < 1e-12);
}
for (auto const &[id, node] : Network.nodes()) {
CAPTURE(layout::number(id));
REQUIRE_FALSE(node.ends.empty());
std::vector<layout::end_ref> through;
for (auto const &end : node.ends) {
if (layout::diverging_start(Network, end)) {
auto const *switched{layout::owner_of(Network, end.line)};
REQUIRE(switched != nullptr);
CHECK(switched->start == id);
auto const main{std::ranges::find_if(node.ends, [&](layout::end_ref const &Each) { return Each.line == switched->main; })};
REQUIRE(main != node.ends.end());
CHECK(main->side() == end.side());
}
else {
through.push_back(end);
}
}
REQUIRE(through.size() <= 2);
if (through.size() == 2) {
CHECK(through[0].side() != through[1].side());
}
for (auto const &end : node.ends) {
auto const *line{Network.find(end.line)};
REQUIRE(line != nullptr);
CHECK(line->node_at(end.end) == id);
auto const &values{end.end == layout::line_end::start ? line->solved.start : line->solved.end};
auto const sense{static_cast<double>(end.sense)};
CHECK(values.position == node.position);
CHECK(std::abs(geometry::signed_angle(node.heading, values.heading * sense)) <= 1e-9);
CHECK(std::abs(values.cant * sense - node.cant) <= 0.5);
CHECK(std::abs(values.grade * sense - node.grade) <= 0.002);
}
}
for (auto const &[id, turnout] : Network.turnouts()) {
CAPTURE(layout::number(id));
auto const type{layout::turnout_type_of(turnout.type)};
REQUIRE(type.has_value());
auto const *main{Network.find(turnout.main)};
auto const *diverging{Network.find(turnout.diverging)};
REQUIRE(main != nullptr);
REQUIRE(diverging != nullptr);
CHECK(main->turnout == id);
CHECK(diverging->turnout == id);
CHECK((turnout.direction > 0 ? main->start : main->end) == turnout.start);
CHECK((turnout.direction > 0 ? main->end : main->start) == turnout.main_end);
CHECK(diverging->start == turnout.start);
CHECK(diverging->end == turnout.diverging_end);
CHECK(main->solved.length == doctest::Approx(type->length).epsilon(1e-9));
CHECK(diverging->solved.start.position == Network.find(turnout.start)->position);
CHECK(diverging->solved.end.position == Network.find(turnout.diverging_end)->position);
CHECK(turnout.speed_diverging > 0.0);
CHECK(turnout.speed_main > 0.0);
CHECK(turnout.velocity == switch_curve::written_velocity(turnout.speed_main, turnout.speed_diverging));
}
}
inline void check_invariants(layout::network const &Network) {
check_structure(Network);
auto const verdict{layout::check_all(Network)};
auto const reasons{reasons_of(verdict)};
CAPTURE(reasons);
CHECK(verdict.fine());
}
} // namespace test::layout_fixture