18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 06:12:41 +01:00

feat(editor): the line traced on the photo: the straights and the curves recognised from the points clicked along the track

quirky-crafting-summit, Etap 1: the user clicks points along the middle of the track seen on the photo, on its straights and on its
curves, and the editor tells the straights from the curves itself. editor/editorTrace.*, the domain alone:

- Each point with the noise of its click, sigma: two pixels of the view at it, 5 cm at least. A run is a straight where its points keep
  within 3 sigma of one and no curve brings the sum of their squares down by more than the noise would (9 sigma^2 a parameter); else a
  curve, a gentle one too (R 5 to 30 km, nearly straight).
- The line is a chain from its start of straights, transitions and arcs fitted by Levenberg-Marquardt to the points, the distance of each
  point measured exactly (to an arc by its centre, to a transition by Newton on Gauss-Legendre), robust beyond 3 sigma. The shapes tried
  at a click grow from the one before: a curve opened on the straight, closed where the line leaves it, a radius more where the curvature
  changes (compound, up to three, its arcs 2.5 times apart at most, the transitions between them of the norms), a curve the other way
  after a straight or with none, the last curve begun afresh where the shape of before can't follow. The transitions in and out keep to
  the ones of the norms at the speed the radius allows unless the points show them shorter or longer (20 m a sigma).
- The line may start anywhere, inside a curve too (its heading from the fit, not from the first two points), or at a free end held: its
  heading and curvature, a curve going on from it, from a switch at its radius. And end at one (tracer::ended).
- Frozen: a curve with two points on the straight after it is fitted no more; its radii and its start stay, only its last arc and its
  transition out still turn the straight after it about its end till the next curve freezes. A click fits the open end alone, so it costs
  no more at the end of a long line. The radii of a curve freezing are chosen among 1, 2 and 3 once more.
- A click takes a budget of evaluations, the same for the same points: where it's spent, or no shape keeps to the points within the noise
  where the line did before, the line stays as it was and the point is told (misfit); two such running, the line goes on with the best of
  them. The same points always make the same line.
- The design of the line: a vertex where the straights either side of each curve cross, its radii, shares and transitions; a line
  beginning or ending within an arc goes on from it or into it (start_curved, end_curved). The norms of each curve asked at the speed of
  the line, else at a speed lower by steps of 10 km/h till it meets them (vertex::speed), the cant for that speed; refused only below the
  least radius of the kind of the track.
- spacing_to: the spacing a run of points keeps from a neighbouring line, parallel where it varies within their noise (for the stations
  traced on the photo, later).

fit_trace_stretch, which the plan named for the curves between straights, takes 0.2 to 3 s a stretch (189 ms for 10 points along a curve
of one radius, 3.1 s for 60 along one of three, RelWithDebInfo) and weighs no noise of the clicks: at 0.2 m it took a curve of three radii
for four. The chain is fitted on its own: 22 ms the slowest click of 60 points along a curve of three radii.

Tests: U, test_trace.cpp: lines of the norms sampled as clicks, sigma 0.1 and 0.2 m, fixed seeds: a straight; R 8 and 20 km over 300 to
1000 m nearly straight; one radius within 2 %; compound curves of 2 and 3 radii within 2 %; an S-curve; a line started inside a curve;
from a free end straight and curved, into one; the curves frozen unchanged by the clicks after them; a point clicked far off leaving the
line as it was; the same points the same line; a curve below the norms slowed down, one below the least radius refused; the worst click,
of 60 points along three radii, within 30 ms; a run parallel to a neighbour.
This commit is contained in:
maj00r
2026-11-24 00:51:47 +01:00
parent 0bd2293da2
commit 656c5b4d75
5 changed files with 2880 additions and 0 deletions
+1
View File
@@ -198,6 +198,7 @@ set(SOURCES
"editor/editorFouling.cpp"
"editor/editorRoad.cpp"
"editor/editorAlignment.cpp"
"editor/editorTrace.cpp"
"editor/editorTrackShape.cpp"
"editor/editorGroundFollow.cpp"
"editor/editorAlignmentStore.cpp"
File diff suppressed because it is too large Load Diff
+149
View File
@@ -0,0 +1,149 @@
/*
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 <cstddef>
#include <optional>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include "editor/editorAlignment.hpp"
// The line traced on the aerial photo: the points clicked one after another along the middle of the track seen on the photo, each with
// the noise of its click, made into the straights and the curves the track runs along, the way the track was built: a straight where the
// points keep to one within the noise, else a curve, a very gentle one too (R 5 - 20 km, "nearly straight"), of one radius or compound,
// with its transitions and its cant as the norms ask for at the speed, and curves the other way with or without a straight between them.
// The line may start at a free end, its heading and its curvature held, or anywhere, inside a curve too.
//
// Each click fits the open end of the line only: a curve with two points clicked on the straight after it is frozen, the straight still
// turning about the end of its last arc until the next curve is frozen, so that what lies behind doesn't change and a click costs as much
// at the end of a long line as at its start. A click's fit takes a budget of evaluations of the line, the same for the same points: where
// it's spent, the fit doesn't converge or the line comes out worse than before the point, the line stays as it was and the point is told
// as one which doesn't fit. A curve sharper than the norms allow at the speed of the line is laid at the speed its radius and its
// transitions allow, the trains slowed down along it; only one below the least radius of the kind of the track is refused.
//
// The domain alone: no scene, no tracks, no user interface; the plan in metres, x and y of the plan (x and z of the scenery).
namespace trace
{
// the noise of a click: two pixels of the view at it, 5 cm at least
double click_sigma(double const MetresPerPixel);
// what the line is held to at its start or its end: the free end of a track it goes on from or into (later the diverging end of a turnout).
// The heading as the line runs, the curvature signed by the side it turns to as the line runs, positive to the left
struct hold
{
glm::dvec2 position{0.0};
glm::dvec2 heading{1.0, 0.0};
double curvature{0.0};
double cant{0.0}; // mm, of the track there, leaning into its turn
bool fixed{false}; // the diverging track of a switch: a curve goes on at its radius with no transition, a straight may begin right there
};
struct settings
{
double speed{100.0}; // km/h of the line
alignment::limits norms;
alignment::transition_shape shape{alignment::transition_shape::cubic_parabola};
double radius_least{0.0}; // m: a curve sharper is refused at any speed, the least radius of the norms for the kind of the track; 0 none
double speed_step{10.0}; // km/h: a curve below the norms is slowed down by these
double speed_least{10.0}; // km/h: the slowest a curve is laid for
std::size_t budget{20000}; // evaluations of the line a click's fit may take
bool operator==(settings const &) const = default;
};
// a curve of the line traced, one vertex of its design
struct curve
{
alignment::vertex vertex; // as the design has it, its speed among it where it's laid slower than the line
std::vector<double> radii; // of its arcs, the first one first
double speed{0.0}; // km/h it's laid for: the one of the line, or below it where its radius or its transitions don't meet the norms at that
bool restricted{false}; // slower than the line
bool refused{false}; // sharper than the least radius of the kind of the track
bool nearly_straight{false}; // R 5 km and more
bool open{false}; // the line ends within its last arc
bool inside{false}; // the line starts within its first arc
bool frozen{false};
double from{0.0}; // chainage of its start and of its end along the line laid
double to{0.0};
double sharpest() const;
bool compound() const { return radii.size() > 1; }
};
struct result
{
bool valid{false}; // a line to lay: two points clicked at least, the design computed
alignment::design design; // in the plan, the heights 0: its start and end, the vertices of its curves
alignment::result laid; // computed
std::vector<curve> curves; // one for each vertex of the design
std::vector<double> offsets; // of the points clicked from the line laid, positive to the left
std::vector<double> sigmas; // of the points
std::vector<char> misfit; // a point the line couldn't be fitted to: the line stayed as it was before it
std::vector<char> on_straight; // a point on a straight of the line, not on a curve
double rms{0.0};
double worst{0.0}; // m, the farthest point
bool refused{false}; // a curve below the least radius: not to be laid
std::string error; // why there's no line, or why it's refused
std::size_t frozen{0}; // curves frozen
// of the last click
double milliseconds{0.0};
std::size_t evaluations{0};
bool budget_spent{false};
};
// the line built click after click
class tracer
{
public:
tracer();
tracer(settings const &Settings, std::optional<hold> const &Start = {});
tracer(tracer const &Other);
tracer &operator=(tracer const &Other);
~tracer();
// a point clicked, with the noise of its click in metres
void add(glm::dvec2 const &Point, double const Sigma);
// the last point taken back, the line as it was before it
void pop();
std::size_t size() const;
std::vector<glm::dvec2> const &points() const;
settings const &options() const;
std::optional<hold> const &start() const;
// the line of the points clicked so far
result const &now() const;
// the same ending at End, a free end the last click reached (the end's position isn't among the points)
result ended(hold const &End) const;
private:
struct impl;
impl *m_impl{nullptr};
};
// the line of the points clicked one after another, as a tracer makes it
result traced(std::vector<glm::dvec2> const &Points, std::vector<double> const &Sigmas, settings const &Settings, std::optional<hold> const &Start = {});
// how far a run of the points of a line traced keeps from a neighbouring line (points along it, in order), each point measured square
// to the neighbour: the mean spacing, positive to the left of the neighbour as it runs, and how much it varies; a run keeping a constant
// spacing within the noise of its points is parallel
struct spacing
{
bool parallel{false};
double mean{0.0};
double spread{0.0}; // the root mean square of the departures from the mean
std::size_t points{0};
};
spacing spacing_to(std::vector<glm::dvec2> const &Points, std::vector<double> const &Sigmas, std::vector<glm::dvec2> const &Neighbour);
// the radii of the curve as the labels tell them: "850 m", "600/450 m", "9 km"
std::string radii_text(curve const &Curve);
} // namespace trace
+2
View File
@@ -24,6 +24,8 @@ add_executable(${UNIT_TESTS_NAME}
editor_stubs.cpp
# the geometry of the lines the track editor designs, standing on its own
"${CMAKE_SOURCE_DIR}/editor/editorAlignment.cpp"
# and the line traced on the photo, its straights and curves recognised from the points clicked
"${CMAKE_SOURCE_DIR}/editor/editorTrace.cpp"
# and the heights of the tracks laid
"${CMAKE_SOURCE_DIR}/editor/editorGroundFollow.cpp"
"${CMAKE_SOURCE_DIR}/editor/editorProfile.cpp"
+474
View File
@@ -0,0 +1,474 @@
#include "stdafx.h"
#include "doctest.h"
#include "editor/editorTrace.hpp"
#include <algorithm>
#include <chrono>
#include <limits>
#include <cmath>
#include <random>
#include <string>
#include <vector>
// The line traced on the aerial photo: lines of known straights and curves sampled as the clicks along the middle of the track would be,
// spread across by the noise of the clicks (fixed seeds), traced click after click; what it makes of them
namespace {
double constexpr pi{3.14159265358979323846};
glm::dvec2 heading(double const Degrees) { return {std::cos(Degrees * pi / 180.0), std::sin(Degrees * pi / 180.0)}; }
// a line from the origin along x: legs of straights between the vertices, each turning by its deflection (degrees, left positive)
struct leg {
double length{0.0};
double deflection{0.0};
alignment::vertex curve;
};
alignment::design line_of(std::vector<leg> const &Legs, double const Last, double const Speed) {
alignment::design design;
design.speed = Speed;
glm::dvec2 point{0.0};
double direction{0.0};
for (auto const &each : Legs) {
point += heading(direction) * each.length;
auto vertex{each.curve};
vertex.position = point;
design.vertices.push_back(vertex);
direction += each.deflection;
}
auto const end{point + heading(direction) * Last};
design.start = {0.0, 0.0, 0.0};
design.end = {end.x, 0.0, end.y};
design.start_direction = heading(0.0);
design.end_direction = heading(direction);
if (design.vertices.size() >= 2) {
design.vertices.front().offset = Legs.front().length;
design.vertices.back().offset = Last;
}
return design;
}
alignment::vertex arc(double const Radius, double const Transition, double const Cant = 0.0) {
alignment::vertex vertex;
vertex.radius = Radius;
vertex.transition_in = vertex.transition_out = Transition;
vertex.cant = Cant;
return vertex;
}
alignment::vertex compound(std::vector<double> const &Radii, std::vector<double> const &Shares, double const Transition, double const Between) {
alignment::vertex vertex;
vertex.radius = Radii.front();
vertex.compound = true;
vertex.share = Shares.front();
for (std::size_t i = 1; i < Radii.size(); ++i)
vertex.arcs.push_back({Radii[i], Between, Shares[i]});
vertex.transition_in = vertex.transition_out = Transition;
return vertex;
}
// the transitions and the cant of the norms at the speed the sharpest arc allows, as a line laid to them has: a compound curve's ends at
// their radii, the cants in proportion to the curvature, the transitions between its arcs of the norms
alignment::vertex normed(alignment::vertex Vertex, double const Speed) {
alignment::limits const norms;
auto sharpest{Vertex.radius};
for (auto const &each : Vertex.arcs)
sharpest = std::min(sharpest, each.radius);
auto const allowed{std::min(Speed, alignment::speed_limit(sharpest, norms))};
auto const cant{alignment::recommend(allowed, sharpest, norms).cant};
auto const last{Vertex.arcs.empty() ? Vertex.radius : Vertex.arcs.back().radius};
Vertex.cant = cant;
Vertex.transition_in = alignment::transition_length(allowed, Vertex.radius, cant * sharpest / Vertex.radius, norms);
Vertex.transition_out = alignment::transition_length(allowed, last, cant * sharpest / last, norms);
if (Vertex.compound)
alignment::recommend_between(Vertex, allowed, norms);
return Vertex;
}
// the clicks along the line: every Straight metres on its straights, every Curve metres along its curves, from From to To, shifted across
// by the noise of the seed
struct clicks {
std::vector<glm::dvec2> points;
std::vector<double> sigmas;
};
clicks sampled(alignment::result const &Line, double const Straight, double const Curve, double const Sigma, unsigned const Seed, double const From = 0.0, double To = -1.0) {
if (To < 0.0)
To = Line.length;
std::mt19937 random{Seed}; // NOSONAR: seeded on purpose, the test is repeated the same
std::normal_distribution<double> noise{0.0, Sigma};
auto const curved = [&](double const Chainage) {
for (auto const &e : Line.elements)
if (e.kind != alignment::element_kind::straight && Chainage >= e.chainage - 1e-6 && Chainage <= e.chainage + e.length + 1e-6)
return true;
return false;
};
clicks result;
for (auto s{From}; s <= To + 1e-6;) {
auto const station{alignment::evaluate(Line, s)};
glm::dvec2 const across{-station.direction.z, station.direction.x};
result.points.push_back(glm::dvec2{station.position.x, station.position.z} + glm::normalize(across) * noise(random));
result.sigmas.push_back(Sigma);
s += curved(s) || curved(s + Straight * 0.5) ? Curve : Straight;
}
return result;
}
trace::settings settings_at(double const Speed) {
trace::settings settings;
settings.speed = Speed;
return settings;
}
double relative(double const A, double const B) { return std::abs(A - B) / std::abs(B); }
// the radii the line was laid with: those given, where the curves had room enough
std::vector<double> radii_of(alignment::result const &Line, std::size_t const Curve) {
auto const &curve{Line.curves.at(Curve)};
std::vector<double> radii{curve.radius};
radii.insert(radii.end(), curve.radii.begin(), curve.radii.end());
if (curve.radii.size() > 1)
radii = curve.radii;
return radii;
}
alignment::result laid(alignment::design const &Design) {
auto const line{alignment::compute(Design)};
REQUIRE(line.valid);
REQUIRE(line.radius_warnings.empty());
return line;
}
// the radii of the curves found, sorted per curve as they are
std::string described(trace::result const &Line) {
std::string text{std::to_string(Line.curves.size()) + " curves:"};
for (auto const &curve : Line.curves)
text += " R " + trace::radii_text(curve) + (curve.restricted ? " v " + std::to_string(static_cast<int>(curve.speed)) : "");
text += ", worst " + std::to_string(Line.worst) + (Line.error.empty() ? "" : ", " + Line.error);
return text;
}
} // namespace
TEST_CASE("trace: points along a straight make a straight") {
auto const truth{alignment::compute(line_of({}, 1000.0, 100.0))};
REQUIRE(truth.valid);
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 5; ++seed) {
auto const points{sampled(truth, 150.0, 150.0, sigma, seed)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO(described(line));
REQUIRE(line.valid);
CHECK(line.curves.empty());
CHECK(line.worst <= 3.0 * sigma);
}
}
TEST_CASE("trace: a curve of one radius between straights") {
auto const truth{alignment::compute(line_of({{500.0, 40.0, normed(arc(800.0, 0.0), 100.0)}}, 500.0, 100.0))};
REQUIRE(truth.valid);
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 3; ++seed) {
auto const points{sampled(truth, 120.0, 40.0, sigma, seed)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO(described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 1);
CHECK(line.curves.front().radii.size() == 1);
CHECK(relative(line.curves.front().radii.front(), 800.0) < 0.02);
CHECK_FALSE(line.curves.front().restricted);
}
}
TEST_CASE("trace: gentle curves of R 8 and 20 km are curves, nearly straight") {
for (auto const &[radius, length] : {std::pair{8000.0, 300.0}, std::pair{8000.0, 1000.0}, std::pair{20000.0, 600.0}, std::pair{20000.0, 1000.0}}) {
auto const deflection{length / radius * 180.0 / pi};
auto const truth{alignment::compute(line_of({{600.0, deflection, arc(radius, 0.0)}}, 600.0, 100.0))};
REQUIRE(truth.valid);
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 3; ++seed) {
auto const points{sampled(truth, 100.0, 100.0, sigma, seed)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO("R " << radius << " over " << length << " m, sigma " << sigma << ": " << described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 1);
CHECK(line.curves.front().nearly_straight);
CHECK(relative(line.curves.front().sharpest(), radius) < 0.25);
}
}
}
TEST_CASE("trace: compound curves of two and three radii") {
struct case_of {
std::vector<double> radii;
std::vector<double> shares;
};
for (auto const &shape : {case_of{{1200.0, 700.0}, {0.5, 0.5}}, case_of{{600.0, 400.0}, {0.5, 0.5}}, case_of{{1200.0, 700.0, 1000.0}, {0.35, 0.35, 0.3}}}) {
auto const truth{laid(line_of({{1200.0, 60.0, normed(compound(shape.radii, shape.shares, 0.0, 0.0), 100.0)}}, 1200.0, 100.0))};
auto const radii{radii_of(truth, 0)};
REQUIRE(radii.size() == shape.radii.size());
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 2; ++seed) {
// clicked closer along a curve of radii telling apart, as the hint asks: the radius of an arc of 250 m read off points 25 m
// apart within 0.2 m is known within 1.6 % only
auto const points{sampled(truth, 120.0, 12.0, sigma, seed)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO("sigma " << sigma << ": " << described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 1);
auto const &found{line.curves.front().radii};
REQUIRE(found.size() == radii.size());
for (std::size_t i = 0; i < found.size(); ++i)
CHECK(relative(found[i], radii[i]) < 0.02);
}
}
}
TEST_CASE("trace: an S-curve, the other way after a short straight") {
auto const truth{laid(line_of({{400.0, 30.0, normed(arc(600.0, 0.0), 100.0)}, {550.0, -35.0, normed(arc(800.0, 0.0), 100.0)}}, 400.0, 100.0))};
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 3; ++seed) {
auto const points{sampled(truth, 120.0, 40.0, sigma, seed)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO(described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 2);
CHECK(relative(line.curves[0].sharpest(), 600.0) < 0.02);
CHECK(relative(line.curves[1].sharpest(), 800.0) < 0.02);
}
}
TEST_CASE("trace: a line started inside a curve") {
auto const truth{alignment::compute(line_of({{400.0, 50.0, normed(arc(700.0, 0.0), 100.0)}}, 400.0, 100.0))};
REQUIRE(truth.valid);
// from the middle of the curve on
double middle{0.0};
for (auto const &e : truth.elements)
if (e.kind == alignment::element_kind::arc)
middle = e.chainage + 0.5 * e.length;
for (auto const sigma : {0.1, 0.2})
for (unsigned seed = 1; seed <= 3; ++seed) {
auto const points{sampled(truth, 120.0, 40.0, sigma, seed, middle)};
auto const line{trace::traced(points.points, points.sigmas, settings_at(100.0))};
INFO(described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 1);
CHECK(line.curves.front().inside);
CHECK(relative(line.curves.front().sharpest(), 700.0) < 0.03);
// the start heading as the curve has it, not as the first two points: their chord is 0.03 rad off the tangent
auto const station{alignment::evaluate(truth, middle)};
auto const truth_heading{std::atan2(station.direction.z, station.direction.x)};
auto const start{line.design.start_direction};
CHECK(std::abs(std::remainder(std::atan2(start.y, start.x) - truth_heading, 2.0 * pi)) < 0.008);
}
}
TEST_CASE("trace: the curves frozen don't change with the clicks after them") {
auto const truth{laid(line_of({{400.0, 35.0, normed(arc(700.0, 0.0), 100.0)}, {800.0, -30.0, normed(arc(900.0, 0.0), 100.0)}, {800.0, 25.0, normed(arc(1000.0, 0.0), 100.0)}}, 500.0, 100.0))};
auto const points{sampled(truth, 120.0, 40.0, 0.15, 3)};
trace::tracer line{settings_at(100.0)};
std::vector<trace::result> after;
for (std::size_t i = 0; i < points.points.size(); ++i) {
line.add(points.points[i], points.sigmas[i]);
after.push_back(line.now());
}
INFO(described(line.now()));
REQUIRE(line.now().curves.size() == 3);
CHECK(line.now().frozen >= 2);
// each curve, once the one after it froze too, stays as it is to the bit
for (std::size_t c = 0; c + 1 < 3; ++c) {
std::size_t locked{after.size()};
for (std::size_t i = 0; i < after.size(); ++i)
if (after[i].frozen >= c + 2) {
locked = i;
break;
}
REQUIRE(locked < after.size());
auto const &then{after[locked].curves[c].vertex};
for (auto k{locked}; k < after.size(); ++k) {
auto const &now{after[k].curves[c].vertex};
CHECK(now.radius == then.radius);
CHECK(now.transition_in == then.transition_in);
CHECK(now.transition_out == then.transition_out);
CHECK(now.position.x == then.position.x);
CHECK(now.position.y == then.position.y);
}
// frozen and only sliding at its end before that: its radius kept from the click it froze at
std::size_t froze{after.size()};
for (std::size_t i = 0; i < after.size(); ++i)
if (after[i].frozen >= c + 1) {
froze = i;
break;
}
for (auto k{froze}; k < after.size(); ++k)
CHECK(after[k].curves[c].radii == after[froze].curves[c].radii);
}
}
TEST_CASE("trace: a point clicked far off leaves the line as it was, told") {
auto const truth{alignment::compute(line_of({{500.0, 40.0, normed(arc(800.0, 0.0), 100.0)}}, 500.0, 100.0))};
auto const points{sampled(truth, 120.0, 40.0, 0.1, 1)};
trace::tracer line{settings_at(100.0)};
for (std::size_t i = 0; i < 6; ++i)
line.add(points.points[i], points.sigmas[i]);
auto const before{line.now()};
// back across the straight, far off it
line.add(points.points[2] + glm::dvec2{0.0, 40.0}, 0.1);
auto const now{line.now()};
REQUIRE(now.misfit.size() == 7);
CHECK(now.misfit.back() != 0);
REQUIRE(now.design.vertices.size() == before.design.vertices.size());
CHECK(now.design.start.x == before.design.start.x);
CHECK(now.design.end.x == before.design.end.x);
CHECK(now.design.end.z == before.design.end.z);
line.pop();
CHECK(line.now().misfit == before.misfit);
}
TEST_CASE("trace: the same points make the same line") {
auto const truth{laid(line_of({{400.0, 35.0, normed(arc(700.0, 0.0), 100.0)}, {900.0, -30.0, normed(compound({900.0, 600.0}, {0.5, 0.5}, 0.0, 0.0), 100.0)}}, 600.0, 100.0))};
auto const points{sampled(truth, 120.0, 30.0, 0.2, 9)};
auto const a{trace::traced(points.points, points.sigmas, settings_at(100.0))};
auto const b{trace::traced(points.points, points.sigmas, settings_at(100.0))};
REQUIRE(a.design.vertices.size() == b.design.vertices.size());
for (std::size_t k = 0; k < a.design.vertices.size(); ++k) {
CHECK(a.design.vertices[k].radius == b.design.vertices[k].radius);
CHECK(a.design.vertices[k].position == b.design.vertices[k].position);
}
CHECK(a.offsets == b.offsets);
}
TEST_CASE("trace: a curve sharper than the norms allow at the speed of the line is slowed down, not refused") {
auto const truth{alignment::compute(line_of({{400.0, 50.0, arc(300.0, 50.0, 100.0)}}, 400.0, 70.0))};
REQUIRE(truth.valid);
auto const points{sampled(truth, 120.0, 25.0, 0.1, 2)};
auto settings{settings_at(120.0)};
settings.radius_least = 180.0;
auto const line{trace::traced(points.points, points.sigmas, settings)};
INFO(described(line));
REQUIRE(line.valid);
REQUIRE(line.curves.size() == 1);
auto const &curve{line.curves.front()};
CHECK(curve.restricted);
CHECK(curve.speed < 120.0);
CHECK(curve.speed >= 60.0);
CHECK(std::fmod(curve.speed, 10.0) == doctest::Approx(0.0));
CHECK_FALSE(line.refused);
// the norms met at the speed it's laid for
CHECK(line.design.vertices.front().speed == curve.speed);
for (auto const &report : line.laid.curves)
CHECK(alignment::meets_norms(report, line.design));
// below the least radius of the track: refused
settings.radius_least = 350.0;
auto const refused{trace::traced(points.points, points.sigmas, settings)};
CHECK(refused.refused);
}
TEST_CASE("trace: a click of the worst case, a compound curve of three radii of 60 points, takes 30 ms at most") {
auto const truth{laid(line_of({{1500.0, 70.0, normed(compound({1200.0, 700.0, 1000.0}, {0.35, 0.35, 0.3}, 0.0, 0.0), 100.0)}}, 1500.0, 100.0))};
double from{truth.length};
double to{0.0};
for (auto const &e : truth.elements)
if (e.kind != alignment::element_kind::straight) {
from = std::min(from, e.chainage);
to = std::max(to, e.chainage + e.length);
}
auto const points{sampled(truth, (to - from) / 55.0, (to - from) / 55.0, 0.2, 4, from - 50.0, to + 80.0)};
REQUIRE(points.points.size() >= 60);
// over and over, each click its quickest: the same work each time, the machine busy with something else at times (the builds of
// the other worktrees beside it); done once every click came in within the time, ten times at most
std::vector<double> quickest(points.points.size(), std::numeric_limits<double>::max());
std::vector<std::size_t> evaluations(points.points.size(), 0);
trace::result last;
auto slowest{std::numeric_limits<double>::max()};
for (int run = 0; run < 10 && slowest > 30.0; ++run) {
trace::tracer line{settings_at(100.0)};
for (std::size_t i = 0; i < points.points.size(); ++i) {
line.add(points.points[i], points.sigmas[i]);
quickest[i] = std::min(quickest[i], line.now().milliseconds);
if (run > 0)
CHECK(line.now().evaluations == evaluations[i]);
evaluations[i] = line.now().evaluations;
}
last = line.now();
slowest = *std::max_element(quickest.begin(), quickest.end());
}
MESSAGE("slowest click " << slowest << " ms, most evaluations " << *std::max_element(evaluations.begin(), evaluations.end()));
INFO(described(last));
CHECK(last.curves.size() == 1);
CHECK(slowest <= 30.0);
}
TEST_CASE("trace: a run beside a neighbour at a constant spacing is parallel") {
std::vector<glm::dvec2> neighbour;
for (int i = 0; i <= 20; ++i)
neighbour.push_back({i * 50.0, 0.0});
std::mt19937 random{5}; // NOSONAR: seeded on purpose
std::normal_distribution<double> noise{0.0, 0.1};
std::vector<glm::dvec2> points;
std::vector<double> sigmas;
for (int i = 1; i < 10; ++i) {
points.push_back({i * 100.0, 4.75 + noise(random)});
sigmas.push_back(0.1);
}
auto const beside{trace::spacing_to(points, sigmas, neighbour)};
CHECK(beside.parallel);
CHECK(beside.mean == doctest::Approx(4.75).epsilon(0.02));
// one drifting away isn't
for (std::size_t i = 0; i < points.size(); ++i)
points[i].y += 0.2 * i;
CHECK_FALSE(trace::spacing_to(points, sigmas, neighbour).parallel);
}
TEST_CASE("trace: from a free end, straight or curved, and into one") {
auto const truth{laid(line_of({{500.0, 40.0, normed(arc(800.0, 0.0), 100.0)}}, 600.0, 100.0))};
auto const points{sampled(truth, 120.0, 40.0, 0.1, 3)};
// held at the start of the line, the first point there not clicked again
trace::hold start;
start.position = {0.0, 0.0};
start.heading = {1.0, 0.0};
std::vector<glm::dvec2> after(points.points.begin() + 1, points.points.end());
std::vector<double> sigmas(points.sigmas.begin() + 1, points.sigmas.end());
auto const held{trace::traced(after, sigmas, settings_at(100.0), start)};
INFO(described(held));
REQUIRE(held.valid);
REQUIRE(held.curves.size() == 1);
CHECK(relative(held.curves.front().sharpest(), 800.0) < 0.02);
CHECK(held.design.start.x == 0.0);
CHECK(held.design.start.z == 0.0);
CHECK(held.design.start_direction.x == 1.0);
// into a free end on the straight after the curve, 100 m past the last point
trace::tracer line{settings_at(100.0), start};
for (std::size_t i = 0; i + 2 < after.size(); ++i)
line.add(after[i], sigmas[i]);
auto const station{alignment::evaluate(truth, truth.length - 50.0)};
trace::hold end;
end.position = {station.position.x, station.position.z};
end.heading = glm::normalize(glm::dvec2{station.direction.x, station.direction.z});
auto const into{line.ended(end)};
INFO(described(into));
REQUIRE(into.valid);
REQUIRE(into.curves.size() == 1);
CHECK(relative(into.curves.front().sharpest(), 800.0) < 0.03);
CHECK(glm::distance(glm::dvec2{into.design.end.x, into.design.end.z}, end.position) < 1e-6);
auto const finish{alignment::evaluate(into.laid, into.laid.length)};
CHECK(glm::distance(glm::dvec2{finish.position.x, finish.position.z}, end.position) < 0.01);
CHECK(std::abs(glm::dot(glm::normalize(glm::dvec2{finish.direction.x, finish.direction.z}), end.heading)) > 1.0 - 1e-6);
// from a free end within the curve: the curve goes on from it at its radius
double middle{0.0};
for (auto const &e : truth.elements)
if (e.kind == alignment::element_kind::arc)
middle = e.chainage + 0.5 * e.length;
auto const on{alignment::evaluate(truth, middle)};
trace::hold curved;
curved.position = {on.position.x, on.position.z};
curved.heading = glm::normalize(glm::dvec2{on.direction.x, on.direction.z});
curved.curvature = 1.0 / truth.curves.front().radius;
curved.cant = truth.curves.front().cant;
auto const rest{sampled(truth, 120.0, 40.0, 0.1, 4, middle + 40.0)};
auto const going{trace::traced(rest.points, rest.sigmas, settings_at(100.0), curved)};
INFO(described(going));
REQUIRE(going.valid);
REQUIRE(going.curves.size() == 1);
CHECK(going.design.start_curved);
CHECK(relative(going.curves.front().radii.front(), 800.0) < 0.02);
}