From 6f0b8be091ffa1364608ba3fb1e01dad94d85f9c Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:34:20 +0100 Subject: [PATCH] feat(editor): the edits E2a left: vertices, the grade line, the class and the profile of a line, a join from a curve, no new BLOCK MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rest of the list E2a left for E2b (editor-tracks-domain.md §8), commands of the domain model of the track geometry like the others: kept whole or refused with the reasons, one step to undo. - insert_vertex (after a vertex, or after the start, with its radius, transitions and cant, its id given) and remove_vertex. - set_grades: the breaks of the grade line with their heights and vertical radii; checked by the solver (overlapping vertical curves) and the norms. - set_line_class and set_line_profile: the class of a line (its speed, traffic, main or side track) and a profile of the norms of its own (the line's own wins over the areas, as norms::area_map has it); a chain of the norms doesn't run on into a line of another profile of its own. The tracks of a turnout take a class too, their speeds derived again. - join_ends from a free end in a curve (a line cut inside one): one curve where the tangents meet, going on from the curve at the end, its arc of the radius there with no transition (or, where that doesn't fit, a radius the norms prefer and a transition from the curvature of the end), the transition at the straight end as long as makes the curve begin right at the curved end (bisected to the bit), refused where both ends lie in curves or the curve would turn the other way. The original join and this one lay the new line the same way (add_joined). - edit_policy::no_new_block for old tracks (stage E6): an edit is refused by a BLOCK finding only where it adds one; one there before, of the same parameter on the same stretch (the elements within 1.5 m, the findings carry their element as points of the plan) and no worse, doesn't refuse; the same rule of the same turnout neither. The invariants refuse in any case. model(network) takes a network over as it is, the norms not asked. Tests: U: a vertex put in and another after it, both taken out, the line as it was; a grade break and its vertical curve, two overlapping and one of no radius refused; a class of 200 km/h refused on curves fine at 80, one of 60 kept; R 280 m at 80 km/h warned by PLK, not by the TSI as the line's own profile; a line joined to a free end in an arc going on from it with the transition the part taken away had, the track the same within a micrometre; both ends in curves refused; R 170 m laid by the TSI and judged by PLK: an edit refused when any BLOCK refuses, kept with no_new_block, a sharper curve and a cant past (R - 50) / 1.5 refused, a wider curve kept. P: 250 random edits of an old track breaking the norms (curves laid for 80 km/h judged at 120), seed 4242, with no_new_block: no BLOCK finding over all the network which wasn't there before, a refusal changing nothing, an undo exact; the random edits with turnouts put vertices into straight lines and take them out. --- editor/layout/layoutCommands.cpp | 271 ++++++++++++++++++++++++---- editor/layout/layoutCommands.hpp | 57 +++++- editor/layout/layoutNetwork.cpp | 45 ++++- editor/layout/layoutNetwork.hpp | 6 + tests/unit/test_layout_turnouts.cpp | 239 +++++++++++++++++++++++- 5 files changed, 571 insertions(+), 47 deletions(-) diff --git a/editor/layout/layoutCommands.cpp b/editor/layout/layoutCommands.cpp index 40a723d5f..f1c569635 100644 --- a/editor/layout/layoutCommands.cpp +++ b/editor/layout/layoutCommands.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include namespace layout { @@ -324,7 +325,7 @@ solved_line const &lay(transaction &Change, line_id const Id) } // the two lines meeting at the node laid as one, the first one's: where the second goes on from the end of the first, nothing else meets -// there, they're alike (the class, the transitions, the ramps, the grade going on) and the line laid so is the same track as +// there, they're alike (the class, the profile, the transitions, the ramps, the grade going on) and the line laid so is the same track as // the two were, within a micrometre. False, nothing changed, where they can't be bool merge_at(transaction &Change, node_id const Node) { @@ -341,7 +342,7 @@ bool merge_at(transaction &Change, node_id const Node) auto const *second{view.find(second_end.line)}; if (first == nullptr || second == nullptr || first->id == second->id || second->end == first->start) return false; - if (first->turnout != turnout_id::none || second->turnout != turnout_id::none || first->category != second->category || + if (first->turnout != turnout_id::none || second->turnout != turnout_id::none || first->category != second->category || first->profile != second->profile || first->shape.transition != second->shape.transition || first->shape.ramp != second->shape.ramp) return false; auto const &a{first->solved}; @@ -513,6 +514,7 @@ reasons lay_turnout(transaction &Change, outcome &Result, line_id const Line, do 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; @@ -669,6 +671,75 @@ reasons propose(transaction &Change, outcome &, remove_turnout const &Command) return {}; } +reasons propose(transaction &Change, outcome &Result, insert_vertex const &Command) +{ + reasons why; + if (own_line(Change.view(), Command.line, why) == nullptr) + return why; + auto const &shape{Command.shape}; + check_curve(shape.radius, shape.transition_in, shape.transition_out, shape.cant, why); + if (false == why.empty()) + return why; + auto &vertices{Change.change(Command.line).shape.vertices}; + auto at{vertices.begin()}; + if (Command.after != vertex_id::none) + { + at = std::ranges::find_if(vertices, [&](vertex const &Each) { return Each.id == Command.after; }); + if (at == vertices.end()) + return {std::format("{} has no vertex {}", line_name(Command.line), number(Command.after))}; + ++at; + } + vertex made{shape}; + made.id = Change.ids().new_vertex(); + Result.vertices.push_back(made.id); + vertices.insert(at, made); + return {}; +} + +reasons propose(transaction &Change, outcome &, remove_vertex const &Command) +{ + reasons why; + if (vertex_of(Change, Command.line, Command.vertex, why) == nullptr) + return why; + std::erase_if(Change.change(Command.line).shape.vertices, [&](vertex const &Each) { return Each.id == Command.vertex; }); + return {}; +} + +reasons propose(transaction &Change, outcome &, set_grades const &Command) +{ + reasons why; + if (own_line(Change.view(), Command.line, why) == nullptr) + return why; + for (std::size_t i = 0; i < Command.grades.size(); ++i) + { + if (Command.grades[i].vertical_radius <= 0.0) + why.push_back(std::format("the grade break at {:.1f} m has no vertical radius", Command.grades[i].chainage)); + if (i > 0 && Command.grades[i].chainage <= Command.grades[i - 1].chainage) + why.push_back(std::format("the grade break at {:.1f} m isn't after the one before it", Command.grades[i].chainage)); + } + if (why.empty()) + Change.change(Command.line).shape.grades = Command.grades; + return why; +} + +reasons propose(transaction &Change, outcome &, set_line_class const &Command) +{ + if (Change.view().find(Command.line) == nullptr) + return {std::format("there's no {}", line_name(Command.line))}; + if (Command.category.vmax <= 0.0) + return {"a line of no speed"}; + Change.change(Command.line).category = Command.category; + return {}; +} + +reasons propose(transaction &Change, outcome &, set_line_profile const &Command) +{ + if (Change.view().find(Command.line) == nullptr) + return {std::format("there's no {}", line_name(Command.line))}; + Change.change(Command.line).profile = Command.profile; + return {}; +} + // the free end at the node: the node, the end it holds and the heading a line joined there leaves it with, its curvature and its cant along // the new line struct free_end @@ -710,11 +781,6 @@ std::optional free_end_at(network const &Network, node_id const Id, bo // 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) result.curvature = 0.0; - if (result.curved()) - { - Why.push_back(std::format("{} lies in a curve; a line joined there would have to go on from it", node_name(Id))); - return std::nullopt; - } return result; } @@ -778,6 +844,131 @@ curve_fit join_fit(vertex const &Vertex, glm::dvec2 const &In, glm::dvec2 const return fit_curve(Shape, geometry::signed_angle(In, Out), Vertex.radius, Vertex.transition_in, Vertex.transition_out); } +// a new line from the free ends, its vertices, how its ends go on from their nodes, the grade line meeting the grades of the ends: one +// break where their tangents meet, or two with a grade between them +reasons add_joined(transaction &Change, outcome &Result, join_ends const &Command, free_end const &From, free_end const &To, std::vector Vertices, bool const StartContinues, + bool const EndContinues) +{ + auto const &view{Change.view()}; + auto const &neighbour{*view.find(From.held.line)}; + auto const profile{view.areas.at({From.at->position.x, From.at->position.z}, {})}; + auto &ids{Change.ids()}; + line joined; + joined.id = ids.new_line(); + joined.start = Command.from; + joined.end = Command.to; + joined.shape.vertices = std::move(Vertices); + for (auto &vertex : joined.shape.vertices) + vertex.id = ids.new_vertex(); + joined.shape.transition = neighbour.shape.transition; + joined.shape.ramp = neighbour.shape.ramp; + joined.category = neighbour.category; + joined.profile = neighbour.profile; + joined.start_continues = StartContinues; + joined.end_continues = EndContinues; + auto const plan{solve(joined.shape, {From.at->position, StartContinues, From.curvature, From.cant}, {To.at->position, EndContinues, To.curvature, To.cant})}; + if (false == plan.valid()) + return {std::format("no line fits between {} and {}: {}", node_name(Command.from), node_name(Command.to), plan.errors.empty() ? std::string{} : plan.errors.front())}; + auto const breaks{join_grades(plan.length, From.at->position.y, From.at->grade * From.sense, To.at->position.y, To.at->grade * To.sense, profile, neighbour.category)}; + if (false == breaks.has_value()) + return {std::format("no grade line fits between the grades of {} and {}", node_name(Command.from), node_name(Command.to))}; + joined.shape.grades = *breaks; + Change.change(Command.from).ends.push_back({joined.id, line_end::start, From.sense}); + Change.change(Command.to).ends.push_back({joined.id, line_end::end, To.sense}); + Result.lines.push_back(joined.id); + Change.add(std::move(joined)); + return {}; +} + +// a line going on from the curve of one of the ends (a line cut inside one), to a straight end: one curve where the tangents meet, which +// begins right at the curved end, its arc going on or a transition from its curvature to one the norms prefer; the transition at the +// straight end as long as makes the curve begin there +reasons join_curved(transaction &Change, outcome &Result, join_ends const &Command, free_end const &From, free_end const &To) +{ + if (From.curved() && To.curved()) + return {std::format("{} and {} both lie in curves: a line going on from both isn't solved yet", node_name(Command.from), node_name(Command.to))}; + auto const &view{Change.view()}; + auto const &neighbour{*view.find(From.held.line)}; + auto const category{neighbour.category}; + auto const shape{neighbour.shape.transition}; + auto const profile{view.areas.at({From.at->position.x, From.at->position.z}, {})}; + auto const p0{geometry::plan_of(From.at->position)}; + auto const p1{geometry::plan_of(To.at->position)}; + double along{0.0}; + double back{0.0}; + if (false == geometry::lines_cross(p0, From.heading, p1, To.heading, along, back) || along <= 0.0 || back >= 0.0) + return {std::format("the tangents of {} and {} don't meet ahead of them: no one curve going on from the curved end joins them", node_name(Command.from), node_name(Command.to))}; + auto const corner{p0 + From.heading * along}; + auto const deflection{geometry::signed_angle(From.heading, To.heading)}; + auto const &curved{From.curved() ? From : To}; + auto const held{From.curved() ? along : -back}; // the tangent which has to be the curve's own to the bit + auto const other{From.curved() ? -back : along}; + if (curved.curvature * deflection <= 0.0 || curved.cant * deflection < -0.5) + return {std::format("the curve at {} turns the other way from the one the line would take", node_name(From.curved() ? Command.from : Command.to))}; + // the radii tried: the curve going on, then the ones the norms prefer, the largest first + auto const going_on{1.0 / std::abs(curved.curvature)}; + std::vector radii{going_on}; + for (auto const radius : {6000.0, 4000.0, 3000.0, 2000.0, 1500.0, 1200.0, 1000.0, 800.0, 600.0, 500.0, 400.0, 300.0}) + if (radius != going_on) + radii.push_back(radius); + for (auto const radius : radii) + { + auto const preferred{preferred_curve(profile, category, radius)}; + auto const going{radius == going_on}; + auto const cant{going ? std::abs(curved.cant) : (Command.cant >= 0.0 ? Command.cant : preferred.cant)}; + auto const leading{going ? 0.0 : std::max(1.0, Command.transition >= 0.0 ? Command.transition : preferred.transition)}; + // a cubic parabola runs between a straight and an arc only + if (false == going && shape == transition_shape::cubic_parabola) + continue; + // the tangent at the curved end as the free transition at the other end is long + auto const tangent = [&](double const Free, bool &Fits) { + auto const fit{From.curved() ? fit_curve(shape, deflection, radius, leading, Free, From.curvature, 0.0) : fit_curve(shape, deflection, radius, Free, leading, 0.0, To.curvature)}; + Fits = fit.error.empty() && false == fit.elements.empty(); + return From.curved() ? fit.tangent_in : fit.tangent_out; + }; + // the free transition from none up to 400 m, the first span whose ends lie on both sides of the tangent wanted + double low{0.0}; + double high{-1.0}; + bool fits{false}; + auto last{tangent(0.0, fits) - held}; + auto last_fits{fits}; + for (double free = 2.0; free <= 400.0; free += 2.0) + { + auto const now{tangent(free, fits) - held}; + if (fits && last_fits && (now == 0.0 || (now < 0.0) != (last < 0.0))) + { + low = free - 2.0; + high = free; + break; + } + last = now; + last_fits = fits; + } + if (high < 0.0) + continue; + bool low_side{(tangent(low, fits) - held) < 0.0}; + for (int i = 0; i < 80; ++i) + { + auto const middle{0.5 * (low + high)}; + (((tangent(middle, fits) - held) < 0.0) == low_side ? low : high) = middle; + } + auto const free{0.5 * (low + high)}; + vertex made; + made.point = corner; + made.radius = radius; + made.cant = cant; + made.transition_in = From.curved() ? leading : free; + made.transition_out = From.curved() ? free : leading; + auto const fit{fit_curve(shape, deflection, radius, made.transition_in, made.transition_out, From.curved() ? From.curvature : 0.0, To.curved() ? To.curvature : 0.0)}; + if (false == fit.error.empty() || (From.curved() ? fit.tangent_out : fit.tangent_in) > other) + continue; + auto const why{add_joined(Change, Result, Command, From, To, {made}, From.curved(), To.curved())}; + if (why.empty()) + return {}; + } + return {std::format("no curve going on from {} fits between {} and {}", node_name(From.curved() ? Command.from : Command.to), node_name(Command.from), node_name(Command.to))}; +} + reasons propose(transaction &Change, outcome &Result, join_ends const &Command) { reasons why; @@ -791,6 +982,8 @@ reasons propose(transaction &Change, outcome &Result, join_ends const &Command) auto const &neighbour{*view.find(from->held.line)}; auto const category{neighbour.category}; auto const shape{neighbour.shape.transition}; + if (from->curved() || to->curved()) + return join_curved(Change, Result, Command, *from, *to); auto const profile{view.areas.at({from->at->position.x, from->at->position.z}, {})}; auto const p0{geometry::plan_of(from->at->position)}; auto const p1{geometry::plan_of(to->at->position)}; @@ -869,30 +1062,7 @@ reasons propose(transaction &Change, outcome &Result, join_ends const &Command) return {std::format("no two curves fit between {} and {}", node_name(Command.from), node_name(Command.to))}; } - auto &ids{Change.ids()}; - line joined; - joined.id = ids.new_line(); - joined.start = Command.from; - joined.end = Command.to; - joined.shape.vertices = std::move(vertices); - for (auto &vertex : joined.shape.vertices) - vertex.id = ids.new_vertex(); - joined.shape.transition = shape; - joined.shape.ramp = neighbour.shape.ramp; - joined.category = category; - // the grade line meeting the grades of the ends: one break where their tangents meet, or two with a grade between them - auto const plan{solve(joined.shape, {from->at->position}, {to->at->position})}; - if (false == plan.valid()) - return {std::format("no line fits between {} and {}: {}", node_name(Command.from), node_name(Command.to), plan.errors.empty() ? std::string{} : plan.errors.front())}; - auto const breaks{join_grades(plan.length, from->at->position.y, from->at->grade * from->sense, to->at->position.y, to->at->grade * to->sense, profile, category)}; - if (false == breaks.has_value()) - return {std::format("no grade line fits between the grades of {} and {}", node_name(Command.from), node_name(Command.to))}; - joined.shape.grades = *breaks; - Change.change(Command.from).ends.push_back({joined.id, line_end::start, from->sense}); - Change.change(Command.to).ends.push_back({joined.id, line_end::end, to->sense}); - Result.lines.push_back(joined.id); - Change.add(std::move(joined)); - return {}; + return add_joined(Change, Result, Command, *from, *to, std::move(vertices), false, false); } reasons propose(transaction &Change, outcome &, delete_line const &Command) @@ -920,20 +1090,53 @@ reasons propose(transaction &Change, outcome &, delete_line const &Command) 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 turnout", - "move turnout", "change turnout", "remove turnout"}; + 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"}; return names[Command.index()]; } -outcome model::execute(command const &Command) +namespace +{ + +// the BLOCK finding was there before the edit +bool was_there(std::vector const &Earlier, notice const &Found) +{ + return std::ranges::any_of(Earlier, [&](notice const &Old) { return same_finding(Old, Found); }); +} + +} // namespace + +outcome model::execute(command const &Command, edit_policy const Policy) { outcome result; + // the state before the edit, to tell the BLOCK findings it adds from the ones there were + std::optional earlier; + if (Policy == edit_policy::no_new_block) + earlier = m_network; transaction change{m_network}; auto why{std::visit([&](auto const &Each) { return propose(change, result, Each); }, Command)}; if (why.empty()) { settle(change); auto judged{check(m_network, change.touched_lines(), change.touched_nodes(), change.touched_turnouts())}; + if (earlier.has_value() && judged.reasons.empty() && false == judged.blocks.empty()) + { + std::set lines; + std::set nodes; + std::set turnouts; + for (auto const id : change.touched_lines()) + if (earlier->find(id) != nullptr) + lines.insert(id); + for (auto const id : change.touched_nodes()) + if (earlier->find(id) != nullptr) + nodes.insert(id); + for (auto const id : change.touched_turnouts()) + if (earlier->find(id) != nullptr) + turnouts.insert(id); + auto const before{check(*earlier, lines, nodes, turnouts).blocks}; + std::erase_if(judged.blocks, [&](notice const &Found) { return was_there(before, Found); }); + } why = judged.refusals(); result.notices = std::move(judged.notices); } diff --git a/editor/layout/layoutCommands.hpp b/editor/layout/layoutCommands.hpp index 9806f775b..87fc39400 100644 --- a/editor/layout/layoutCommands.hpp +++ b/editor/layout/layoutCommands.hpp @@ -66,7 +66,9 @@ struct split_line // a new line laid between two free ends, solved between the nodes: a straight where they face each other on one, a curve where their // tangents meet, two curves otherwise. The radius 0 takes the largest the norms prefer which fits, the transition and the cant below 0 -// the ones the norms prefer for the radius +// the ones the norms prefer for the radius. An end in a curve (a line cut inside one) has the new line go on from its curve: the curve +// where the tangents meet begins right at it, its arc or a transition from its curvature, and the transition at the other end long +// enough for the curve to fit struct join_ends { node_id from{node_id::none}; @@ -82,6 +84,42 @@ struct delete_line line_id line{line_id::none}; }; +// a vertex put into the line after the one given (none: after its start), its radius, transitions and cant with it (the id is given to it) +struct insert_vertex +{ + line_id line{line_id::none}; + vertex_id after{vertex_id::none}; + vertex shape; +}; + +// the vertex taken out of the line: the tangents of its neighbours meet with no curve of it +struct remove_vertex +{ + line_id line{line_id::none}; + vertex_id vertex{vertex_id::none}; +}; + +// the grade line of the line: its breaks with their heights and vertical radii, from its start +struct set_grades +{ + line_id line{line_id::none}; + std::vector grades; +}; + +// the class of the line: its speed, its traffic, main or side track +struct set_line_class +{ + line_id line{line_id::none}; + norms::line_class category; +}; + +// the profile of the norms of the line's own, none: the one of the area it lies in +struct set_line_profile +{ + line_id line{line_id::none}; + std::optional profile; +}; + // a turnout of the type of the catalogue laid into the line, its start at the chainage: the line cut at its legs, the part between them its // main track, its diverging track leaving it to the hand (1 left, -1 right) as it runs from its start, along the line (direction 1) or // against it (-1); bent as the line under it is. Refused with the reasons where a rule of the turnouts or a BLOCK limit of the norms is broken @@ -117,10 +155,20 @@ struct remove_turnout turnout_id turnout{turnout_id::none}; }; -using command = std::variant; +using command = std::variant; char const *command_name(command const &Command); +// how the norms refuse an edit: any BLOCK limit passed along the lines it touches, or only one it adds (an old track, which may break the +// norms where nobody touched it, stage E6): a BLOCK finding there before the edit, of the same parameter at the same place and no worse, isn't +// one it adds. The invariants refuse in any case +enum class edit_policy +{ + refuse_block, + no_new_block +}; + struct outcome { bool done{false}; @@ -129,6 +177,7 @@ struct outcome std::vector lines; // made by it std::vector nodes; std::vector turnouts; + std::vector vertices; }; // the lines and the nodes with the commands which edited them, to undo and redo one by one @@ -137,9 +186,11 @@ class model public: model() = default; explicit model(norms::area_map Areas) { m_network.areas = std::move(Areas); } + // the lines, the nodes and the turnouts taken over as they are, the norms not asked: the tracks of an old scenery (stage E6) + explicit model(network Network) : m_network{std::move(Network)} {} network const &state() const { return m_network; } - outcome execute(command const &Command); + outcome execute(command const &Command, edit_policy const Policy = edit_policy::refuse_block); bool undo(); bool redo(); std::size_t undoable() const { return m_undo.size(); } diff --git a/editor/layout/layoutNetwork.cpp b/editor/layout/layoutNetwork.cpp index 7eade2dd3..842b42d73 100644 --- a/editor/layout/layoutNetwork.cpp +++ b/editor/layout/layoutNetwork.cpp @@ -446,7 +446,7 @@ std::vector chain_of(network const &Network, line_id const Line) result.push_back({Line, true}); auto const contains = [&](line_id const Id) { return std::ranges::any_of(result, [&](chain_part const &Part) { return Part.line == Id; }); }; // the other end at the node, where it holds two; at the start of a turnout the line it starts from runs on into its main track, its - // diverging track into none + // 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})) @@ -460,6 +460,9 @@ std::vector chain_of(network const &Network, line_id const Line) others.push_back(&end); if (from == nullptr || others.size() != 1 || others.front()->side() == from->side()) return nullptr; + auto const *next{Network.find(others.front()->line)}; + if (next == nullptr || next->profile != Network.find(From)->profile) + return nullptr; return others.front(); }; // on beyond the end of the last part @@ -569,6 +572,22 @@ described_chain describe_chain(network const &Network, std::vector c return result; } +bool same_finding(notice const &Old, notice const &Found) +{ + if (Found.turnout != turnout_id::none || Old.turnout != turnout_id::none) + return Found.turnout == Old.turnout && Found.text == Old.text; + if (Old.finding.what != Found.finding.what) + return false; + auto const margin{1e-6 * std::max(1.0, std::abs(Old.finding.value))}; + if (norms::minimum(Found.finding.what) ? Found.finding.value < Old.finding.value - margin : Found.finding.value > Old.finding.value + margin) + return false; + for (auto const &a : Old.along) + for (auto const &b : Found.along) + if (glm::distance(a, b) <= 1.5) + return true; + return false; +} + std::vector verdict::refusals() const { auto result{reasons}; @@ -681,7 +700,11 @@ void check_norms(network const &Network, std::set const &Lines, verdict continue; auto const described{describe_chain(Network, chain)}; auto const &first{*Network.find(chain.front().line)}; - for (auto const &finding : norms::evaluate(described.line, first.category, Network.areas)) + // the profile of the lines' own wins over the areas: the lines of a chain have the same + auto areas{Network.areas}; + if (first.profile.has_value()) + areas.lines.insert(areas.lines.begin(), {described.line.name, *first.profile}); + for (auto const &finding : norms::evaluate(described.line, first.category, areas)) { if (finding.level == norms::tier::none) continue; @@ -693,7 +716,23 @@ void check_norms(network const &Network, std::set const &Lines, verdict auto const chainage{chain[part].forward ? along : owner.solved.length - along}; auto const text{finding_text(finding)}; if (finding.level == norms::tier::block) - Result.blocks.push_back({owner.id, chainage, finding, text}); + { + // where along the chain: the element it's on, as points of the plan + auto const position_at = [&](double const At) { + auto index{static_cast(std::upper_bound(described.starts.begin(), described.starts.end(), At + 1e-9) - described.starts.begin())}; + index = index > 0 ? index - 1 : 0; + auto const &line{Network.find(chain[index].line)->solved}; + auto const local{At - described.starts[index]}; + return line.position(std::clamp(chain[index].forward ? local : line.length - local, 0.0, line.length)); + }; + notice found{owner.id, chainage, finding, text}; + auto const &element{described.line.elements[std::min(finding.element, described.line.elements.size() - 1)]}; + auto const count{std::clamp(static_cast(std::ceil(element.length / 2.0)), 1, 256)}; + for (int i = 0; i <= count; ++i) + found.along.push_back(position_at(element.start + element.length * i / count)); + found.along.push_back(position_at(finding.chainage)); + Result.blocks.push_back(std::move(found)); + } else if (Lines.contains(owner.id)) Result.notices.push_back({owner.id, chainage, finding, text}); } diff --git a/editor/layout/layoutNetwork.hpp b/editor/layout/layoutNetwork.hpp index 0bafd2671..4d20f1465 100644 --- a/editor/layout/layoutNetwork.hpp +++ b/editor/layout/layoutNetwork.hpp @@ -71,6 +71,7 @@ struct line bool end_continues{false}; double chainage_start{0.0}; // m, the kilometrage of the start of the line norms::line_class category; + std::optional profile; // of the norms, its own; none: the one of the area it lies in // derived: the line laid between its nodes solved_line solved; @@ -222,8 +223,13 @@ struct notice norms::finding finding; // of a rule of a turnout: no parameter (norms::parameter::count), the tier std::string text; turnout_id turnout{turnout_id::none}; // a rule of the turnout + std::vector along; // a BLOCK finding: the element it's on, as points of the plan 2 m apart at most }; +// the BLOCK finding Found is the one Old was: a rule of the same turnout telling the same, or a limit of the same parameter passed on the +// same stretch of the track (their elements within a metre and a half of each other), by no more +bool same_finding(notice const &Old, notice const &Found); + struct verdict { std::vector reasons; // what breaks an invariant diff --git a/tests/unit/test_layout_turnouts.cpp b/tests/unit/test_layout_turnouts.cpp index 04b0b5bcc..c18a9b825 100644 --- a/tests/unit/test_layout_turnouts.cpp +++ b/tests/unit/test_layout_turnouts.cpp @@ -1,7 +1,9 @@ // the turnouts of the track geometry (editor/layout, editor-tracks-domain.md §4 "Rozjazd", §7, §8): the types of the catalogue the // model lays, the legs of a turnout against the catalogue (a, b, the angle), the bending of table 11.8 and the speeds of Q2 as the model // computes them, the rules of Q2 and §11.4 refusing, the commands laying, moving, changing and taking out a turnout kept whole or refused -// with the state as it was, one step to undo; random edits with turnouts, the seed in the name of the case +// with the state as it was, one step to undo; the edits E2a left (a vertex put in and taken out, the grade line, the class and the profile +// of a line, a line going on from the curve of a free end) and the edits which may add no BLOCK finding to an old track; random edits with +// turnouts, the seed in the name of the case #include "stdafx.h" #include "doctest.h" @@ -292,6 +294,7 @@ TEST_CASE("layout turnouts: moved along its line, changed, taken out with the li // its tracks aren't edited as lines CHECK_FALSE(model.execute(layout::split_line{turnout.main, 10.0}).done); CHECK_FALSE(model.execute(layout::delete_line{turnout.diverging}).done); + CHECK_FALSE(model.execute(layout::set_grades{turnout.main, {}}).done); CHECK(model.state().same(laid)); // moved 25 m on: the start 25 m further, its ids kept, the lines on both sides one step to undo auto const moved{model.execute(layout::move_turnout{id, 25.0})}; @@ -381,13 +384,179 @@ TEST_CASE("layout turnouts: a line going on from the end of the diverging track CHECK(model.state().same(before)); } +TEST_CASE("layout commands: a vertex put in and taken out, the grade line, the class and the profile of a line") { + layout::model model; + REQUIRE(model.execute(straight_line()).done); + auto const line{only_line(model)}; + auto const plain{model.state()}; + // a vertex off the line with a curve of the radius the norms prefer: the line turns there; taken out, the straight again + auto bend{curve({500.0, 20.0}, 1500.0)}; + auto const put{model.execute(layout::insert_vertex{line, layout::vertex_id::none, bend})}; + CAPTURE(reasons_of(put)); + REQUIRE(put.done); + REQUIRE(put.vertices.size() == 1); + check_invariants(model.state()); + CHECK(model.state().find(line)->shape.vertices.size() == 1); + CHECK(model.state().find(line)->solved.elements.size() == 5); + // another after it, then the first one taken out + auto const second{model.execute(layout::insert_vertex{line, put.vertices.front(), curve({800.0, 0.0}, 1500.0)})}; + CAPTURE(reasons_of(second)); + REQUIRE(second.done); + CHECK(model.state().find(line)->shape.vertices.back().id == second.vertices.front()); + REQUIRE(model.execute(layout::remove_vertex{line, second.vertices.front()}).done); + REQUIRE(model.execute(layout::remove_vertex{line, put.vertices.front()}).done); + CHECK(model.state().same(plain)); + CHECK_FALSE(model.execute(layout::remove_vertex{line, put.vertices.front()}).done); + CHECK_FALSE(model.execute(layout::insert_vertex{line, layout::vertex_id{999}, bend}).done); + // the grade line: a break with its vertical curve; two overlapping, a break of no radius refused + REQUIRE(model.execute(layout::set_grades{line, {{400.0, 103.0, 10000.0}}}).done); + check_invariants(model.state()); + CHECK(model.state().find(line)->solved.elevation(400.0) < 103.0); + auto const graded{model.state()}; + CHECK_FALSE(model.execute(layout::set_grades{line, {{400.0, 103.0, 10000.0}, {420.0, 100.0, 10000.0}}}).done); + CHECK_FALSE(model.execute(layout::set_grades{line, {{400.0, 103.0, 0.0}}}).done); + CHECK(model.state().same(graded)); + // the class: a curve fine at 80 km/h passes a BLOCK limit at 200; the profile of the line's own judges it + layout::model curved; + REQUIRE(curved.execute(three_curves()).done); + auto const id{only_line(curved)}; + auto fast{line_class()}; + fast.vmax = 200.0; + auto const before{curved.state()}; + auto const refused{curved.execute(layout::set_line_class{id, fast})}; + CHECK_FALSE(refused.done); + CHECK(curved.state().same(before)); + auto slow{line_class()}; + slow.vmax = 60.0; + REQUIRE(curved.execute(layout::set_line_class{id, slow}).done); + CHECK(curved.state().find(id)->category.vmax == 60.0); + // R 280 m at 80 km/h warns by PLK (P1 300 m), by the TSI alone it doesn't + REQUIRE(curved.execute(layout::set_line_class{id, line_class()}).done); + auto const tight{layout::preferred_curve(norms::profile_id::plk, line_class(), 280.0)}; + auto const v2{curved.state().find(id)->shape.vertices[1].id}; + auto const warned{curved.execute(layout::set_vertex{id, v2, 280.0, tight.transition, tight.transition, tight.cant})}; + REQUIRE(warned.done); + auto const radius_warning = [](layout::outcome const &Outcome) { + return std::ranges::any_of(Outcome.notices, [](layout::notice const &Notice) { return Notice.finding.what == norms::parameter::radius && Notice.finding.level == norms::tier::warn; }); + }; + CHECK(radius_warning(warned)); + auto const tsi{curved.execute(layout::set_line_profile{id, norms::profile_id::tsi})}; + REQUIRE(tsi.done); + CHECK(curved.state().find(id)->profile == norms::profile_id::tsi); + CHECK_FALSE(radius_warning(tsi)); + REQUIRE(curved.execute(layout::set_line_profile{id, std::nullopt}).done); + CHECK_FALSE(curved.state().find(id)->profile.has_value()); +} + +TEST_CASE("layout commands: a line joined to a free end in a curve goes on from its curve, the track as it was") { + layout::model model; + REQUIRE(model.execute(one_curve(800.0, 50.0)).done); + auto const line{only_line(model)}; + auto const original{model.state().find(line)->solved}; + auto const &vertex{model.state().find(line)->shape.vertices.front()}; + auto const transition{vertex.transition_out}; + // cut in the middle of the arc, the part after it taken away: a free end in the curve + auto const at{middle_of_arc(*model.state().find(line))}; + auto const cut{model.execute(layout::split_line{line, at})}; + REQUIRE(cut.done); + auto const end_point{model.state().find(cut.lines.front())->solved.end.position}; + REQUIRE(model.execute(layout::delete_line{cut.lines.front()}).done); + auto const free_curve{cut.nodes.front()}; + CHECK(model.state().find(free_curve)->curvature != 0.0); + // a line on along the tangent the curve ended on, its start free + auto const out{original.end.heading}; + layout::create_line on; + on.start = end_point; + on.end = end_point + glm::dvec3{out.x, 0.0, out.y} * 200.0; + on.category = line_class(); + auto const beyond{model.execute(on)}; + REQUIRE(beyond.done); + auto const joined{model.execute(layout::join_ends{free_curve, model.state().find(beyond.lines.front())->start})}; + CAPTURE(reasons_of(joined)); + REQUIRE(joined.done); + check_invariants(model.state()); + // the arc going on, the transition out as long as it was: the track the part taken away had + auto const &link{*model.state().find(joined.lines.front())}; + CHECK(link.start_continues); + REQUIRE(link.shape.vertices.size() == 1); + CHECK(link.shape.vertices.front().radius == doctest::Approx(800.0)); + CHECK(link.shape.vertices.front().transition_out == doctest::Approx(transition).epsilon(1e-6)); + for (double s = 0.0; s <= link.solved.length; s += 5.0) { + CAPTURE(s); + CHECK(glm::distance(link.solved.point(s), original.point(at + s)) < 1e-6); + } + // both ends in curves: not yet + layout::model two; + REQUIRE(two.execute(three_curves()).done); + auto const id{only_line(two)}; + auto const &curves{*two.state().find(id)}; + auto const first{two.execute(layout::split_line{id, middle_of_arc(curves)})}; + REQUIRE(first.done); + double second_arc{0.0}; + for (auto const &e : two.state().find(first.lines.front())->solved.elements) { + if (e.role == layout::element_role::arc) { + second_arc = e.chainage + 0.5 * e.length; + break; + } + } + auto const second{two.execute(layout::split_line{first.lines.front(), second_arc})}; + REQUIRE(second.done); + auto const a{two.state().find(first.lines.front())->start}; + auto const b{two.state().find(first.lines.front())->end}; + REQUIRE(two.execute(layout::delete_line{first.lines.front()}).done); + auto const both{two.execute(layout::join_ends{a, b})}; + CHECK_FALSE(both.done); + CHECK(says(both.reasons, "both lie in curves")); +} + +TEST_CASE("layout commands: an old track breaking the norms is edited with no BLOCK added, the BLOCK it had not refusing") { + // R 170 m at 40 km/h laid by the TSI alone (150 m), then judged by PLK: below its 180 m, a BLOCK there before any edit + norms::area_map lenient; + lenient.scenery = norms::profile_id::tsi; + layout::model laid{lenient}; + auto line{one_curve(170.0, 60.0)}; + line.category.vmax = 40.0; + line.vertices.front().transition_in = line.vertices.front().transition_out = 40.0; + auto const made{laid.execute(line)}; + CAPTURE(reasons_of(made)); + REQUIRE(made.done); + auto old{laid.state()}; + old.areas.scenery = norms::profile_id::plk; + auto const existing{layout::check_all(old)}; + REQUIRE_FALSE(existing.blocks.empty()); + CHECK(std::ranges::any_of(existing.blocks, [](layout::notice const &Block) { return Block.finding.what == norms::parameter::radius; })); + layout::model model{old}; + auto const id{only_line(model)}; + auto const vertex{model.state().find(id)->shape.vertices.front().id}; + auto const before{model.state()}; + // refused when any BLOCK refuses, kept when it isn't one the edit adds + CHECK_FALSE(model.execute(layout::set_vertex{id, vertex, std::nullopt, std::nullopt, std::nullopt, 55.0}).done); + CHECK(model.state().same(before)); + auto const kept{model.execute(layout::set_vertex{id, vertex, std::nullopt, std::nullopt, std::nullopt, 55.0}, layout::edit_policy::no_new_block)}; + CAPTURE(reasons_of(kept)); + CHECK(kept.done); + // a sharper curve makes it worse, a cant past (R - 50) / 1.5 adds one: refused; a wider one makes it better: kept + auto const now{model.state()}; + CHECK_FALSE(model.execute(layout::set_vertex{id, vertex, 160.0}, layout::edit_policy::no_new_block).done); + auto const steep{model.execute(layout::set_vertex{id, vertex, std::nullopt, std::nullopt, std::nullopt, 100.0}, layout::edit_policy::no_new_block)}; + CHECK_FALSE(steep.done); + CHECK(says(steep.reasons, "cant")); + CHECK(model.state().same(now)); + auto const wider{model.execute(layout::set_vertex{id, vertex, 175.0}, layout::edit_policy::no_new_block)}; + CAPTURE(reasons_of(wider)); + CHECK(wider.done); + // the invariants refuse in any case + CHECK_FALSE(model.execute(layout::move_vertex{id, vertex, {0.0, 0.0}}, layout::edit_policy::no_new_block).done); +} + namespace { std::vector const kTypes{"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 49E1-500-1:14"}; -// random edits with turnouts: laid, moved, changed and taken out, with the edits of the lines (vertices moved and set, splits, joins, -// deletions), undoing and redoing; after each the invariants, a refusal changing nothing, an undo giving the state before -void random_turnout_edits(unsigned const Seed, int const Count) { +// random edits with turnouts: laid, moved, changed and taken out, with the edits of the lines (vertices moved and set, put in and taken +// out, splits, joins, deletions), undoing and redoing; after each the invariants, a refusal changing nothing, an undo giving the state +// before. Policy: of the norms; with no_new_block the network starts from one breaking them and no BLOCK finding is added +void random_turnout_edits(unsigned const Seed, int const Count, layout::edit_policy const Policy = layout::edit_policy::refuse_block) { 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); }; @@ -395,7 +564,29 @@ void random_turnout_edits(unsigned const Seed, int const Count) { std::vector history; REQUIRE(model.execute(three_curves()).done); REQUIRE(model.execute(straight_line({0.0, 100.0, -300.0}, 1200.0)).done); - auto const checked = [&](layout::network const &State) { check_invariants(State); }; + if (Policy == layout::edit_policy::no_new_block) { + // the lines judged at 120 km/h, which they weren't laid for: an old track breaking the norms in its curves + auto old{model.state()}; + layout::transaction faster{old}; + auto fast{line_class()}; + fast.vmax = 120.0; + for (auto const &[id, line] : model.state().lines()) { + faster.change(id).category = fast; + } + layout::settle(faster); + REQUIRE_FALSE(layout::check_all(old).blocks.empty()); + model = layout::model{old}; + } + // the invariants, and no BLOCK at all where the norms refuse any + auto const checked = [&](layout::network const &State) { + if (Policy == layout::edit_policy::refuse_block) { + check_invariants(State); + } + else { + check_structure(State); + CHECK(layout::check_all(State).reasons.empty()); + } + }; history.push_back(model.state()); checked(model.state()); std::vector redone; @@ -482,6 +673,21 @@ void random_turnout_edits(unsigned const Seed, int const Count) { b += b >= a ? 1 : 0; command = layout::join_ends{free[a], free[b]}; } + else if (roll < 0.92 && false == plain.empty()) { + // a vertex put into a straight line and taken out again + auto const &line{*state.find(plain[pick(plain.size())])}; + if (line.shape.vertices.empty() && line.solved.length > 300.0) { + auto const s{uniform(100.0, line.solved.length - 100.0)}; + auto const across{layout::left_of(line.solved.tangent(s)) * uniform(-15.0, 15.0)}; + command = layout::insert_vertex{line.id, layout::vertex_id::none, curve(line.solved.position(s) + across, 2000.0)}; + } + else if (false == line.shape.vertices.empty()) { + command = layout::remove_vertex{line.id, line.shape.vertices[pick(line.shape.vertices.size())].id}; + } + else { + continue; + } + } else if (false == plain.empty()) { auto const &line{*state.find(plain[pick(plain.size())])}; command = layout::delete_line{line.id}; @@ -491,7 +697,8 @@ void random_turnout_edits(unsigned const Seed, int const Count) { } std::string const kind{layout::command_name(command)}; auto const before{model.state()}; - auto const result{model.execute(command)}; + auto const blocks_before{layout::check_all(before).blocks}; + auto const result{model.execute(command, Policy)}; if (result.done) { ++tally[kind].first; history.push_back(model.state()); @@ -503,6 +710,14 @@ void random_turnout_edits(unsigned const Seed, int const Count) { CHECK(model.state().same(before)); } checked(model.state()); + if (Policy == layout::edit_policy::no_new_block) { + // no BLOCK finding added: each one there now was there before, of the parameter, on the same stretch, no worse + auto const now{layout::check_all(model.state())}; + for (auto const &block : now.blocks) { + CAPTURE(block.text); + CHECK(std::ranges::any_of(blocks_before, [&](layout::notice const &Old) { return layout::same_finding(Old, block); })); + } + } } std::string summary; for (auto const &[kind, counts] : tally) { @@ -514,7 +729,13 @@ void random_turnout_edits(unsigned const Seed, int const Count) { CHECK(tally["remove turnout"].first >= 1); CHECK(tally["insert turnout"].second >= 1); CHECK(tally["undo"].first >= 1); - CHECK(tally["move turnout"].first >= 2); + if (Policy == layout::edit_policy::refuse_block) { + CHECK(tally["move turnout"].first >= 2); + } + else { + // the old BLOCK findings didn't refuse every edit of the lines they're on + CHECK(tally["move vertex"].first + tally["set vertex"].first >= 1); + } } } // namespace @@ -530,3 +751,7 @@ TEST_CASE("layout turnouts P: 250 random edits with turnouts, seed 31: the legs TEST_CASE("layout turnouts P: 250 random edits with turnouts, seed 977: the legs and the nodes whole, no BLOCK, a refusal changes nothing") { random_turnout_edits(977u, 250); } + +TEST_CASE("layout turnouts P: 250 random edits of an old track breaking the norms, seed 4242: no BLOCK added, a refusal changes nothing") { + random_turnout_edits(4242u, 250, layout::edit_policy::no_new_block); +}