18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/editor/editorSwitchCurve.cpp
T
maj00r 10f2ac36f9 fix(editor): a switch laid gets the speed of its diverging track, one breaking a hard rule of ST-T1-A6 isn't laid (#454)
* fix(editor): track edits keep the joints, the norms, the records and the saved speed together

- The cant typed in the inspector goes on into the track joined at that end, as the gizmo and the
  joints tool do it; the neighbours go into the same undo step. The joint stepped before.
- A track laid on from a canted joint takes the cant off along the ramp the norms ask for at the
  speed (`alignment::cant_ramp_length`), not at a fixed 1.5 mm a metre whatever the speed and the
  norms: 120 mm at 100 km/h over 120 m, not 80 m.
- Deleting a track of the line under the route tool keeps the laying norms. They went back to the
  defaults, and every later lay, extend and join used those.
- The gauge is a paste group of its own, off by default: pasting how a track rides or sounds no
  longer changes its gauge.
- The speed of a turntable set in the editor is the one saved. The editor changed the speed the
  turntable runs at, while the file keeps the limit it was loaded with.
- Deleting a set of tracks does what Del does with each: a double switch, a slip and a crossover go
  whole, the records of the switches go with them, and the model and events of a turntable too. The
  records stayed behind and on saving were given to another switch, which then showed a wrong type.
- Deleting a switch of a crossover takes the crossover out whole, as the connection tools do: the
  outer track of a crossover on a curve comes down by its lift and the record of the connection
  goes. Alone, the switch left the outer track up for good, and the connection to the next switch
  given its name. A switch bent out of the shape of its record is found by its name.

Tests: a unit test of the norm ramp. The other changes are in the editor's UI, which the unit tests
don't reach.

* test(editor): self-test scripts that fail, a runner, and a case for each fix of the track editing

The editor's self-test scripts only logged: nothing made a run fail. Now:

- `expect` commands check the state and log `SELFTEST PASS` or `SELFTEST FAIL ... got X, expected Y`:
  the cant at a track end, the joint at an end (gap, angle, cant step), the records of a switch and
  the connections naming it, speed, gauge, a track present or removed, the laying norms' gauge,
  heights and lifts against a mark, whether a track is in the line under the route tool, and the
  includes of a turntable. A command no group takes counts as a failure, so a mistyped one can't
  make the checks after it pass.
- At `quit` the result is logged, and a script whose expectations failed ends the editor with exit
  code 2 (a failed start stays 1; the simulator's test mode is unchanged).
- The commit of the inspector's cant field is `track_cant_commit`, which the inspector and the
  `trackcant` command both call; `routegauge` sets the laying norms as the route tool does, and
  `tracksetdelete` deletes a set of tracks by name.
- `tests/editor/run.py` runs each script of `tests/editor/cases` on a copy of the fixture scenery
  in the game folder, with a wall-time limit and a memory cap, reads the exit code and the
  `SELFTEST` lines, and deletes the copy. A `#> reopen` line restarts the editor on the saved
  scenery. See `tests/editor/README.md`.
- One case per fix of the track editing quick fixes (inspector cant, norms after deleting, the
  gauge paste group, turntable speed saved, deleting a set, a set with a double switch, a slip
  and a crossover, deleting a switch of a crossover, a bent switch). All pass; with the fixes
  reverted all fail.

* feat(editor): the track tool panels keep their fields, results and buttons within the panel

- The card of the tool: after a long gesture the action goes under it, at its indent, when what is
  left of the row is narrower than 8 font sizes or the longest word of the action. It was squeezed
  into a sliver and broken in the middle of its words.
- A click on the title row of the card folds it to that row (icon, name, family, key, and a small
  triangle marker) and opens it again. One flag for all the tools, kept with the editor settings
  (`track_card_folded` in eu07_editor.ini), open by default: the card took 250-300 px at the top
  of every tool and repeats the hint bar of the view.
- The grade labels of the vertical profile, on the graph and in the view, write the per mille as
  the panel does (the translated "per mille", the per mille sign in Polish), not "o/oo".
- The track analysis window: the thresholds of the speed check and of the joints keep to their
  widest name and a field of 10 font sizes where the window is wider than that, as docked across
  the bottom (`begin_fields_fitted`). The results are tables with each column as wide as what is in
  it: the speed check with its column names kept at the top as the list scrolls, the joints with
  the kind, the value and the two tracks next to each other. A click on a row shows it as before.
- `same_line_if_fits` puts the next button beside the one before only when it fits in what is left
  of the line, at the start of the next line otherwise. Used where the route design puts buttons
  side by side, for "Forget the design" in the profile panel, which was cut off at the edge, and for
  the buttons of a switch in the inspector.
- The laying options: the speed, the curve radius and the longest path are fields with the name on
  the left in two columns (`begin_fields`/`field_row`), as in the inspector, the route and the
  profile, the chips of the quick values under them. The segmented choices give the short names
  less of the room when a long one doesn't fit in its even share, and go on in a second bar when
  they don't fit side by side: "Through the clicked points" overflowed its third of the bar.

* fix(editor): a switch laid gets the speed of its diverging track, one breaking a hard rule of ST-T1-A6 isn't laid

- Every switch laid writes the speed of its diverging track (`velocity`): the switch tool on a
  straight, into a curve, from a free end and linked, a double switch, the switches of a slip, a
  switch given another template of the catalogue, and a switch moved along the curve it's bent
  into. A switch of the catalogue had no limit before, one copied from a switch of the scenery kept
  that one's, one laid without a track had the laying speed on both its tracks. The limit is for the
  diverging track alone (`velocity -V`); the own limit of the track it goes into stays for both
  where it's the lower. The switch tool says the speed and what gives it.
- The speed is formula 9.9 of PKP PLK ST-T1-A6 the other way round, V = sqrt((I + D) r / 11.8), with
  the normal cant deficiency of the frog of table 9.7 (110 mm a fixed frog, 130 mm a movable nose),
  rounded down to the steps of the speed limits and no more than table 11.2 gives the basic switch.
  For the fixed frogs the formula gives table 11.2 itself (190 40, 300 50, 500 60, 760 80, 1200
  100 km/h); the 2500-1:26.5 with a movable nose stays 130. A switch bent into a curve takes the
  radius of its diverging track from table 11.8 (one-sided r" = (Rr - t^2)/(R + r), two-sided
  (Rr + t^2)/(R - r)) and the cant of the line with its sign: leaning against the diverging track it
  takes speed away, where the speed check counted it as helping. Rz 60E1-190-1:9 bent out of the
  curve R 600 m with 60 mm: 30 km/h, not 40; the 60E1-500-1:12 of the review, out of R 1200 m with
  60 mm: 60 km/h at its fixed frog (54 at I = 100 mm, the 0.65 m/s2 the editor took), not 107.
- A switch breaking a hard rule isn't laid, the tracks left as they were, and the switch tool says
  why, with the value, the limit and the paragraph: the cant changing within it (11.4 pkt 7), a cant
  ramp ending within it or nearer than 6 m (9.6 pkt 16-17; one ending right at an end of the switch
  is let be, as the snapping of the start offers it), a double switch, a slip or a crossing bent
  (11.4 pkt 4), the curvature of its diverging or its main track turning from one side to the other
  (9.7 pkt 12, KZW II). They were warnings shown after the switch was laid, and on a straight the
  cant wasn't looked at at all. The line is looked at 10 m past the ends of the curve or the
  straight too, so the ramp on the straight before a curve counts. The previews show the refusals.
- A switch moved along its straight or the curve it's bent into, a double switch moved along its
  straight, and a switch given another template go through the same checks; one breaking a rule
  stays as it is.
- Warnings still: 1:9.403 and steeper than 1:9 bent (11.4 pkt 4c-d), a rule of the running and the
  main tracks, which the editor doesn't tell from the side ones, as the warning now says; the limits
  of table 11.9 and a cant over 100 mm in a bent switch (table 9.6), as before.

Tests: unit tests of tables 11.8 and 11.9 (the least main radius bends the diverging track to the
least radius the table gives), table 9.7, the speeds of table 11.2 by formula 9.9, the speed of a
bent switch with the cant with its sign (the example of the review), the cant over a switch on a
straight and a double switch bent. Editor cases `q2_1_switch_speed`, `q2_2_switch_refusals` and
`q2_3_switch_move`, with `expect status` (the status of the switch tool) and `expect steps` (the
undo steps), the fixture with a straight taking a cant on and a compound curve without cant. All
pass; with the changes of the editor reverted all three fail.

* test(editor): a mistyped track fails the self-test, indented comments are skipped, case 7b checks the switch got bent

- A blank line or an indented `#` comment in a script is no longer a command no group takes.
- `trackcant`, `routegauge` and `tracksetdelete` with a track that isn't there count as a failed expectation.
- `expect shaped <switch> <yes|no>`: 7b checks that the gizmo drag bent the switch out of its record before deleting it.
- The ramp-length loop of test_alignment counts in an integer (Sonar S2193).
2026-11-27 03:57:25 +01:00

414 lines
20 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/.
*/
#include "stdafx.h"
#include "editor/editorSwitchCurve.hpp"
#include "editor/editorFormat.hpp"
#include "utilities/translation.h"
#include <algorithm>
#include <cmath>
#include <limits>
namespace switch_curve
{
namespace
{
// 1/m: a curvature below it is a straight, the curvature measured over a few metres never quite 0
constexpr double kStraight{2e-5};
// m: an end of a ramp this close to an end of the switch is at it
constexpr double kAtEnd{0.3};
// m: how far off the switch a ramp keeps, 9.6 pkt 16
constexpr double kRampClear{6.0};
// m: the least radius of a side track, table 9.2
constexpr double kSideRadius{180.0};
// the curved switches bent from the basic ones, table 11.9: the radius of the basic switch, the limiting curvature of the
// diverging track and the least radius of the main track, then the values in its brackets, allowed only by way of exception
constexpr bent_limit kBent[]{{300.0, 4.000e-3, 1505.522, 4.673e-3, 749.722},
{500.0, 4.000e-3, 501.730, 4.673e-3, 375.638},
{760.0, 3.333e-3, 497.250, 3.333e-3, 497.250},
{1200.0, 2.169e-3, 750.001, 2.169e-3, 750.001},
{2500.0, 1.063e-3, 1510.0, 1.063e-3, 1510.0}};
// formula 9.9: I = 11.8 V^2 / R - D, the standard gauge in it
constexpr double kDeficiencyFactor{11.8};
double heading_at(std::vector<track_point> const &Track, double const Station)
{
if (Station <= Track.front().station)
return Track.front().heading;
if (Station >= Track.back().station)
return Track.back().heading;
auto const next{std::lower_bound(Track.begin(), Track.end(), Station, [](track_point const &Point, double const Value) { return Point.station < Value; })};
auto const previous{std::prev(next)};
auto const span{std::max(1e-9, next->station - previous->station)};
return previous->heading + (next->heading - previous->heading) * (Station - previous->station) / span;
}
double cant_at(std::vector<track_point> const &Track, double const Station)
{
if (Station <= Track.front().station)
return Track.front().cant;
if (Station >= Track.back().station)
return Track.back().cant;
auto const next{std::lower_bound(Track.begin(), Track.end(), Station, [](track_point const &Point, double const Value) { return Point.station < Value; })};
auto const previous{std::prev(next)};
auto const span{std::max(1e-9, next->station - previous->station)};
return previous->cant + (next->cant - previous->cant) * (Station - previous->station) / span;
}
// the cant of the line over a switch From to To along it: the same all along it, a ramp off it by 6 m at least or ending at its ends
// (11.4 pkt 7, 9.6 pkt 16-17), as much as table 9.6 lets a switch have, Curved or straight. A ramp under the switch or ending near it
// isn't one the switch is laid or moved with, an end of one right at an end of the switch is
void check_cant(std::vector<track_point> const &Line, double const From, double const To, bool const Curved, double &Start, double &End, std::vector<std::string> &Notes, std::vector<std::string> &Warnings,
std::vector<std::string> &Refusals)
{
Start = std::abs(cant_at(Line, From));
End = std::abs(cant_at(Line, To));
if (std::abs(End - Start) > 1.0)
Refusals.emplace_back(format(STR_C("The cant changes within the switch, %.0f to %.0f mm: it's to stay the same over it (11.4 pkt 7)"), Start, End));
auto const low{std::min(From, To)};
auto const high{std::max(From, To)};
for (auto const end : ramp_ends(Line))
{
if (end > low + kAtEnd && end < high - kAtEnd)
Refusals.emplace_back(format(STR_C("A cant ramp starts or ends within the switch, %.1f m from its start: at the start or the end of the switch, or 6 m off it at least (9.6 pkt 16-17)"), std::abs(end - From)));
else if (std::abs(end - low) <= kAtEnd || std::abs(end - high) <= kAtEnd)
Notes.emplace_back(STR_C("A cant ramp starts or ends at an end of the switch"));
else if (auto const off{end < low ? low - end : end - high}; off < kRampClear)
Refusals.emplace_back(format(STR_C("A cant ramp ends %.1f m off the switch, 6 m at least (9.6 pkt 16)"), off));
}
auto const cant{std::max(Start, End)};
if (Curved && cant > 100.0 + 1e-6)
Warnings.emplace_back(format(STR_C("Cant %.0f mm in a bent switch, over 100 mm (table 9.6)"), cant));
else if (Curved && cant > 60.0 + 1e-6)
Notes.emplace_back(format(STR_C("Cant %.0f mm: over the 60 mm recommended for a bent switch, up to 100 mm (table 9.6)"), cant));
else if (false == Curved && cant > 1.0)
Notes.emplace_back(format(STR_C("Cant %.0f mm in a straight switch: none recommended, up to 100 mm (table 9.6)"), cant));
}
} // namespace
double curvature_at(std::vector<track_point> const &Track, double const Station)
{
if (Track.size() < 2)
return 0.0;
auto const from{std::max(Track.front().station, Station - 1.0)};
auto const to{std::min(Track.back().station, Station + 1.0)};
if (to - from < 0.2)
return 0.0;
return (heading_at(Track, to) - heading_at(Track, from)) / (to - from);
}
std::vector<double> ramp_ends(std::vector<track_point> const &Track)
{
std::vector<double> ends;
auto ramping{false};
for (std::size_t i = 0; i + 1 < Track.size(); ++i)
{
auto const span{Track[i + 1].station - Track[i].station};
if (span < 1e-6)
continue;
// a change of the cant of 1 mm over 20 m and more
auto const changing{std::abs(Track[i + 1].cant - Track[i].cant) / span > 0.05};
if (changing == ramping)
continue;
ramping = changing;
// a ramp going on after a break of under a metre, where the paths meet, is the same ramp
if (changing && false == ends.empty() && Track[i].station - ends.back() < 1.0)
ends.pop_back();
else
ends.push_back(Track[i].station);
}
if (ramping)
ends.push_back(Track.back().station);
return ends;
}
switch_report check(std::vector<track_point> const &Main, std::vector<track_point> const &Branch, std::vector<track_point> const &Line, double const From, double const To, double const Radius,
double const Ratio, double const Straight, bool const Double)
{
switch_report report;
if (Main.size() < 2 || Branch.size() < 2 || Line.size() < 2)
return report;
// the curvatures counted positive the way the line turns, as the switch goes along it
auto const direction{To >= From ? 1.0 : -1.0};
auto const sign{(Line.back().heading - Line.front().heading < 0.0 ? -1.0 : 1.0) * direction};
struct span
{
double first{0.0};
double last{0.0};
double low{std::numeric_limits<double>::max()};
double high{std::numeric_limits<double>::lowest()};
double sharpest{0.0};
};
// of the part bent: up to the frog of a switch bent up to it
auto const along = [&](std::vector<track_point> const &Track) {
span result;
auto const finish{Track.back().station - (Straight > 0.5 ? Straight : 0.0)};
auto const length{finish - Track.front().station};
auto const margin{std::min(1.0, length * 0.25)};
result.first = sign * curvature_at(Track, Track.front().station + margin);
result.last = sign * curvature_at(Track, finish - margin);
for (int k = 0; Track.front().station + margin + k * 0.5 <= finish - margin + 1e-9; ++k)
{
auto const s{Track.front().station + margin + k * 0.5};
auto const curvature{sign * curvature_at(Track, s)};
result.low = std::min(result.low, curvature);
result.high = std::max(result.high, curvature);
result.sharpest = std::max(result.sharpest, std::abs(curvature));
}
return result;
};
auto const main{along(Main)};
auto const branch{along(Branch)};
report.main_start = main.first;
report.main_end = main.last;
report.branch_start = branch.first;
report.branch_end = branch.last;
report.curved = main.sharpest > kStraight;
report.transition = report.curved && std::abs(main.last - main.first) > std::max(2e-6, 0.05 * std::max(std::abs(main.first), std::abs(main.last)));
// the curvature of each track of one sign all along it, the cases KZW I and KZW III of 9.7 pkt 12, not KZW II
if (branch.low < -kStraight && branch.high > kStraight)
report.refusals.emplace_back(format(STR_C("The curvature of the diverging track turns from one side to the other within the switch (KZW II), R %.0f m one way and R %.0f m the other, which isn't allowed (9.7 pkt 12)"),
1.0 / branch.high, -1.0 / branch.low));
if (main.low < -kStraight && main.high > kStraight)
report.refusals.emplace_back(format(STR_C("The curvature of the main track turns from one side to the other within the switch, R %.0f m one way and R %.0f m the other, which isn't allowed (9.7 pkt 12)"),
1.0 / main.high, -1.0 / main.low));
// the editor tells no main track from a side one: a double switch is bent nowhere
if (report.curved && Double)
report.refusals.emplace_back(format(STR_C("Bent into R %.0f m: no double switch, slip nor crossing is bent (11.4 pkt 4)"), 1.0 / main.sharpest));
if (report.curved)
{
auto const limit{std::find_if(std::begin(kBent), std::end(kBent), [&](bent_limit const &Limit) { return std::abs(Limit.radius - Radius) < 2.0; })};
if (limit == std::end(kBent))
report.notes.emplace_back(format(STR_C("The basic switch of R %.0f m isn't in table 11.9, its limiting curvature isn't checked"), Radius));
else
{
if (branch.sharpest > limit->branch + 1e-9)
report.warnings.emplace_back(format(STR_C("The curvature of the diverging track %.3f 1/km is over the limiting %.3f 1/km for the switch of R %.0f m (table 11.9)"), branch.sharpest * 1000.0, limit->branch * 1000.0, Radius));
if (main.sharpest > kStraight && 1.0 / main.sharpest < limit->main - 1e-6)
report.warnings.emplace_back(format(STR_C("The main track bent to R %.0f m, below the least %.0f m for the switch of R %.0f m (table 11.9)"), 1.0 / main.sharpest, limit->main, Radius));
}
// 1:9 itself is bent, as the Rz 300-1:9 of table 11.9; the ratio may come from the angle of the tracks, a bit off the catalogue,
// 1:9.403 told from 1:9.51 by half of what keeps them apart. A warning only: the rule holds in the running and the main tracks,
// which the editor doesn't tell from the side ones
if (Ratio > 0.0 && (Ratio < 8.95 || std::abs(Ratio - 9.403) < 0.05))
report.warnings.emplace_back(format(STR_C("A switch of 1:%.3f isn't bent in the running and the main tracks: none of 1:9.403 nor steeper than 1:9 (11.4 pkt 4). The editor doesn't tell a main track from a side one, so it only warns"), Ratio));
// the basic switches of table 11.9 have the curve of the diverging track running through the frog; one with a straight frog is
// bent up to the frog, the frog and both tracks over it straight, the curvature jumping there
if (Straight > 0.5)
report.warnings.emplace_back(format(STR_C("Bent up to the frog, as the DB bends the switches with a straight frog: both tracks keep straight over its last %.1f m, the curvature jumps there; table 11.9 lists no such bent switch"), Straight));
}
check_cant(Line, From, To, report.curved, report.cant_start, report.cant_end, report.notes, report.warnings, report.refusals);
return report;
}
switch_report check_line(std::vector<track_point> const &Line, double const From, double const To)
{
switch_report report;
if (Line.size() >= 2)
check_cant(Line, From, To, false, report.cant_start, report.cant_end, report.notes, report.warnings, report.refusals);
return report;
}
std::optional<bent_limit> bent_limit_of(double const Radius)
{
auto const limit{std::find_if(std::begin(kBent), std::end(kBent), [&](bent_limit const &Limit) { return std::abs(Limit.radius - Radius) < 2.0; })};
if (limit == std::end(kBent))
return std::nullopt;
return *limit;
}
double bent_radius(double const Main, double const Radius, double const Ratio)
{
if (Radius <= 0.0)
return 0.0;
if (std::abs(Main) < 1e-6)
return Radius;
auto const tangent{Ratio > 0.0 ? Radius * std::tan(0.5 * std::atan(1.0 / Ratio)) : 0.0};
auto const product{std::abs(Main) * Radius};
auto const squared{tangent * tangent};
// a) two-sided, the main track bent against the diverging one, and b) one-sided, the main track bent with it: r" = (R r -+ t^2) / (R +- r);
// the main track sharper than the diverging curve takes the diverging track past straight, the one-sided switch of b) bent from its
// diverging track: r" = (R r + t^2) / (r - R)
if (Main > 0.0)
return (product - squared) / (Main + Radius);
auto const apart{-Main - Radius};
if (std::abs(apart) < 1e-6)
return 0.0;
return (product + squared) / apart;
}
deficiency switch_deficiency(bool const Movable)
{
return Movable ? deficiency{100.0, 130.0, 130.0} : deficiency{90.0, 110.0, 110.0};
}
double curve_speed(double const Radius, double const Cant, double const Deficiency)
{
if (Radius <= 0.0)
return std::numeric_limits<double>::infinity();
return std::sqrt(std::max(0.0, Deficiency + Cant) * Radius / kDeficiencyFactor);
}
slip_report check_slip(std::vector<std::vector<track_point>> const &Tracks, std::vector<std::vector<track_point>> const &Curves, std::vector<track_point> const &Line, double const From, double const To)
{
slip_report report;
if (Tracks.size() < 2 || Line.size() < 2 || std::any_of(Tracks.begin(), Tracks.end(), [](std::vector<track_point> const &Track) { return Track.size() < 2; }))
return report;
// the curvatures counted positive the way the line turns, as the first track goes along it
auto const sign{(Line.back().heading - Line.front().heading < 0.0 ? -1.0 : 1.0) * (To >= From ? 1.0 : -1.0)};
struct span
{
double low{std::numeric_limits<double>::max()};
double high{std::numeric_limits<double>::lowest()};
double sharpest{0.0};
};
auto const along = [&](std::vector<track_point> const &Track) {
span result;
auto const margin{std::min(1.0, (Track.back().station - Track.front().station) * 0.25)};
for (int k = 0; Track.front().station + margin + k * 0.5 <= Track.back().station - margin + 1e-9; ++k)
{
auto const s{Track.front().station + margin + k * 0.5};
auto const curvature{sign * curvature_at(Track, s)};
result.low = std::min(result.low, curvature);
result.high = std::max(result.high, curvature);
result.sharpest = std::max(result.sharpest, std::abs(curvature));
}
return result;
};
auto const middle = [&](std::vector<track_point> const &Track) { return sign * curvature_at(Track, 0.5 * (Track.front().station + Track.back().station)); };
report.first = middle(Tracks[0]);
report.second = middle(Tracks[1]);
// the curvature of each track through it of one sign all along it, as in the main track of a switch (9.7 pkt 12); the slip curves
// begin with the leads along the stock rails, which bend with the line even where the curve turns against it
auto turning{false};
double sharpest{0.0};
for (auto const &track : Tracks)
{
auto const curvatures{along(track)};
report.curved = report.curved || curvatures.sharpest > kStraight;
sharpest = std::max(sharpest, curvatures.sharpest);
turning = turning || (curvatures.low < -kStraight && curvatures.high > kStraight);
}
if (turning)
report.refusals.emplace_back(STR_C("The curvature of a track through the slip turns from one side to the other within it, which isn't allowed (9.7 pkt 12)"));
for (auto const &curve : Curves)
if (curve.size() >= 2)
report.curves.push_back(along(curve).sharpest);
if (report.curved)
{
// the editor tells no main track from a side one: a slip is bent nowhere
report.refusals.emplace_back(format(STR_C("Bent into R %.0f m: no double switch, slip nor crossing is bent (11.4 pkt 4)"), 1.0 / sharpest));
// bent the way the curve of the slip turns it gets sharper: no sharper than the side tracks take
for (auto const curvature : report.curves)
if (curvature > 1.0 / kSideRadius + 1e-9)
report.warnings.emplace_back(format(STR_C("A slip curve bent to R %.0f m, below the least %.0f m of a side track (table 9.2)"), 1.0 / curvature, kSideRadius));
}
check_cant(Line, From, To, report.curved, report.cant_start, report.cant_end, report.notes, report.warnings, report.refusals);
if (report.curved && std::max(report.cant_start, report.cant_end) > 1.0)
report.notes.emplace_back(STR_C("In a side track the layouts needing no cant are recommended (9.3 pkt 5)"));
return report;
}
exit_report check_exit(double const Curvature, double const Straight, std::vector<std::array<double, 3>> const &Line, double const Speed, double const Gauge)
{
exit_report report;
// table 9.15 and table 9.16, track connections
report.allowed = Speed <= 60.0 ? 130.0 : Speed <= 100.0 ? 100.0 : Speed <= 160.0 ? 80.0 : 60.0;
report.element = Speed <= 60.0 ? 6.0 : Speed <= 100.0 ? 0.15 * Speed : 0.25 * Speed;
// mm of the cant deficiency the change of the curvature makes at once: 11.8 V^2 / R at the standard gauge
auto const sudden = [&](double const Before, double const After) { return Gauge * Speed * Speed / (9.81 * 3.6 * 3.6) * std::abs(After - Before); };
// 1/m, a change of the curvature as large at once is a sudden one
constexpr double kSudden{1e-4};
struct change
{
double along; // from the end of the switch
double before;
double after;
};
std::vector<change> changes;
auto curvature{Curvature};
if (Straight > 0.0 && std::abs(Curvature) > kSudden)
{
changes.push_back({-Straight, Curvature, 0.0});
curvature = 0.0;
}
double along{0.0};
for (auto const &[length, start, end] : Line)
{
if (std::abs(start - curvature) > kSudden)
changes.push_back({along, curvature, start});
curvature = end;
along += length;
}
// the curve of the switch going on into the line: nothing of the switch to judge
if (changes.empty() || changes.front().along > 1e-6)
return report;
auto const &first{changes.front()};
auto const alone = [&](change const &Change) {
auto const value{sudden(Change.before, Change.after)};
report.change = std::max(report.change, value);
if (value > report.allowed)
report.warnings.emplace_back(format(STR_C("Behind the switch the curvature changes at once by a sudden change of the cant deficiency of %.0f mm, over the %.0f mm allowed at %.0f km/h (table 9.15)"), value, report.allowed, Speed));
};
if (changes.size() > 1)
{
auto const &next{changes[1]};
report.apart = next.along - first.along;
if (report.apart < report.element)
{
// taken together, from before the first one to after the next one (9.11 pkt 8b, 9)
report.change = sudden(first.before, next.after);
if (report.change > report.allowed)
report.warnings.emplace_back(format(STR_C("The curve behind the switch begins %.1f m after the end of its curve, closer than the %.1f m which take the sudden changes of the cant deficiency apart: together %.0f mm, over the %.0f mm allowed at %.0f km/h (tables 9.15, 9.16)"),
report.apart, report.element, report.change, report.allowed, Speed));
return report;
}
alone(next);
}
// the end of the curve of the switch at its end, not within it over a straight frog: that one is the switch's own
if (first.along > -1e-6)
alone(first);
return report;
}
double snapped_start(std::vector<double> const &Ramps, double const Pressed, double const Length, int const Direction, double const Reach, std::string *What)
{
auto const d{Direction < 0 ? -1.0 : 1.0};
auto best{Pressed};
auto nearest{Reach};
std::string name;
auto const consider = [&](double const Start, char const *Name) {
if (auto const distance{std::abs(Start - Pressed)}; distance <= nearest)
{
nearest = distance;
best = Start;
name = Name;
}
};
for (auto const end : Ramps)
{
consider(end, STR_C("the switch starts at an end of the cant ramp"));
consider(end - d * Length, STR_C("the switch ends at an end of the cant ramp"));
consider(end + d * kRampClear, STR_C("the switch starts 6 m off the cant ramp"));
consider(end - d * (kRampClear + Length), STR_C("the switch ends 6 m off the cant ramp"));
}
if (What != nullptr)
*What = name;
return best;
}
} // namespace switch_curve