diff --git a/CMakeLists.txt b/CMakeLists.txt index 90f52237f..f196cb322 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -222,6 +222,7 @@ set(SOURCES "editor/layout/layoutCommands.cpp" "editor/layout/layoutTurnout.cpp" "editor/layout/layoutGenerator.cpp" +"editor/layout/layoutConnection.cpp" "editor/project/projectFile.cpp" "editor/project/projectGenerated.cpp" "editor/project/projectMigration.cpp" diff --git a/editor/layout/layoutCommands.cpp b/editor/layout/layoutCommands.cpp index 8bc0d8d0c..37f46fc0c 100644 --- a/editor/layout/layoutCommands.cpp +++ b/editor/layout/layoutCommands.cpp @@ -1383,6 +1383,34 @@ reasons propose(transaction &Change, outcome &Result, join_ends const &Command) return add_joined(Change, Result, Command, *from, *to, std::move(vertices), false, false); } +reasons propose(transaction &Change, outcome &Result, connect_tracks const &Command) +{ + auto const plan{plan_connection(Change.view(), Command)}; + if (false == plan.fits) + return plan.reasons; + auto why{lay_turnout(Change, Result, Command.from, Command.chainage, Command.type, plan.hand, Command.direction, {})}; + if (false == why.empty()) + return why; + auto const first{Result.turnouts.back()}; + why = lay_turnout(Change, Result, Command.to, plan.chainage, Command.type, plan.hand2, plan.direction, {}); + if (false == why.empty()) + return why; + auto const second{Result.turnouts.back()}; + // the line between the ends of their diverging tracks: a straight where they face each other on one, else one arc with no transitions nor + // cant, the element between the two turnouts; run at the speed of their diverging tracks + auto const &one{*Change.view().find(first)}; + auto const &other{*Change.view().find(second)}; + auto const speed{std::min(one.speed_diverging, other.speed_diverging)}; + why = propose(Change, Result, join_ends{one.diverging_end, other.diverging_end, 0.0, 0.0, 0.0}); + if (false == why.empty()) + return why; + // a part of the connection of the tracks (połączenie torów) for the norms, as the tracks of the turnouts are + auto &between{Change.change(Result.lines.back())}; + between.category.vmax = std::min(between.category.vmax, speed); + between.category.features |= norms::feature::switch_track; + return {}; +} + reasons propose(transaction &Change, outcome &, delete_line const &Command) { reasons why; @@ -1410,7 +1438,8 @@ char const *command_name(command const &Command) { static std::array> constexpr names{"create line", "move vertex", "set vertex", "split line", "join ends", "delete line", "insert vertex", "remove vertex", "set grades", "set line class", - "set line profile", "insert turnout", "move turnout", "change turnout", "remove turnout"}; + "set line profile", "insert turnout", "move turnout", "change turnout", "remove turnout", + "connect tracks"}; return names[Command.index()]; } diff --git a/editor/layout/layoutCommands.hpp b/editor/layout/layoutCommands.hpp index 87fc39400..720c87a1d 100644 --- a/editor/layout/layoutCommands.hpp +++ b/editor/layout/layoutCommands.hpp @@ -9,6 +9,7 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include "editor/layout/layoutConnection.hpp" #include "editor/layout/layoutNetwork.hpp" #include @@ -155,8 +156,10 @@ struct remove_turnout turnout_id turnout{turnout_id::none}; }; +// a crossover between two lines, as the solver of the connections plans it (layoutConnection.hpp): the two turnouts and the line between the +// ends of their diverging tracks, one step; refused with the best which fits where the norms can't be met using command = std::variant; + move_turnout, change_turnout, remove_turnout, connect_tracks>; char const *command_name(command const &Command); diff --git a/editor/layout/layoutConnection.cpp b/editor/layout/layoutConnection.cpp new file mode 100644 index 000000000..c01af7328 --- /dev/null +++ b/editor/layout/layoutConnection.cpp @@ -0,0 +1,322 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "editor/layout/layoutConnection.hpp" +#include "editor/layout/layoutCommands.hpp" +#include "editor/layout/layoutTurnout.hpp" +#include "editor/editorGeometry.hpp" +#include "editor/editorSwitchCurve.hpp" +#include "editor/editorSwitchDesign.hpp" + +#include +#include +#include +#include +#include + +namespace layout +{ + +namespace +{ + +double constexpr kStep{1.0}; // m between the points of the first line the spacing is measured at +double constexpr kAlongside{0.2}; // rad: the lines run alongside where their headings differ by less +double constexpr kSolved{1e-13}; // rad: the line between the ends turns alike at both of them within this +int constexpr kIterations{40}; + +// the nearest point of the line to the point of the plan: its chainage +double nearest_on(solved_line const &Line, glm::dvec2 const &Point) +{ + auto best{0.0}; + auto nearest{std::numeric_limits::max()}; + auto const count{std::max(2, static_cast(std::ceil(Line.length / 2.0)))}; + for (int i = 0; i <= count; ++i) + { + auto const s{Line.length * i / count}; + if (auto const d{glm::distance(Line.position(s), Point)}; d < nearest) + { + nearest = d; + best = s; + } + } + for (int i = 0; i < 20; ++i) + { + auto const shift{glm::dot(Point - Line.position(best), glm::normalize(Line.tangent(best)))}; + best = std::clamp(best + shift, 0.0, Line.length); + if (std::abs(shift) < 1e-12) + break; + } + return best; +} + +// the end of the diverging track of the type laid on a straight from the origin along x: how far along, how far aside, its angle +struct diverging_end +{ + double along{0.0}; + double aside{0.0}; + double angle{0.0}; + bool valid{false}; +}; + +diverging_end end_of(std::string const &Type) +{ + diverging_end result; + auto const type{turnout_type_of(Type)}; + if (false == type.has_value() || type->kind != turnout_kind::ordinary) + return result; + turnout_frame frame; + frame.heading = {1.0, 0.0}; + frame.valid = true; + auto const laid{lay_diverging(frame, *type, 1)}; + if (false == laid.valid()) + return result; + result.along = laid.end.position.x; + result.aside = laid.end.position.z; + result.angle = geometry::signed_angle({1.0, 0.0}, laid.end.heading); + result.valid = true; + return result; +} + +// the least of tab. 11.2 the catalogue gives the type, 0 where it gives none +double table_spacing(std::string const &Type) +{ + auto const &entries{switch_design::catalogue_entries()}; + auto const found{std::ranges::find_if(entries, [&](switch_design::design const &Each) { return Each.name == Type; })}; + return found != entries.end() ? found->spacing : 0.0; +} + +// m, the least straight the norms ask between two turnouts on the line, at the speed through them: the normal limit of the profile, or the +// one it prefers where it has none, and the hard ones +double least_straight(network const &Network, line const &Line, glm::dvec2 const &At, double const Speed) +{ + norms::query where; + where.speed = Speed; + where.kind = Line.category.kind; + where.track = Line.category.track; + where.features = Line.category.vmax >= 160.0 ? norms::feature::fast_line : 0u; + auto const profile{Line.profile.value_or(Network.areas.at({At.x, At.y}, {}))}; + double result{0.0}; + for (auto const &limits : {norms::limits(profile, norms::parameter::switch_straight, where), norms::hard_limits(norms::parameter::switch_straight, where)}) + { + if (limits.warn.has_value()) + result = std::max(result, limits.warn->value); + else if (limits.prefer.has_value()) + result = std::max(result, limits.prefer->value); + if (limits.block.has_value()) + result = std::max(result, limits.block->value); + } + return result; +} + +// the spacing of the type for the norms on the line: of tab. 11.2, or of its geometry with the least straight at its speed +double spacing_needed(network const &Network, line const &Line, glm::dvec2 const &At, std::string const &Type, double &Straight) +{ + auto const type{turnout_type_of(Type)}; + if (false == type.has_value()) + return 0.0; + auto const speed{switch_curve::branch_speed(type->radius, type->ratio, 0.0, 0.0, type->movable).speed}; + Straight = least_straight(Network, Line, At, speed); + return std::max(table_spacing(Type), crossover_spacing(Type, Straight)); +} + +// the type of the catalogue which fits the spacing: of the rail of the type asked for before the others, the fastest of them +std::string best_fitting(network const &Network, line const &Line, glm::dvec2 const &At, std::string const &Asked, double const Spacing) +{ + auto const asked{turnout_type_of(Asked)}; + std::string best; + auto best_rank{-1.0}; + for (auto const &name : turnout_types()) + { + auto const type{turnout_type_of(name)}; + if (type->kind != turnout_kind::ordinary || name.find("16/12") != std::string::npos) + continue; + double straight{0.0}; + if (spacing_needed(Network, Line, At, name, straight) > Spacing) + continue; + auto const speed{switch_curve::branch_speed(type->radius, type->ratio, 0.0, 0.0, type->movable).speed}; + auto const rank{(asked.has_value() && type->rail == asked->rail ? 1e7 : 0.0) + speed * 1e4 + type->radius}; + if (rank > best_rank) + { + best_rank = rank; + best = name; + } + } + return best; +} + +// the second turnout laid on its line in a copy of the network with the first one: the end of its diverging track, its heading +struct second_end +{ + glm::dvec2 point{0.0}; + glm::dvec2 heading{0.0}; + std::vector reasons; +}; + +second_end lay_second(network const &Trial, connect_tracks const &Request, double const Chainage, int const Hand, int const Direction) +{ + second_end result; + model again{Trial}; + auto const made{again.execute(insert_turnout{Request.to, Chainage, Request.type, Hand, Direction})}; + if (false == made.done) + { + result.reasons = made.reasons; + return result; + } + auto const *end{again.state().find(again.state().find(made.turnouts.front())->diverging_end)}; + result.point = geometry::plan_of(end->position); + result.heading = end->heading; + return result; +} + +} // namespace + +double crossover_spacing(std::string const &Type, double const Straight) +{ + auto const end{end_of(Type)}; + if (false == end.valid) + return 0.0; + return 2.0 * std::abs(end.aside) + Straight * std::sin(std::abs(end.angle)); +} + +connection_plan plan_connection(network const &Network, connect_tracks const &Request) +{ + connection_plan plan; + auto const refuse = [&plan](std::string Why) { + plan.reasons.push_back(std::move(Why)); + return plan; + }; + auto const *from{Network.find(Request.from)}; + auto const *to{Network.find(Request.to)}; + if (from == nullptr || to == nullptr || from->turnout != turnout_id::none || to->turnout != turnout_id::none) + return refuse("a crossover joins two lines of their own"); + if (Request.from == Request.to) + return refuse("a crossover joins two lines, not a line to itself"); + if (false == from->solved.valid() || false == to->solved.valid()) + return refuse("the lines aren't laid"); + if (Request.direction != 1 && Request.direction != -1) + return refuse("the first turnout runs along its line (1) or against it (-1)"); + std::string why; + auto const type{turnout_type_of(Request.type, &why)}; + if (false == type.has_value()) + return refuse(why); + if (type->kind != turnout_kind::ordinary) + return refuse(std::format("a crossover is made of ordinary turnouts, not of {}", Request.type)); + auto const end{end_of(Request.type)}; + if (false == end.valid || std::abs(end.angle) < 1e-6) + return refuse(std::format("{}: its diverging track doesn't leave the main one", Request.type)); + // the first turnout, toward the other line + auto const &a{from->solved}; + auto const &b{to->solved}; + auto const s1{std::clamp(Request.chainage, 0.0, a.length)}; + auto const at{a.position(s1)}; + auto const heading{glm::normalize(a.tangent(s1)) * static_cast(Request.direction)}; + auto const beside{nearest_on(b, at)}; + auto const there{b.position(beside)}; + auto const other{glm::normalize(b.tangent(beside))}; + if (std::abs(geometry::cross(heading, other)) > std::sin(kAlongside)) + return refuse(std::format("line {} doesn't run alongside line {} there", number(Request.to), number(Request.from))); + plan.hand = geometry::cross(heading, there - at) > 0.0 ? 1 : -1; + // the second one runs back toward the first: against the other line where it runs the way of the crossover + auto const progress{glm::dot(other, heading) > 0.0 ? 1.0 : -1.0}; + plan.direction = progress > 0.0 ? -1 : 1; + plan.hand2 = geometry::cross(other * static_cast(plan.direction), at - there) > 0.0 ? 1 : -1; + model trial{Network}; + auto const first{trial.execute(insert_turnout{Request.from, s1, Request.type, plan.hand, Request.direction})}; + if (false == first.done) + { + plan.reasons = first.reasons; + return plan; + } + auto const &laid{*trial.state().find(first.turnouts.front())}; + auto const &end1{*trial.state().find(laid.diverging_end)}; + auto const p1{geometry::plan_of(end1.position)}; + auto const e1{end1.heading}; + // where the line between the ends turns by as much at both of them: on one straight between straight lines alongside + auto const turning = [&](second_end const &Second) { + auto const toward{glm::normalize(Second.point - p1)}; + return geometry::signed_angle(e1, toward) - geometry::signed_angle(toward, -Second.heading); + }; + auto const beside_end{nearest_on(b, p1)}; + auto const lateral{glm::distance(p1, b.position(beside_end)) - std::abs(end.aside)}; + auto const guess{beside_end + progress * (lateral / std::tan(std::abs(end.angle)) + end.along)}; + std::vector> tried; + auto const evaluate = [&](double const Chainage, second_end &Into) { + Into = lay_second(trial.state(), Request, Chainage, plan.hand2, plan.direction); + if (false == Into.reasons.empty()) + return false; + tried.emplace_back(Chainage, turning(Into)); + return true; + }; + second_end second; + for (auto const chainage : {guess, guess + progress}) + if (false == evaluate(chainage, second)) + { + plan.reasons = second.reasons; + plan.reasons.insert(plan.reasons.begin(), std::format("the second turnout doesn't fit into line {} at {:.2f} m", number(Request.to), chainage)); + return plan; + } + // the secant along the other line, the turn nearly even in the chainage + for (int i = 0; i < kIterations && std::abs(tried.back().second) > kSolved; ++i) + { + auto const &[s0, f0]{tried[tried.size() - 2]}; + auto const &[s1n, f1]{tried.back()}; + if (std::abs(f1 - f0) < 1e-18) + break; + auto const next{s1n - f1 * (s1n - s0) / (f1 - f0)}; + if (std::abs(next - s1n) < 1e-12) + break; + if (false == evaluate(next, second)) + { + plan.reasons = second.reasons; + plan.reasons.insert(plan.reasons.begin(), std::format("the second turnout doesn't fit into line {} at {:.2f} m", number(Request.to), next)); + return plan; + } + } + plan.chainage = tried.back().first; + plan.straight = glm::distance(second.point, p1); + plan.turn = geometry::signed_angle(e1, -second.heading); + // the spacing along the crossover: from the start of the first turnout as far as the start of the second one lies along the first line + auto const reach{plan.straight * std::cos(std::abs(end.angle)) + 2.0 * end.along}; + plan.spacing_least = std::numeric_limits::max(); + for (double s = 0.0; s <= reach + 1e-9; s += kStep) + { + auto const station{std::clamp(s1 + Request.direction * s, 0.0, a.length)}; + auto const point{a.position(station)}; + auto const spacing{glm::distance(point, b.position(nearest_on(b, point)))}; + plan.spacing_least = std::min(plan.spacing_least, spacing); + plan.spacing_most = std::max(plan.spacing_most, spacing); + } + // the norms: the straight between the turnouts at the speed through them, the spacing of the type + plan.straight_needed = least_straight(Network, *from, at, laid.speed_diverging); + plan.spacing_needed = std::max(table_spacing(Request.type), crossover_spacing(Request.type, plan.straight_needed)); + if (plan.straight < plan.straight_needed - 1e-9) + plan.reasons.push_back(std::format("the line between the turnouts is {:.2f} m long, the norms ask for {:.2f} m between two turnouts at {:.0f} km/h (ST-T1-A6 11.3 pkt 7)", plan.straight, + plan.straight_needed, laid.speed_diverging)); + if (plan.spacing_least < plan.spacing_needed - 1e-9) + plan.reasons.push_back(std::format("the lines are {:.3f} m apart along the crossover (to {:.3f} m), {} needs {:.3f} m (ST-T1-A6 tab. 11.2 and the straight between its turnouts)", + plan.spacing_least, plan.spacing_most, Request.type, plan.spacing_needed)); + if (false == plan.reasons.empty()) + { + auto const best{best_fitting(Network, *from, at, Request.type, plan.spacing_least)}; + if (best.empty()) + plan.proposal = std::format("no ordinary turnout of the catalogue fits lines {:.3f} m apart; {} needs them {:.3f} m apart", plan.spacing_least, Request.type, plan.spacing_needed); + else + plan.proposal = std::format("{} fits lines {:.3f} m apart, at {:.0f} km/h; {} needs them {:.3f} m apart", best, plan.spacing_least, + switch_curve::branch_speed(turnout_type_of(best)->radius, turnout_type_of(best)->ratio, 0.0, 0.0, turnout_type_of(best)->movable).speed, Request.type, + plan.spacing_needed); + plan.reasons.push_back("proposed: " + plan.proposal); + return plan; + } + plan.fits = true; + return plan; +} + +} // namespace layout diff --git a/editor/layout/layoutConnection.hpp b/editor/layout/layoutConnection.hpp new file mode 100644 index 000000000..6dcd96efd --- /dev/null +++ b/editor/layout/layoutConnection.hpp @@ -0,0 +1,63 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include "editor/layout/layoutNetwork.hpp" + +#include +#include + +// The solver of the connections of the tracks (editor-tracks-domain.md §7 "Połącz tory", §8): a crossover between two lines running +// alongside, straight or curved, made of a turnout on each, their diverging tracks facing each other, a line between their ends. The second +// turnout is put where the line between the ends runs as one straight (the tangents of both ends on it), or, between curved lines, where it +// turns by as much at both ends (one curve between them). Checked before it's laid: the spacing of the lines measured along the whole +// crossover, not at one point, against the least of ST-T1-A6 tab. 11.2 for the type (or what its geometry needs where the table hasn't it); +// the line between the turnouts no shorter than the norms ask of the element between two turnouts (the module of the norms, +// parameter::switch_straight: max(0.15 V; 6) m, 30 m on a line of 160 km/h or more). Where the norms can't be met it's refused with the best +// which fits: another type of the catalogue, or the spacing the type needs. +namespace layout +{ + +// the crossover asked for: the first turnout's line, where it starts and which way it runs (the crossover leads that way), the other line +// and the type of both turnouts +struct connect_tracks +{ + line_id from{line_id::none}; + double chainage{0.0}; + int direction{1}; + line_id to{line_id::none}; + std::string type; +}; + +struct connection_plan +{ + bool fits{false}; + std::vector reasons; // why it doesn't, the proposal among them + int hand{1}; // of the first turnout: the side the other line lies on + double chainage{0.0}; // where the second turnout starts along its line + int direction{1}; // the second turnout along its line or against it, its diverging track facing the first one's + int hand2{1}; + double spacing_least{0.0}; // m, of the lines along the crossover, every metre of it + double spacing_most{0.0}; + double spacing_needed{0.0}; // m: of tab. 11.2 for the type, or of its geometry with the least straight + double straight{0.0}; // m, between the ends of the diverging tracks + double straight_needed{0.0}; // m, of the norms + double turn{0.0}; // rad, by which the line between the ends turns: 0 between straights alongside + std::string proposal; // the best which fits where this doesn't +}; + +// the least spacing of two parallel straight lines a crossover of the type needs with a straight between its diverging tracks of the +// length: twice the offset of the end of a diverging track and the straight across at the angle of the turnout; 0 where it's no ordinary +// switch +double crossover_spacing(std::string const &Type, double const Straight); + +connection_plan plan_connection(network const &Network, connect_tracks const &Request); + +} // namespace layout diff --git a/editor/layout/layoutNetwork.cpp b/editor/layout/layoutNetwork.cpp index af4b9f22c..55f1004f9 100644 --- a/editor/layout/layoutNetwork.cpp +++ b/editor/layout/layoutNetwork.cpp @@ -185,6 +185,7 @@ char const *parameter_name(norms::parameter const What) case norms::parameter::grade: return "grade"; case norms::parameter::intermediate_length: return "element between steps"; case norms::parameter::profile_boundary: return "arc across a profile boundary"; + case norms::parameter::switch_straight: return "element between turnouts"; default: return "value"; } } @@ -522,6 +523,10 @@ 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 line of a connection of the tracks, the track between the turnouts of a crossover, is judged as their tracks are + if ((owner.category.features & norms::feature::switch_track) != 0u) + for (auto &element : own.elements) + element.features |= norms::feature::switch_track; // 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)}) diff --git a/editor/norms/norms.hpp b/editor/norms/norms.hpp index ca712b298..635779e4d 100644 --- a/editor/norms/norms.hpp +++ b/editor/norms/norms.hpp @@ -62,6 +62,8 @@ enum class parameter grade, // per mille, at most intermediate_length, // m: abrupt changes of the cant deficiency further apart are taken one by one (ST-T1-A6 tab. 9.16) profile_boundary, // an arc running across the boundary of two profiles: to be settled (§6.4) + switch_straight, // m, at least: the element between two turnouts, their curves turning the opposite ways (ST-T1-A6 11.3 pkt 7); asked by the + // solver of the connections at the speed through them, not found along a line count }; @@ -101,6 +103,7 @@ inline constexpr unsigned sag{1u << 12}; inline constexpr unsigned short_stretch{1u << 13}; // a grade held for less than 500 m (ST-T1-A6 tab. 10.1 footnote 2) inline constexpr unsigned low_deficiency{1u << 14}; // I ≤ 75 mm on both sides of the abrupt change (TSI 4.2.4.4(2)) inline constexpr unsigned abrupt{1u << 15}; // an arc with no transition curve at an end (AB-EBV AB 17 5.2.2) +inline constexpr unsigned fast_line{1u << 16}; // a line of 160 km/h or more (ST-T1-A6 11.3 pkt 7e) } // namespace feature // the class of the line (§2): the highest speed, the slow trains, the traffic, main or side track, and what holds along all of it diff --git a/editor/norms/normsLimits.cpp b/editor/norms/normsLimits.cpp index 1fb019bf3..6fa395325 100644 --- a/editor/norms/normsLimits.cpp +++ b/editor/norms/normsLimits.cpp @@ -76,7 +76,8 @@ bool minimum(parameter const What) case parameter::radius: case parameter::cant_min: case parameter::vertical_radius: - case parameter::intermediate_length: return true; + case parameter::intermediate_length: + case parameter::switch_straight: return true; default: return false; } } diff --git a/editor/norms/normsTables.cpp b/editor/norms/normsTables.cpp index 98d77a87e..badea440a 100644 --- a/editor/norms/normsTables.cpp +++ b/editor/norms/normsTables.cpp @@ -201,6 +201,11 @@ constexpr std::array plk{ row{.what = intermediate_length, .prefer = none, .warn = none, .block = share(0.15), .place = "tab. 9.16", .needs = switch_track, .speed_above = 60, .speed_to = 100}, row{.what = intermediate_length, .prefer = none, .warn = none, .block = share(0.25), .place = "tab. 9.16", .needs = switch_track, .speed_above = 100, .speed_to = 200}, row{.what = intermediate_length, .prefer = none, .warn = none, .block = share(0.10), .place = "tab. 9.16", .excludes = switch_track, .tracks = side_track, .speed_to = 40}, + // 11.3 pkt 7, the element between two turnouts whose curves turn the opposite ways, at the speed through them: in the running and the main + // tracks max(0.15 V; 6) m (lit. d, "should"), 30 m on new lines of 160 km/h or more (lit. e, "may not"); 6 m recommended elsewhere (lit. c) + row{.what = switch_straight, .prefer = share(0.15, 6.0), .warn = share(0.15, 6.0), .block = none, .place = "11.3 pkt 7d", .tracks = main_track}, + row{.what = switch_straight, .prefer = at(30.0), .warn = at(30.0), .block = at(30.0), .place = "11.3 pkt 7e", .needs = fast_line, .tracks = main_track}, + row{.what = switch_straight, .prefer = at(6.0), .warn = none, .block = none, .place = "11.3 pkt 7c", .tracks = side_track}, }; // the TSI rows the profile "TSI only" and the global hard limits share diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index d35b6f7da..a142cd396 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -64,6 +64,7 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/layout/layoutCommands.cpp" "${CMAKE_SOURCE_DIR}/editor/layout/layoutTurnout.cpp" "${CMAKE_SOURCE_DIR}/editor/layout/layoutGenerator.cpp" + "${CMAKE_SOURCE_DIR}/editor/layout/layoutConnection.cpp" # the project file of the domain, the tracks written kept by their checksums, the //$ lines migrated into it, the scenarios using a map (test_project_*.cpp, test_scenario_index.cpp) "${CMAKE_SOURCE_DIR}/editor/project/projectFile.cpp" "${CMAKE_SOURCE_DIR}/editor/project/projectGenerated.cpp" diff --git a/tests/unit/test_layout_connection.cpp b/tests/unit/test_layout_connection.cpp new file mode 100644 index 000000000..0601dfda7 --- /dev/null +++ b/tests/unit/test_layout_connection.cpp @@ -0,0 +1,243 @@ +// the solver of the connections of the tracks (editor/layout/layoutConnection, editor-tracks-domain.md §7 "Połącz tory", stage E5): a +// crossover between two lines alongside, straight or curved, of two turnouts and the line between their diverging tracks, one step to undo; +// the spacing of the lines measured along the whole crossover, not at one point; the least straight between the turnouts from the norms +// (ST-T1-A6 11.3 pkt 7: max(0.15 V; 6) m, 30 m on a line of 160 km/h or more); the least spacing of tab. 11.2; refused with the best which +// fits where the norms can't be met + +#include "stdafx.h" +#include "doctest.h" +#include "layout_fixture.h" + +#include + +namespace { + +using namespace test::layout_fixture; +using layout::line_id; + +// a straight from the point along x, 1 km +layout::create_line along_x(glm::dvec3 const &Start, glm::dvec3 const &End, double const Speed = 80.0) { + layout::create_line result; + result.start = Start; + result.end = End; + result.category = line_class(); + result.category.vmax = Speed; + return result; +} + +// two lines, the second one made after the first +std::pair two_lines(layout::model &Model, layout::create_line const &First, layout::create_line const &Second) { + auto const a{Model.execute(First)}; + auto const b{Model.execute(Second)}; + REQUIRE(a.done); + REQUIRE(b.done); + return {a.lines.front(), b.lines.front()}; +} + +} // namespace + +TEST_CASE("layout connection: a crossover between straights alongside, one straight between the turnouts, one step to undo") { + for (auto const direction : {1, -1}) { + for (auto const side : {1.0, -1.0}) { + CAPTURE(direction); + CAPTURE(side); + layout::model model; + auto const [a, b]{two_lines(model, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}), along_x({0.0, 100.0, side * 4.75}, {1000.0, 100.0, side * 4.75}))}; + auto const before{model.state()}; + layout::connect_tracks const request{a, direction > 0 ? 300.0 : 700.0, direction, b, "Rz 60E1-500-1:12"}; + auto const plan{layout::plan_connection(model.state(), request)}; + CAPTURE(plan.reasons.empty() ? std::string{} : plan.reasons.front()); + REQUIRE(plan.fits); + // the spacing the same all along it, the straight between the turnouts: 4.75 m less the offsets of the two ends, at 1:12 + CHECK(plan.spacing_least == doctest::Approx(4.75).epsilon(1e-9)); + CHECK(plan.spacing_most == doctest::Approx(4.75).epsilon(1e-9)); + CHECK(plan.straight == doctest::Approx((4.75 - layout::crossover_spacing("Rz 60E1-500-1:12", 0.0)) / std::sin(std::atan(1.0 / 12.0))).epsilon(1e-9)); + CHECK(plan.straight_needed == doctest::Approx(9.0)); + CHECK(plan.turn == doctest::Approx(0.0).scale(1.0).epsilon(1e-12)); + auto const made{model.execute(request)}; + CAPTURE(reasons_of(made)); + REQUIRE(made.done); + auto const &state{model.state()}; + check_invariants(state); + REQUIRE(made.turnouts.size() == 2); + // the line between the ends of the diverging tracks a straight, the ends facing each other on it + auto const &first{*state.find(made.turnouts[0])}; + auto const &second{*state.find(made.turnouts[1])}; + auto const *from{state.find(first.diverging_end)}; + auto const *to{state.find(second.diverging_end)}; + CHECK(std::abs(geometry::signed_angle(from->heading, -to->heading)) < 1e-12); + auto const joining{made.lines.back()}; + CHECK(state.find(joining)->shape.vertices.empty()); + CHECK(state.find(joining)->solved.length == doctest::Approx(plan.straight).epsilon(1e-9)); + CHECK(first.hand == static_cast(side) * direction); + // one step to undo + REQUIRE(model.undo()); + CHECK(model.state().same(before)); + } + } +} + +TEST_CASE("layout connection: the spacing measured along the whole crossover, not at its start") { + layout::model model; + auto const needed{layout::crossover_spacing("Rz 60E1-500-1:12", 9.0)}; + // the second line closing in on the first, by 2 mm a metre: as wide as needs be where the crossover starts, narrower on along it + auto const at_start{needed + 0.05}; + auto const [a, b]{two_lines(model, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}), along_x({0.0, 100.0, at_start + 0.6}, {1000.0, 100.0, at_start - 1.4}))}; + layout::connect_tracks const request{a, 300.0, 1, b, "Rz 60E1-500-1:12"}; + auto const plan{layout::plan_connection(model.state(), request)}; + CHECK_FALSE(plan.fits); + CHECK(plan.spacing_most > needed); + CHECK(plan.spacing_least < needed); + CHECK(plan.spacing_most - plan.spacing_least > 0.15); + std::string reasons; + for (auto const &reason : plan.reasons) { + reasons += reason + "; "; + } + CAPTURE(reasons); + CHECK(reasons.find("apart along the crossover") != std::string::npos); + auto const before{model.state()}; + CHECK_FALSE(model.execute(request).done); + CHECK(model.state().same(before)); +} + +TEST_CASE("layout connection: the least straight between the turnouts from the norms, refused with the type which fits") { + layout::model model; + auto const spacing{layout::crossover_spacing("Rz 60E1-500-1:12", 9.0) - 0.15}; + auto const [a, b]{two_lines(model, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}), along_x({0.0, 100.0, spacing}, {1000.0, 100.0, spacing}))}; + auto const plan{layout::plan_connection(model.state(), {a, 300.0, 1, b, "Rz 60E1-500-1:12"})}; + CHECK_FALSE(plan.fits); + // 60 km/h through it: max(0.15 V; 6) = 9 m (ST-T1-A6 11.3 pkt 7d) + CHECK(plan.straight_needed == doctest::Approx(9.0)); + CHECK(plan.straight < 9.0); + std::string reasons; + for (auto const &reason : plan.reasons) { + reasons += reason + "; "; + } + CAPTURE(reasons); + CHECK(reasons.find("11.3 pkt 7") != std::string::npos); + // the proposal: a type of the catalogue which fits these lines, laid as proposed + REQUIRE_FALSE(plan.proposal.empty()); + auto const proposed{plan.proposal.substr(0, plan.proposal.find(" fits "))}; + CAPTURE(proposed); + REQUIRE(layout::turnout_type_of(proposed).has_value()); + auto const made{model.execute(layout::connect_tracks{a, 300.0, 1, b, proposed})}; + CAPTURE(reasons_of(made)); + CHECK(made.done); + check_invariants(model.state()); + // on a line of 160 km/h the norms ask for 30 m between them (11.3 pkt 7e) + layout::model fast; + auto const [c, d]{two_lines(fast, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}, 160.0), along_x({0.0, 100.0, 4.5}, {1000.0, 100.0, 4.5}, 160.0))}; + auto const fast_plan{layout::plan_connection(fast.state(), {c, 300.0, 1, d, "Rz 60E1-500-1:12"})}; + CHECK(fast_plan.straight_needed == doctest::Approx(30.0)); + CHECK_FALSE(fast_plan.fits); +} + +TEST_CASE("layout connection: the least spacing of tab. 11.2 for the type where it's more than its geometry needs") { + layout::model model; + // Rz 60E1-760-1:14: 4.75 m by tab. 11.2, a little less by its geometry with 12 m between its turnouts + auto const geometric{layout::crossover_spacing("Rz 60E1-760-1:14", 12.0)}; + REQUIRE(geometric < 4.75); + auto const [a, b]{two_lines(model, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}), along_x({0.0, 100.0, 4.74}, {1000.0, 100.0, 4.74}))}; + auto const plan{layout::plan_connection(model.state(), {a, 300.0, 1, b, "Rz 60E1-760-1:14"})}; + CHECK(plan.straight >= plan.straight_needed); + CHECK(plan.spacing_needed == doctest::Approx(4.75)); + CHECK_FALSE(plan.fits); + std::string reasons; + for (auto const &reason : plan.reasons) { + reasons += reason + "; "; + } + CAPTURE(reasons); + CHECK(reasons.find("tab. 11.2") != std::string::npos); +} + +TEST_CASE("layout connection: a crossover between curved lines alongside, one curve between the ends turning alike at both") { + layout::model model; + // two curves of 1200 m and 1195.5 m turning left by 30 degrees, 4.5 m apart, no cant + auto const curved = [](double const Offset, double const Radius) { + // the tangents of the first line moved to their left by the offset + layout::create_line result; + auto const turn{3.14159265358979323846 / 6.0}; + glm::dvec2 const out{std::cos(turn), std::sin(turn)}; + glm::dvec2 const left{-std::sin(turn), std::cos(turn)}; + auto const at{glm::dvec2{800.0, 0.0} + left * Offset + out * (Offset * std::tan(turn * 0.5))}; + result.start = {0.0, 100.0, Offset}; + auto const end{glm::dvec2{800.0, 0.0} + out * 800.0 + left * Offset}; + result.end = {end.x, 100.0, end.y}; + layout::vertex vertex; + vertex.point = at; + vertex.radius = Radius; + vertex.transition_in = vertex.transition_out = 80.0; + result.vertices = {vertex}; + result.category = line_class(); + return result; + }; + auto const [a, b]{two_lines(model, curved(0.0, 1200.0), curved(4.5, 1195.5))}; + double middle{0.0}; + for (auto const &e : model.state().find(a)->solved.elements) { + if (e.role == layout::element_role::arc) { + middle = e.chainage + 0.5 * e.length - 60.0; + } + } + layout::connect_tracks const request{a, middle, 1, b, "Rz 60E1-500-1:12"}; + auto const plan{layout::plan_connection(model.state(), request)}; + std::string reasons; + for (auto const &reason : plan.reasons) { + reasons += reason + "; "; + } + CAPTURE(reasons); + REQUIRE(plan.fits); + MESSAGE("between curves: spacing " << plan.spacing_least << " to " << plan.spacing_most << " m, " << plan.straight << " m between the ends, turning by " << glm::degrees(plan.turn) << " deg"); + CHECK(plan.spacing_least == doctest::Approx(4.5).epsilon(0.01)); + CHECK(std::abs(plan.turn) > 1e-4); + auto const before{model.state()}; + auto const made{model.execute(request)}; + CAPTURE(reasons_of(made)); + REQUIRE(made.done); + check_invariants(model.state()); + // both bent with their lines, a curve between their ends + CHECK(model.state().find(made.turnouts[0])->bent()); + CHECK(model.state().find(made.turnouts[1])->bent()); + CHECK(model.state().find(made.lines.back())->shape.vertices.size() == 1); + REQUIRE(model.undo()); + CHECK(model.state().same(before)); +} + +TEST_CASE("layout connection: refused for what isn't a crossover of two ordinary turnouts between two lines alongside") { + layout::model model; + auto const [a, b]{two_lines(model, along_x({0.0, 100.0, 0.0}, {1000.0, 100.0, 0.0}), along_x({0.0, 100.0, 4.75}, {1000.0, 100.0, 4.75}))}; + auto const across{model.execute(along_x({500.0, 100.0, -300.0}, {500.0, 100.0, 300.0}))}; + REQUIRE(across.done); + CHECK_FALSE(layout::plan_connection(model.state(), {a, 300.0, 1, b, "Rkpd 60E1-190-1:9"}).fits); + CHECK_FALSE(layout::plan_connection(model.state(), {a, 300.0, 1, a, "Rz 60E1-500-1:12"}).fits); + CHECK_FALSE(layout::plan_connection(model.state(), {a, 300.0, 1, across.lines.front(), "Rz 60E1-500-1:12"}).fits); + CHECK_FALSE(layout::plan_connection(model.state(), {a, 300.0, 1, b, "Rz 60E1-999-1:99"}).fits); +} + +TEST_CASE("layout connection: the element between two turnouts in the norms, ST-T1-A6 11.3 pkt 7: max(0.15 V; 6) m, 30 m at 160 km/h and more") { + norms::query where; + where.track = norms::track_kind::main; + for (auto const &[speed, least] : std::vector>{{30.0, 6.0}, {40.0, 6.0}, {60.0, 9.0}, {100.0, 15.0}, {130.0, 19.5}}) { + CAPTURE(speed); + where.speed = speed; + auto const limits{norms::limits(norms::profile_id::plk, norms::parameter::switch_straight, where)}; + REQUIRE(limits.warn.has_value()); + CHECK(limits.warn->value == doctest::Approx(least)); + CHECK_FALSE(limits.block.has_value()); + CHECK(limits.warn->from.place == "11.3 pkt 7d"); + } + // on a line of 160 km/h or more: 30 m, which may not be less + where.speed = 60.0; + where.features = norms::feature::fast_line; + auto const fast{norms::limits(norms::profile_id::plk, norms::parameter::switch_straight, where)}; + REQUIRE(fast.block.has_value()); + CHECK(fast.block->value == doctest::Approx(30.0)); + CHECK(fast.warn->value == doctest::Approx(30.0)); + // a side track: 6 m recommended + where.features = 0; + where.track = norms::track_kind::side; + auto const side{norms::limits(norms::profile_id::plk, norms::parameter::switch_straight, where)}; + REQUIRE(side.prefer.has_value()); + CHECK(side.prefer->value == doctest::Approx(6.0)); + CHECK_FALSE(side.warn.has_value()); + CHECK(norms::minimum(norms::parameter::switch_straight)); +}