From 31dc959cecc3408bc66045fee580c5196624d9f7 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:28:52 +0100 Subject: [PATCH] feat(editor): the speed of the diverging track of a switch, one rule for the editor and the domain model The formula Q2 gave the editor (editor_mode::branch_speed_of) taken out into a pure function of the switch curve module, which the domain model of the track geometry (editor/layout, E2b) calls for its turnouts too, so the two write the same speed. - switch_curve::branch_speed(): of the basic switch of R and 1:n, its main track bent by table 11.8, the cant with its sign toward the side the diverging track leaves to: formula 9.9 with the normal deficiency of the frog (table 9.7), rounded down to the steps of the speed limits, no more than table 11.2 gives the basic switch. It reports the radius, the cant into the turn, the deficiency, the formula, the rounded speed and the table, which the editor's message tells. - switch_curve::basic_speed(): table 11.2, moved from editor_track::diverging_speed(), which now returns it. - switch_curve::written_velocity(): the one velocity a switch keeps, the main limit for both where it's the lower, else the diverging one below 0; write_switch_speed() calls it. - speed_check::suggested() inline in editorSpeed.hpp, so the switch curve module takes it in the unit tests, which don't build the checks of the speed. The editor computes the same numbers the same way: the basic radius and the bent main radius as before (bent only where the type is known), the cant toward the diverging side the same product, its sign flipped by the bent radius as before (a multiplication by -1, exact), then the same calls in the same order. tests/unit/test_switch_curve.cpp unchanged and passing. Tests (U): the examples of Q2 through branch_speed (Rz 60E1-500-1:12 outward in R 1200 m with 60 mm 60 km/h, inward 60 by the table; Rz 60E1-190-1:9 in R 600 m with 60 mm 30 and 40 km/h), the table of the basic switches, a movable nose kept to 130 km/h, a diverging track bent straight, the editor's table the same, the velocity written. --- application/editormodeswitchplace.cpp | 30 +++++------ editor/editorSpeed.cpp | 7 --- editor/editorSpeed.hpp | 12 ++++- editor/editorSwitchCurve.cpp | 37 ++++++++++++++ editor/editorSwitchCurve.hpp | 24 +++++++++ editor/editorTrackShape.cpp | 11 +---- tests/unit/test_switch_speed.cpp | 71 +++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 35 deletions(-) create mode 100644 tests/unit/test_switch_speed.cpp diff --git a/application/editormodeswitchplace.cpp b/application/editormodeswitchplace.cpp index 3ce3037cc..3fc5a0971 100644 --- a/application/editormodeswitchplace.cpp +++ b/application/editormodeswitchplace.cpp @@ -2116,26 +2116,22 @@ double editor_mode::branch_speed_of(TTrack const &Switch, double const Radius, d return -1.0; // bent with the main track, one-sided where it turns the way the diverging track does, table 11.8: signed the way the diverging track // turns off it, below 0 the other way - auto radius{basic}; - if (auto const bend{bent_main_radius(main)}; Radius > 0.5 && bend != 0.0) - radius = switch_curve::bent_radius((bend > 0.0) == (side > 0.0) ? std::abs(bend) : -std::abs(bend), Radius, Ratio); - // the cant of the line under it with its sign: above 0 leaning into the turn of the diverging track, below 0 against it. A roll above - // 0 leans the track to where the headings grow + auto const bend{Radius > 0.5 ? bent_main_radius(main) : 0.0}; + auto const main_radius{bend != 0.0 ? ((bend > 0.0) == (side > 0.0) ? std::abs(bend) : -std::abs(bend)) : 0.0}; + // the cant of the line under it with its sign, above 0 leaning to the side the diverging track leaves to. A roll above 0 leans the + // track to where the headings grow auto const roll{0.5 * (static_cast(diverging.rolls[0]) + static_cast(diverging.rolls[1]))}; - auto const cant{m_route.design.norms.gauge * std::sin(glm::radians(roll)) * (radius >= 0.0 ? side : -side)}; - auto const deficiency{switch_curve::switch_deficiency(Switch.SwitchExtension != nullptr && Switch.SwitchExtension->m_movablenose).normal}; - auto const formula{switch_curve::curve_speed(std::abs(radius), cant, deficiency)}; - // no more than the basic switch has: table 11.2, which the formula gives for a fixed frog, a movable nose kept to it as Id-4 keeps it - auto const table{editor_track::diverging_speed(basic)}; - auto const rounded{std::isinf(formula) ? table : speed_check::suggested(formula)}; - auto const speed{std::min(table, rounded)}; + auto const toward{m_route.design.norms.gauge * std::sin(glm::radians(roll)) * side}; + // formula 9.9 with the deficiency of its frog, no more than table 11.2 gives the basic switch: the rule the domain model keeps too + auto const branch{switch_curve::branch_speed(basic, Ratio, main_radius, toward, Switch.SwitchExtension != nullptr && Switch.SwitchExtension->m_movablenose)}; + auto const speed{branch.speed}; if (Why != nullptr) { *Why = format(STR_C("Speed on the diverging track %.0f km/h"), speed); - if (std::abs(radius - basic) > 0.5 || std::abs(cant) > 0.5) - *Why += format(STR_C(": R %.0f m, cant %+.0f mm into its turn, I up to %.0f mm (formula 9.9, table 9.7)"), std::abs(radius), cant, deficiency); - if (rounded > table) - *Why += format(STR_C(", no more than %.0f km/h of the basic switch (table 11.2)"), table); + if (std::abs(branch.radius - basic) > 0.5 || std::abs(branch.cant) > 0.5) + *Why += format(STR_C(": R %.0f m, cant %+.0f mm into its turn, I up to %.0f mm (formula 9.9, table 9.7)"), std::abs(branch.radius), branch.cant, branch.deficiency); + if (branch.rounded > branch.table) + *Why += format(STR_C(", no more than %.0f km/h of the basic switch (table 11.2)"), branch.table); } return speed; } @@ -2148,7 +2144,7 @@ std::string editor_mode::write_switch_speed(TTrack &Switch, double const Radius, return {}; // one value for both tracks of a switch: the own limit of the main track where it's the lower, for both, else the one of the diverging // track for it alone, below 0 - editor_track::velocity(Switch, Main > 0.0 && Main <= speed ? Main : -std::max(2.0, speed)); + editor_track::velocity(Switch, switch_curve::written_velocity(Main, speed)); return why; } diff --git a/editor/editorSpeed.cpp b/editor/editorSpeed.cpp index 249d43d7f..cd30ec487 100644 --- a/editor/editorSpeed.cpp +++ b/editor/editorSpeed.cpp @@ -201,13 +201,6 @@ std::string explain(verdict const &Verdict, options const &Options) return text; } -double suggested(double const Safe) -{ - if (Safe >= 40.0) - return std::floor(Safe / 10.0) * 10.0; - return std::max(5.0, std::floor(Safe / 5.0) * 5.0); -} - double path_velocity(TTrack const &Track, int const Path) { auto const velocity{editor_track::velocity(Track)}; diff --git a/editor/editorSpeed.hpp b/editor/editorSpeed.hpp index bea32fc2a..92632b656 100644 --- a/editor/editorSpeed.hpp +++ b/editor/editorSpeed.hpp @@ -9,6 +9,8 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include +#include #include #include #include @@ -68,8 +70,14 @@ struct verdict std::string explain(verdict const &Verdict, options const &Options); verdict check(TTrack &Track, int const Path, options const &Options); -// the safe speed rounded down to the usual steps of the speed limits -double suggested(double const Safe); +// the safe speed rounded down to the usual steps of the speed limits; inline, so the speed of the switches (editorSwitchCurve) takes it +// where the checks of the speed aren't built +inline double suggested(double const Safe) +{ + if (Safe >= 40.0) + return std::floor(Safe / 10.0) * 10.0; + return std::max(5.0, std::floor(Safe / 5.0) * 5.0); +} // speed limit in force on the path, -1 for none. a switch keeps one for both its tracks: positive applies to both, // -2 and below only to the diverging one, its main track having no limit then double path_velocity(TTrack const &Track, int const Path); diff --git a/editor/editorSwitchCurve.cpp b/editor/editorSwitchCurve.cpp index 5519264da..8c094e077 100644 --- a/editor/editorSwitchCurve.cpp +++ b/editor/editorSwitchCurve.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "editor/editorSwitchCurve.hpp" #include "editor/editorFormat.hpp" +#include "editor/editorSpeed.hpp" #include "utilities/translation.h" #include @@ -262,6 +263,42 @@ double curve_speed(double const Radius, double const Cant, double const Deficien return std::sqrt(std::max(0.0, Deficiency + Cant) * Radius / kDeficiencyFactor); } +double basic_speed(double const Radius) +{ + if (Radius < 300.0) + return 40.0; + if (Radius < 500.0) + return 50.0; + if (Radius < 760.0) + return 60.0; + if (Radius < 1200.0) + return 80.0; + return Radius < 2500.0 ? 100.0 : 130.0; +} + +branch_speed_report branch_speed(double const Basic, double const Ratio, double const Main, double const Cant, bool const Movable) +{ + branch_speed_report report; + // bent with the main track, one-sided where it turns the way the diverging track does, table 11.8 + report.radius = Basic; + if (Main != 0.0) + report.radius = bent_radius(Main, Basic, Ratio); + // the cant with its sign: leaning into the turn of the diverging track, or against it where the bending turns it the other way + report.cant = report.radius >= 0.0 ? Cant : -Cant; + report.deficiency = switch_deficiency(Movable).normal; + report.formula = curve_speed(std::abs(report.radius), report.cant, report.deficiency); + // no more than the basic switch has: table 11.2, which the formula gives for a fixed frog + report.table = basic_speed(Basic); + report.rounded = std::isinf(report.formula) ? report.table : speed_check::suggested(report.formula); + report.speed = std::min(report.table, report.rounded); + return report; +} + +double written_velocity(double const Main, double const Diverging) +{ + return Main > 0.0 && Main <= Diverging ? Main : -std::max(2.0, Diverging); +} + slip_report check_slip(std::vector> const &Tracks, std::vector> const &Curves, std::vector const &Line, double const From, double const To) { slip_report report; diff --git a/editor/editorSwitchCurve.hpp b/editor/editorSwitchCurve.hpp index 444b973b5..e7cd84b8f 100644 --- a/editor/editorSwitchCurve.hpp +++ b/editor/editorSwitchCurve.hpp @@ -96,6 +96,30 @@ deficiency switch_deficiency(bool const Movable); // cant against the turn takes up all the deficiency already double curve_speed(double const Radius, double const Cant, double const Deficiency); +// the speed on the diverging track of the basic switch of the Radius (m), table 11.2, km/h +double basic_speed(double const Radius); + +// the speed on the diverging track of a switch: of the basic switch of Basic (m) and 1:Ratio, its main track bent to Main (m, signed as +// bent_radius() takes it, 0 not bent) under the Cant (mm, above 0 leaning to the side the diverging track leaves to), by formula 9.9 +// with the normal cant deficiency of its frog (table 9.7), rounded down to the steps of the speed limits and no more than table 11.2 +// gives the basic switch; a movable nose kept to it as Id-4 keeps it. The editor writes it when a switch is laid, and so does the +// domain model of the track geometry (editor/layout) +struct branch_speed_report +{ + double speed{0.0}; // km/h + double radius{0.0}; // m, of the diverging track, bent by table 11.8, signed as bent_radius() gives it + double cant{0.0}; // mm, above 0 leaning into the turn of the diverging track, below 0 against it + double deficiency{0.0}; // mm, the normal one of the frog + double formula{0.0}; // km/h, of formula 9.9, infinite on a straight diverging track + double rounded{0.0}; // km/h, the formula rounded down to the steps of the speed limits + double table{0.0}; // km/h, of the basic switch, table 11.2 +}; +branch_speed_report branch_speed(double const Basic, double const Ratio, double const Main, double const Cant, bool const Movable); + +// the one velocity a switch keeps for both its tracks, km/h: the limit of the main track for both where it's the lower one, else the one +// of the diverging track for it alone, below 0 (TTrack: -40 limits the diverging track only) +double written_velocity(double const Main, double const Diverging); + // a slip switch bent into a curve, as ST-T1-A6 sees it: none is bent (11.4 pkt 4b, the editor telling no main track from a side // one), the curvature of each track through it of one sign all along it (9.7 pkt 12), its slip curves no sharper than a side track may // be (table 9.2), the cant as over a switch bent, none recommended in a side track (9.3 pkt 5) diff --git a/editor/editorTrackShape.cpp b/editor/editorTrackShape.cpp index 81ecb0b33..bc0aa3b8f 100644 --- a/editor/editorTrackShape.cpp +++ b/editor/editorTrackShape.cpp @@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/. #include "editor/editorTrack.hpp" #include "editor/editorGeometry.hpp" #include "editor/editorGroundFollow.hpp" +#include "editor/editorSwitchCurve.hpp" #include "world/Track.h" #include "utilities/Globals.h" @@ -409,15 +410,7 @@ double editor_track::reverse_insert(double const Speed) double editor_track::diverging_speed(double const Radius) { - if (Radius < 300.0) - return 40.0; - if (Radius < 500.0) - return 50.0; - if (Radius < 760.0) - return 60.0; - if (Radius < 1200.0) - return 80.0; - return Radius < 2500.0 ? 100.0 : 130.0; + return switch_curve::basic_speed(Radius); } namespace diff --git a/tests/unit/test_switch_speed.cpp b/tests/unit/test_switch_speed.cpp new file mode 100644 index 000000000..5c4765b8d --- /dev/null +++ b/tests/unit/test_switch_speed.cpp @@ -0,0 +1,71 @@ +// the speed on the diverging track of a switch, the rule the editor writes when it lays a switch and the domain model of the track +// geometry (editor/layout) computes for its turnouts: formula 9.9 with the cant with its sign and the deficiency of the frog (table 9.7), +// the main track bent by table 11.8, rounded down to the steps of the speed limits and no more than table 11.2; and the one velocity a +// switch keeps for both its tracks + +#include "stdafx.h" +#include "doctest.h" +#include "editor/editorSwitchCurve.hpp" +#include "editor/editorTrack.hpp" + +#include + +TEST_CASE("switch speed: the examples of Q2, the main track bent outward and inward, the cant with its sign") { + // Rz 60E1-500-1:12 bent outward into R 1200 m with 60 mm, two-sided: the cant leans against the diverging track, 60.3 km/h at the + // normal 110 mm of a fixed frog, 60 km/h + auto const outward{switch_curve::branch_speed(500.0, 12.0, -1200.0, -60.0, false)}; + CHECK(outward.radius == doctest::Approx(857.76).epsilon(1e-5)); + CHECK(outward.cant == -60.0); + CHECK(outward.deficiency == 110.0); + CHECK(outward.formula == doctest::Approx(60.3).epsilon(0.002)); + CHECK(outward.speed == 60.0); + // inward, one-sided: r" = 352.7 m, 71.3 km/h, no more than the 60 km/h of the basic switch + auto const inward{switch_curve::branch_speed(500.0, 12.0, 1200.0, 60.0, false)}; + CHECK(inward.radius == doctest::Approx(352.69).epsilon(1e-5)); + CHECK(inward.cant == 60.0); + CHECK(inward.rounded == 70.0); + CHECK(inward.table == 60.0); + CHECK(inward.speed == 60.0); + // Rz 60E1-190-1:9 in R 600 m with 60 mm: outward 34.3 km/h, 30; inward 45.6 km/h, 40 + CHECK(switch_curve::branch_speed(190.0, 9.0, -600.0, -60.0, false).speed == 30.0); + CHECK(switch_curve::branch_speed(190.0, 9.0, 600.0, 60.0, false).speed == 40.0); + CHECK(switch_curve::branch_speed(190.0, 9.0, -600.0, -60.0, false).formula == doctest::Approx(34.3).epsilon(0.002)); + CHECK(switch_curve::branch_speed(190.0, 9.0, 600.0, 60.0, false).formula == doctest::Approx(45.6).epsilon(0.002)); + // the cant toward the side the diverging track leaves to is taken against it where the bending turns the diverging track the other + // way: R 400 m against a 500 m switch turns it past straight + auto const past{switch_curve::branch_speed(500.0, 12.0, -400.0, 60.0, false)}; + CHECK(past.radius < 0.0); + CHECK(past.cant == -60.0); +} + +TEST_CASE("switch speed: the basic switches by table 11.2, a movable nose kept to it, a straight diverging track") { + CHECK(switch_curve::branch_speed(190.0, 9.0, 0.0, 0.0, false).speed == 40.0); + CHECK(switch_curve::branch_speed(300.0, 9.0, 0.0, 0.0, false).speed == 50.0); + CHECK(switch_curve::branch_speed(500.0, 12.0, 0.0, 0.0, false).speed == 60.0); + CHECK(switch_curve::branch_speed(760.0, 14.0, 0.0, 0.0, false).speed == 80.0); + CHECK(switch_curve::branch_speed(1200.0, 18.5, 0.0, 0.0, false).speed == 100.0); + // the 2500-1:26.5 with a movable nose: 166 km/h by the formula at 130 mm, 130 by the table and Id-4 + auto const fast{switch_curve::branch_speed(2500.0, 26.5, 0.0, 0.0, true)}; + CHECK(fast.deficiency == 130.0); + CHECK(fast.rounded == 160.0); + CHECK(fast.speed == 130.0); + // the main track as sharp as the diverging curve against it straightens the diverging track: the table alone + auto const straight{switch_curve::branch_speed(500.0, 12.0, -500.0, -60.0, false)}; + CHECK(straight.radius == 0.0); + CHECK(std::isinf(straight.formula)); + CHECK(straight.speed == 60.0); + // the table of the editor is the one of the switches + for (auto const radius : {150.0, 190.0, 299.9, 300.0, 499.0, 500.0, 760.0, 1199.0, 1200.0, 2499.0, 2500.0, 5000.0}) { + CAPTURE(radius); + CHECK(editor_track::diverging_speed(radius) == switch_curve::basic_speed(radius)); + } +} + +TEST_CASE("switch speed: one velocity for both tracks, the main track's limit where it's the lower, else the diverging one's below 0") { + CHECK(switch_curve::written_velocity(80.0, 60.0) == -60.0); + CHECK(switch_curve::written_velocity(40.0, 60.0) == 40.0); + CHECK(switch_curve::written_velocity(60.0, 60.0) == 60.0); + CHECK(switch_curve::written_velocity(-1.0, 60.0) == -60.0); + // never -1, which is no limit at all + CHECK(switch_curve::written_velocity(80.0, 1.0) == -2.0); +}