From 1275c953820230870a93933161656bd15cef3c6d Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:18:08 +0100 Subject: [PATCH] fix(editor): the tracer keeps to the exit of a curve and lays the transitions its points show: a compound curve open at the end whose last arc begins past the point clicked last isn't taken, its radius free to run off anywhere, so a point in the transition out of a curve no longer passes for a sharper arc beyond it (R 305 traced as R 308/123, then 9 m off the points on the straight after it, closed with no transition out and slowed to 10 km/h); the cant the fitted transitions ramp taken from their length as the norms count it, whole 5 m (52 m ramping the cant of 50 m), so a curve meets the norms at the speed they give it (R 300 with transitions of 50 m at 60 km/h, not 50); a curve fitted right off a straight with no transition laid with the one of the norms at the speed tried, not slowed to the slowest; the noise of the unit tests the same on every compiler (std::normal_distribution differs between MSVC and libstdc++, the tests of the tracer failing on Linux only), the radius of a gentle curve checked within what its points can tell, a curve slowed down checked at the speed of the norms with the transitions its points show over three radii --- editor/editorTrace.cpp | 32 ++++++++++++++++--- tests/unit/noise.h | 34 ++++++++++++++++++++ tests/unit/test_trace.cpp | 65 +++++++++++++++++++++++---------------- 3 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 tests/unit/noise.h diff --git a/editor/editorTrace.cpp b/editor/editorTrace.cpp index 936e8b628..6c49d8b72 100644 --- a/editor/editorTrace.cpp +++ b/editor/editorTrace.cpp @@ -106,13 +106,15 @@ double norm_between(double const Speed, double const Curvature0, double const Ca return std::max(length, std::abs(unbalanced(Curvature1, Cant1) - unbalanced(Curvature0, Cant0)) * Speed / (3.6 * Norms.jerk)); } -// the cant the transitions of the length ramp at the speed, of the one the norms ask for at most, in steps of 5 mm +// the cant the transitions of the length ramp at the speed, of the one the norms ask for at most, in steps of 5 mm: the length as the +// norms count it, whole 5 m, since they round the length a cant needs up to those (a transition of 52 m ramps the cant of 50 m) double ramped_cant(double const Cant, double const Length, double const Speed, alignment::limits const &Norms) { - if (Length <= 0.0) + auto const length{std::floor(Length / 5.0 + 1e-9) * 5.0}; + if (length <= 0.0) return 0.0; auto const speed{std::max(1.0, Speed)}; - auto const ramp{std::min({Cant, Length * 1000.0 / (Norms.ramp_factor * speed), Norms.cant_rate * 3.6 * Length / speed})}; + auto const ramp{std::min({Cant, length * 1000.0 / (Norms.ramp_factor * speed), Norms.cant_rate * 3.6 * length / speed})}; return ramp < 5.0 ? 0.0 : std::floor(ramp / 5.0) * 5.0; } @@ -964,6 +966,11 @@ struct tracer::impl return result; auto const laid{chain_of(line.start, line.segments)}; auto const measured{measure(laid, Indices)}; + // the last arc of a compound curve open at the end beginning past the point clicked last: none of it shown, its radius free to + // run off anywhere, the curve one of an arc less going on (a point in its transition out passing for a sharper arc beyond it) + if (false == line.curves.empty() && line.curves.back().shape.open && line.curves.back().curvatures.size() > 1 && false == measured.pieces.empty() && + measured.pieces.back() + 1 < line.spans.back().second) + return result; result.fit = {shape, parameters}; result.valid = true; result.cost = *cost; @@ -2025,12 +2032,27 @@ struct tracer::impl ramp = std::max(starts_curved ? 0.0 : vertex.transition_in, ends_curved ? 0.0 : vertex.transition_out); // the cant a held curve goes on with auto const keeps_cant{starts_curved && held.has_value() && std::abs(held->curvature) > 1e-12 && std::abs(One.radii.front() - 1.0 / std::abs(held->curvature)) <= 0.05 / std::abs(held->curvature)}; + // a curve the fit begins or ends right off a straight, with no transition: the norms ask for one at any speed, 5 m at the slowest, so + // no speed meets them and slowing the curve down would only lay it at the slowest; it gets the one of the norms at the speed tried, the + // points telling how far the track is from it. a transition the points show, however short, is kept and the curve slowed down for it + auto const none_in{false == starts_curved && vertex.transition_in <= 0.0}; + auto const none_out{false == ends_curved && vertex.transition_out <= 0.0}; auto speed{options.speed}; for (;;) { auto const at{allowed_speed(speed, sharpest, norms)}; auto laid{vertex}; - laid.cant = keeps_cant ? std::max(0.0, held->cant) : starts_curved && ends_curved ? alignment::recommend(at, sharpest, norms).cant : ramped_cant(alignment::recommend(at, sharpest, norms).cant, ramp, at, norms); + auto const norm_cant_at{alignment::recommend(at, sharpest, norms).cant}; + if (keeps_cant) + laid.cant = std::max(0.0, held->cant); + else if ((starts_curved && ends_curved) || (ramp <= 0.0 && (none_in || none_out))) + laid.cant = norm_cant_at; // ramped along the transitions of the norms + else + laid.cant = ramped_cant(norm_cant_at, ramp, at, norms); + if (none_in) + laid.transition_in = alignment::transition_length(at, One.radii.front(), laid.cant * sharpest / std::max(1.0, One.radii.front()), norms); + if (none_out) + laid.transition_out = alignment::transition_length(at, One.radii.back(), laid.cant * sharpest / std::max(1.0, One.radii.back()), norms); laid.speed = speed < options.speed - 1e-6 ? speed : 0.0; auto trial{alone(Design, laid, In, Out, starts_curved, ends_curved)}; if (ends_curved) @@ -2051,6 +2073,8 @@ struct tracer::impl } if (meets || speed <= options.speed_least + 1e-6) { + result.in = laid.transition_in; + result.out = laid.transition_out; result.cant = laid.cant; result.speed = laid.speed; break; diff --git a/tests/unit/noise.h b/tests/unit/noise.h new file mode 100644 index 000000000..7a91223b5 --- /dev/null +++ b/tests/unit/noise.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include + +// gaussian noise of a seed, the same on every compiler: std::normal_distribution is left to the standard library, so MSVC and +// libstdc++ make other numbers of one seed and a test passing on the one fails on the other; std::mt19937 itself is fixed by the +// standard, and this turns its numbers into the noise by Box-Muller, called as std::normal_distribution is +class portable_normal { +public: + portable_normal(double const Mean, double const Sigma) : m_mean{Mean}, m_sigma{Sigma} {} + + double operator()(std::mt19937 &Random) { + if (m_has_spare) { + m_has_spare = false; + return m_mean + m_sigma * m_spare; + } + constexpr double range{4294967296.0}; // 2^32, the numbers of std::mt19937 + constexpr double two_pi{6.283185307179586}; + double const u1{(static_cast(Random()) + 1.0) / range}; // (0, 1], so the log is finite + double const u2{static_cast(Random()) / range}; + double const r{std::sqrt(-2.0 * std::log(u1))}; + m_spare = r * std::sin(two_pi * u2); + m_has_spare = true; + return m_mean + m_sigma * r * std::cos(two_pi * u2); + } + +private: + double m_mean; + double m_sigma; + double m_spare{0.0}; + bool m_has_spare{false}; +}; diff --git a/tests/unit/test_trace.cpp b/tests/unit/test_trace.cpp index 993a74a3b..689f07595 100644 --- a/tests/unit/test_trace.cpp +++ b/tests/unit/test_trace.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "doctest.h" #include "editor/editorTrace.hpp" +#include "noise.h" #include #include @@ -95,7 +96,7 @@ clicks sampled(alignment::result const &Line, double const Straight, double cons if (To < 0.0) To = Line.length; std::mt19937 random{Seed}; // NOSONAR: seeded on purpose, the test is repeated the same - std::normal_distribution noise{0.0, Sigma}; + portable_normal 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) @@ -192,7 +193,12 @@ TEST_CASE("trace: gentle curves of R 8 and 20 km are curves, nearly straight") { REQUIRE(line.valid); REQUIRE(line.curves.size() == 1); CHECK(line.curves.front().nearly_straight); - CHECK(relative(line.curves.front().sharpest(), radius) < 0.25); + // the radius within what the points can tell of it: a parabola through n points spread evenly over an arc of the length L, + // each off by sigma, has its curvature within 2 sigma sqrt(180 / (n L^4)); at R 20 km over 600 m, a point every 100 m within + // 0.2 m, that's 11 % of it. the radius found within three of that, a tenth at least + auto const clicked{std::floor(length / 100.0) + 1.0}; + auto const told{2.0 * sigma * std::sqrt(180.0 / (clicked * std::pow(length, 4.0))) * radius}; + CHECK(relative(line.curves.front().sharpest(), radius) < std::max(0.1, 3.0 * told)); } } } @@ -338,29 +344,36 @@ TEST_CASE("trace: the same points make the same line") { } 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); + // R 290 - 320 m with transitions of 50 m and a cant of 100 mm on a line of 120 km/h: transitions of 50 m ramp 80 mm at 60 km/h, which + // leaves 0.35 - 0.44 m/s2 unbalanced, and 70 mm at 70 km/h, which leaves 0.73 - 0.85, over the 0.6 the norms allow: laid for 60 km/h + for (auto const radius : {290.0, 305.0, 320.0}) { + auto const truth{alignment::compute(line_of({{400.0, 50.0, arc(radius, 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("R " << radius << ": " << described(line)); + REQUIRE(line.valid); + REQUIRE(line.curves.size() == 1); + auto const &curve{line.curves.front()}; + CHECK(curve.restricted); + CHECK(curve.speed == 60.0); + CHECK_FALSE(line.refused); + // the line through the points, the exit of the curve too, and the transitions they show: about 50 m, the norms of the line pulling + // them longer only as far as the points let them + CHECK(line.worst <= 3.0 * 0.1); + CHECK(std::abs(curve.vertex.transition_in - 50.0) <= 15.0); + CHECK(std::abs(curve.vertex.transition_out - 50.0) <= 15.0); + // 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") { @@ -403,7 +416,7 @@ TEST_CASE("trace: a run beside a neighbour at a constant spacing is parallel") { 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 noise{0.0, 0.1}; + portable_normal noise{0.0, 0.1}; std::vector points; std::vector sigmas; for (int i = 1; i < 10; ++i) {