mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 15:26:53 +01:00
3f5f2af1ce
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.
139 lines
5.3 KiB
C++
139 lines
5.3 KiB
C++
#pragma once
|
|
|
|
// old tracks made of a line of the domain, for the tests of their takeover (test_takeover_*.cpp): the line cut at random places, each
|
|
// piece one Bezier curve between its ends with the handles of an arc, the joints and the heights shaken a little and the rolls rounded to
|
|
// the tenth of a degree, as old sceneries have them
|
|
|
|
#include "doctest.h"
|
|
#include "editor/editorGeometry.hpp"
|
|
#include "editor/layout/layoutLine.hpp"
|
|
#include "editor/layout/layoutRecognition.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
namespace test::takeover_fixture {
|
|
|
|
struct noise {
|
|
double plan{0.0}; // m, the joints moved across the line, evenly within this either way
|
|
double height{0.0}; // m, up or down
|
|
bool round_rolls{true}; // to the tenth of a degree
|
|
};
|
|
|
|
struct old_line {
|
|
layout::solved_line line;
|
|
std::vector<layout::chain_path> chain; // as run along the line, some of the paths written the other way round
|
|
std::vector<double> cuts; // the chainages of the joints, the ends included
|
|
};
|
|
|
|
inline layout::vertex vertex_at(glm::dvec2 const &Point, double const Radius, double const In, double const Out, double const Cant) {
|
|
layout::vertex result;
|
|
result.point = Point;
|
|
result.radius = Radius;
|
|
result.transition_in = In;
|
|
result.transition_out = Out;
|
|
result.cant = Cant;
|
|
return result;
|
|
}
|
|
|
|
// the line laid between the ends, its vertices numbered from 1
|
|
inline layout::solved_line laid(layout::line_shape Shape, glm::dvec3 const &Start, glm::dvec3 const &End) {
|
|
for (std::size_t k = 0; k < Shape.vertices.size(); ++k) {
|
|
Shape.vertices[k].id = static_cast<layout::vertex_id>(k + 1);
|
|
}
|
|
layout::end_condition start;
|
|
start.position = Start;
|
|
layout::end_condition end;
|
|
end.position = End;
|
|
return layout::solve(Shape, start, end);
|
|
}
|
|
|
|
// the path of the old track from one chainage of the line to another: its ends and the handles of an arc of the turn between their headings
|
|
inline segment_data path_between(layout::solved_line const &Line, glm::dvec3 const &From, glm::dvec3 const &To, double const A, double const B, bool const Round) {
|
|
segment_data path;
|
|
path.points[segment_data::point::start] = From;
|
|
path.points[segment_data::point::end] = To;
|
|
auto const t0{glm::normalize(Line.tangent(A))};
|
|
auto const t1{glm::normalize(Line.tangent(B))};
|
|
auto const turn{geometry::signed_angle(t0, t1)};
|
|
auto const chord{geometry::plan_distance(From, To)};
|
|
auto const cosine{std::cos(turn / 4.0)};
|
|
auto const handle{chord / (3.0 * cosine * cosine)};
|
|
if (std::abs(turn) > 1e-9 || std::abs(Line.grade(A) - Line.grade(B)) > 1e-9) {
|
|
path.points[segment_data::point::control1] = glm::dvec3{t0.x * handle, Line.grade(A) * handle, t0.y * handle};
|
|
path.points[segment_data::point::control2] = glm::dvec3{-t1.x * handle, -Line.grade(B) * handle, -t1.y * handle};
|
|
}
|
|
auto const roll = [&](double const S) {
|
|
auto const value{layout::roll_of(Line.cant_at(S))};
|
|
return static_cast<float>(Round ? std::round(value * 10.0) / 10.0 : value);
|
|
};
|
|
path.rolls = {roll(A), roll(B)};
|
|
path.radius = 0.f;
|
|
return path;
|
|
}
|
|
|
|
// the line cut at random chainages Shortest to Longest apart, the seed given; the joints shaken by the noise
|
|
inline old_line cut(layout::solved_line const &Line, unsigned const Seed, double const Shortest, double const Longest, noise const &Noise) {
|
|
old_line result;
|
|
result.line = Line;
|
|
std::mt19937 random{Seed};
|
|
std::uniform_real_distribution<double> length{Shortest, Longest};
|
|
std::uniform_real_distribution<double> shake{-1.0, 1.0};
|
|
std::bernoulli_distribution backwards{0.3};
|
|
result.cuts.push_back(0.0);
|
|
while (result.cuts.back() + Longest < Line.length) {
|
|
result.cuts.push_back(result.cuts.back() + length(random));
|
|
}
|
|
if (Line.length - result.cuts.back() < Shortest * 0.5 && result.cuts.size() > 1) {
|
|
result.cuts.pop_back();
|
|
}
|
|
result.cuts.push_back(Line.length);
|
|
std::vector<glm::dvec3> joints;
|
|
for (std::size_t i = 0; i < result.cuts.size(); ++i) {
|
|
auto const s{result.cuts[i]};
|
|
auto point{Line.point(s)};
|
|
auto const across{layout::left_of(glm::normalize(Line.tangent(s)))};
|
|
auto const inner{i > 0 && i + 1 < result.cuts.size()};
|
|
auto const side{inner ? shake(random) * Noise.plan : 0.0};
|
|
auto const up{inner ? shake(random) * Noise.height : 0.0};
|
|
point += glm::dvec3{across.x * side, up, across.y * side};
|
|
joints.push_back(point);
|
|
}
|
|
for (std::size_t i = 0; i + 1 < result.cuts.size(); ++i) {
|
|
auto const path{path_between(Line, joints[i], joints[i + 1], result.cuts[i], result.cuts[i + 1], Noise.round_rolls)};
|
|
if (backwards(random)) {
|
|
result.chain.push_back({geometry::reversed(path), false});
|
|
}
|
|
else {
|
|
result.chain.push_back({path, true});
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// the chain run the other way
|
|
inline std::vector<layout::chain_path> turned_round(std::vector<layout::chain_path> Chain) {
|
|
std::ranges::reverse(Chain);
|
|
for (auto &part : Chain) {
|
|
part.forward = false == part.forward;
|
|
}
|
|
return Chain;
|
|
}
|
|
|
|
// the vertex of the recognized line nearest the point, none farther than Within
|
|
inline layout::vertex const *nearest(layout::line_shape const &Shape, glm::dvec2 const &Point, double const Within) {
|
|
layout::vertex const *result{nullptr};
|
|
double best{Within};
|
|
for (auto const &vertex : Shape.vertices) {
|
|
if (auto const distance{glm::distance(vertex.point, Point)}; distance < best) {
|
|
best = distance;
|
|
result = &vertex;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace test::takeover_fixture
|