18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 05:57:08 +01:00
Files
maszyna/tests/unit/test_takeover_fit.cpp
T
maj00r 3f5f2af1ce feat(editor): the old tracks recognized as lines of the domain: the curves read off the curvature, fitted by least squares, the deviation measured
The first part of E6 (editor-tracks-domain.md §10 "Automatyczne przejęcie odcinka", editor-tracks-tests.md §4 "E6"): a chain of the
paths of an old scenery, Bezier curves drawn by hand or by older tools, made a line of the domain (editor/layout/layoutRecognition).
Model only, nothing of the editor asks it yet.

- The chain sampled along its arc length in the plan, every metre for the fit and every half metre for the deviation, each joint with
  the turn of the heading there: the point, the heading and the curvature of the Bezier curve as the simulator draws it, the cant of the
  roll turned evenly with the parameter as the simulator turns it (1500 mm sin roll, of the other sign where a path runs against the
  chain).
- The curves found where the curvature changes: the heading differenced over 30 m (the noise of the ends of the old paths, written to the
  millimetre, and the ripple of the Bezier curves evened out), runs of one sign past 1/5000 m; a run whose curvature falls to a third of
  what it is on both sides cut in two; the arc of each where the curvature is half of it or more (the area over that width is the turn),
  its transitions where it rises from a tenth to nine tenths, the window taken off; a turn of more than a quarter of a circle laid as
  curves of a quarter at most. A turn of more than 5 mrad at one joint making up a curve is a kink (told, laid as the shortest curve
  which fits); an arc whose halves differ by a quarter is a compound curve (told, laid as one).
- Each curve a vertex of the domain, clothoid - arc - clothoid, its point where the straights fitted around it cross; then the points,
  the radii and the transitions of all of them fitted by least squares to the offsets of the points of the chain from the line the domain
  lays (Levenberg-Marquardt, the Jacobian by differences over the points each parameter moves; the transitions by their squares, since
  what a transition moves the curve by grows with its square; the points and the radii first, then all of it). An end held by what stays
  beyond it (a switch, an old track not taken over) keeps the first or the last vertex on its heading. The curves the curvature doesn't
  show (a radius over 5 km, a turn spread over the joints) are added where the line keeps off the points by the tolerance, six at most;
  the small turns of old tracks laid jagged, joint by joint, are left out where the line keeps nearer without them; where it keeps more
  than a metre off, the plan of the added curves alone is tried; a vertex whose tangents cross behind the curve tells nothing of it, and
  a vertex no radius lays is taken out.
- The cant of each vertex fitted to the rolls along its arc and its transitions; a cant the rolls ramp up with no transition under it is
  fitted again with transitions as long as the ramps, kept where the line keeps nearer so. The grade line: the heights as straight grades
  within 1 cm (Douglas and Peucker), the heights of the breaks by least squares, each vertical curve the largest of the usual radii which
  keeps within it.
- The deviation measured densely: each point of the chain every half metre set square onto the line, the farthest and the root mean
  square of the distance in the plan, of the height and of the cant, where the farthest is. The tolerance of a takeover with no question
  (takeover_tolerance): 20 mm in the plan and in the height, as the simulator itself joins the ends of the tracks within 2 cm, so the
  tracks made again lie where the simulator takes the old ones to be; 10 mm of the cant, 0.4 degree of the roll, under the half degree
  many old files round the rolls to. The norms aren't asked here.

Tests: U: three curves (R 800 m with clothoids of 80 m and 60 mm, R 600 m with 60 and 70 m and 90 mm, R 1500 m with none) and a grade line
with two breaks, cut at random places 8 to 25 m apart, each piece a Bezier curve, the joints shaken by up to 4 mm across and in height,
the rolls rounded to the tenth of a degree, seeds 1101, 2202 and 3303: the radii within 1 %, the transitions within 3 m or moving the arc
within 5 mm of where the laid ones move it, the cants within 3 mm, the deviation within the tolerance; the same walked the other way
round, the transitions swapped. A kink of 0.6 degree at a joint told and laid within the tolerance; an arc of 11 km found where the
curvature shows none; an end held in a heading 2 mrad off the chain kept to 1e-9 rad. Unit tests 1101 -> 1103.
2026-11-23 16:59:26 +01:00

125 lines
6.2 KiB
C++

// The recognition of old tracks (editor-tracks-domain.md §10, editor-tracks-tests.md §4 "E6", U): lines of the domain cut at random places
// into old tracks, one Bezier curve each, the joints shaken; the curvature sampled, the curves found and fitted by least squares, the
// radii and the transitions read back within a tolerance, the deviation of the line from the tracks measured.
#include "stdafx.h"
#include "doctest.h"
#include "takeover_fixture.h"
#include <format>
namespace {
using test::takeover_fixture::cut;
using test::takeover_fixture::laid;
using test::takeover_fixture::nearest;
using test::takeover_fixture::noise;
using test::takeover_fixture::turned_round;
using test::takeover_fixture::vertex_at;
// three curves: R 800 m with 80 m transitions and 60 mm, R 600 m with 60 and 70 m and 90 mm, R 1500 m with no transitions nor cant
layout::line_shape three_curves() {
layout::line_shape shape;
shape.vertices = {vertex_at({600.0, 0.0}, 800.0, 80.0, 80.0, 60.0), vertex_at({1100.0, 300.0}, 600.0, 60.0, 70.0, 90.0), vertex_at({1700.0, 200.0}, 1500.0, 0.0, 0.0, 0.0)};
shape.grades = {{900.0, 104.0, 10000.0}, {1600.0, 101.0, 8000.0}};
return shape;
}
glm::dvec3 const kStart{0.0, 100.0, 0.0};
glm::dvec3 const kEnd{2300.0, 102.0, 200.0};
std::string described(layout::recognized_line const &Line) {
std::string text;
for (auto const &v : Line.shape.vertices) {
text += std::format("[{:.2f} {:.2f} R {:.2f} in {:.2f} out {:.2f} D {:.1f}] ", v.point.x, v.point.y, v.radius, v.transition_in, v.transition_out, v.cant);
}
text += std::format("plan {:.4f} at {:.1f} {:.1f} rms {:.4f} height {:.4f} cant {:.2f}", Line.measured.plan, Line.measured.plan_at.x, Line.measured.plan_at.z, Line.measured.plan_rms, Line.measured.height, Line.measured.cant);
for (auto const &finding : Line.findings) {
text += "; " + finding;
}
return text;
}
// a transition found within 3 m of the one laid, or moving the arc by no more than 5 mm from where that one moves it: the shift of a
// transition, L^2 / 24R, tells a short one apart from none by too little to be read through the noise of the joints, and a Bezier curve
// of an old track across the start of an arc eases the curvature in over its length
bool same_transition(double const Found, double const Laid, double const Radius) {
return std::abs(Found - Laid) < 3.0 || std::abs(Found * Found - Laid * Laid) / (24.0 * Radius) < 0.005;
}
// each vertex of the original line found again: its radius within a percent, its transitions as same_transition() has it, its cant
// within 3 mm
void check_found(layout::line_shape const &Original, layout::recognized_line const &Found, bool const Turned) {
REQUIRE(Found.valid());
CHECK(Found.shape.vertices.size() == Original.vertices.size());
for (auto const &vertex : Original.vertices) {
CAPTURE(vertex.radius);
auto const *match{nearest(Found.shape, vertex.point, 5.0)};
REQUIRE(match != nullptr);
CHECK(std::abs(match->radius - vertex.radius) < vertex.radius * 0.01);
CHECK(same_transition(Turned ? match->transition_out : match->transition_in, vertex.transition_in, vertex.radius));
CHECK(same_transition(Turned ? match->transition_in : match->transition_out, vertex.transition_out, vertex.radius));
CHECK(std::abs(match->cant - vertex.cant) < 3.0);
}
}
} // namespace
TEST_CASE("takeover fit: three curves cut at random places, seeds 1101, 2202, 3303") {
auto const shape{three_curves()};
auto const line{laid(shape, kStart, kEnd)};
REQUIRE(line.valid());
for (unsigned const seed : {1101u, 2202u, 3303u}) {
CAPTURE(seed);
auto const old{cut(line, seed, 8.0, 25.0, noise{0.004, 0.004, true})};
auto const found{layout::recognize(old.chain)};
CAPTURE(described(found));
check_found(shape, found, false);
CHECK(found.measured.within(layout::takeover_tolerance{}));
CHECK(found.joints.size() == old.cuts.size());
// the walk the other way round: the same curves, the transitions the other way
auto const back{layout::recognize(turned_round(old.chain))};
CAPTURE(described(back));
check_found(shape, back, true);
CHECK(back.measured.within(layout::takeover_tolerance{}));
}
}
TEST_CASE("takeover fit: a kink at a joint told, a curve of a radius of 11 km found where the curvature shows none, a held end kept") {
// a straight of 300 m turning by 0.6 degree at a joint, a straight of 300 m on
auto const turn{glm::radians(0.6)};
glm::dvec2 const along{std::cos(turn), std::sin(turn)};
segment_data first;
first.points[segment_data::point::start] = {0.0, 10.0, 0.0};
first.points[segment_data::point::end] = {300.0, 10.0, 0.0};
segment_data second;
second.points[segment_data::point::start] = {300.0, 10.0, 0.0};
second.points[segment_data::point::end] = {300.0 + along.x * 300.0, 10.0, along.y * 300.0};
auto const kinked{layout::recognize({{first, true}, {second, true}})};
REQUIRE(kinked.valid());
REQUIRE(kinked.shape.vertices.size() == 1);
CHECK(glm::distance(kinked.shape.vertices.front().point, glm::dvec2{300.0, 0.0}) < 0.01);
REQUIRE_FALSE(kinked.findings.empty());
CHECK(kinked.findings.front().find("at a joint") != std::string::npos);
CHECK(kinked.measured.plan < 0.02);
// an arc of 11 km over 550 m in pieces of 50 m: 1/11000 is under what the curvature is read as a curve at, the line found keeping off it
layout::line_shape wide;
wide.vertices = {vertex_at({300.0, 0.0}, 11000.0, 0.0, 0.0, 0.0)};
auto const deflection{0.05};
auto const arc{laid(wide, {0.0, 0.0, 0.0}, {300.0 + std::cos(deflection) * 300.0, 0.0, std::sin(deflection) * 300.0})};
REQUIRE(arc.valid());
auto const old{cut(arc, 1212u, 45.0, 50.0, noise{0.0, 0.0, true})};
auto const found{layout::recognize(old.chain)};
CAPTURE(described(found));
REQUIRE(found.shape.vertices.size() == 1);
CHECK(found.shape.vertices.front().radius == doctest::Approx(11000.0).epsilon(0.01));
CHECK(found.measured.plan < 0.005);
// the end held by a switch in a heading the chain itself runs off by 2 mrad: the line ends in it
layout::recognition_options options;
options.end = {true, glm::normalize(glm::dvec2{std::cos(deflection + 0.002), std::sin(deflection + 0.002)})};
auto const held{layout::recognize(old.chain, options)};
REQUIRE(held.valid());
CHECK(std::abs(geometry::signed_angle(held.solved.end.heading, options.end.heading)) < 1e-9);
}