mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 10:37:07 +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.
143 lines
6.0 KiB
C++
143 lines
6.0 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/.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "editor/layout/layoutLine.hpp"
|
|
#include "world/Segment.h"
|
|
|
|
#include <cstddef>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <glm/glm.hpp>
|
|
|
|
// The recognition of the old tracks (editor-tracks-domain.md §10, "Automatyczne przejęcie odcinka"): a chain of the paths of an old
|
|
// scenery, Bezier curves drawn by hand or by older tools, made a line of the domain. The curvature sampled along the arc length, the
|
|
// chain cut where it changes (the curves between the straights, the transitions and the arcs inside them, the kinks at the joints), the
|
|
// clothoid - arc - clothoid of each curve fitted by least squares to the points of the paths, the cant fitted to their rolls and the grade
|
|
// line to their heights; then the deviation of the line from the paths measured densely, in the plan, in the height and in the cant.
|
|
// A line the domain can't lay as the paths run (a compound curve, a cant with no transition to ramp it along) is fitted as near as it
|
|
// goes and told; the norms aren't asked here.
|
|
namespace layout
|
|
{
|
|
|
|
// a path of the chain, run from its start to its end or the other way
|
|
struct chain_path
|
|
{
|
|
segment_data path;
|
|
bool forward{true};
|
|
};
|
|
|
|
// a point of the chain, every few metres along it and at each joint
|
|
struct chain_sample
|
|
{
|
|
double s{0.0}; // m along the chain, in the plan
|
|
glm::dvec3 point{0.0};
|
|
glm::dvec2 heading{0.0, 1.0}; // plan, unit, as the chain runs
|
|
double curvature{0.0}; // 1/m, above 0 turning left
|
|
double cant{0.0}; // mm, above 0 leaning left as the chain runs
|
|
std::size_t part{0}; // the path of the chain it lies on
|
|
double joint_turn{0.0}; // rad, at the start of a path: how the heading turns from the end of the path before it
|
|
};
|
|
|
|
// the chain sampled no farther apart than Step along each path, its ends and the joints included
|
|
std::vector<chain_sample> sample_chain(std::vector<chain_path> const &Chain, double const Step);
|
|
// m in the plan, of each path of the chain
|
|
std::vector<double> part_lengths(std::vector<chain_path> const &Chain);
|
|
|
|
// how near the line has to run to the old tracks for them to be taken over with no question asked
|
|
struct takeover_tolerance
|
|
{
|
|
double plan{0.02}; // m: the simulator itself joins the ends of the tracks within 2 cm
|
|
double height{0.02}; // m
|
|
double cant{10.0}; // mm: 0.4 degree of the roll, under the half degree many old files round the rolls to
|
|
};
|
|
|
|
// how far the line lies from the old tracks, measured at points of them every half metre: the farthest and the root mean square of the
|
|
// distance in the plan, of the height and of the cant, where the farthest is
|
|
struct measured_deviation
|
|
{
|
|
double plan{0.0};
|
|
double plan_rms{0.0};
|
|
double height{0.0};
|
|
double height_rms{0.0};
|
|
double cant{0.0};
|
|
double cant_rms{0.0};
|
|
glm::dvec3 plan_at{0.0};
|
|
glm::dvec3 height_at{0.0};
|
|
glm::dvec3 cant_at{0.0};
|
|
std::size_t samples{0};
|
|
|
|
bool within(takeover_tolerance const &Tolerance) const { return plan <= Tolerance.plan && height <= Tolerance.height && cant <= Tolerance.cant; }
|
|
// the worst of the three in the units of the tolerance
|
|
double share(takeover_tolerance const &Tolerance) const;
|
|
// the farthest of each of both
|
|
void add(measured_deviation const &Other);
|
|
};
|
|
|
|
// the deviation of the line from the samples of the chain: each point of the chain set square onto the line
|
|
measured_deviation measure(solved_line const &Line, std::vector<chain_sample> const &Samples);
|
|
|
|
// an end of the chain held by what goes on from there and stays (a switch, an old track which isn't taken over): the line has to run on
|
|
// from it in its heading
|
|
struct held_end
|
|
{
|
|
bool held{false};
|
|
glm::dvec2 heading{0.0, 1.0}; // into the chain at its start, out of it at its end
|
|
};
|
|
|
|
struct recognition_options
|
|
{
|
|
double step{1.0}; // m between the points the curves are fitted to
|
|
double measure_step{0.5}; // m between the points the deviation is measured at
|
|
takeover_tolerance tolerance;
|
|
transition_shape transition{transition_shape::clothoid};
|
|
held_end start;
|
|
held_end end;
|
|
};
|
|
|
|
// a curve found where the curvature of the chain changes: from where to where along the chain (its transitions included), how much
|
|
// it turns
|
|
struct found_curve
|
|
{
|
|
double from{0.0};
|
|
double to{0.0};
|
|
double deflection{0.0}; // rad, above 0 turning left
|
|
double radius{0.0}; // m, of the arc, as first read off the curvature
|
|
double transition_in{0.0};
|
|
double transition_out{0.0};
|
|
bool kink{false}; // the chain turns at a joint with no curve
|
|
bool compound{false}; // arcs of more than one radius with no straight between them
|
|
};
|
|
// the curves of the chain between its straights, read off the curvature sampled along it (each one a curve of a vertex, a kink a curve
|
|
// of the shortest length the domain allows)
|
|
std::vector<found_curve> find_curves(std::vector<chain_sample> const &Samples);
|
|
|
|
struct recognized_line
|
|
{
|
|
line_shape shape; // the vertices with their radii, transitions and cants, the grade line; no ids given
|
|
glm::dvec3 start{0.0}; // the ends of the chain
|
|
glm::dvec3 end{0.0};
|
|
solved_line solved; // laid between them
|
|
measured_deviation measured;
|
|
std::vector<double> joints; // the chainage of each joint between the paths along the line, the ends included
|
|
std::vector<std::string> findings; // what the domain lays otherwise than the paths run: kinks, compound curves, cants with no ramp
|
|
std::string error; // why there's no line
|
|
bool valid() const { return error.empty() && solved.valid(); }
|
|
bool silent(takeover_tolerance const &Tolerance) const { return valid() && measured.within(Tolerance); }
|
|
};
|
|
|
|
// the line of the chain: the curves found fitted by least squares, the cant and the grade line fitted, the deviation measured
|
|
recognized_line recognize(std::vector<chain_path> const &Chain, recognition_options const &Options = {});
|
|
|
|
// the cant of the roll of a path, mm, above 0 leaning left as the path runs from its start: on the base of the rail heads
|
|
double cant_of(double const Roll);
|
|
|
|
} // namespace layout
|