From ca1f2239df4257ec9afb2b35af24a31bc107c638 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:28:20 +0100 Subject: [PATCH] feat(editor): the double, symmetric and secant switches, the slips and the crossings of the catalogue in the domain model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit E5 (editor-tracks-domain.md §4 "Rozjazd", §7): every type of the catalogue is a turnout the model lays, its tracks lines it owns and its legs nodes, kept whole or refused like the ordinary switches E2b laid. E2b refused the rest with "stage E5". - Secant blades (S42): the diverging track runs along the main one for the pre-blade stretch and turns off it by the angle of the blades at their tips, the rest of 1:n on its arc; a turn inside the track, the node at the start keeping the heading of the main track. The generator ends a piece there with the heading of the element before it (unchanged where a line runs on in its heading). - Double switches (Rpd, Rpj, Rps): the main track cut at the start of the second switch, its node holding the main track, the main track of the second switch (main2) and its diverging track, to the other side or the same one, as switch_design::build() lays it. Written as the switch tool writes them: the first switch, a plain track of a metre before the second one (the simulation joins a switch to plain tracks only), the second switch. - Symmetric switches (Rls): both tracks turning off by half of the angle, to both sides, laid at a free end of a line, which they end (the line beyond it would have to turn at the node); not moved along the line. - Slips (Rkp, Rkpd) and diamond crossings (Skrz): the main track a part of the line, the crossing track across its middle at the angle of the slip between two nodes of its own, the slip curves from the start to the end of the crossing track and, of a double slip, from its start to the end of the main track; slip_of() lays the curves with the blades of the ordinary switch of the rail, radius and angle (no data of the slips' own blades was found), passing the crossing as near as the rails of the simulator's profile let them. The three with the blades outside of the diamond whose curves then reach past the half length of Id-4 (49E1 190-1:6,6, 300-1:9, 500-1:9, by up to 0.3 m) are laid as long as their curves. Written as the switch tool writes them: a switch at each leg a slip curve leaves, the tracks crossing and the middles of the slip curves plain tracks, the joints inside it the same numbers on both sides. - The rules: a double switch, a slip, a crossing (11.4 pkt 4 a, b) and a symmetric switch (no rule of table 11.8 for it) lie on straights only; an invariant, refused with the reason before anything is laid. The norms judge the tracks crossing at a slip or a crossing as crossings (no cant, tab. 9.6), its slip curves as diverging tracks. - The ends of the tracks of a turnout which carry no line through their node (the starts of the diverging tracks, the ends of the slip curves) are branch_end(); a node holds such an end beside a track of the same turnout on its side. settle() lays all the tracks a turnout lays itself (lay_tracks()), the nodes of its own where they end; lifting, moving, changing (to another kind too) and removing a turnout take all of them along. - The project file: the turnout record gets "second " and "crossing slip " where the turnout has them (diverging 0 0 for a crossing); version 1, unreleased, read as before without them. The generated section keeps each track a turnout is made of. - TSI INF 4.2.3.4(1), 150 m at least, judges the plain line, not the curves of switches (4.2.5): its hard row leaves out the tracks of the turnouts, so the industrial S42 switches of R 70 and 140 m are laid. Tests: U: every type of the catalogue laid (41 ordinary, 3 symmetric, 15 double, 18 slips, 20 crossings) on a straight to both sides along the line and against it, its legs where the catalogue puts them (the main track a + b, 1:n at the ends, the second switch at the stagger, the crossing track at the angle of the slip, half its length from the crossing), moved and taken out, the line one again, each a step to undo; the turn of the secant blades of S42-190-1:7 at the end of the pre-blade stretch; a double switch, two slips and a crossing refused in a curve, a secant S42 bent in a side track, a symmetric switch refused at the end of a curve. The three fitted to the tangents of S42-140-1:6,6 16/12 measured are refused by the norms (tab. 9.14) where the transition one is. P: 200 random edits with every type along arcs and transitions, seeds 5150, 2718 and 1618: the invariants after each, a refusal changing nothing, undone back to the start exactly. G: every type made into the tracks of the simulator and read back by TTrack::Load, the ends of the paths at the legs or at each other to the bit, the velocity and the frog; a double switch, a double slip and a crossing written to the project file and read back, their tracks laid again the same to the bit. The E2b case of the types refused now checks that they're laid. Unit tests 1077 -> 1086. --- editor/layout/layoutCommands.cpp | 542 +++++++++++++++----- editor/layout/layoutGenerator.cpp | 226 +++++++-- editor/layout/layoutGenerator.hpp | 24 +- editor/layout/layoutNetwork.cpp | 157 +++--- editor/layout/layoutNetwork.hpp | 20 +- editor/layout/layoutTurnout.cpp | 598 +++++++++++++++++++---- editor/layout/layoutTurnout.hpp | 99 +++- editor/norms/normsTables.cpp | 2 +- editor/project/projectFile.cpp | 85 +++- editor/project/projectFile.hpp | 5 +- editor/project/projectGenerated.cpp | 32 +- tests/unit/layout_fixture.h | 82 +++- tests/unit/test_layout_turnout_kinds.cpp | 563 +++++++++++++++++++++ tests/unit/test_layout_turnouts.cpp | 13 +- 14 files changed, 2053 insertions(+), 395 deletions(-) create mode 100644 tests/unit/test_layout_turnout_kinds.cpp diff --git a/editor/layout/layoutCommands.cpp b/editor/layout/layoutCommands.cpp index f1c569635..8bc0d8d0c 100644 --- a/editor/layout/layoutCommands.cpp +++ b/editor/layout/layoutCommands.cpp @@ -426,20 +426,257 @@ bool merge_at(transaction &Change, node_id const Node) return false; } -// the ids a turnout laid again keeps: of itself, its legs, its tracks and the line beyond its second leg; none where new ones are given +// the ids a turnout laid again keeps: of itself, its legs, its tracks and the line beyond its last leg; none where new ones are given struct turnout_ids { turnout_id turnout{turnout_id::none}; node_id start{node_id::none}; node_id main_end{node_id::none}; node_id diverging_end{node_id::none}; + node_id second{node_id::none}; + node_id diverging2_end{node_id::none}; + node_id cross_start{node_id::none}; + node_id cross_end{node_id::none}; line_id main{line_id::none}; + line_id main2{line_id::none}; line_id beyond{line_id::none}; line_id diverging{line_id::none}; + line_id diverging2{line_id::none}; + line_id crossing{line_id::none}; }; -// the turnout laid into the line, its start at the chainage: the line cut at its legs, the part between them its main track, the diverging -// track laid off its start to a node at its end +turnout_ids ids_of(turnout const &Turnout) +{ + turnout_ids result; + result.turnout = Turnout.id; + result.start = Turnout.start; + result.main_end = Turnout.main_end; + result.diverging_end = Turnout.diverging_end; + result.second = Turnout.second; + result.diverging2_end = Turnout.diverging2_end; + result.cross_start = Turnout.cross_start; + result.cross_end = Turnout.cross_end; + result.main = Turnout.main; + result.main2 = Turnout.main2; + result.diverging = Turnout.diverging; + result.diverging2 = Turnout.diverging2; + result.crossing = Turnout.crossing; + return result; +} + +// the side of the node the line lies on at its end there +int side_at(network const &Network, line_id const Line, node_id const Node) +{ + auto const *owner{Network.find(Line)}; + if (owner == nullptr) + return 1; + auto const *held{Network.end_of(Line, owner->start == Node ? line_end::start : line_end::end)}; + return held != nullptr ? held->side() : 1; +} + +// the end of a track of the turnout put into a node of the line it lies on, on the side given +void hold_end(transaction &Change, node_id const Node, line_id const Line, line_end const End, int const Side) +{ + auto &at{Change.change(Node)}; + if (std::ranges::any_of(at.ends, [&](end_ref const &Each) { return Each.line == Line && Each.end == End; })) + return; + // the side of a start is its sense, of an end the other one + at.ends.push_back({Line, End, End == line_end::start ? Side : -Side}); +} + +// a track the turnout lays itself, between the nodes, of the class of the line it lies on; a node of its own made where there's none yet, +// holding the ends given +void own_track(transaction &Change, line_id const Id, turnout_id const Turnout, node_id const From, node_id const To, line const &Like) +{ + auto const &view{Change.view()}; + if (view.find(Id) != nullptr) + { + auto &track{Change.change(Id)}; + track.start = From; + track.end = To; + track.turnout = Turnout; + return; + } + line track; + track.id = Id; + track.turnout = Turnout; + track.start = From; + track.end = To; + track.category = Like.category; + track.profile = Like.profile; + track.shape.transition = Like.shape.transition; + track.shape.ramp = Like.shape.ramp; + Change.add(std::move(track)); +} + +void own_node(transaction &Change, node_id const Id, std::vector const &Ends) +{ + if (Change.view().find(Id) != nullptr) + return; + node made; + made.id = Id; + made.ends = Ends; + Change.add(std::move(made)); +} + +// the tracks the turnout lays itself laid off its frame, the nodes of its own put where they end +reasons lay_own(transaction &Change, turnout const &Made, turnout_type const &Type) +{ + auto const laid{lay_tracks(Change.view(), Made, Type)}; + if (false == laid.errors.empty()) + return laid.errors; + for (auto const &track : laid.tracks) + { + Change.change(track.line).solved = track.solved; + for (auto const at_end : {false, true}) + { + if (false == (at_end ? track.sets_end : track.sets_start)) + continue; + auto const &values{at_end ? track.solved.end : track.solved.start}; + auto &node{Change.change(at_end ? track.end : track.start)}; + node.position = values.position; + node.heading = values.heading; + node.curvature = values.curvature; + node.cant = values.cant; + node.grade = values.grade; + } + } + return {}; +} + +// the tracks of the kind of the turnout it lays itself, with their nodes of its own, and the ends of them in the nodes of the line +void add_own_tracks(transaction &Change, turnout const &Made, turnout_type const &Type, line const &Like) +{ + auto const &view{Change.view()}; + auto const main_side{side_at(view, Made.main, Made.start)}; + switch (Type.kind) + { + case turnout_kind::twofold: + { + own_track(Change, Made.diverging, Made.id, Made.start, Made.diverging_end, Like); + own_track(Change, Made.diverging2, Made.id, Made.second, Made.diverging2_end, Like); + own_node(Change, Made.diverging_end, {{Made.diverging, line_end::end, 1}}); + own_node(Change, Made.diverging2_end, {{Made.diverging2, line_end::end, 1}}); + hold_end(Change, Made.start, Made.diverging, line_end::start, main_side); + hold_end(Change, Made.second, Made.diverging2, line_end::start, side_at(view, Made.main2, Made.second)); + break; + } + case turnout_kind::slip: + case turnout_kind::crossing: + { + own_track(Change, Made.crossing, Made.id, Made.cross_start, Made.cross_end, Like); + std::vector at_start{{Made.crossing, line_end::start, 1}}; + std::vector at_end{{Made.crossing, line_end::end, 1}}; + if (Type.slip_curves >= 1) + { + own_track(Change, Made.diverging, Made.id, Made.start, Made.cross_end, Like); + at_end.push_back({Made.diverging, line_end::end, 1}); + hold_end(Change, Made.start, Made.diverging, line_end::start, main_side); + } + if (Type.slip_curves >= 2) + { + own_track(Change, Made.diverging2, Made.id, Made.cross_start, Made.main_end, Like); + at_start.push_back({Made.diverging2, line_end::start, 1}); + hold_end(Change, Made.main_end, Made.diverging2, line_end::end, side_at(view, Made.main, Made.main_end)); + } + own_node(Change, Made.cross_start, at_start); + own_node(Change, Made.cross_end, at_end); + break; + } + case turnout_kind::symmetric: + own_track(Change, Made.diverging, Made.id, Made.start, Made.diverging_end, Like); + own_node(Change, Made.diverging_end, {{Made.diverging, line_end::end, 1}}); + hold_end(Change, Made.start, Made.diverging, line_end::start, main_side); + break; + case turnout_kind::ordinary: + default: + own_track(Change, Made.diverging, Made.id, Made.start, Made.diverging_end, Like); + own_node(Change, Made.diverging_end, {{Made.diverging, line_end::end, 1}}); + hold_end(Change, Made.start, Made.diverging, line_end::start, main_side); + break; + } +} + +// a node id given, or a new one +node_id given(transaction &Change, node_id const Id) +{ + return Id != node_id::none ? Id : Change.ids().new_node(); +} + +line_id given(transaction &Change, line_id const Id) +{ + return Id != line_id::none ? Id : Change.ids().new_line(); +} + +// the ids of the tracks and the legs of its own the kind of the turnout has, the given ones or new ones +void own_ids(transaction &Change, turnout &Made, turnout_type const &Type, turnout_ids const &Ids) +{ + if (Type.kind != turnout_kind::crossing) + { + Made.diverging = given(Change, Ids.diverging); + Made.diverging_end = Type.kind == turnout_kind::slip ? node_id::none : given(Change, Ids.diverging_end); + } + if (Type.kind == turnout_kind::twofold) + { + Made.diverging2 = given(Change, Ids.diverging2); + Made.diverging2_end = given(Change, Ids.diverging2_end); + } + if (Type.kind == turnout_kind::slip || Type.kind == turnout_kind::crossing) + { + Made.crossing = given(Change, Ids.crossing); + Made.cross_start = given(Change, Ids.cross_start); + Made.cross_end = given(Change, Ids.cross_end); + if (Type.kind == turnout_kind::slip) + Made.diverging_end = Made.cross_end; + if (Type.slip_curves >= 2) + { + Made.diverging2 = given(Change, Ids.diverging2); + Made.diverging2_end = Made.main_end; + } + } +} + +// a symmetric switch laid at the free end of the line, the start of the line (Direction -1, Chainage 0) or its end (1): its two tracks off +// the node there, to its free side +reasons lay_symmetric(transaction &Change, outcome &Result, line const &Line, double const Chainage, std::string const &Type, turnout_type const &Kind, int const Hand, int const Direction, + turnout_ids const &Ids) +{ + auto const at_end{Direction > 0}; + if (std::abs(Chainage - (at_end ? Line.solved.length : 0.0)) > 1e-6) + return {std::format("{} is laid at a free end of a line: its start at 0 against {} or at {:.3f} m along it, not at {:.2f} m", Type, line_name(Line.id), Line.solved.length, Chainage)}; + auto const node{Line.node_at(at_end ? line_end::end : line_end::start)}; + auto const *at{Change.view().find(node)}; + if (at == nullptr || at->ends.size() != 1) + return {std::format("{}: {} isn't a free end of {}", Type, node_name(node), line_name(Line.id))}; + // its tracks on the side of the node the line isn't on + auto const side{-at->ends.front().side()}; + turnout made; + made.id = Ids.turnout != turnout_id::none ? Ids.turnout : Change.ids().new_turnout(); + made.type = Type; + made.hand = Hand; + made.direction = Direction; + made.start = node; + made.main = given(Change, Ids.main); + made.main_end = given(Change, Ids.main_end); + own_ids(Change, made, Kind, Ids); + own_track(Change, made.main, made.id, made.start, made.main_end, Line); + own_node(Change, made.main_end, {{made.main, line_end::end, 1}}); + hold_end(Change, made.start, made.main, line_end::start, side); + add_own_tracks(Change, made, Kind, Line); + Change.add(made); + if (auto const bent{bent_refusal(Change.view(), made, Kind)}; false == bent.empty()) + return {std::format("{}: {}", Type, bent)}; + if (auto why{lay_own(Change, made, Kind)}; false == why.empty()) + return why; + derive_turnout(Change.view(), Change.change(made.id)); + Result.turnouts.push_back(made.id); + Result.lines.insert(Result.lines.end(), {made.main, made.diverging}); + Result.nodes.insert(Result.nodes.end(), {made.start, made.main_end, made.diverging_end}); + return {}; +} + +// the turnout laid into the line, its start at the chainage: the line cut at its legs, the part between them its main track (cut again at +// the start of the second switch of a double one), its own tracks laid off its start to nodes of their own reasons lay_turnout(transaction &Change, outcome &Result, line_id const Line, double const Chainage, std::string const &Type, int const Hand, int const Direction, turnout_ids const &Ids) { reasons why; @@ -456,15 +693,20 @@ reasons lay_turnout(transaction &Change, outcome &Result, line_id const Line, do return why; if (false == found->solved.valid()) return {std::format("{} isn't laid", line_name(Line))}; + if (type->kind == turnout_kind::symmetric) + return lay_symmetric(Change, Result, *found, Chainage, Type, *type, Hand, Direction, Ids); auto const length{type->length}; auto const total{found->solved.length}; - auto const category{found->category}; + auto const like{*found}; + auto const twofold{type->kind == turnout_kind::twofold}; // the leg nearer the start of the line auto const from{Direction > 0 ? Chainage : Chainage - length}; if (from < kShortest || from + length > total - kShortest) return {std::format("{} is {:.2f} m long: it doesn't fit into {} ({:.2f} m long) with its start at {:.2f} m", Type, length, line_name(Line), total, Chainage)}; + // the part between the legs: the main track, of a double switch against its line the main track of its second switch + auto const between{twofold && Direction < 0 ? Ids.main2 : Ids.main}; split_made first; - why = split_at(Change, Line, from, Direction > 0 ? Ids.start : Ids.main_end, Ids.main, first); + why = split_at(Change, Line, from, Direction > 0 ? Ids.start : Ids.main_end, between, first); if (false == why.empty()) return why; if (false == lay(Change, first.line).valid()) @@ -473,90 +715,112 @@ reasons lay_turnout(transaction &Change, outcome &Result, line_id const Line, do why = split_at(Change, first.line, length, Direction > 0 ? Ids.main_end : Ids.start, Ids.beyond, second); if (false == why.empty()) return why; - auto &ids{Change.ids()}; turnout made; - made.id = Ids.turnout != turnout_id::none ? Ids.turnout : ids.new_turnout(); + made.id = Ids.turnout != turnout_id::none ? Ids.turnout : Change.ids().new_turnout(); made.type = Type; made.hand = Hand; made.direction = Direction; - made.main = first.line; made.start = Direction > 0 ? first.node : second.node; made.main_end = Direction > 0 ? second.node : first.node; - made.diverging = Ids.diverging != line_id::none ? Ids.diverging : ids.new_line(); - made.diverging_end = Ids.diverging_end != node_id::none ? Ids.diverging_end : ids.new_node(); - Change.change(made.main).turnout = made.id; - lay(Change, made.main); - // the diverging track from the start, on the side of it the main track lies on - auto const &view{Change.view()}; - auto const *held{view.end_of(made.main, Direction > 0 ? line_end::start : line_end::end)}; - if (held == nullptr) - return {std::format("{}: no main track at its start", Type)}; - auto const side{held->side()}; - Change.change(made.start).ends.push_back({made.diverging, line_end::start, side}); - derive_turnout(view, made); - auto laid{lay_diverging(frame_of(view, made), *type, Hand)}; - if (false == laid.valid()) - return laid.errors; - if (auto const *existing{view.find(made.diverging)}) + made.main = first.line; + if (twofold) { - auto &track{Change.change(made.diverging)}; - track.start = made.start; - track.end = made.diverging_end; - track.turnout = made.id; - track.solved = laid; - track.chainage_start = made.chainage; + // the second switch starts on the main track at the stagger, as the turnout runs + if (false == lay(Change, first.line).valid()) + return {std::format("{}: the line can't be cut at the legs of {}", line_name(Line), Type)}; + split_made third; + why = split_at(Change, first.line, Direction > 0 ? type->stagger : length - type->stagger, Ids.second, Direction > 0 ? Ids.main2 : Ids.main, third); + if (false == why.empty()) + return why; + made.second = third.node; + made.main = Direction > 0 ? first.line : third.line; + made.main2 = Direction > 0 ? third.line : first.line; } - else - { - line track; - track.id = made.diverging; - track.turnout = made.id; - track.start = made.start; - track.end = made.diverging_end; - track.category = category; - track.profile = view.find(made.main)->profile; - track.shape.transition = view.find(made.main)->shape.transition; - track.shape.ramp = view.find(made.main)->shape.ramp; - track.chainage_start = made.chainage; - track.solved = laid; - Change.add(std::move(track)); - } - if (view.find(made.diverging_end) == nullptr) - { - node end; - end.id = made.diverging_end; - end.ends.push_back({made.diverging, line_end::end, 1}); - Change.add(std::move(end)); - } - auto &end{Change.change(made.diverging_end)}; - end.position = laid.end.position; - end.heading = laid.end.heading; - end.curvature = laid.end.curvature; - end.cant = laid.end.cant; - end.grade = laid.end.grade; + for (auto const id : {made.main, made.main2}) + if (id != line_id::none) + { + Change.change(id).turnout = made.id; + lay(Change, id); + } + if (auto const bent{bent_refusal(Change.view(), made, *type)}; false == bent.empty()) + return {std::format("{}: {}", Type, bent)}; + own_ids(Change, made, *type, Ids); + add_own_tracks(Change, made, *type, like); + Change.add(made); + derive_turnout(Change.view(), Change.change(made.id)); + if (why = lay_own(Change, *Change.view().find(made.id), *type); false == why.empty()) + return why; Result.turnouts.push_back(made.id); - Result.lines.insert(Result.lines.end(), {made.main, second.line, made.diverging}); - Result.nodes.insert(Result.nodes.end(), {made.start, made.main_end, made.diverging_end}); - Change.add(std::move(made)); + // the main track and the line beyond it first, as for any turnout, then the tracks of its kind + Result.lines.insert(Result.lines.end(), {made.main, second.line}); + for (auto const id : tracks_of(made)) + if (id != made.main) + Result.lines.push_back(id); + for (auto const id : {made.start, made.main_end, made.diverging_end, made.second, made.diverging2_end, made.cross_start, made.cross_end}) + if (id != node_id::none && std::ranges::find(Result.nodes, id) == Result.nodes.end()) + Result.nodes.push_back(id); return {}; } -// the turnout lifted off its line: its diverging track off its start, its main track and the lines on both sides of it one line again, -// the first of them; none, and why, where they can't be +// the line is a track of the turnout +bool track_of(turnout const &Turnout, line_id const Line) +{ + auto const tracks{tracks_of(Turnout)}; + return std::ranges::find(tracks, Line) != tracks.end(); +} + +// the nodes of the turnout's own, which its tracks put where they end: the ends of its diverging tracks, of its crossing track, of the main +// track of a symmetric switch +std::vector own_nodes(network const &Network, turnout const &Turnout) +{ + std::vector result; + if (kind_of(Turnout) == turnout_kind::symmetric) + result.push_back(Turnout.main_end); + for (auto const id : {Turnout.diverging_end, Turnout.diverging2_end, Turnout.cross_start, Turnout.cross_end}) + if (id != node_id::none && id != Turnout.main_end && std::ranges::find(result, id) == result.end() && Network.find(id) != nullptr) + result.push_back(id); + return result; +} + +// the ends of the tracks the turnout lays itself taken out of the nodes of the line it lies on +void unhold(transaction &Change, turnout const &Turnout) +{ + auto const own{own_nodes(Change.view(), Turnout)}; + for (auto const leg : {Turnout.start, Turnout.second, Turnout.main_end}) + { + if (leg == node_id::none || Change.view().find(leg) == nullptr || std::ranges::find(own, leg) != own.end()) + continue; + auto &at{Change.change(leg)}; + std::erase_if(at.ends, [&](end_ref const &End) { return laid_by_turnout(Change.view(), End.line) && owner_of(Change.view(), End.line)->id == Turnout.id; }); + } +} + +// the turnout lifted off its line: its own tracks off the nodes of the line, its main track and the lines on both sides of it one line +// again, the first of them; none, and why, where they can't be. A symmetric switch leaves the line it ends as it was std::optional lift_turnout(transaction &Change, turnout const &Turnout, reasons &Why) { auto const &view{Change.view()}; + unhold(Change, Turnout); + if (kind_of(Turnout) == turnout_kind::symmetric) + { + auto const *at{view.find(Turnout.start)}; + if (at != nullptr && at->ends.size() == 1) + return at->ends.front().line; + Why.push_back(std::format("{}: no line ends at its start", turnout_name(Turnout.id))); + return std::nullopt; + } // the line before it along the line it lies on: the one it starts from, or the one going on from the end of its main track auto const *before{view.find(Turnout.direction > 0 ? Turnout.start : Turnout.main_end)}; line_id first{line_id::none}; if (before != nullptr) for (auto const &end : before->ends) - if (end.end == line_end::end && end.line != Turnout.main && end.line != Turnout.diverging) + if (end.end == line_end::end && false == track_of(Turnout, end.line)) first = end.line; - auto &start{Change.change(Turnout.start)}; - std::erase_if(start.ends, [&](end_ref const &End) { return End.line == Turnout.diverging && End.end == line_end::start; }); - Change.change(Turnout.main).turnout = turnout_id::none; - if (first == line_id::none || false == merge_at(Change, Turnout.start) || false == merge_at(Change, Turnout.main_end)) + for (auto const id : {Turnout.main, Turnout.main2}) + if (id != line_id::none) + Change.change(id).turnout = turnout_id::none; + if (first == line_id::none || (Turnout.second != node_id::none && false == merge_at(Change, Turnout.second)) || false == merge_at(Change, Turnout.start) || + false == merge_at(Change, Turnout.main_end)) { Why.push_back(std::format("{}: the lines on both sides of it and its main track can't be laid as one line again", turnout_name(Turnout.id))); return std::nullopt; @@ -564,24 +828,29 @@ std::optional lift_turnout(transaction &Change, turnout const &Turnout, return first; } -// the lines going on from the node at the end of a diverging track carried with it, from where it was to where it is: the vertex next to it -// turned and moved as the end did -void carry(transaction &Change, node_id const End, line_id const Track, glm::dvec2 const &From, glm::dvec2 const &FromHeading) +// the lines going on from the nodes of the turnout's own carried with them, from where they were to where they are: the vertex next to +// each node turned and moved as the node was +void carry(transaction &Change, std::vector> const &Before, turnout const &Turnout) { - auto const *at{Change.view().find(End)}; - if (at == nullptr) - return; - auto const to{geometry::plan_of(at->position)}; - auto const turn{geometry::signed_angle(FromHeading, at->heading)}; - for (auto const &end : at->ends) + auto const own{tracks_of(Turnout)}; + for (auto const &[id, was] : Before) { - if (end.line == Track) + auto const *at{Change.view().find(id)}; + if (at == nullptr) continue; - auto &vertices{Change.change(end.line).shape.vertices}; - if (vertices.empty()) - continue; - auto &next{end.end == line_end::start ? vertices.front() : vertices.back()}; - next.point = to + geometry::turned(next.point - From, turn); + auto const from{geometry::plan_of(was.position)}; + auto const to{geometry::plan_of(at->position)}; + auto const turn{geometry::signed_angle(was.heading, at->heading)}; + for (auto const &end : at->ends) + { + if (std::ranges::find(own, end.line) != own.end()) + continue; + auto &vertices{Change.change(end.line).shape.vertices}; + if (vertices.empty()) + continue; + auto &next{end.end == line_end::start ? vertices.front() : vertices.back()}; + next.point = to + geometry::turned(next.point - from, turn); + } } } @@ -597,35 +866,74 @@ reasons relay_turnout(transaction &Change, outcome &Result, turnout_id const Id, if (found == nullptr) return {std::format("there's no {}", turnout_name(Id))}; auto const old{*found}; - auto const *end{Change.view().find(old.diverging_end)}; auto const *main{Change.view().find(old.main)}; - if (end == nullptr || main == nullptr) + if (main == nullptr) return {std::format("{} lacks a leg or a track", turnout_name(Id))}; - auto const was{geometry::plan_of(end->position)}; - auto const heading{end->heading}; + auto const symmetric{kind_of(old) == turnout_kind::symmetric}; + if (symmetric && Distance != 0.0) + return {std::format("{} ends its line: it isn't moved along it, but taken out and laid at the new end", turnout_name(Id))}; + // where the nodes of its own were, for the lines going on from them + std::vector> before; + for (auto const id : own_nodes(Change.view(), old)) + before.emplace_back(id, *Change.view().find(id)); // the start along the line the lifting makes: past the line before it, and its main track where it runs against the line - auto const *before{Change.view().find(old.direction > 0 ? old.start : old.main_end)}; + auto const *previous{Change.view().find(old.direction > 0 ? old.start : old.main_end)}; double offset{0.0}; - if (before != nullptr) - for (auto const &each : before->ends) - if (each.end == line_end::end && each.line != old.main && each.line != old.diverging) + if (previous != nullptr && false == symmetric) + for (auto const &each : previous->ends) + if (each.end == line_end::end && false == track_of(old, each.line)) if (auto const *line{Change.view().find(each.line)}) offset = line->solved.length; - auto const start{offset + (old.direction > 0 ? 0.0 : main->solved.length)}; - // the line beyond its second leg along the line keeps its id - turnout_ids kept{old.id, old.start, old.main_end, old.diverging_end, old.main, line_id::none, old.diverging}; - if (auto const *after{Change.view().find(old.direction > 0 ? old.main_end : old.start)}) + auto main_length{main->solved.length}; + if (auto const *main2{Change.view().find(old.main2)}) + main_length += main2->solved.length; + auto const start{offset + (old.direction > 0 ? 0.0 : main_length)}; + // the line beyond its last leg along the line keeps its id + auto kept{ids_of(old)}; + if (auto const *after{Change.view().find(old.direction > 0 ? old.main_end : old.start)}; after != nullptr && false == symmetric) for (auto const &each : after->ends) - if (each.end == line_end::start && each.line != old.main && each.line != old.diverging) + if (each.end == line_end::start && false == track_of(old, each.line)) kept.beyond = each.line; reasons why; auto const line{lift_turnout(Change, old, why)}; if (false == line.has_value()) return why; - why = lay_turnout(Change, Result, *line, start + old.direction * Distance, Type, Hand, old.direction, kept); + auto const chainage{symmetric ? (old.direction > 0 ? Change.view().find(*line)->solved.length : 0.0) : start + old.direction * Distance}; + // another kind of turnout has other tracks: the ones of the old kind it has no place for go + std::string problem; + auto const type{turnout_type_of(Type, &problem)}; + if (false == type.has_value()) + return {problem}; + if (type->kind != kind_of(old)) + { + for (auto const id : tracks_of(old)) + if (auto const part_of_line{(id == old.main || id == old.main2) && false == symmetric}; false == part_of_line && Change.view().find(id) != nullptr) + { + for (auto const end : {line_end::start, line_end::end}) + { + auto const node{Change.view().find(id)->node_at(end)}; + if (auto const *at{Change.view().find(node)}; at != nullptr) + { + std::erase_if(Change.change(node).ends, [&](end_ref const &Each) { return Each.line == id; }); + if (Change.view().find(node)->ends.empty()) + Change.erase(node); + } + } + Change.erase(id); + } + kept = turnout_ids{old.id}; + if (false == symmetric) + { + kept.start = old.start; + kept.main_end = old.main_end; + kept.main = old.main; + } + } + why = lay_turnout(Change, Result, *line, chainage, Type, Hand, old.direction, kept); if (false == why.empty()) return why; - carry(Change, old.diverging_end, old.diverging, was, heading); + if (auto const *made{Change.view().find(Id)}) + carry(Change, before, *made); return {}; } @@ -651,21 +959,31 @@ reasons propose(transaction &Change, outcome &, remove_turnout const &Command) if (found == nullptr) return {std::format("there's no {}", turnout_name(Command.turnout))}; auto const gone{*found}; - // the diverging track, and the node at its end where nothing goes on from there - if (Change.view().find(gone.diverging_end) != nullptr) + // the tracks it lays itself, and the nodes of its own where nothing goes on from there + for (auto const id : tracks_of(gone)) { - auto &end{Change.change(gone.diverging_end)}; - std::erase_if(end.ends, [&](end_ref const &End) { return End.line == gone.diverging; }); - if (end.ends.empty()) - Change.erase(gone.diverging_end); + if (false == laid_by_turnout(Change.view(), id)) + continue; + auto const *track{Change.view().find(id)}; + for (auto const node : {track->start, track->end}) + { + if (Change.view().find(node) == nullptr) + continue; + std::erase_if(Change.change(node).ends, [&](end_ref const &End) { return End.line == id; }); + if (Change.view().find(node)->ends.empty()) + Change.erase(node); + } } - if (Change.view().find(gone.start) != nullptr) - std::erase_if(Change.change(gone.start).ends, [&](end_ref const &End) { return End.line == gone.diverging; }); - Change.erase(gone.diverging); - if (Change.view().find(gone.main) != nullptr) - Change.change(gone.main).turnout = turnout_id::none; + for (auto const id : tracks_of(gone)) + if (laid_by_turnout(Change.view(), id)) + Change.erase(id); + for (auto const id : {gone.main, gone.main2}) + if (id != line_id::none && Change.view().find(id) != nullptr) + Change.change(id).turnout = turnout_id::none; Change.erase(gone.id); // the line again where it can be + if (gone.second != node_id::none) + merge_at(Change, gone.second); merge_at(Change, gone.start); merge_at(Change, gone.main_end); return {}; @@ -762,7 +1080,7 @@ std::optional free_end_at(network const &Network, node_id const Id, bo Why.push_back(std::format("there's no {}", node_name(Id))); return std::nullopt; } - auto const turnout_start{at->ends.size() == 2 && (diverging_start(Network, at->ends[0]) || diverging_start(Network, at->ends[1]))}; + auto const turnout_start{at->ends.size() == 2 && (branch_end(Network, at->ends[0]) || branch_end(Network, at->ends[1]))}; if (at->ends.size() != 1 && false == turnout_start) { Why.push_back(std::format("{} isn't a free end", node_name(Id))); @@ -770,7 +1088,7 @@ std::optional free_end_at(network const &Network, node_id const Id, bo } free_end result; result.at = at; - result.held = turnout_start && diverging_start(Network, at->ends[0]) ? at->ends[1] : at->ends[0]; + result.held = turnout_start && branch_end(Network, at->ends[0]) ? at->ends[1] : at->ends[0]; // the new line lies on the other side of the node: from its start, along the side; to its end, against it auto const side{-result.held.side()}; result.sense = Start ? side : -side; @@ -779,7 +1097,7 @@ std::optional free_end_at(network const &Network, node_id const Id, bo result.cant = at->cant * result.sense; // the end of the diverging track of a turnout, where its curve runs through the frog: a line joined there may start from a straight, // the curvature stepping at the end of the turnout as the norms of the track connections allow (tab. 9.15) - if (auto const *switched{owner_of(Network, result.held.line)}; switched != nullptr && switched->diverging == result.held.line && result.held.end == line_end::end) + if (laid_by_turnout(Network, result.held.line)) result.curvature = 0.0; return result; } diff --git a/editor/layout/layoutGenerator.cpp b/editor/layout/layoutGenerator.cpp index 05149830e..845e0f302 100644 --- a/editor/layout/layoutGenerator.cpp +++ b/editor/layout/layoutGenerator.cpp @@ -26,12 +26,14 @@ double constexpr kStraightened{0.02}; // m, TTrack::straightened: control vector double constexpr kShortestPiece{0.1}; // m: the simulation takes a path under 5 cm for one of no length int constexpr kDeepest{14}; // halvings of a piece, at most -// a point of the line where a piece ends: at a node with the numbers of the node +// a point of the line where a piece ends: at a node with the numbers of the node. A piece ending there arrives along the element before it, +// which a secant blade turns off from (arriving: the heading of that element, the same where the line runs on in its heading) struct piece_end { double chainage{0.0}; glm::dvec3 point{0.0}; glm::dvec2 heading{0.0, 1.0}; + glm::dvec2 arriving{0.0, 1.0}; double grade{0.0}; double cant{0.0}; }; @@ -61,19 +63,27 @@ class cutter kept.chainage = chainage; kept.point = at != nullptr ? at->position : m_solved.point(chainage); kept.heading = at != nullptr ? at->heading * sense : m_solved.tangent(chainage); + kept.arriving = kept.heading; kept.cant = at != nullptr ? at->cant * sense : m_solved.cant_at(chainage); kept.grade = m_solved.grade(chainage); } } - std::vector pieces(std::vector &Errors) + std::vector pieces(std::vector &Errors) { return pieces(0.0, m_solved.length, Errors); } + + // the pieces between the chainages: from the node at an end of the line, from the point of the line inside it + std::vector pieces(double const From, double const To, std::vector &Errors) { - auto const cuts{breaks()}; + auto cuts{breaks()}; + std::erase_if(cuts, [&](double const Chainage) { return Chainage <= From + kShortestPiece * 0.5 || Chainage >= To - kShortestPiece * 0.5; }); + cuts.insert(cuts.begin(), From); + cuts.push_back(To); std::vector result; - auto from{m_start}; + auto from{From <= 0.0 ? m_start : end_at(From)}; + auto const last{To >= m_solved.length ? m_end : end_at(To)}; for (std::size_t i = 1; i < cuts.size(); ++i) { - auto const to{i + 1 == cuts.size() ? m_end : end_at(cuts[i])}; + auto const to{i + 1 == cuts.size() ? last : end_at(cuts[i])}; // no piece longer than a track may be auto const count{std::max(1, static_cast(std::ceil((to.chainage - from.chainage) / geometry::longest_path - 1e-9)))}; auto start{from}; @@ -101,6 +111,11 @@ class cutter result.chainage = Chainage; result.point = m_solved.point(Chainage); result.heading = glm::normalize(m_solved.tangent(Chainage)); + result.arriving = result.heading; + // at the tip of a secant blade the element before heads another way + auto const &before{m_solved.elements[m_solved.element_at(std::max(0.0, Chainage - 1e-9))]}; + if (auto const arriving{glm::normalize(before.tangent(std::clamp(Chainage - before.chainage, 0.0, before.length)))}; std::abs(geometry::signed_angle(arriving, result.heading)) > 1e-9) + result.arriving = arriving; result.grade = m_solved.grade(Chainage); result.cant = m_solved.cant_at(Chainage); return result; @@ -287,7 +302,7 @@ class cutter path.radius = radius(From.chainage, To.chainage); auto const p0{geometry::plan_of(From.point)}; auto const p3{geometry::plan_of(To.point)}; - auto const turn{geometry::signed_angle(From.heading, To.heading)}; + auto const turn{geometry::signed_angle(From.heading, To.arriving)}; auto const *single{only_element(From.chainage, To.chainage)}; // a straight of an even grade is drawn by the simulator as a straight of no control vectors if (single != nullptr && single->kind == element_kind::straight && level_grade(From.chainage, To.chainage)) @@ -298,9 +313,9 @@ class cutter auto const handle{chord / (3.0 * cosine * cosine)}; auto handles{std::make_pair(handle, handle)}; if (single == nullptr || single->kind != element_kind::arc) - handles = curvature_handles(p0, From.heading, p3, To.heading, curvature_inside(From.chainage, true), curvature_inside(To.chainage, false), handle); + handles = curvature_handles(p0, From.heading, p3, To.arriving, curvature_inside(From.chainage, true), curvature_inside(To.chainage, false), handle); glm::dvec2 p1{p0 + From.heading * handles.first}; - glm::dvec2 p2{p3 - To.heading * handles.second}; + glm::dvec2 p2{p3 - To.arriving * handles.second}; if (single != nullptr) parabola_controls(*single, From.chainage, To.chainage, p1, p2, p0, p3); auto const y1{From.point.y + From.grade * glm::distance(p0, p1)}; @@ -511,6 +526,87 @@ generated_line generate(network const &Network, line_id const Line, generator_op return result; } +namespace +{ + +// the pieces of a track of the turnout between two chainages of its line, run from the node given: the pieces of a line running the other +// way turned round. A joint inside the turnout is one chainage, the same number for the parts on both sides of it +std::vector run_of(network const &Network, line const &Track, node_id const From, double const A, double const B, generator_options const &Options, std::vector &Errors) +{ + cutter cut{Network, Track, Options}; + auto const forward{Track.start == From}; + auto pieces{cut.pieces(std::min(A, B), std::max(A, B), Errors)}; + if (false == forward) + { + std::ranges::reverse(pieces); + for (auto &piece : pieces) + { + piece.path = geometry::reversed(piece.path); + std::swap(piece.from, piece.to); + } + } + return pieces; +} + +// the chainages of a track of the turnout at distances from the node it's run from, its ends exactly where it ends +struct along_track +{ + double length{0.0}; + bool forward{true}; + along_track(line const &Track, node_id const From) : length{Track.solved.length}, forward{Track.start == From} {} + double at(double const Distance) const { return forward ? Distance : length - Distance; } + double from_end() const { return forward ? 0.0 : length; } + double to_end() const { return forward ? length : 0.0; } +}; + +// a switch track of two paths, each the pieces given +generated_part switch_part(std::string const &Name, std::array, 2> Pieces, double const Length, double const Velocity, turnout_type const &Type, generator_options const &Options) +{ + generated_part result; + result.name = Name; + result.is_switch = true; + result.pieces = std::move(Pieces); + for (std::size_t i = 0; i < 2; ++i) + result.paths[i] = whole_path(result.pieces[i]); + auto text{std::format("node -1 0 {} track switch {:.3f} {} {} {} velocity {} frogs ", Name, Length, Options.surface, path_text(result.paths[0]), path_text(result.paths[1]), number_text(Velocity))}; + if (Type.movable) + text += "movablenose "; + if (Type.heel > 0.0 || Type.bladethrow > 0.0) + text += std::format("blades {} {} ", number_text(Type.heel), number_text(Type.bladethrow)); + for (std::size_t i = 0; i < 2; ++i) + { + if (result.pieces[i].size() < 2) + continue; + text += std::format("pieces{} {} ", i + 1, result.pieces[i].size()); + for (auto const &piece : result.pieces[i]) + text += path_text(piece.path) + " "; + } + result.text = text + "endtrack\n"; + return result; +} + +// a plain track of the turnout: each piece a track of its own, the first one of the name given, the next ones numbered on +generated_part plain_part(std::string const &Name, std::vector Pieces, double const Velocity, generator_options const &Options) +{ + generated_part result; + result.name = Name; + for (std::size_t i = 0; i < Pieces.size(); ++i) + { + auto &piece{Pieces[i]}; + piece.name = i == 0 ? Name : std::format("{}_{}", Name, i + 1); + auto const &p{piece.path.points}; + piece.text = std::format("node -1 0 {} track normal {:.3f} {} {} {} {} {} {} {} {} velocity {} endtrack\n", piece.name, std::abs(piece.to - piece.from), Options.surface, point_text(p[segment_data::point::start]), + number_text(piece.path.rolls[0]), point_text(p[segment_data::point::control1]), point_text(p[segment_data::point::control2]), point_text(p[segment_data::point::end]), + number_text(piece.path.rolls[1]), number_text(piece.path.radius), number_text(Velocity)); + result.text += piece.text; + } + result.pieces[0] = std::move(Pieces); + result.paths[0] = whole_path(result.pieces[0]); + return result; +} + +} // namespace + generated_turnout generate(network const &Network, turnout_id const Turnout, generator_options const &Options) { generated_turnout result; @@ -529,45 +625,91 @@ generated_turnout generate(network const &Network, turnout_id const Turnout, gen } result.name = std::format("{}{}", Options.turnout_prefix, number(Turnout)); result.velocity = switched->velocity; - // the main track and the diverging one, each from the start of the turnout: the pieces of a line run the other way turned round - std::array const tracks{switched->main, switched->diverging}; - for (std::size_t i = 0; i < 2; ++i) - { - auto const *track{Network.find(tracks[i])}; - if (track == nullptr || false == track->solved.valid()) + for (auto const id : tracks_of(*switched)) + if (auto const *track{Network.find(id)}; track == nullptr || false == track->solved.valid()) { - result.errors.push_back(std::format("turnout {}: its {} track isn't laid", number(Turnout), i == 0 ? "main" : "diverging")); + result.errors.push_back(std::format("turnout {}: its track {} isn't laid", number(Turnout), number(id))); return result; } - cutter cut{Network, *track, Options}; - auto pieces{cut.pieces(result.errors)}; - if (track->start != switched->start) - { - std::ranges::reverse(pieces); - for (auto &piece : pieces) - { - piece.path = geometry::reversed(piece.path); - std::swap(piece.from, piece.to); - } - } - result.pieces[i] = std::move(pieces); - result.paths[i] = whole_path(result.pieces[i]); - } - auto text{std::format("node -1 0 {} track switch {:.3f} {} {} {} velocity {} frogs ", result.name, type->length, Options.surface, path_text(result.paths[0]), path_text(result.paths[1]), - number_text(result.velocity))}; - if (type->movable) - text += "movablenose "; - if (type->heel > 0.0 || type->bladethrow > 0.0) - text += std::format("blades {} {} ", number_text(type->heel), number_text(type->bladethrow)); - for (std::size_t i = 0; i < 2; ++i) + auto const &main{*Network.find(switched->main)}; + auto const whole = [&](line const &Track, node_id const From) { return run_of(Network, Track, From, 0.0, Track.solved.length, Options, result.errors); }; + auto const run = [&](line const &Track, node_id const From, double const A, double const B) { return run_of(Network, Track, From, A, B, Options, result.errors); }; + switch (type->kind) { - if (result.pieces[i].size() < 2) - continue; - text += std::format("pieces{} {} ", i + 1, result.pieces[i].size()); - for (auto const &piece : result.pieces[i]) - text += path_text(piece.path) + " "; + case turnout_kind::twofold: + { + // the first switch up to a short plain track before the second one, which the simulation joins to a switch only through a plain track + auto const &diverging{*Network.find(switched->diverging)}; + auto const &main2{*Network.find(switched->main2)}; + auto const &diverging2{*Network.find(switched->diverging2)}; + along_track const along{main, switched->start}; + auto const stagger{main.solved.length}; + auto const joint{along.at(stagger - std::min(1.0, stagger * 0.5))}; + result.parts.push_back(switch_part(result.name, {run(main, switched->start, along.from_end(), joint), whole(diverging, switched->start)}, stagger, result.velocity, *type, Options)); + result.parts.push_back(plain_part(result.name + "_l", run(main, switched->start, joint, along.to_end()), switched->speed_main, Options)); + result.parts.push_back(switch_part(result.name + "_2", {whole(main2, switched->second), whole(diverging2, switched->second)}, main2.solved.length, result.velocity, *type, Options)); + break; } - result.text = text + "endtrack\n"; + case turnout_kind::slip: + { + // a switch at each leg a slip curve leaves, the tracks crossing and the middles of the slip curves plain tracks, their ends further + // from the crossing than the simulation joins ends (build_double_slip of the switch tool) + auto const &crossing{*Network.find(switched->crossing)}; + auto const &slip{*Network.find(switched->diverging)}; + auto const half{type->half}; + auto const gap{std::max(0.25, 0.06 / std::sin(type->angle * 0.5))}; + auto const slow{switched->speed_diverging}; + along_track const along{main, switched->start}; + auto const main_near{along.at(half - gap)}; + auto const main_far{along.at(half + gap)}; + auto const cross_near{half - gap}; + auto const cross_far{half + gap}; + auto const slip_length{slip.solved.length}; + auto const slip_near{slip_length * 0.5 - gap}; + auto const slip_far{slip_length * 0.5 + gap}; + result.parts.push_back(switch_part(result.name + "_a", {run(main, switched->start, along.from_end(), main_near), run(slip, switched->start, 0.0, slip_near)}, half, result.velocity, *type, Options)); + if (type->slip_curves >= 2) + { + // from B back along the main track and the second slip curve; from C along the crossing track and the second slip curve + auto const &slip2{*Network.find(switched->diverging2)}; + auto const slip2_length{slip2.solved.length}; + auto const slip2_near{slip2_length * 0.5 - gap}; + auto const slip2_far{slip2_length * 0.5 + gap}; + result.parts.push_back(switch_part(result.name + "_b", {run(main, switched->main_end, along.to_end(), main_far), run(slip2, switched->main_end, slip2_length, slip2_far)}, half, result.velocity, *type, Options)); + result.parts.push_back(switch_part(result.name + "_c", {run(crossing, switched->cross_start, 0.0, cross_near), run(slip2, switched->cross_start, 0.0, slip2_near)}, half, result.velocity, *type, Options)); + result.parts.push_back(plain_part(result.name + "_s2", run(slip2, switched->cross_start, slip2_near, slip2_far), slow, Options)); + } + else + { + result.parts.push_back(plain_part(result.name + "_b", run(main, switched->start, main_far, along.to_end()), switched->speed_main, Options)); + result.parts.push_back(plain_part(result.name + "_c", run(crossing, switched->cross_start, 0.0, cross_near), switched->speed_main, Options)); + } + result.parts.push_back(switch_part(result.name + "_d", {run(crossing, switched->cross_end, crossing.solved.length, cross_far), run(slip, switched->cross_end, slip_length, slip_far)}, half, result.velocity, *type, Options)); + result.parts.push_back(plain_part(result.name + "_m", run(main, switched->start, main_near, main_far), switched->speed_main, Options)); + result.parts.push_back(plain_part(result.name + "_x", run(crossing, switched->cross_start, cross_near, cross_far), switched->speed_main, Options)); + result.parts.push_back(plain_part(result.name + "_s1", run(slip, switched->start, slip_near, slip_far), slow, Options)); + break; + } + case turnout_kind::crossing: + // two plain tracks crossing + result.parts.push_back(plain_part(result.name + "_1", whole(main, switched->start), switched->speed_main, Options)); + result.parts.push_back(plain_part(result.name + "_2", whole(*Network.find(switched->crossing), switched->cross_start), switched->speed_main, Options)); + break; + case turnout_kind::ordinary: + case turnout_kind::symmetric: + default: + // the main track and the diverging one, each from the start of the turnout + result.parts.push_back(switch_part(result.name, {whole(main, switched->start), whole(*Network.find(switched->diverging), switched->start)}, type->length, result.velocity, *type, Options)); + break; + } + // the first switch track as the fields of a turnout of one have it + if (result.parts.front().is_switch) + { + result.pieces = result.parts.front().pieces; + result.paths = result.parts.front().paths; + } + for (auto const &part : result.parts) + result.text += part.text; return result; } diff --git a/editor/layout/layoutGenerator.hpp b/editor/layout/layoutGenerator.hpp index 57710db94..bafc99d93 100644 --- a/editor/layout/layoutGenerator.hpp +++ b/editor/layout/layoutGenerator.hpp @@ -24,7 +24,9 @@ http://mozilla.org/MPL/2.0/. // the element. A piece ends at a node with the numbers of the node, the same on both sides of it; the names of the tracks come from the // identifiers of the line and of its vertices, so they don't change with a change of the shape which keeps its elements. A turnout is one // switch track of two paths, its main and its diverging track, each made of the pieces of its line (pieces1, pieces2), the ends of its legs -// the numbers of its nodes, its velocity as the speed rule gives it, its frog and its blades as its type has them. +// the numbers of its nodes, its velocity as the speed rule gives it, its frog and its blades as its type has them; a double switch, a slip +// and a crossing the switch tracks and the plain tracks the switch tool of the editor makes of them, their joints inside the turnout the +// numbers of the points of its lines there. namespace layout { @@ -70,17 +72,33 @@ struct generated_line // the pieces of a line of its own; a track of a turnout is made with its turnout generated_line generate(network const &Network, line_id const Line, generator_options const &Options = {}); +// a track of the simulator a turnout is made of: a switch track of two paths, or a plain track (each of its pieces an entry) +struct generated_part +{ + std::string name; + bool is_switch{false}; + // of a switch the main and the diverging path from its start, each the pieces of its line, as the paths of the switch have them: the + // path the pieces make, whole, with them (none where one piece makes it); of a plain track its pieces + std::array, 2> pieces; + std::array paths; + std::string text; // the entries +}; + struct generated_turnout { turnout_id turnout{turnout_id::none}; std::string name; // the main and the diverging track from the start of the turnout, each the pieces of its line, as the paths of the switch have them: - // the path the pieces make, whole, with them (none where one piece makes it) + // the path the pieces make, whole, with them (none where one piece makes it); of its first switch track, where it has more std::array, 2> pieces; std::array paths; double velocity{0.0}; std::vector errors; - std::string text; // the entry + // the tracks of the simulator it's made of: an ordinary or a symmetric switch one switch track; a double switch two and a short plain + // track between them (, _l, _2); a slip a switch at each leg a slip curve leaves (_a ... _d, plain tracks at the + // others), the tracks crossing and the middles of the slip curves plain ones (_m, _x, _s1, _s2); a crossing two plain tracks (_1, _2) + std::vector parts; + std::string text; // the entries, of all the parts }; generated_turnout generate(network const &Network, turnout_id const Turnout, generator_options const &Options = {}); diff --git a/editor/layout/layoutNetwork.cpp b/editor/layout/layoutNetwork.cpp index 842b42d73..af4b9f22c 100644 --- a/editor/layout/layoutNetwork.cpp +++ b/editor/layout/layoutNetwork.cpp @@ -54,8 +54,9 @@ seen_end seen(line const &Line, end_ref const &End) } // the node takes what the ends it holds tell: the heading of the only one, the curvature and the cant of the first one which doesn't go -// on from it (one going on reads them), the grade of the first; the end of the diverging track of a turnout first, which the turnout lays, -// the start of one not at all, which leaves the node at the curvature of the main track. True where the curvature or the cant changed +// on from it (one going on reads them), the grade of the first; a track a turnout lays first at a node of the turnout's own (the end of a +// diverging track, the ends of a crossing track), which the turnout puts there, a branch of a turnout not at all (the start of a diverging +// track, which leaves the node at the curvature of the main track). True where the curvature or the cant changed bool derive(network const &Network, node &Node) { auto const before{std::make_pair(Node.curvature, Node.cant)}; @@ -63,9 +64,9 @@ bool derive(network const &Network, node &Node) bool curved{false}; std::vector ends; for (auto const &end : Node.ends) - if (auto const *owner{owner_of(Network, end.line)}; owner != nullptr && owner->diverging == end.line && end.end == line_end::end) + if (auto const *owner{owner_of(Network, end.line)}; owner != nullptr && owner->start != Node.id && laid_by_turnout(Network, end.line) && false == branch_end(Network, end)) ends.insert(ends.begin(), end); - else if (false == diverging_start(Network, end)) + else if (false == branch_end(Network, end)) ends.push_back(end); for (auto const &end : ends) { @@ -90,6 +91,56 @@ bool derive(network const &Network, node &Node) return before != std::make_pair(Node.curvature, Node.cant); } +// the turnout derives what it takes from the line under it again and lays its tracks, the nodes of its own where they end now; the lines +// going on from those nodes laid again +void settle_turnout(transaction &Change, turnout_id const Id, std::set &Nodes, std::set &Next) +{ + auto const *existing{Change.view().find(Id)}; + if (existing == nullptr) + return; + auto updated{*existing}; + derive_turnout(Change.view(), updated); + if (updated != *existing) + Change.change(Id) = updated; + auto const type{turnout_type_of(updated.type)}; + if (false == type.has_value()) + return; + auto const laid{lay_tracks(Change.view(), updated, *type)}; + for (auto const &track : laid.tracks) + { + auto const *line{Change.view().find(track.line)}; + if (line == nullptr) + continue; + if (track.solved != line->solved) + Change.change(track.line).solved = track.solved; + if (false == track.solved.valid()) + continue; + for (auto const at_end : {false, true}) + { + if (false == (at_end ? track.sets_end : track.sets_start)) + continue; + auto const id{at_end ? track.end : track.start}; + auto const &values{at_end ? track.solved.end : track.solved.start}; + auto const *at{Change.view().find(id)}; + if (at == nullptr) + continue; + auto moved{*at}; + moved.position = values.position; + moved.heading = values.heading; + moved.curvature = values.curvature; + moved.cant = values.cant; + moved.grade = values.grade; + if (moved == *at) + continue; + Change.change(id) = moved; + Nodes.insert(id); + for (auto const &end : moved.ends) + if (end.line != track.line) + Next.insert(end.line); + } + } +} + norms::element reversed(norms::element Element, double const Length) { auto const start{Element.start}; @@ -342,10 +393,13 @@ turnout const *owner_of(network const &Network, line_id const Line) return owner != nullptr && owner->turnout != turnout_id::none ? Network.find(owner->turnout) : nullptr; } -bool diverging_start(network const &Network, end_ref const &End) +bool branch_end(network const &Network, end_ref const &End) { auto const *owner{owner_of(Network, End.line)}; - return owner != nullptr && owner->diverging == End.line && End.end == line_end::start; + if (owner == nullptr || (End.line != owner->diverging && End.line != owner->diverging2)) + return false; + // a slip curve meets the crossing track or the main one at its end too + return End.end == line_end::start || owner->crossing != line_id::none; } void settle(transaction &Change) @@ -359,17 +413,17 @@ void settle(transaction &Change) if (Change.view().find(id) != nullptr) turnouts.insert(id); // the nodes touched take the values of their ends too, the lines going on from them laid again where those changed; a few rounds - // settle it, each line going on from a curve depending on one node at each end, a diverging track on the start and the main track of - // its turnout + // settle it, each line going on from a curve depending on one node at each end, a track a turnout lays on the start and the main track + // of its turnout std::set nodes{Change.touched_nodes()}; for (int round = 0; round < 8 && (false == pending.empty() || false == nodes.empty() || false == turnouts.empty()); ++round) { std::set next; for (auto const id : pending) { - if (auto const *owner{owner_of(Change.view(), id)}; owner != nullptr && owner->diverging == id) + if (laid_by_turnout(Change.view(), id)) { - turnouts.insert(owner->id); + turnouts.insert(owner_of(Change.view(), id)->id); continue; } auto &laid{Change.change(id)}; @@ -378,44 +432,13 @@ void settle(transaction &Change) nodes.insert(laid.start); nodes.insert(laid.end); } - // the turnouts whose main track or start changed lay their diverging tracks, the node at the end of one where it ends now, and the - // lines going on from there laid again + // the turnouts whose main track or start changed lay their tracks, the nodes of their own where they end now, and the lines going on + // from there laid again for (auto const &[id, each] : Change.view().turnouts()) - if (pending.contains(each.main) || nodes.contains(each.start)) + if (pending.contains(each.main) || pending.contains(each.main2) || nodes.contains(each.start) || nodes.contains(each.second)) turnouts.insert(id); for (auto const id : turnouts) - { - auto const *existing{Change.view().find(id)}; - if (existing == nullptr) - continue; - auto updated{*existing}; - derive_turnout(Change.view(), updated); - if (updated != *existing) - Change.change(id) = updated; - auto const type{turnout_type_of(updated.type)}; - auto const *track{Change.view().find(updated.diverging)}; - if (false == type.has_value() || track == nullptr) - continue; - auto const laid{lay_diverging(frame_of(Change.view(), updated), *type, updated.hand)}; - if (laid != track->solved) - Change.change(updated.diverging).solved = laid; - auto const *at{Change.view().find(updated.diverging_end)}; - if (at == nullptr || false == laid.valid()) - continue; - auto moved{*at}; - moved.position = laid.end.position; - moved.heading = laid.end.heading; - moved.curvature = laid.end.curvature; - moved.cant = laid.end.cant; - moved.grade = laid.end.grade; - if (moved == *at) - continue; - Change.change(updated.diverging_end) = moved; - nodes.insert(updated.diverging_end); - for (auto const &end : moved.ends) - if (end.line != updated.diverging) - next.insert(end.line); - } + settle_turnout(Change, id, nodes, next); turnouts.clear(); for (auto const id : nodes) { @@ -449,14 +472,14 @@ std::vector chain_of(network const &Network, line_id const Line) // diverging track into none. Not into a line of another profile of its own auto const across = [&](node_id const At, line_id const From, line_end const End) -> end_ref const * { auto const *at{Network.find(At)}; - if (at == nullptr || diverging_start(Network, {From, End, 1})) + if (at == nullptr || branch_end(Network, {From, End, 1})) return nullptr; end_ref const *from{nullptr}; std::vector others; for (auto const &end : at->ends) if (end.line == From && end.end == End) from = &end; - else if (false == diverging_start(Network, end)) + else if (false == branch_end(Network, end)) others.push_back(&end); if (from == nullptr || others.size() != 1 || others.front()->side() == from->side()) return nullptr; @@ -499,12 +522,14 @@ described_chain describe_chain(network const &Network, std::vector c auto const &owner{*Network.find(part.line)}; auto own{described(owner.solved, owner.category)}; auto const length{owner.solved.length}; - // a track of a turnout: a connection of tracks, its frog, bent or not, its own speed + // a track of a turnout: a connection of tracks, its frog, bent or not, its own speed; the tracks crossing at a slip or a crossing + // over its crossings (the slip curves its diverging tracks) if (auto const *switched{owner_of(Network, owner.id)}) { - auto const diverging{switched->diverging == owner.id}; + auto const diverging{switched->diverging == owner.id || switched->diverging2 == owner.id}; + auto const crossing{switched->crossing != line_id::none && false == diverging}; auto const features{norms::feature::switch_track | (switched->movable ? norms::feature::movable_frog : 0u) | (switched->bent() ? norms::feature::curved_switch : 0u) | - (diverging ? norms::feature::diverging : 0u)}; + (diverging ? norms::feature::diverging : 0u) | (crossing ? norms::feature::crossing : 0u)}; auto const speed{diverging ? switched->speed_diverging : switched->speed_main}; for (auto &element : own.elements) { @@ -617,7 +642,8 @@ void check_line(network const &Network, line const &Line, verdict &Result) if (Line.turnout != turnout_id::none) { auto const *owner{Network.find(Line.turnout)}; - if (owner == nullptr || (owner->main != Line.id && owner->diverging != Line.id)) + auto const tracks{owner != nullptr ? tracks_of(*owner) : std::vector{}}; + if (std::ranges::find(tracks, Line.id) == tracks.end()) Result.reasons.push_back(std::format("{} is a track of turnout {}, which hasn't it", line_name(Line.id), number(Line.turnout))); } for (auto const end : {line_end::start, line_end::end}) @@ -634,24 +660,32 @@ void check_node(network const &Network, node const &Node, verdict &Result) { if (Node.ends.empty()) Result.reasons.push_back(std::format("{} holds no end of a line", node_name(Node.id))); - // the start of a turnout: its diverging track beside its main track, on its side of the node; one line at most on the other + // a leg of a turnout: a branch of it (the start of a diverging track, the end of a slip curve) beside a track of it which carries the + // line through the node, on its side of the node; one line at most on the other std::vector ends; for (auto const &end : Node.ends) { - if (false == diverging_start(Network, end)) + if (false == branch_end(Network, end)) { ends.push_back(end); continue; } auto const *switched{owner_of(Network, end.line)}; - auto const main{std::ranges::find_if(Node.ends, [&](end_ref const &Each) { return Each.line == switched->main; })}; - if (switched->start != Node.id || main == Node.ends.end()) - Result.reasons.push_back(std::format("{}: the diverging track of turnout {} starts there, its main track doesn't", node_name(Node.id), number(switched->id))); - else if (main->side() != end.side()) + auto const carrying = [&](bool const SameSide) { + return std::ranges::any_of(Node.ends, [&](end_ref const &Each) { + auto const *owner{owner_of(Network, Each.line)}; + return owner != nullptr && owner->id == switched->id && false == branch_end(Network, Each) && (Each.side() == end.side()) == SameSide; + }); + }; + if (carrying(true)) + continue; + if (carrying(false)) Result.reasons.push_back(std::format("{}: the tracks of turnout {} leave it to both sides", node_name(Node.id), number(switched->id))); + else + Result.reasons.push_back(std::format("{}: a diverging track of turnout {} meets it, no other track of it does", node_name(Node.id), number(switched->id))); } if (ends.size() > 2) - Result.reasons.push_back(std::format("{} holds {} ends; more than two meet only at the start of a turnout", node_name(Node.id), Node.ends.size())); + Result.reasons.push_back(std::format("{} holds {} ends; more than two meet only at a leg of a turnout", node_name(Node.id), Node.ends.size())); if (ends.size() == 2 && ends[0].side() == ends[1].side()) Result.reasons.push_back(std::format("{}: both lines lie on the same side of it", node_name(Node.id))); for (auto const &end : Node.ends) @@ -772,11 +806,8 @@ verdict check(network const &Network, std::set const &Lines, std::set lines{Lines}; for (auto const id : turnouts) - { - auto const &switched{*Network.find(id)}; - lines.insert(switched.main); - lines.insert(switched.diverging); - } + for (auto const track : tracks_of(*Network.find(id))) + lines.insert(track); if (false == result.reasons.empty()) return result; for (auto const id : turnouts) diff --git a/editor/layout/layoutNetwork.hpp b/editor/layout/layoutNetwork.hpp index 9bb8d6505..acbb4ce10 100644 --- a/editor/layout/layoutNetwork.hpp +++ b/editor/layout/layoutNetwork.hpp @@ -92,9 +92,20 @@ struct turnout int direction{1}; // 1: its main track runs as the line it was laid into does, from the start of the turnout; -1 against it node_id start{node_id::none}; node_id main_end{node_id::none}; - node_id diverging_end{node_id::none}; + node_id diverging_end{node_id::none}; // none of a crossing line_id main{line_id::none}; - line_id diverging{line_id::none}; + line_id diverging{line_id::none}; // none of a crossing + // a double switch (layoutTurnout.hpp, "twofold"): the node where its second switch starts on the main track, the main track on from + // there to main_end, the diverging track of the second switch and the node at its end + node_id second{node_id::none}; + line_id main2{line_id::none}; + line_id diverging2{line_id::none}; + node_id diverging2_end{node_id::none}; + // a slip or a crossing: the crossing track and the nodes at its ends; the diverging track of a slip runs from the start to cross_end, + // the second one of a double slip (diverging2) from cross_start to main_end + line_id crossing{line_id::none}; + node_id cross_start{node_id::none}; + node_id cross_end{node_id::none}; // derived from the line under it double chainage{0.0}; // m, the kilometrage of its start double main_radius{0.0}; // m, of the main track, signed as table 11.8 takes it: above 0 bent the way the diverging track leaves, 0 straight @@ -218,8 +229,9 @@ void settle(transaction &Change); // the turnout owning the line, none for a line of its own turnout const *owner_of(network const &Network, line_id const Line); -// the end is the start of the diverging track of a turnout, which runs on into no line through its node and tells it nothing -bool diverging_start(network const &Network, end_ref const &End); +// the end of a track of a turnout which runs on into no line through its node and tells it nothing, another track of the turnout carrying +// the line through there: the start of a diverging track, and the end of a slip curve +bool branch_end(network const &Network, end_ref const &End); // a line running on through the nodes of two ends: the lines in their order, each forward or backward struct chain_part diff --git a/editor/layout/layoutTurnout.cpp b/editor/layout/layoutTurnout.cpp index 82eabbd7c..3d87fd64e 100644 --- a/editor/layout/layoutTurnout.cpp +++ b/editor/layout/layoutTurnout.cpp @@ -27,42 +27,95 @@ double constexpr kStraight{1e-6}; // 1/m: a main track turning less is a straigh double constexpr kSample{0.5}; // m between the points of the tracks the rules of switch_curve look at double constexpr kReach{20.0}; // m of the line looked at past the ends of a turnout, for the ends of the cant ramps (6 m off it) double constexpr kLength{1e-6}; // m: the main track is as long as its type within this +double constexpr kJoint{0.05}; // m: the slip curves end at least this far inside the joints of the slip -// the catalogue, made once -std::vector const &catalogue() +turnout_kind kind_of_type(std::string const &Type) { - static std::vector const designs{switch_design::catalogue()}; - return designs; + if (switch_design::symmetric_switch(Type)) + return turnout_kind::symmetric; + if (switch_design::double_switch(Type)) + return turnout_kind::twofold; + if (Type == "Skrz") + return turnout_kind::crossing; + if (switch_design::slip_switch(Type)) + return turnout_kind::slip; + return turnout_kind::ordinary; +} + +// the slip or the crossing of the catalogue: its half length and its angle, the slip curves of the blades of the ordinary switch of the +// same rail, radius and angle, passing the crossing as the switch tool lays them on a standard gauge track +std::optional slip_type_of(switch_design::design const &Design) +{ + turnout_type result; + result.name = Design.name; + result.rail = Design.rail; + result.kind = kind_of_type(Design.type); + result.radius = Design.radius; + result.ratio = Design.ratio; + result.half = Design.a; + result.a = Design.a; + result.b = Design.a; + result.length = 2.0 * Design.a; + result.angle = std::atan(1.0 / Design.ratio); + if (result.kind == turnout_kind::slip) + { + auto const blade{switch_design::secant_blade(Design.rail, Design.radius, Design.ratio)}; + result.curve = switch_design::slip_of(Design.radius, Design.middle, result.angle, kSlipPass, blade.first, blade.second); + result.curve.tip = switch_design::cut_tip(Design.rail, result.curve.radius); + result.slip_curves = Design.type == "Rkp" ? 1 : 2; + // the slip curves of the blades outside of the diamond passing the crossing as near as the rails of the profile of the simulator + // let them may reach past the joints of Id-4: the slip as long as they are, as the switch tool lays it + if (auto const tangent{result.curve.tangent()}; false == (tangent < result.half - kJoint)) + { + result.half = tangent + kJoint; + result.a = result.half; + result.b = result.half; + result.length = 2.0 * result.half; + } + } + return result; } std::optional type_of(switch_design::design const &Design, std::string *Why) { - auto const refuse = [&](std::string const &Reason) -> std::optional { - if (Why != nullptr) - *Why = Reason; - return std::nullopt; - }; - if (Design.type != "Rz") - return refuse(std::format("{} is a {} switch, which the model doesn't lay yet (stage E5)", Design.name, switch_design::symmetric_switch(Design.type) ? "symmetric" : "double")); - if (Design.entry > 0.0) - return refuse(std::format("the blades of {} meet the stock rails at an angle, which the model doesn't lay yet (stage E5)", Design.name)); + auto const kind{kind_of_type(Design.type)}; + if (kind == turnout_kind::slip || kind == turnout_kind::crossing) + return slip_type_of(Design); std::string error; auto const shape{switch_design::build(Design, error)}; if (false == error.empty() || shape.paths.size() < 2) - return refuse(std::format("{}: {}", Design.name, error)); + { + if (Why != nullptr) + *Why = std::format("{}: {}", Design.name, error); + return std::nullopt; + } turnout_type result; result.name = Design.name; result.rail = Design.rail; + result.kind = kind; result.radius = shape.radius; result.ratio = shape.ratio; result.a = shape.a; result.b = shape.b; result.length = shape.length; result.lead = Design.lead; + result.entry = glm::radians(Design.entry); + result.tip = shape.tip; result.parts = Design.parts; result.movable = Design.movable; result.heel = Design.heel; result.bladethrow = Design.bladethrow; + if (kind == turnout_kind::twofold) + { + // the second switch as switch_design::build() lays it: of its own parts, or the first one's; the radius the sharper one's + auto const mirrored{Design.type == "Rps"}; + result.stagger = shape.stagger; + result.lead2 = Design.lead2 >= 0.0 && false == mirrored ? Design.lead2 : Design.lead; + result.entry2 = glm::radians(Design.entry2 >= 0.0 && false == mirrored ? Design.entry2 : Design.entry); + result.parts2 = Design.parts2.empty() || mirrored ? Design.parts : Design.parts2; + result.tip2 = shape.tip2 - shape.stagger; + result.opposite = Design.type != "Rpj"; + } return result; } @@ -105,6 +158,293 @@ std::string turnout_name(turnout const &Turnout) return std::format("turnout {} ({})", number(Turnout.id), Turnout.type); } +// the elements of a track one after another from the origin along the direction, a turn between two where a secant blade meets the stock +// rail; the cant and the grade as given all along it +class track_builder +{ + public: + track_builder(glm::dvec2 const &Origin, glm::dvec2 const &Direction) : m_origin{Origin}, m_direction{Direction} {} + void push(element Element) + { + Element.origin = m_origin; + Element.direction = m_direction; + Element.chainage = m_chainage; + m_origin = Element.position(Element.length); + m_direction = glm::normalize(Element.tangent(Element.length)); + m_chainage += Element.length; + m_result.elements.push_back(Element); + } + void straight(double const Length) + { + if (Length > 1e-9) + push(make_straight({}, {1.0, 0.0}, Length)); + } + void curve(double const Curvature, double const Length) + { + if (Length > 1e-9) + push(std::abs(Curvature) < 1e-12 ? make_straight({}, {1.0, 0.0}, Length) : make_arc({}, {1.0, 0.0}, Curvature, Length)); + } + void turn(double const Angle) { m_direction = geometry::turned(m_direction, Angle); } + double chainage() const { return m_chainage; } + // the track laid from the start, its heights evenly between the ones given + solved_line finish(glm::dvec3 const &Start, glm::dvec2 const &Heading, double const Cant, double const Grade, double const EndHeight) + { + auto result{std::move(m_result)}; + result.length = m_chainage; + result.cant = {{0.0, m_chainage, Cant, Cant, ramp_shape::linear}}; + result.profile = {{0.0, Start.y, 0.0, 0.0}, {m_chainage, EndHeight, 0.0, 0.0}}; + auto const &first{result.elements.front()}; + auto const &last{result.elements.back()}; + result.start = {Start, Heading, first.curvature(0.0), Cant, Grade}; + result.end = {glm::dvec3{m_origin.x, EndHeight, m_origin.y}, m_direction, last.curvature(last.length), Cant, Grade}; + return result; + } + + private: + glm::dvec2 m_origin; + glm::dvec2 m_direction; + double m_chainage{0.0}; + solved_line m_result; +}; + +// the frame at the node the main track goes on from +turnout_frame frame_at(network const &Network, node_id const Node, line_id const Main) +{ + turnout_frame result; + auto const *at{Network.find(Node)}; + auto const *main{Network.find(Main)}; + if (at == nullptr || main == nullptr) + return result; + auto const *held{end_at(Network, *main, Node)}; + if (held == nullptr) + return result; + result.side = held->side(); + result.along = held->end == line_end::start ? 1 : -1; + result.position = at->position; + result.heading = at->heading * static_cast(result.side); + result.cant = at->cant * result.side; + result.grade = at->grade * result.side; + if (laid_by_turnout(Network, Main)) + { + // a symmetric switch at the end of its line: as the node holds it + result.curvature = at->curvature * result.side; + result.valid = true; + return result; + } + if (false == main->solved.valid()) + return result; + auto const &solved{main->solved}; + // the mean of the main track: the curvature of an arc, the one of the middle of a clothoid + auto const turn{geometry::signed_angle(solved.start.heading, solved.end.heading)}; + auto const curvature{result.along * turn / solved.length}; + result.curvature = std::abs(curvature) < kStraight ? 0.0 : curvature; + result.valid = true; + return result; +} + +// the main track of a turnout which isn't bent lies on a straight: every element of it +bool straight_line(line const &Line) +{ + return std::ranges::all_of(Line.solved.elements, [](element const &Each) { return std::abs(Each.curvature_start) < kStraight && std::abs(Each.curvature_end) < kStraight; }); +} + +// the slip curve from the point along the direction, turning to the side by the angle of the slip: along the track it leaves up to the +// curve, the curve, along the track it meets after it; to the point given at its end, which it reaches within the rounding +solved_line slip_curve_track(turnout_type const &Type, glm::dvec3 const &From, glm::dvec2 const &Direction, int const Side, glm::dvec3 const &To, glm::dvec2 const &Arrival, double const Cant) +{ + auto const &curve{Type.curve}; + auto const side{static_cast(Side)}; + auto const straight{Type.half - curve.tangent()}; + track_builder build{geometry::plan_of(From), Direction}; + build.straight(straight + curve.lead); + build.turn(side * curve.entry); + build.curve(side / curve.radius, curve.ends); + build.curve(curve.middle > 0.0 ? side / curve.middle : 0.0, curve.between); + build.curve(side / curve.radius, curve.ends); + build.turn(side * curve.entry); + build.straight(curve.lead + straight); + auto const length{build.chainage()}; + auto result{build.finish(From, Direction, Cant, length > 0.0 ? (To.y - From.y) / length : 0.0, To.y)}; + result.end.position = To; + result.end.heading = Arrival; + return result; +} + +laid_tracks lay_slip(network const &Network, turnout const &Turnout, turnout_type const &Type) +{ + laid_tracks result; + auto const frame{frame_of(Network, Turnout)}; + if (false == frame.valid) + { + result.errors.push_back(std::format("{}: no main track", Type.name)); + return result; + } + // in the plane of the main track: the grade along it, the crossing track rising by the grade of its projection on it + auto const origin{geometry::plan_of(frame.position)}; + auto const along{frame.heading}; + auto const at = [&](glm::dvec2 const &Point) { return glm::dvec3{Point.x, frame.position.y + frame.grade * glm::dot(Point - origin, along), Point.y}; }; + auto const hand{static_cast(Turnout.hand)}; + auto const centre{origin + along * Type.half}; + auto const across{geometry::turned(along, hand * Type.angle)}; + auto const cross_start{at(centre - across * Type.half)}; + auto const cross_end{at(centre + across * Type.half)}; + auto const crossing_grade{frame.grade * std::cos(Type.angle)}; + track_builder crossing{geometry::plan_of(cross_start), across}; + crossing.straight(2.0 * Type.half); + laid_track track; + track.line = Turnout.crossing; + track.start = Turnout.cross_start; + track.end = Turnout.cross_end; + track.sets_start = true; + track.sets_end = true; + track.solved = crossing.finish(cross_start, across, frame.cant, crossing_grade, cross_end.y); + track.solved.end.position = cross_end; + result.tracks.push_back(track); + if (Type.slip_curves >= 1) + { + // from the start to the end of the crossing track, turning to the hand + laid_track slip; + slip.line = Turnout.diverging; + slip.start = Turnout.start; + slip.end = Turnout.cross_end; + slip.solved = slip_curve_track(Type, frame.position, along, Turnout.hand, cross_end, track.solved.end.heading, frame.cant); + result.tracks.push_back(slip); + } + if (Type.slip_curves >= 2) + { + // from the start of the crossing track to the end of the main one, turning back + laid_track slip; + slip.line = Turnout.diverging2; + slip.start = Turnout.cross_start; + slip.end = Turnout.main_end; + auto const *end{Network.find(Turnout.main_end)}; + auto const main_end{end != nullptr ? end->position : at(origin + along * Type.length)}; + slip.solved = slip_curve_track(Type, cross_start, across, -Turnout.hand, main_end, along, frame.cant); + result.tracks.push_back(slip); + } + return result; +} + +// the type of the second switch of a double one, as an ordinary switch is laid +turnout_type second_switch(turnout_type const &Type) +{ + auto result{Type}; + result.kind = turnout_kind::ordinary; + result.lead = Type.lead2; + result.entry = Type.entry2; + result.parts = Type.parts2; + result.tip = Type.tip2; + return result; +} + +double kilometrage_at_start(network const &Network, turnout const &Turnout, line const &Main, turnout_frame const &Frame) +{ + if (false == laid_by_turnout(Network, Main.id)) + return Main.chainage_start + (Frame.along > 0 ? 0.0 : Main.solved.length); + // the end of the line it's laid at + if (auto const *at{Network.find(Turnout.start)}) + for (auto const &end : at->ends) + if (auto const *before{Network.find(end.line)}; before != nullptr && before->turnout == turnout_id::none) + return before->chainage_start + (end.end == line_end::end ? before->solved.length : 0.0); + return 0.0; +} + +// the line runs between the nodes as the turnout has it: a part of the line it lies on along the turnout or against it as the turnout runs, +// a track it lays itself from its start +bool runs_between(line const &Line, node_id const From, node_id const To, int const Direction) +{ + return Direction > 0 ? Line.start == From && Line.end == To : Line.start == To && Line.end == From; +} + +// the legs and the tracks of the turnout as its kind has them: each there and the turnout's, between its legs, laid; the main tracks as long +// as the type has them, straight where it isn't bent. False where they aren't whole, for the rules not to be looked at +bool check_layout(network const &Network, turnout const &Turnout, turnout_type const &Type, std::vector &Reasons) +{ + auto const name{turnout_name(Turnout)}; + auto const count{Reasons.size()}; + auto const kind{Type.kind}; + struct between + { + line_id line; + node_id from; + node_id to; + int direction; // 1: from its start to its end, else as the turnout runs along its line + }; + std::vector tracks; + auto const direction{Turnout.direction}; + switch (kind) + { + case turnout_kind::symmetric: + tracks = {{Turnout.main, Turnout.start, Turnout.main_end, 1}, {Turnout.diverging, Turnout.start, Turnout.diverging_end, 1}}; + break; + case turnout_kind::twofold: + tracks = {{Turnout.main, Turnout.start, Turnout.second, direction}, + {Turnout.main2, Turnout.second, Turnout.main_end, direction}, + {Turnout.diverging, Turnout.start, Turnout.diverging_end, 1}, + {Turnout.diverging2, Turnout.second, Turnout.diverging2_end, 1}}; + break; + case turnout_kind::slip: + tracks = {{Turnout.main, Turnout.start, Turnout.main_end, direction}, {Turnout.crossing, Turnout.cross_start, Turnout.cross_end, 1}, {Turnout.diverging, Turnout.start, Turnout.cross_end, 1}}; + if (Type.slip_curves >= 2) + tracks.push_back({Turnout.diverging2, Turnout.cross_start, Turnout.main_end, 1}); + break; + case turnout_kind::crossing: tracks = {{Turnout.main, Turnout.start, Turnout.main_end, direction}, {Turnout.crossing, Turnout.cross_start, Turnout.cross_end, 1}}; break; + case turnout_kind::ordinary: + default: tracks = {{Turnout.main, Turnout.start, Turnout.main_end, direction}, {Turnout.diverging, Turnout.start, Turnout.diverging_end, 1}}; break; + } + for (auto const &track : tracks) + { + for (auto const leg : {track.from, track.to}) + if (Network.find(leg) == nullptr) + Reasons.push_back(std::format("{}: no node {} at a leg", name, number(leg))); + auto const *found{Network.find(track.line)}; + if (found == nullptr) + { + Reasons.push_back(std::format("{}: a track of it is missing", name)); + return false; + } + if (found->turnout != Turnout.id) + Reasons.push_back(std::format("{}: a track of it is another's", name)); + if (false == runs_between(*found, track.from, track.to, track.direction)) + Reasons.push_back(std::format("{}: line {} doesn't run between the legs of its track", name, number(track.line))); + } + // the tracks of no other kind + if (tracks_of(Turnout).size() != tracks.size()) + Reasons.push_back(std::format("{}: tracks of another kind of turnout", name)); + if (Turnout.hand != 1 && Turnout.hand != -1) + Reasons.push_back(std::format("{}: no hand", name)); + if (Reasons.size() > count) + return false; + for (auto const &track : tracks) + if (false == Network.find(track.line)->solved.valid()) + return false; + auto const *main{Network.find(Turnout.main)}; + // the main tracks as long as the type has them + auto const length_of = [&](line_id const Id, double const Length) { + if (auto const *track{Network.find(Id)}; std::abs(track->solved.length - Length) > kLength) + Reasons.push_back(std::format("{}: its main track is {:.6f} m long, the type {:.6f} m", name, track->solved.length, Length)); + }; + if (kind == turnout_kind::twofold) + { + length_of(Turnout.main, Type.stagger); + length_of(Turnout.main2, Type.length - Type.stagger); + } + else if (kind != turnout_kind::symmetric) + length_of(Turnout.main, Type.length); + // both tracks start at the start, on the one side of it + auto const *main_held{end_at(Network, *main, Turnout.start)}; + if (Turnout.diverging != line_id::none) + { + auto const *diverging_held{Network.end_of(Turnout.diverging, line_end::start)}; + if (main_held == nullptr || diverging_held == nullptr || main_held->side() != diverging_held->side()) + Reasons.push_back(std::format("{}: its tracks don't leave its start to one side", name)); + } + // the kinds which aren't bent: their main tracks on straights, a symmetric switch at the end of a straight + if (auto const bent{bent_refusal(Network, Turnout, Type)}; false == bent.empty()) + Reasons.push_back(std::format("{}: {}", name, bent)); + return Reasons.size() == count; +} + } // namespace double turnout_type::diverging_length() const @@ -117,7 +457,7 @@ double turnout_type::diverging_length() const std::optional turnout_type_of(std::string const &Name, std::string *Why) { - for (auto const &design : catalogue()) + for (auto const &design : switch_design::catalogue_entries()) if (design.name == Name) return type_of(design, Why); if (Why != nullptr) @@ -128,35 +468,52 @@ std::optional turnout_type_of(std::string const &Name, std::string std::vector turnout_types() { std::vector result; - for (auto const &design : catalogue()) + for (auto const &design : switch_design::catalogue_entries()) if (type_of(design, nullptr).has_value()) result.push_back(design.name); return result; } +turnout_kind kind_of(turnout const &Turnout) +{ + if (Turnout.crossing != line_id::none) + return Turnout.diverging != line_id::none ? turnout_kind::slip : turnout_kind::crossing; + if (Turnout.second != node_id::none) + return turnout_kind::twofold; + auto const &entries{switch_design::catalogue_entries()}; + auto const found{std::ranges::find_if(entries, [&](switch_design::design const &Each) { return Each.name == Turnout.type; })}; + return found != entries.end() ? kind_of_type(found->type) : turnout_kind::ordinary; +} + +std::string bent_refusal(network const &Network, turnout const &Turnout, turnout_type const &Type) +{ + if (Type.bendable()) + return {}; + auto bent{false}; + if (Type.kind == turnout_kind::symmetric) + bent = std::abs(frame_of(Network, Turnout).curvature) >= kStraight; + else + for (auto const id : {Turnout.main, Turnout.main2}) + if (auto const *part{Network.find(id)}; part != nullptr && false == straight_line(*part)) + bent = true; + if (false == bent) + return {}; + char const *what{"slip nor crossing"}; + if (Type.kind == turnout_kind::twofold) + what = "double switch"; + else if (Type.kind == turnout_kind::symmetric) + what = "symmetric switch"; + return std::format("it lies in a curve, where no {} is laid (ST-T1-A6 11.4 pkt 4; table 11.8 bends the ordinary switches only)", what); +} + turnout_frame frame_of(network const &Network, turnout const &Turnout) { - turnout_frame result; - auto const *at{Network.find(Turnout.start)}; - auto const *main{Network.find(Turnout.main)}; - if (at == nullptr || main == nullptr || false == main->solved.valid()) - return result; - auto const *held{end_at(Network, *main, Turnout.start)}; - if (held == nullptr) - return result; - auto const &solved{main->solved}; - result.side = held->side(); - result.along = held->end == line_end::start ? 1 : -1; - result.position = at->position; - result.heading = at->heading * static_cast(result.side); - // the mean of the main track: the curvature of an arc, the one of the middle of a clothoid - auto const turn{geometry::signed_angle(solved.start.heading, solved.end.heading)}; - auto const curvature{result.along * turn / solved.length}; - result.curvature = std::abs(curvature) < kStraight ? 0.0 : curvature; - result.cant = at->cant * result.side; - result.grade = at->grade * result.side; - result.valid = true; - return result; + return frame_at(Network, Turnout.start, Turnout.main); +} + +turnout_frame second_frame_of(network const &Network, turnout const &Turnout) +{ + return frame_at(Network, Turnout.second, Turnout.main2); } double bending_of(double const Curvature, int const Hand) @@ -172,39 +529,31 @@ solved_line lay_diverging(turnout_frame const &Frame, turnout_type const &Type, auto const curvature{Frame.curvature}; auto const bending{bending_of(curvature, Hand)}; auto const hand{static_cast(Hand)}; - auto origin{geometry::plan_of(Frame.position)}; - auto direction{Frame.heading}; - double chainage{0.0}; - auto const push = [&](element Element) { - Element.origin = origin; - Element.direction = direction; - Element.chainage = chainage; - origin = Element.position(Element.length); - direction = glm::normalize(Element.tangent(Element.length)); - chainage += Element.length; - result.elements.push_back(Element); - }; + track_builder build{geometry::plan_of(Frame.position), Frame.heading}; // along the main track, bent with it: the lead and the straights through the frog - auto const along_main = [&](double const Length) { + auto const along_main = [curvature](double const Length) { auto made{curvature == 0.0 ? make_straight({}, {1.0, 0.0}, Length) : make_arc({}, {1.0, 0.0}, curvature, Length)}; made.role = element_role::straight; return made; }; if (Type.lead > 1e-9) - push(along_main(Type.lead)); + build.push(along_main(Type.lead)); + // secant blades: the diverging track turns off the main one at their tips, the rest of the angle of the frog on its parts + if (Type.entry > 0.0) + build.turn(hand * Type.entry); for (auto const &part : Type.parts) { auto const from{part.radius1 != 0.0 ? hand / part.radius1 : 0.0}; auto const to{part.radius2 != 0.0 ? hand / part.radius2 : 0.0}; if (from == 0.0 && to == 0.0) { - push(along_main(part.length)); + build.push(along_main(part.length)); continue; } if (from != to) { // a transition of the diverging track: the curvatures of the main track added - push(make_spiral(element_kind::clothoid, {}, {1.0, 0.0}, from + curvature, to + curvature, part.length)); + build.push(make_spiral(element_kind::clothoid, {}, {1.0, 0.0}, from + curvature, to + curvature, part.length)); continue; } auto bent{from + curvature}; @@ -227,25 +576,74 @@ solved_line lay_diverging(turnout_frame const &Frame, turnout_type const &Type, } length = turn / relative; } - push(std::abs(bent) < 1e-12 ? make_straight({}, {1.0, 0.0}, length) : make_arc({}, {1.0, 0.0}, bent, length)); + build.push(std::abs(bent) < 1e-12 ? make_straight({}, {1.0, 0.0}, length) : make_arc({}, {1.0, 0.0}, bent, length)); } - if (result.elements.empty() || chainage <= 0.0) + if (build.chainage() <= 0.0) { result.errors.push_back(std::format("{}: no diverging track", Type.name)); return result; } - result.length = chainage; - auto const height{Frame.position.y}; - auto const rise{Frame.grade * chainage}; - result.cant = {{0.0, chainage, Frame.cant, Frame.cant, ramp_shape::linear}}; - result.profile = {{0.0, height, 0.0, 0.0}, {chainage, height + rise, 0.0, 0.0}}; - auto const &first{result.elements.front()}; - auto const &last{result.elements.back()}; - result.start = {Frame.position, Frame.heading, first.curvature(0.0), Frame.cant, Frame.grade}; - result.end = {glm::dvec3{origin.x, height + rise, origin.y}, direction, last.curvature(last.length), Frame.cant, Frame.grade}; + auto const rise{Frame.grade * build.chainage()}; + return build.finish(Frame.position, Frame.heading, Frame.cant, Frame.grade, Frame.position.y + rise); +} + +laid_tracks lay_tracks(network const &Network, turnout const &Turnout, turnout_type const &Type) +{ + laid_tracks result; + auto const add = [&result](line_id const Line, node_id const Start, node_id const End, solved_line Solved) { + laid_track track; + track.line = Line; + track.start = Start; + track.end = End; + track.sets_end = true; + for (auto const &error : Solved.errors) + result.errors.push_back(error); + track.solved = std::move(Solved); + result.tracks.push_back(std::move(track)); + }; + switch (Type.kind) + { + case turnout_kind::slip: + case turnout_kind::crossing: return lay_slip(Network, Turnout, Type); + case turnout_kind::symmetric: + { + // both tracks to both sides of the line it ends, each by half of the angle + auto const frame{frame_of(Network, Turnout)}; + add(Turnout.main, Turnout.start, Turnout.main_end, lay_diverging(frame, Type, -Turnout.hand)); + add(Turnout.diverging, Turnout.start, Turnout.diverging_end, lay_diverging(frame, Type, Turnout.hand)); + return result; + } + case turnout_kind::twofold: + add(Turnout.diverging, Turnout.start, Turnout.diverging_end, lay_diverging(frame_of(Network, Turnout), Type, Turnout.hand)); + add(Turnout.diverging2, Turnout.second, Turnout.diverging2_end, lay_diverging(second_frame_of(Network, Turnout), second_switch(Type), Type.opposite ? -Turnout.hand : Turnout.hand)); + return result; + case turnout_kind::ordinary: + default: + add(Turnout.diverging, Turnout.start, Turnout.diverging_end, lay_diverging(frame_of(Network, Turnout), Type, Turnout.hand)); + return result; + } +} + +std::vector tracks_of(turnout const &Turnout) +{ + std::vector result; + for (auto const id : {Turnout.main, Turnout.main2, Turnout.diverging, Turnout.diverging2, Turnout.crossing}) + if (id != line_id::none) + result.push_back(id); return result; } +bool laid_by_turnout(network const &Network, line_id const Line) +{ + auto const *owner{owner_of(Network, Line)}; + if (owner == nullptr) + return false; + if (Line == owner->diverging || Line == owner->diverging2 || Line == owner->crossing) + return true; + // the main track of a symmetric switch is one of its two curved tracks, laid at the free end of its line + return Line == owner->main && kind_of(*owner) == turnout_kind::symmetric; +} + void derive_turnout(network const &Network, turnout &Turnout) { auto const type{turnout_type_of(Turnout.type)}; @@ -254,17 +652,27 @@ void derive_turnout(network const &Network, turnout &Turnout) if (false == type.has_value() || false == frame.valid || main == nullptr) return; Turnout.movable = type->movable; - Turnout.chainage = main->chainage_start + (frame.along > 0 ? 0.0 : main->solved.length); - Turnout.main_radius = bending_of(frame.curvature, Turnout.hand); + Turnout.chainage = kilometrage_at_start(Network, Turnout, *main, frame); + Turnout.main_radius = type->bendable() ? bending_of(frame.curvature, Turnout.hand) : 0.0; Turnout.cant = frame.cant * Turnout.hand; - // the diverging track by the rule the editor writes + auto const vmax{main->category.vmax}; + if (type->kind == turnout_kind::crossing) + { + // two plain tracks crossing + Turnout.diverging_radius = 0.0; + Turnout.speed_main = vmax; + Turnout.speed_diverging = vmax; + Turnout.velocity = vmax; + return; + } + // the diverging track by the rule the editor writes; of a double switch the sharper of its two, of a slip its slip curves auto const branch{switch_curve::branch_speed(type->radius, type->ratio, Turnout.main_radius, Turnout.cant, type->movable)}; Turnout.speed_diverging = branch.speed; Turnout.diverging_radius = branch.radius; - // the main track at the speed of its line, bent no faster than the deficiency of the frog lets it - auto const vmax{main->category.vmax}; - Turnout.speed_main = vmax; - if (frame.curvature != 0.0) + // the main track at the speed of its line, bent no faster than the deficiency of the frog lets it; a symmetric switch has no straight + // track, both of its tracks at the speed of its curves + Turnout.speed_main = type->kind == turnout_kind::symmetric ? branch.speed : vmax; + if (frame.curvature != 0.0 && type->bendable()) { auto const into{frame.curvature > 0.0 ? frame.cant : -frame.cant}; auto const formula{switch_curve::curve_speed(1.0 / std::abs(frame.curvature), into, branch.deficiency)}; @@ -284,42 +692,19 @@ void check_turnout(network const &Network, turnout const &Turnout, verdict &Resu Result.reasons.push_back(std::format("{}: {}", name, why)); return; } + if (false == check_layout(Network, Turnout, *type, Result.reasons)) + return; + auto const symmetric{type->kind == turnout_kind::symmetric}; auto const *main{Network.find(Turnout.main)}; - auto const *diverging{Network.find(Turnout.diverging)}; - for (auto const leg : {Turnout.start, Turnout.main_end, Turnout.diverging_end}) - if (Network.find(leg) == nullptr) - Result.reasons.push_back(std::format("{}: no node {} at a leg", name, number(leg))); - if (main == nullptr || diverging == nullptr) - { - Result.reasons.push_back(std::format("{}: a track of it is missing", name)); - return; - } - if (main->turnout != Turnout.id || diverging->turnout != Turnout.id) - Result.reasons.push_back(std::format("{}: a track of it is another's", name)); - auto const main_from{Turnout.direction > 0 ? main->start : main->end}; - auto const main_to{Turnout.direction > 0 ? main->end : main->start}; - if (main_from != Turnout.start || main_to != Turnout.main_end) - Result.reasons.push_back(std::format("{}: its main track doesn't run from its start to the end of it", name)); - if (diverging->start != Turnout.start || diverging->end != Turnout.diverging_end) - Result.reasons.push_back(std::format("{}: its diverging track doesn't run from its start to the end of it", name)); - if (Turnout.hand != 1 && Turnout.hand != -1) - Result.reasons.push_back(std::format("{}: no hand", name)); - if (false == Result.reasons.empty() || false == main->solved.valid() || false == diverging->solved.valid()) - return; - if (std::abs(main->solved.length - type->length) > kLength) - Result.reasons.push_back(std::format("{}: its main track is {:.6f} m long, the type {:.6f} m", name, main->solved.length, type->length)); - // both tracks start at the start, on the one side of it + // the track which leaves the main one: the diverging track, the main one itself of a crossing, whose tracks only cross + auto const *diverging{Network.find(Turnout.diverging != line_id::none ? Turnout.diverging : Turnout.main)}; auto const *main_held{end_at(Network, *main, Turnout.start)}; - auto const *diverging_held{Network.end_of(diverging->id, line_end::start)}; - if (main_held == nullptr || diverging_held == nullptr || main_held->side() != diverging_held->side()) - Result.reasons.push_back(std::format("{}: its tracks don't leave its start to one side", name)); - if (false == Result.reasons.empty()) - return; // the tracks from the start, and the line around it: the line it starts from, its main track, the line going on from its end auto const frame{frame_of(Network, Turnout)}; + auto const forward{symmetric || frame.along > 0}; std::vector tracks[2]; - sample(main->solved, frame.along > 0 ? 0.0 : main->solved.length, frame.along > 0 ? main->solved.length : 0.0, tracks[0]); + sample(main->solved, forward ? 0.0 : main->solved.length, forward ? main->solved.length : 0.0, tracks[0]); sample(diverging->solved, 0.0, diverging->solved.length, tracks[1]); std::vector around; auto const *start{Network.find(Turnout.start)}; @@ -339,12 +724,15 @@ void check_turnout(network const &Network, turnout const &Turnout, verdict &Resu break; } auto const from{around.empty() ? 0.0 : around.back().station}; - sample(main->solved, frame.along > 0 ? 0.0 : main->solved.length, frame.along > 0 ? main->solved.length : 0.0, around); + // along its main track, and the one of the second switch of a double switch + for (auto const id : {Turnout.main, Turnout.main2}) + if (auto const *part{Network.find(id)}) + sample(part->solved, forward ? 0.0 : part->solved.length, forward ? part->solved.length : 0.0, around); auto const to{around.back().station}; - if (auto const *at{Network.find(Turnout.main_end)}) + if (auto const *at{Network.find(Turnout.main_end)}; at != nullptr && false == symmetric) for (auto const &end : at->ends) { - if (end.line == main->id) + if (auto const *owner{owner_of(Network, end.line)}; owner != nullptr && owner->id == Turnout.id) continue; if (auto const *after{Network.find(end.line)}; after != nullptr && after->solved.valid()) { @@ -361,7 +749,7 @@ void check_turnout(network const &Network, turnout const &Turnout, verdict &Resu auto const told = [&](std::string const &Text, norms::tier const Level, std::vector &Into) { notice found; found.line = main->id; - found.chainage = frame.along > 0 ? 0.0 : main->solved.length; + found.chainage = forward ? 0.0 : main->solved.length; found.finding.level = Level; found.finding.chainage = found.chainage; found.text = std::format("{}: {}", name, Text); diff --git a/editor/layout/layoutTurnout.hpp b/editor/layout/layoutTurnout.hpp index 2cabfbd03..3537b3d61 100644 --- a/editor/layout/layoutTurnout.hpp +++ b/editor/layout/layoutTurnout.hpp @@ -17,40 +17,86 @@ http://mozilla.org/MPL/2.0/. #include #include -// The turnouts of the track geometry (editor-tracks-domain.md §4 "Rozjazd", §7): the type of the catalogue the editor lays its switches -// from (editorSwitchDesign, Id-4), the diverging track laid off the start of the main track and bent as the line under it is (table 11.8, -// the curve of the basic switch bent to keep the angle of the frog, the rest of it along with the main track), the speeds by the rule the -// editor writes (switch_curve::branch_speed), and the rules of Q2 and §11.4 as invariants: the hard ones refuse, the others warn. +// The turnouts of the track geometry (editor-tracks-domain.md §4 "Rozjazd", §7): the types of the catalogue the editor lays its switches +// from (editor/data/turnouts.txt, Id-4), each track the turnout has a line and each leg a node; the diverging track laid off the start of the +// main track and bent as the line under it is (table 11.8, the curve of the basic switch bent to keep the angle of the frog, the rest of it +// along with the main track), the speeds by the rule the editor writes (switch_curve::branch_speed), and the rules of Q2 and §11.4 as +// invariants: the hard ones refuse, the others warn. +// +// The kinds of the catalogue as the model lays them: +// ordinary Rz: its main track a part of the line it lies on, the diverging track off its start, tangent to it or with secant blades +// meeting the stock rail at an angle at the end of the pre-blade stretch (S42) +// symmetric Rls: both its tracks turning off its start by half of the angle, to both sides; it ends the line it's laid at the free end of +// twofold Rpd, Rpj, Rps: its main track a part of the line cut at the start of its second switch (the node second), the main track of +// which goes on (main2) and whose diverging track (diverging2) leaves there, to the other side or to the same one +// slip Rkp, Rkpd: its main track a part of the line, the crossing track across its middle at the angle of the slip, between two nodes +// of its own (cross_start, cross_end), the slip curves between the two: the diverging track from its start to the end of the +// crossing track, and of a double slip the second one (diverging2) from the start of the crossing track to the end of the main one +// crossing Skrz: its main track a part of the line, the crossing track across it +// Doubles, slips and crossings aren't bent (11.4 pkt 4 a, b), nor symmetric switches (table 11.8 has no rule for them): their main tracks lie +// on straights. namespace layout { -// a type of the catalogue as the model lays it: an ordinary switch (Rz) whose diverging track leaves the main one tangentially +enum class turnout_kind +{ + ordinary, + symmetric, + twofold, + slip, + crossing +}; + +// a type of the catalogue as the model lays it struct turnout_type { std::string name; std::string rail; - double radius{0.0}; // m, of the diverging curve of the basic switch - double ratio{0.0}; // 1:n at the end of the diverging track - double a{0.0}; // m, from the start to the centre of the switch + turnout_kind kind{turnout_kind::ordinary}; + double radius{0.0}; // m, of the diverging curve of the basic switch; of the slip curves of a slip + double ratio{0.0}; // 1:n at the end of the diverging track; of the two tracks of a symmetric switch, of the crossing of a slip + double a{0.0}; // m, from the start to the centre of the switch; a slip or a crossing from its centre to its legs double b{0.0}; // m, from the centre to the end of the main track - double length{0.0}; // m, of the main track + double length{0.0}; // m, of the main track; of a symmetric switch how far its tracks reach along it double lead{0.0}; // m, the straight of the diverging track along the main one before its curve + double entry{0.0}; // rad, the angle the secant blades meet the stock rail at, at the end of the lead; 0 for tangent blades + double tip{0.0}; // m from the start, the tip of the blade std::vector parts; // of the diverging track after the lead, turning to the side it leaves to bool movable{false}; // the frog with a movable nose double heel{0.0}; // where the moving blades end, 0: as the simulator makes them double bladethrow{0.0}; + // a double switch: where its second switch starts along the main track, its diverging track, to the other side or the same one + double stagger{0.0}; + double lead2{0.0}; + double entry2{0.0}; + double tip2{0.0}; + std::vector parts2; + bool opposite{false}; + // a slip or a crossing: half of the length of its tracks, the angle they cross at; the slip curves, one of a single slip, two of a + // double one, none of a crossing + double half{0.0}; + double angle{0.0}; + switch_design::slip_curve curve; + int slip_curves{0}; double diverging_length() const; + bool bendable() const { return kind == turnout_kind::ordinary; } }; -// the type of the catalogue by its name; none, and the reason in Why, where there's no such type or the model doesn't lay it yet: double -// switches, slips and symmetric switches, the blades meeting the stock rails at an angle (stage E5) +// the kind of the turnout, by the tracks it has and its type +turnout_kind kind_of(turnout const &Turnout); + +// the type of the catalogue by its name; none, and the reason in Why, where there's no such type or the model can't lay it std::optional turnout_type_of(std::string const &Name, std::string *Why = nullptr); // the names of the types the model lays, in the order of the catalogue std::vector turnout_types(); +// m, from the crossing to the rails of a slip curve passing it, of a standard gauge track of the rail profile of the simulator: half of +// the gauge, the head and 2 mm, as the switch tool takes it for the slip curves with the blades outside of the diamond +inline constexpr double kSlipPass{0.7175 + 0.0735 + 0.002}; // the turnout on its line, as its start node and its main track tell it: the place, the heading along the turnout, the side of the start -// node it lies on, the curvature of the main track (its mean, 0 on a straight), the cant and the grade, all along the turnout +// node it lies on, the curvature of the main track (its mean, 0 on a straight), the cant and the grade, all along the turnout; of the second +// switch of a double one as its node and the main track going on from there tell it struct turnout_frame { glm::dvec3 position{0.0}; @@ -63,6 +109,11 @@ struct turnout_frame bool valid{false}; }; turnout_frame frame_of(network const &Network, turnout const &Turnout); +turnout_frame second_frame_of(network const &Network, turnout const &Turnout); + +// why the turnout of a kind which isn't bent can't lie where it does: its main track in a curve (ST-T1-A6 11.4 pkt 4 a, b; table 11.8 has +// no rule for a symmetric switch); empty where it can, or it's an ordinary switch, which is bent +std::string bent_refusal(network const &Network, turnout const &Turnout, turnout_type const &Type); // the main track of the bending, signed as table 11.8 takes it: above 0 bent the way the diverging track leaves to, below 0 the other way, // 0 a straight @@ -73,6 +124,30 @@ double bending_of(double const Curvature, int const Hand); // of the main track all along it, the grade of its start. Errors where it can't be laid so solved_line lay_diverging(turnout_frame const &Frame, turnout_type const &Type, int const Hand); +// a track the turnout lays itself, not a part of the line it lies on: where it starts and ends, and the nodes it puts where it starts or +// ends (the nodes of the turnout's own: the ends of the diverging tracks, the ends of the crossing track) +struct laid_track +{ + line_id line{line_id::none}; + node_id start{node_id::none}; + node_id end{node_id::none}; + bool sets_start{false}; + bool sets_end{false}; + solved_line solved; +}; +struct laid_tracks +{ + std::vector tracks; + std::vector errors; +}; +// the tracks of the turnout it lays off its frame, the parts of the line it lies on laid as they are +laid_tracks lay_tracks(network const &Network, turnout const &Turnout, turnout_type const &Type); + +// the lines of the turnout: its main track (and the one of its second switch), its diverging tracks, its crossing track +std::vector tracks_of(turnout const &Turnout); +// the line is a track the turnout lays itself, not a part of the line the turnout lies on +bool laid_by_turnout(network const &Network, line_id const Line); + // what the turnout takes from the line under it: the kilometrage of its start, the bending, the cant, the speeds of both tracks (the // diverging one by switch_curve::branch_speed, the main one by formula 9.9 with the deficiency of the frog where it's bent, no faster than // the line), the velocity written diff --git a/editor/norms/normsTables.cpp b/editor/norms/normsTables.cpp index 362883484..98d77a87e 100644 --- a/editor/norms/normsTables.cpp +++ b/editor/norms/normsTables.cpp @@ -220,7 +220,7 @@ constexpr std::array tsi_rows{ row{.what = deficiency_jump, .block = at(125), .place = "4.2.4.4(1)", .speed_above = 60, .speed_to = 200}, row{.what = deficiency_jump, .block = at(85), .place = "4.2.4.4(1)", .speed_above = 200, .speed_to = 230}, row{.what = deficiency_jump, .block = at(25), .place = "4.2.4.4(1)", .speed_above = 230}, - row{.what = radius, .block = at(150), .place = "4.2.3.4(1)"}, + row{.what = radius, .block = at(150), .place = "4.2.3.4(1)", .excludes = switch_track}, row{.what = vertical_radius, .block = at(500), .place = "4.2.3.5(1)", .needs = crest}, row{.what = vertical_radius, .block = at(900), .place = "4.2.3.5(1)", .needs = sag}, }; diff --git a/editor/project/projectFile.cpp b/editor/project/projectFile.cpp index 3e5a4f68e..fdc6342a1 100644 --- a/editor/project/projectFile.cpp +++ b/editor/project/projectFile.cpp @@ -348,9 +348,18 @@ std::string write(document const &Project) } put("turnouts"); for (auto const &[id, turnout] : network.turnouts()) - put(std::format("turnout {} type {} hand {} direction {} start {} main {} {} diverging {} {} chainage {} radii {} {} cant {} speeds {} {} velocity {} movable {}", layout::number(id), word(turnout.type), turnout.hand, turnout.direction, - layout::number(turnout.start), layout::number(turnout.main), layout::number(turnout.main_end), layout::number(turnout.diverging), layout::number(turnout.diverging_end), number(turnout.chainage), - number(turnout.main_radius), number(turnout.diverging_radius), number(turnout.cant), number(turnout.speed_main), number(turnout.speed_diverging), number(turnout.velocity), turnout.movable ? 1 : 0)); + { + auto text{std::format("turnout {} type {} hand {} direction {} start {} main {} {} diverging {} {} chainage {} radii {} {} cant {} speeds {} {} velocity {} movable {}", layout::number(id), word(turnout.type), turnout.hand, + turnout.direction, layout::number(turnout.start), layout::number(turnout.main), layout::number(turnout.main_end), layout::number(turnout.diverging), layout::number(turnout.diverging_end), + number(turnout.chainage), number(turnout.main_radius), number(turnout.diverging_radius), number(turnout.cant), number(turnout.speed_main), number(turnout.speed_diverging), + number(turnout.velocity), turnout.movable ? 1 : 0)}; + // the tracks and the legs of the double switches, the slips and the crossings, only where the turnout has them + if (turnout.second != node_id::none) + text += std::format(" second {} {} {} {}", layout::number(turnout.second), layout::number(turnout.main2), layout::number(turnout.diverging2), layout::number(turnout.diverging2_end)); + if (turnout.crossing != line_id::none) + text += std::format(" crossing {} {} {} slip {}", layout::number(turnout.crossing), layout::number(turnout.cross_start), layout::number(turnout.cross_end), layout::number(turnout.diverging2)); + put(text); + } put("end"); put("anchors"); for (auto const &line : Project.anchors) @@ -372,6 +381,30 @@ std::string write(document const &Project) return text; } +namespace +{ + +// the tracks of the turnout there and its, its legs there, the tracks it lays itself between its legs +bool whole_turnout(layout::network const &Network, layout::turnout const &Turnout) +{ + auto const held = [&](node_id const Node) { return Node == node_id::none || Network.find(Node) != nullptr; }; + auto const between = [&](line_id const Track, node_id const From, node_id const To) { + auto const *track{Network.find(Track)}; + return Track == line_id::none || (track != nullptr && track->start == From && track->end == To); + }; + auto const tracks{layout::tracks_of(Turnout)}; + if (Network.find(Turnout.main) == nullptr || (Turnout.diverging == line_id::none && Turnout.crossing == line_id::none)) + return false; + if (false == std::ranges::all_of(tracks, [&](line_id const Track) { return Network.find(Track) != nullptr && Network.find(Track)->turnout == Turnout.id; })) + return false; + auto const slip{Turnout.crossing != line_id::none}; + return held(Turnout.start) && held(Turnout.main_end) && held(Turnout.diverging_end) && held(Turnout.second) && held(Turnout.diverging2_end) && held(Turnout.cross_start) && + held(Turnout.cross_end) && between(Turnout.diverging, Turnout.start, slip ? Turnout.cross_end : Turnout.diverging_end) && + between(Turnout.diverging2, slip ? Turnout.cross_start : Turnout.second, Turnout.diverging2_end) && between(Turnout.crossing, Turnout.cross_start, Turnout.cross_end); +} + +} // namespace + read_result read(std::string_view const Text) { read_result result; @@ -481,6 +514,25 @@ read_result read(std::string_view const Text) turnout.velocity = words.real("the velocity"); words.expect("movable"); turnout.movable = words.choice("movable", std::array{"0", "1"}) == 1; + if (words.peek() == "second") + { + words.next("second"); + turnout.second = static_cast(words.id("the node of the second switch")); + turnout.main2 = static_cast(words.id("the main track of the second switch")); + turnout.diverging2 = static_cast(words.id("the diverging track of the second switch")); + turnout.diverging2_end = static_cast(words.id("the node at the end of the diverging track of the second switch")); + } + if (words.peek() == "crossing") + { + words.next("crossing"); + turnout.crossing = static_cast(words.id("the crossing track")); + turnout.cross_start = static_cast(words.id("the node at the start of the crossing track")); + turnout.cross_end = static_cast(words.id("the node at the end of the crossing track")); + words.expect("slip"); + turnout.diverging2 = static_cast(words.id("the second slip curve")); + if (turnout.diverging2 != line_id::none) + turnout.diverging2_end = turnout.main_end; + } words.finish(); if (false == words.failed()) { @@ -796,25 +848,25 @@ read_result read(std::string_view const Text) result.error = std::format("node {}: line {} doesn't end at it", layout::number(id), layout::number(end.line)); return result; } - // the turnouts and their tracks, each of them of the other + // the turnouts and their tracks, each of them of the other; the tracks it lays itself between its legs as written for (auto const &[id, turnout] : network.turnouts()) { - auto const *main{network.find(turnout.main)}; - auto const *diverging{network.find(turnout.diverging)}; - auto const held = [&](node_id const Node) { return network.find(Node) != nullptr; }; - if (main == nullptr || diverging == nullptr || main->turnout != id || diverging->turnout != id || false == held(turnout.start) || false == held(turnout.main_end) || false == held(turnout.diverging_end) || - diverging->start != turnout.start || diverging->end != turnout.diverging_end) + if (false == whole_turnout(network, turnout)) { result.error = std::format("turnout {}: its tracks or its legs aren't the ones written", layout::number(id)); return result; } } for (auto const &[id, laid] : network.lines()) - if (auto const *owner{network.find(laid.turnout)}; laid.turnout != turnout_id::none && (owner == nullptr || (owner->main != id && owner->diverging != id))) + { + auto const *owner{network.find(laid.turnout)}; + auto const tracks{owner != nullptr ? layout::tracks_of(*owner) : std::vector{}}; + if (laid.turnout != turnout_id::none && std::ranges::find(tracks, id) == tracks.end()) { result.error = std::format("line {}: turnout {} hasn't it", layout::number(id), layout::number(laid.turnout)); return result; } + } // the identifiers: none handed out again, whatever the record of them says layout::network::handed_out last; for (auto const &[id, node] : network.nodes()) @@ -836,14 +888,10 @@ read_result read(std::string_view const Text) result.notes.push_back("the next identifiers written are lower than the ones in use, taken past them"); network.hand_out(*next); network.hand_out(last); - // the lines laid between their nodes as read; the diverging tracks then, laid by their turnouts off their main tracks - auto const diverging = [&](layout::line const &Line) { - auto const *owner{network.find(Line.turnout)}; - return owner != nullptr && owner->diverging == Line.id; - }; + // the lines laid between their nodes as read; the tracks the turnouts lay themselves then, off their main tracks for (auto const &[id, laid] : network.lines()) { - if (diverging(laid)) + if (layout::laid_by_turnout(network, id)) continue; auto solved{layout::solve(laid.shape, network.condition(laid, layout::line_end::start), network.condition(laid, layout::line_end::end))}; for (auto const &error : solved.errors) @@ -859,10 +907,11 @@ read_result read(std::string_view const Text) result.notes.push_back(std::format("turnout {}: {}", layout::number(id), why.empty() ? "no type " + turnout.type : why)); continue; } - auto laid{layout::lay_diverging(layout::frame_of(network, turnout), *type, turnout.hand)}; + auto const laid{layout::lay_tracks(network, turnout, *type)}; for (auto const &error : laid.errors) result.notes.push_back(std::format("turnout {}: {}", layout::number(id), error)); - change.change(turnout.diverging).solved = std::move(laid); + for (auto const &track : laid.tracks) + change.change(track.line).solved = track.solved; } result.project = std::move(project); return result; diff --git a/editor/project/projectFile.hpp b/editor/project/projectFile.hpp index 6ab5c8ddf..db7a6dd4f 100644 --- a/editor/project/projectFile.hpp +++ b/editor/project/projectFile.hpp @@ -39,7 +39,10 @@ http://mozilla.org/MPL/2.0/. // turnouts // turnout type hand <1|-1> direction <1|-1> start main diverging chainage // radii
cant speeds
velocity movable <0|1> -// end the turnouts (E2b), their tracks lines of them; the diverging ones laid by the turnouts +// [second
] a double switch: its second switch (E5) +// [crossing slip ] a slip or a crossing: the crossing track, the second slip +// curve; the diverging track 0 0 of a crossing (E5) +// end the turnouts (E2b), their tracks lines of them; the ones they lay laid by the turnouts // anchors ... end kept for the anchors of the lineside objects (E6), as read; empty in version 1 // generated // track line file sum <16 hex digits> a track written by the generator, the checksum of its text diff --git a/editor/project/projectGenerated.cpp b/editor/project/projectGenerated.cpp index f3b3bc00d..d209fd1b4 100644 --- a/editor/project/projectGenerated.cpp +++ b/editor/project/projectGenerated.cpp @@ -51,14 +51,30 @@ void record_generated(document &Project, layout::generated_turnout const &Turnou if (turnout == nullptr) return; auto const main{turnout->main}; - std::erase_if(Project.generated, [&](generated_node const &Node) { return Node.kind == "switch" && Node.line == main; }); - generated_node node; - node.kind = "switch"; - node.name = Turnout.name; - node.line = main; - node.file = File; - node.sum = checksum(Turnout.text); - Project.generated.push_back(std::move(node)); + std::erase_if(Project.generated, [&](generated_node const &Node) { return Node.line == main; }); + // each track of the simulator it's made of, by its main track: the switch tracks, and the plain tracks of a double switch, a slip or a + // crossing piece by piece + auto const keep = [&](std::string const &Kind, std::string const &Name, std::string const &Text) { + generated_node node; + node.kind = Kind; + node.name = Name; + node.line = main; + node.file = File; + node.sum = checksum(Text); + Project.generated.push_back(std::move(node)); + }; + if (Turnout.parts.empty()) + keep("switch", Turnout.name, Turnout.text); + for (auto const &part : Turnout.parts) + { + if (part.is_switch) + { + keep("switch", part.name, part.text); + continue; + } + for (auto const &piece : part.pieces[0]) + keep("track", piece.name, piece.text); + } Project.hand_changed.erase(main); } diff --git a/tests/unit/layout_fixture.h b/tests/unit/layout_fixture.h index 6af81e4ad..b91c0079b 100644 --- a/tests/unit/layout_fixture.h +++ b/tests/unit/layout_fixture.h @@ -90,7 +90,9 @@ inline void check_structure(layout::network const &Network) { auto const &a{solved.elements[i - 1]}; auto const &b{solved.elements[i]}; CHECK(glm::distance(a.position(a.length), b.origin) < 1e-6); - CHECK(std::abs(geometry::signed_angle(a.tangent(a.length), b.direction)) < 1e-9); + // a track a turnout lays may turn where its secant blades meet the stock rails, by less than 2 degrees + auto const kink{std::abs(geometry::signed_angle(a.tangent(a.length), b.direction))}; + CHECK((kink < 1e-9 || (layout::laid_by_turnout(Network, id) && kink < 0.035))); CHECK(a.end_chainage() == doctest::Approx(b.chainage).epsilon(1e-12)); } for (std::size_t i = 1; i < solved.cant.size(); ++i) { @@ -104,13 +106,14 @@ inline void check_structure(layout::network const &Network) { REQUIRE_FALSE(node.ends.empty()); std::vector through; for (auto const &end : node.ends) { - if (layout::diverging_start(Network, end)) { + if (layout::branch_end(Network, end)) { + // a branch of a turnout beside a track of it carrying the line through the node, on its side auto const *switched{layout::owner_of(Network, end.line)}; REQUIRE(switched != nullptr); - CHECK(switched->start == id); - auto const main{std::ranges::find_if(node.ends, [&](layout::end_ref const &Each) { return Each.line == switched->main; })}; - REQUIRE(main != node.ends.end()); - CHECK(main->side() == end.side()); + CHECK(std::ranges::any_of(node.ends, [&](layout::end_ref const &Each) { + auto const *owner{layout::owner_of(Network, Each.line)}; + return owner != nullptr && owner->id == switched->id && false == layout::branch_end(Network, Each) && Each.side() == end.side(); + })); } else { through.push_back(end); @@ -134,24 +137,65 @@ inline void check_structure(layout::network const &Network) { } for (auto const &[id, turnout] : Network.turnouts()) { CAPTURE(layout::number(id)); + CAPTURE(turnout.type); auto const type{layout::turnout_type_of(turnout.type)}; REQUIRE(type.has_value()); + using layout::turnout_kind; + auto const kind{type->kind}; + CHECK(layout::kind_of(turnout) == kind); + for (auto const track : layout::tracks_of(turnout)) { + auto const *line{Network.find(track)}; + REQUIRE(line != nullptr); + CHECK(line->turnout == id); + } auto const *main{Network.find(turnout.main)}; - auto const *diverging{Network.find(turnout.diverging)}; - REQUIRE(main != nullptr); - REQUIRE(diverging != nullptr); - CHECK(main->turnout == id); - CHECK(diverging->turnout == id); - CHECK((turnout.direction > 0 ? main->start : main->end) == turnout.start); - CHECK((turnout.direction > 0 ? main->end : main->start) == turnout.main_end); - CHECK(diverging->start == turnout.start); - CHECK(diverging->end == turnout.diverging_end); - CHECK(main->solved.length == doctest::Approx(type->length).epsilon(1e-9)); - CHECK(diverging->solved.start.position == Network.find(turnout.start)->position); - CHECK(diverging->solved.end.position == Network.find(turnout.diverging_end)->position); + auto const between = [&](layout::line_id const Track, layout::node_id const From, layout::node_id const To, int const Direction) { + auto const *line{Network.find(Track)}; + REQUIRE(line != nullptr); + CHECK((Direction > 0 ? line->start : line->end) == From); + CHECK((Direction > 0 ? line->end : line->start) == To); + CHECK(line->solved.start.position == Network.find(line->start)->position); + CHECK(line->solved.end.position == Network.find(line->end)->position); + }; + switch (kind) { + case turnout_kind::twofold: + between(turnout.main, turnout.start, turnout.second, turnout.direction); + between(turnout.main2, turnout.second, turnout.main_end, turnout.direction); + between(turnout.diverging, turnout.start, turnout.diverging_end, 1); + between(turnout.diverging2, turnout.second, turnout.diverging2_end, 1); + CHECK(main->solved.length == doctest::Approx(type->stagger).epsilon(1e-9)); + CHECK(Network.find(turnout.main2)->solved.length == doctest::Approx(type->length - type->stagger).epsilon(1e-9)); + break; + case turnout_kind::symmetric: + between(turnout.main, turnout.start, turnout.main_end, 1); + between(turnout.diverging, turnout.start, turnout.diverging_end, 1); + break; + case turnout_kind::slip: + case turnout_kind::crossing: + between(turnout.main, turnout.start, turnout.main_end, turnout.direction); + between(turnout.crossing, turnout.cross_start, turnout.cross_end, 1); + if (kind == turnout_kind::slip) { + between(turnout.diverging, turnout.start, turnout.cross_end, 1); + } + if (turnout.diverging2 != layout::line_id::none) { + between(turnout.diverging2, turnout.cross_start, turnout.main_end, 1); + } + CHECK(main->solved.length == doctest::Approx(type->length).epsilon(1e-9)); + break; + default: + between(turnout.main, turnout.start, turnout.main_end, turnout.direction); + between(turnout.diverging, turnout.start, turnout.diverging_end, 1); + CHECK(main->solved.length == doctest::Approx(type->length).epsilon(1e-9)); + break; + } CHECK(turnout.speed_diverging > 0.0); CHECK(turnout.speed_main > 0.0); - CHECK(turnout.velocity == switch_curve::written_velocity(turnout.speed_main, turnout.speed_diverging)); + if (kind == turnout_kind::crossing) { + CHECK(turnout.velocity == turnout.speed_main); + } + else { + CHECK(turnout.velocity == switch_curve::written_velocity(turnout.speed_main, turnout.speed_diverging)); + } } } diff --git a/tests/unit/test_layout_turnout_kinds.cpp b/tests/unit/test_layout_turnout_kinds.cpp new file mode 100644 index 000000000..edbe85d44 --- /dev/null +++ b/tests/unit/test_layout_turnout_kinds.cpp @@ -0,0 +1,563 @@ +// the kinds of turnouts of the catalogue in the domain model of the track geometry (editor/layout, editor-tracks-domain.md §4 "Rozjazd", +// §7, stage E5): the double switches with their second switch on the main track, the symmetric ones at the free end of a line, the S42 +// switches with secant blades, the slips and the diamond crossings with their crossing track and slip curves; every type laid on a straight +// both ways to both sides, its legs where the catalogue puts them, its tracks and legs nodes; the kinds which aren't bent refused in a curve; +// random edits with every type along lines with arcs and transitions, the seed in the name of the case; every type made into the tracks of +// the simulator and read back by TTrack::Load, the legs at the nodes to the bit; the project file keeping them + +#include "stdafx.h" +#include "doctest.h" +#include "layout_fixture.h" +#include "track_text.h" +#include "editor/layout/layoutGenerator.hpp" +#include "editor/project/projectFile.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace test::layout_fixture; +using layout::line_id; +using layout::node_id; +using layout::turnout_id; +using layout::turnout_kind; + +// a straight line along x on a grade of 4 per mille +layout::create_line straight(glm::dvec3 const &Start = {0.0, 100.0, 0.0}, double const Length = 1000.0) { + layout::create_line result; + result.start = Start; + result.end = Start + glm::dvec3{Length, Length * 0.004, 0.0}; + result.category = line_class(); + return result; +} + +// the chainage a turnout of the type starts at on the line of the length: in its middle, or at its end for a symmetric switch +double start_for(layout::turnout_type const &Type, double const Length, int const Direction) { + if (Type.kind == turnout_kind::symmetric) { + return Direction > 0 ? Length : 0.0; + } + return Direction > 0 ? Length * 0.4 : Length * 0.6; +} + +glm::dvec2 plan(layout::network const &Network, node_id const Node) { + auto const *at{Network.find(Node)}; + REQUIRE(at != nullptr); + return geometry::plan_of(at->position); +} + +// the turn of the parts of a diverging track and the angle of its blade +double turn_of(std::vector const &Parts, double const Entry) { + auto result{Entry}; + for (auto const &part : Parts) { + auto const k1{part.radius1 != 0.0 ? 1.0 / part.radius1 : 0.0}; + auto const k2{part.radius2 != 0.0 ? 1.0 / part.radius2 : 0.0}; + result += 0.5 * (k1 + k2) * part.length; + } + return result; +} + +// the legs of the turnout where the type puts them on a straight from its start along the direction, to the hand +void check_legs(layout::network const &State, layout::turnout const &Turnout, layout::turnout_type const &Type, glm::dvec2 const &Along) { + auto const start{plan(State, Turnout.start)}; + auto const hand{static_cast(Turnout.hand)}; + auto const heading_at_end = [&](line_id const Track) { return State.find(Track)->solved.end.heading; }; + switch (Type.kind) { + case turnout_kind::symmetric: { + // each track turning by half of the angle between them, to both sides + auto const half{0.5 * std::atan(1.0 / Type.ratio)}; + CHECK(geometry::signed_angle(Along, heading_at_end(Turnout.main)) == doctest::Approx(-hand * half).epsilon(1e-9)); + CHECK(geometry::signed_angle(Along, heading_at_end(Turnout.diverging)) == doctest::Approx(hand * half).epsilon(1e-9)); + CHECK(State.find(Turnout.start)->ends.size() == 3); + break; + } + case turnout_kind::slip: + case turnout_kind::crossing: { + // the crossing track across the middle at the angle of the slip, its ends half of its length from the crossing + auto const centre{start + Along * Type.half}; + auto const across{geometry::turned(Along, hand * Type.angle)}; + CHECK(glm::distance(plan(State, Turnout.main_end), start + Along * 2.0 * Type.half) < 1e-9); + CHECK(glm::distance(plan(State, Turnout.cross_start), centre - across * Type.half) < 1e-9); + CHECK(glm::distance(plan(State, Turnout.cross_end), centre + across * Type.half) < 1e-9); + CHECK(std::abs(geometry::signed_angle(across, heading_at_end(Turnout.crossing))) < 1e-12); + if (Type.kind == turnout_kind::slip) { + // the slip curves from the start to the end of the crossing track, from its start to the end of the main one + CHECK(std::abs(geometry::signed_angle(across, heading_at_end(Turnout.diverging))) < 1e-12); + // along the track it leaves up to the slip curve, the curve, along the track it meets after it + CHECK(State.find(Turnout.diverging)->solved.length == doctest::Approx(2.0 * (Type.half - Type.curve.tangent()) + Type.curve.length()).epsilon(1e-12)); + CHECK(State.find(Turnout.cross_end)->ends.size() == 2); + if (Type.slip_curves == 2) { + CHECK(std::abs(geometry::signed_angle(Along, heading_at_end(Turnout.diverging2))) < 1e-12); + CHECK(State.find(Turnout.cross_start)->ends.size() == 2); + CHECK(State.find(Turnout.main_end)->ends.size() == 3); + } + // the speed of the slip curves, the line's on the tracks crossing + CHECK(Turnout.speed_diverging == switch_curve::branch_speed(Type.radius, Type.ratio, 0.0, 0.0, false).speed); + } + CHECK(Turnout.speed_main == 80.0); + break; + } + case turnout_kind::twofold: { + // the second switch at the stagger along the main track, its diverging track to the other side or the same one + CHECK(glm::distance(plan(State, Turnout.second), start + Along * Type.stagger) < 1e-9); + CHECK(glm::distance(plan(State, Turnout.main_end), start + Along * Type.length) < 1e-9); + CHECK(geometry::signed_angle(Along, heading_at_end(Turnout.diverging)) == doctest::Approx(hand * turn_of(Type.parts, Type.entry)).epsilon(1e-9)); + auto const hand2{Type.opposite ? -hand : hand}; + CHECK(geometry::signed_angle(Along, heading_at_end(Turnout.diverging2)) == doctest::Approx(hand2 * turn_of(Type.parts2, Type.entry2)).epsilon(1e-9)); + CHECK(State.find(Turnout.second)->ends.size() == 3); + break; + } + default: { + // the main track a + b, the end of the diverging track at 1:n to the hand + CHECK(glm::distance(plan(State, Turnout.main_end), start + Along * Type.length) < 1e-9); + CHECK(geometry::signed_angle(Along, heading_at_end(Turnout.diverging)) == doctest::Approx(hand * std::atan(1.0 / Type.ratio)).epsilon(1e-9)); + CHECK(geometry::cross(Along, plan(State, Turnout.diverging_end) - start) * hand > 0.0); + // the speed rule of the editor on a straight: tab. 11.2, no more than formula 9.9 gives the small radii of the industrial ones + CHECK(Turnout.speed_diverging == switch_curve::branch_speed(Type.radius, Type.ratio, 0.0, 0.0, Type.movable).speed); + break; + } + } +} + +} // namespace + +TEST_CASE("layout turnout kinds: every type of the catalogue is a type the model lays: ordinary, secant, symmetric, double, slips, crossings") { + auto const names{layout::turnout_types()}; + auto const &entries{switch_design::catalogue_entries()}; + CHECK(names.size() == entries.size()); + std::map kinds; + for (auto const &entry : entries) { + CAPTURE(entry.name); + std::string why; + auto const type{layout::turnout_type_of(entry.name, &why)}; + CAPTURE(why); + REQUIRE(type.has_value()); + ++kinds[type->kind]; + if (entry.blades == "secant" && type->kind == turnout_kind::ordinary) { + // the blades meet the stock rail at their angle at the end of the pre-blade stretch + CHECK(type->entry == doctest::Approx(glm::radians(entry.entry))); + CHECK(type->lead > 0.0); + } + } + MESSAGE("the kinds: ordinary " << kinds[turnout_kind::ordinary] << ", symmetric " << kinds[turnout_kind::symmetric] << ", double " << kinds[turnout_kind::twofold] << ", slips " + << kinds[turnout_kind::slip] << ", crossings " << kinds[turnout_kind::crossing]); + CHECK(kinds[turnout_kind::symmetric] == 3); + CHECK(kinds[turnout_kind::twofold] == 15); + CHECK(kinds[turnout_kind::slip] == 18); + CHECK(kinds[turnout_kind::crossing] == 20); + std::string why; + CHECK_FALSE(layout::turnout_type_of("Rz 60E1-999-1:99", &why).has_value()); +} + +TEST_CASE("layout turnout kinds: every type laid on a straight both ways to both sides: its legs where the catalogue puts them, one step to undo") { + for (auto const &name : layout::turnout_types()) { + auto const type{layout::turnout_type_of(name)}; + REQUIRE(type.has_value()); + for (auto const hand : {1, -1}) { + for (auto const direction : {1, -1}) { + CAPTURE(name); + CAPTURE(hand); + CAPTURE(direction); + layout::model model; + auto const length{type->kind == turnout_kind::symmetric ? 400.0 : 1000.0}; + REQUIRE(model.execute(straight({0.0, 100.0, 0.0}, length)).done); + auto const line{model.state().lines().begin()->first}; + auto const before{model.state()}; + auto const chainage{start_for(*type, length, direction)}; + auto const made{model.execute(layout::insert_turnout{line, chainage, name, hand, direction})}; + CAPTURE(reasons_of(made)); + if (false == made.done && type->name.find("16/12") != std::string::npos) { + // fitted to the tangents measured on a scenery, not of Id-4: its transition curve too short for the norms at its speed + CHECK(reasons_of(made).find("ST-T1-A6 tab. 9.14") != std::string::npos); + CHECK(model.state().same(before)); + continue; + } + REQUIRE(made.done); + auto const &state{model.state()}; + check_invariants(state); + REQUIRE(made.turnouts.size() == 1); + auto const &turnout{*state.find(made.turnouts.front())}; + CHECK(layout::kind_of(turnout) == type->kind); + // the start where it was put, the turnout running along the line or against it; a symmetric switch away from its line + glm::dvec2 const along{static_cast(direction), 0.0}; + CHECK(glm::distance(plan(state, turnout.start), glm::dvec2{chainage, 0.0}) < 1e-9); + check_legs(state, turnout, *type, along); + // moved along its line and back, the same to the micrometre; a symmetric switch isn't moved off the end of its line + auto const moved{model.execute(layout::move_turnout{turnout.id, 7.5})}; + CAPTURE(reasons_of(moved)); + CHECK(moved.done == (type->kind != turnout_kind::symmetric)); + if (moved.done) { + check_invariants(model.state()); + CHECK(glm::distance(plan(model.state(), model.state().find(turnout.id)->start), glm::dvec2{chainage + direction * 7.5, 0.0}) < 1e-9); + REQUIRE(model.undo()); + } + // taken out: the line one again, as it was laid + auto const removed{model.execute(layout::remove_turnout{turnout.id})}; + REQUIRE(removed.done); + check_invariants(model.state()); + CHECK(model.state().lines().size() == 1); + CHECK(model.state().turnouts().empty()); + REQUIRE(model.undo()); + REQUIRE(model.undo()); + CHECK(model.state().same(before)); + } + } + } +} + +TEST_CASE("layout turnout kinds: the secant blades of S42 meet the stock rail at their angle at the end of the pre-blade stretch") { + layout::model model; + REQUIRE(model.execute(straight()).done); + auto const line{model.state().lines().begin()->first}; + auto const made{model.execute(layout::insert_turnout{line, 400.0, "Rz S42-190-1:7", 1, 1})}; + CAPTURE(reasons_of(made)); + REQUIRE(made.done); + auto const &state{model.state()}; + auto const &turnout{*state.find(made.turnouts.front())}; + auto const type{layout::turnout_type_of("Rz S42-190-1:7")}; + auto const &elements{state.find(turnout.diverging)->solved.elements}; + REQUIRE(elements.size() >= 2); + // along the main track for the pre-blade stretch, then turned by 25' at the tip of the blade, the arc turning the rest of 1:7 + CHECK(elements.front().kind == layout::element_kind::straight); + CHECK(elements.front().length == doctest::Approx(type->lead)); + auto const kink{geometry::signed_angle(elements[0].tangent(elements[0].length), elements[1].direction)}; + CHECK(kink == doctest::Approx(glm::radians(25.0 / 60.0))); + CHECK(geometry::signed_angle({1.0, 0.0}, state.find(turnout.diverging)->solved.end.heading) == doctest::Approx(std::atan(1.0 / 7.0)).epsilon(1e-9)); + // the turn at the tip isn't a step of the line: the node at the start holds the heading of the main track + check_invariants(state); +} + +TEST_CASE("layout turnout kinds: double switches, slips, crossings and symmetric switches aren't laid into a curve (ST-T1-A6 11.4 pkt 4)") { + layout::model model; + layout::create_line curved; + curved.start = {0.0, 100.0, 0.0}; + curved.end = {1400.0, 100.0, 400.0}; + layout::vertex corner; + corner.point = {700.0, 0.0}; + corner.radius = 1200.0; + corner.transition_in = corner.transition_out = 70.0; + curved.vertices = {corner}; + curved.category = line_class(); + curved.category.track = norms::track_kind::side; + REQUIRE(model.execute(curved).done); + auto const line{model.state().lines().begin()->first}; + auto const &laid{model.state().find(line)->solved}; + double middle{0.0}; + for (auto const &e : laid.elements) { + if (e.role == layout::element_role::arc) { + middle = e.chainage + 0.5 * e.length; + } + } + auto const before{model.state()}; + for (auto const *name : {"Rpd 49E1-190-1:9/1:9", "Rkpd 60E1-190-1:9", "Rkp 49E1-190-1:9", "Skrz 49E1-1:9"}) { + CAPTURE(name); + auto const refused{model.execute(layout::insert_turnout{line, middle, name, 1, 1})}; + CHECK_FALSE(refused.done); + CAPTURE(reasons_of(refused)); + CHECK(reasons_of(refused).find("11.4 pkt 4") != std::string::npos); + CHECK(model.state().same(before)); + } + // an ordinary switch with secant blades is bent, in a side track + auto const bent{model.execute(layout::insert_turnout{line, middle, "Rz S42-205-1:9 I", 1, 1})}; + CAPTURE(reasons_of(bent)); + CHECK(bent.done); + if (bent.done) { + check_invariants(model.state()); + CHECK(model.state().find(bent.turnouts.front())->bent()); + REQUIRE(model.undo()); + } + // a symmetric switch at the end of a line ending in its curve + auto const split{model.execute(layout::split_line{line, middle})}; + REQUIRE(split.done); + REQUIRE(model.execute(layout::delete_line{split.lines.front()}).done); + auto const symmetric{model.execute(layout::insert_turnout{line, model.state().find(line)->solved.length, "Rls 49E1-215-1:4,8", 1, 1})}; + CHECK_FALSE(symmetric.done); + CAPTURE(reasons_of(symmetric)); + CHECK(reasons_of(symmetric).find("symmetric") != std::string::npos); +} + +namespace { + +// random edits with every type of the catalogue: laid along the lines (symmetric switches at their free ends), moved, changed to any +// other type, taken out, with splits, joins and deletions of the lines, undoing and redoing; after each the invariants, a refusal changing +// nothing, an undo giving the state before +void random_kind_edits(unsigned const Seed, int const Count) { + std::mt19937 random{Seed}; + auto const uniform = [&](double const Low, double const High) { return std::uniform_real_distribution{Low, High}(random); }; + auto const pick = [&](std::size_t const Size) { return std::uniform_int_distribution{0, Size - 1}(random); }; + auto const types{layout::turnout_types()}; + layout::model model; + REQUIRE(model.execute(three_curves()).done); + REQUIRE(model.execute(straight({0.0, 100.0, -300.0}, 1200.0)).done); + REQUIRE(model.execute(straight({0.0, 100.0, -600.0}, 300.0)).done); + std::vector history{model.state()}; + std::vector redone; + std::map> tally; + std::map laid; + check_invariants(model.state()); + for (int step = 0; step < Count; ++step) { + CAPTURE(step); + auto const &state{model.state()}; + std::vector plain; + for (auto const &[id, line] : state.lines()) { + if (line.turnout == turnout_id::none) { + plain.push_back(id); + } + } + std::vector turnouts; + for (auto const &[id, turnout] : state.turnouts()) { + turnouts.push_back(id); + } + std::vector free; + for (auto const &[id, node] : state.nodes()) { + if (node.ends.size() == 1) { + free.push_back(id); + } + } + auto const roll{uniform(0.0, 1.0)}; + if (roll < 0.07 && history.size() > 1) { + REQUIRE(model.undo()); + redone.push_back(history.back()); + history.pop_back(); + CHECK(model.state().same(history.back())); + check_invariants(model.state()); + ++tally["undo"].first; + continue; + } + if (roll < 0.10 && false == redone.empty()) { + REQUIRE(model.redo()); + history.push_back(redone.back()); + redone.pop_back(); + CHECK(model.state().same(history.back())); + check_invariants(model.state()); + ++tally["redo"].first; + continue; + } + layout::command command; + auto const &type{types[pick(types.size())]}; + if (roll < 0.40 && false == plain.empty()) { + auto const &line{*state.find(plain[pick(plain.size())])}; + auto chainage{uniform(0.0, line.solved.length)}; + auto direction{uniform(0.0, 1.0) < 0.5 ? 1 : -1}; + // a symmetric switch at an end of the line + if (layout::turnout_type_of(type)->kind == turnout_kind::symmetric) { + chainage = direction > 0 ? line.solved.length : 0.0; + } + command = layout::insert_turnout{line.id, chainage, type, uniform(0.0, 1.0) < 0.5 ? 1 : -1, direction}; + } + else if (roll < 0.55 && false == turnouts.empty()) { + command = layout::move_turnout{turnouts[pick(turnouts.size())], uniform(-30.0, 30.0)}; + } + else if (roll < 0.62 && false == turnouts.empty()) { + command = layout::change_turnout{turnouts[pick(turnouts.size())], type}; + } + else if (roll < 0.70 && false == turnouts.empty()) { + command = layout::remove_turnout{turnouts[pick(turnouts.size())]}; + } + else if (roll < 0.80 && false == plain.empty()) { + auto const &line{*state.find(plain[pick(plain.size())])}; + command = layout::split_line{line.id, uniform(0.0, line.solved.length)}; + } + else if (roll < 0.90 && free.size() >= 2) { + auto const a{pick(free.size())}; + auto b{pick(free.size() - 1)}; + b += b >= a ? 1 : 0; + command = layout::join_ends{free[a], free[b]}; + } + else if (false == plain.empty() && plain.size() > 2) { + command = layout::delete_line{plain[pick(plain.size())]}; + } + else { + continue; + } + std::string const kind{layout::command_name(command)}; + auto const before{model.state()}; + auto const result{model.execute(command)}; + if (result.done) { + ++tally[kind].first; + history.push_back(model.state()); + redone.clear(); + for (auto const id : result.turnouts) { + if (auto const *made{model.state().find(id)}) { + ++laid[layout::kind_of(*made)]; + } + } + } + else { + ++tally[kind].second; + CHECK_FALSE(result.reasons.empty()); + CHECK(model.state().same(before)); + } + check_invariants(model.state()); + } + std::string summary; + for (auto const &[kind, counts] : tally) { + summary += kind + " " + std::to_string(counts.first) + "/" + std::to_string(counts.first + counts.second) + ", "; + } + MESSAGE("seed " << Seed << ": " << summary << "laid: ordinary " << laid[turnout_kind::ordinary] << ", symmetric " << laid[turnout_kind::symmetric] << ", double " + << laid[turnout_kind::twofold] << ", slips " << laid[turnout_kind::slip] << ", crossings " << laid[turnout_kind::crossing] << "; " << model.state().turnouts().size() + << " turnouts at the end"); + CHECK(tally["insert turnout"].first >= 5); + CHECK(tally["insert turnout"].second >= 1); + CHECK(tally["move turnout"].first >= 2); + CHECK(tally["remove turnout"].first >= 1); + CHECK(laid.size() >= 3); + // every state undone back to the start, the lines laid first left + while (model.undoable() > 3) { + REQUIRE(model.undo()); + check_invariants(model.state()); + } + CHECK(model.state().same(history.front())); +} + +} // namespace + +TEST_CASE("layout turnout kinds P: 200 random edits with every type along arcs and transitions, seed 5150: valid nodes or a refusal, undo exact") { + random_kind_edits(5150u, 200); +} + +TEST_CASE("layout turnout kinds P: 200 random edits with every type along arcs and transitions, seed 2718: valid nodes or a refusal, undo exact") { + random_kind_edits(2718u, 200); +} + +TEST_CASE("layout turnout kinds P: 200 random edits with every type along arcs and transitions, seed 1618: valid nodes or a refusal, undo exact") { + random_kind_edits(1618u, 200); +} + +TEST_CASE("layout turnout kinds G: every type made into the tracks of the simulator, read back by TTrack::Load, the legs at the nodes to the bit") { + test::track_reading const reading; + for (auto const &name : layout::turnout_types()) { + CAPTURE(name); + auto const type{layout::turnout_type_of(name)}; + layout::model model; + auto const length{type->kind == turnout_kind::symmetric ? 400.0 : 1000.0}; + REQUIRE(model.execute(straight({0.0, 100.0, 0.0}, length)).done); + auto const line{model.state().lines().begin()->first}; + auto const made{model.execute(layout::insert_turnout{line, start_for(*type, length, 1), name, -1, 1})}; + CAPTURE(reasons_of(made)); + if (false == made.done && type->name.find("16/12") != std::string::npos) { + continue; + } + REQUIRE(made.done); + auto const &state{model.state()}; + auto const &turnout{*state.find(made.turnouts.front())}; + auto const generated{layout::generate(state, turnout.id)}; + CAPTURE(generated.text); + for (auto const &error : generated.errors) { + CAPTURE(error); + CHECK(false); + } + REQUIRE_FALSE(generated.parts.empty()); + // the legs of the turnout, the nodes of its own and those of the line it lies on + std::vector legs; + for (auto const id : {turnout.start, turnout.main_end, turnout.diverging_end, turnout.diverging2_end, turnout.cross_start, turnout.cross_end}) { + if (id != node_id::none && std::ranges::find(legs, state.find(id)->position) == legs.end()) { + legs.push_back(state.find(id)->position); + } + } + // each end of each path read back: at a leg, or meeting the end of another path of the turnout there to the bit + std::vector ends; + std::set legs_met; + for (auto const &part : generated.parts) { + CAPTURE(part.name); + if (part.is_switch) { + auto const track{test::read_track(part.text)}; + REQUIRE(track->eType == tt_Switch); + REQUIRE(track->m_paths.size() == 2); + CHECK(track->name() == part.name); + REQUIRE(track->SwitchExtension != nullptr); + CHECK(track->SwitchExtension->fVelocity == static_cast(turnout.velocity)); + CHECK(track->SwitchExtension->m_movablenose == type->movable); + for (auto const &path : track->m_paths) { + auto const &pieces{path.pieces.empty() ? std::vector{path} : path.pieces}; + for (std::size_t k = 1; k < pieces.size(); ++k) { + CHECK(pieces[k - 1].points[segment_data::point::end] == pieces[k].points[segment_data::point::start]); + } + ends.push_back(path.points[segment_data::point::start]); + ends.push_back(path.points[segment_data::point::end]); + } + continue; + } + for (auto const &piece : part.pieces[0]) { + auto const track{test::read_track(piece.text)}; + REQUIRE(track->eType == tt_Normal); + CHECK(track->name() == piece.name); + ends.push_back(track->m_paths.front().points[segment_data::point::start]); + ends.push_back(track->m_paths.front().points[segment_data::point::end]); + } + } + // the paths of a switch from its start: its two starts at one point, counted once + std::map, int> counts; + for (auto const &end : ends) { + ++counts[{end.x, end.y, end.z}]; + } + for (auto const &end : ends) { + CAPTURE(end.x); + CAPTURE(end.z); + auto const at_leg{std::ranges::find(legs, end)}; + if (at_leg != legs.end()) { + legs_met.insert(static_cast(at_leg - legs.begin())); + continue; + } + // inside the turnout: two ends meet, or the ends of the paths of the switch which ends there and of the track going on + CHECK(counts[{end.x, end.y, end.z}] >= 2); + } + CHECK(legs_met.size() == legs.size()); + } +} + +TEST_CASE("layout turnout kinds G: the project file keeps the double switches, slips and crossings and lays their tracks again the same to the bit") { + layout::model model; + REQUIRE(model.execute(straight()).done); + auto const line{model.state().lines().begin()->first}; + auto const twofold{model.execute(layout::insert_turnout{line, 100.0, "Rpd 49E1-190-1:9/1:9", 1, 1})}; + CAPTURE(reasons_of(twofold)); + REQUIRE(twofold.done); + std::vector plain; + for (auto const &[id, each] : model.state().lines()) { + if (each.turnout == turnout_id::none && each.solved.length > 200.0) { + plain.push_back(id); + } + } + REQUIRE_FALSE(plain.empty()); + auto const tail{plain.back()}; + REQUIRE(model.execute(layout::insert_turnout{tail, 200.0, "Rkpd 49E1-300-1:9", -1, -1}).done); + for (auto const &[id, each] : model.state().lines()) { + if (each.turnout == turnout_id::none && each.solved.length > 300.0) { + REQUIRE(model.execute(layout::insert_turnout{id, 150.0, "Skrz 49E1-1:9", 1, 1}).done); + break; + } + } + REQUIRE(model.state().turnouts().size() == 3); + check_invariants(model.state()); + // the network as settle() leaves it (test_project_file.cpp): the tracks a turnout lays laid before the node at its start takes the grade + // of the line laid beyond it, 1e-16 off the ones laid again from the nodes as they are + auto settled{model.state()}; + { + layout::transaction change{settled}; + for (auto const &[id, each] : settled.turnouts()) { + change.change(id); + } + layout::settle(change); + } + project::document written; + written.network = settled; + auto const text{project::write(written)}; + CAPTURE(text); + auto const read{project::read(text)}; + CAPTURE(read.error); + REQUIRE(read.project.has_value()); + CHECK(read.notes.empty()); + auto const &network{read.project->network}; + CHECK(network.same(settled)); + for (auto const &[id, each] : settled.lines()) { + CAPTURE(layout::number(id)); + CHECK(network.find(id)->solved == each.solved); + } + CHECK(project::write(*read.project) == text); +} diff --git a/tests/unit/test_layout_turnouts.cpp b/tests/unit/test_layout_turnouts.cpp index c18a9b825..4810bbcbd 100644 --- a/tests/unit/test_layout_turnouts.cpp +++ b/tests/unit/test_layout_turnouts.cpp @@ -84,7 +84,7 @@ bool says(std::vector const &Reasons, std::string const &What) { } // namespace -TEST_CASE("layout turnouts: the ordinary switches of the catalogue are laid, double, symmetric and secant ones refused with the reason") { +TEST_CASE("layout turnouts: the ordinary switches of the catalogue are laid, the double, symmetric and secant ones too (test_layout_turnout_kinds.cpp)") { auto const names{layout::turnout_types()}; for (auto const *name : {"Rz 60E1-190-1:9", "Rz 60E1-300-1:9", "Rz 60E1-500-1:12", "Rz 60E1-760-1:14", "Rz 60E1-1200-1:18,5", "Rz 60E1-2500-1:26,5 movable nose", "Rz 49E1-500-1:14"}) { CAPTURE(name); @@ -102,13 +102,12 @@ TEST_CASE("layout turnouts: the ordinary switches of the catalogue are laid, dou CHECK(type->diverging_length() == doctest::Approx(500.0 * std::atan(1.0 / 12.0))); CHECK(layout::turnout_type_of("Rz 60E1-2500-1:26,5 movable nose")->movable); std::string why; - CHECK_FALSE(layout::turnout_type_of("Rls 49E1-215-1:4,8", &why).has_value()); - CHECK(why.find("symmetric") != std::string::npos); - CHECK_FALSE(layout::turnout_type_of("Rpd 49E1-190-1:9/1:9", &why).has_value()); - CHECK(why.find("double") != std::string::npos); - CHECK_FALSE(layout::turnout_type_of("Rz S42-190-1:7", &why).has_value()); - CHECK(why.find("angle") != std::string::npos); + for (auto const *name : {"Rls 49E1-215-1:4,8", "Rpd 49E1-190-1:9/1:9", "Rz S42-190-1:7"}) { + CAPTURE(name); + CHECK(layout::turnout_type_of(name, &why).has_value()); + } CHECK_FALSE(layout::turnout_type_of("Rz 60E1-999-1:99", &why).has_value()); + CHECK(why.find("catalogue") != std::string::npos); } TEST_CASE("layout turnouts: the legs on a straight against the catalogue: the main track a + b, the centre at a, the diverging end at 1:n") {