/* 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/layoutLine.hpp" #include "editor/norms/norms.hpp" #include #include #include #include #include #include // The lines and the nodes of the track geometry (editor-tracks-domain.md ยง4): the aggregates, their invariants and the checks of them. // A node is the point the ends of the lines meet at: its place, the heading, the curvature, the cant and the grade there, and the ends // meeting in it. The continuity of the track is the invariant of the node: the ends of its lines have its place by construction (a line // has no coordinates of its own at its ends), and its heading, its cant and its grade within what the norms let pass; the curvature // may change there only by a step of the cant deficiency the profile allows. Turnouts, whose legs are nodes too, come with stage E5; // until then a node has two ends at most. namespace layout { enum class line_end { start, end }; // an end of a line meeting at a node; the sense tells whether the line runs from there along the heading of the node or against it struct end_ref { line_id line{line_id::none}; line_end end{line_end::start}; int sense{1}; bool operator==(end_ref const &) const = default; // the side of the node the line lies on: 1 ahead of it, along its heading, -1 behind it int side() const { return end == line_end::start ? sense : -sense; } }; struct node { node_id id{node_id::none}; glm::dvec3 position{0.0}; // the scenery's x, the height, z glm::dvec2 heading{0.0, 1.0}; // plan, unit double curvature{0.0}; // 1/m along the heading, above 0 turning left double cant{0.0}; // mm along the heading, above 0 leaning left double grade{0.0}; // along the heading, above 0 rising std::vector ends; bool operator==(node const &) const = default; }; struct line { line_id id{line_id::none}; node_id start{node_id::none}; node_id end{node_id::none}; line_shape shape; // the curve next to the end goes on from the curve the node holds, beginning or ending right at it: a line split inside a curve bool start_continues{false}; bool end_continues{false}; double chainage_start{0.0}; // m, the kilometrage of the start of the line norms::line_class category; // derived: the line laid between its nodes solved_line solved; bool operator==(line const &) const = default; node_id node_at(line_end const End) const { return End == line_end::start ? start : end; } bool continues(line_end const End) const { return End == line_end::start ? start_continues : end_continues; } }; class transaction; // the lines and the nodes of a scenery, with the profiles of the norms by place; identifiers given once and never again class network { public: node const *find(node_id const Id) const; line const *find(line_id const Id) const; std::map const &nodes() const { return m_nodes; } std::map const &lines() const { return m_lines; } // the same lines and nodes: the identifiers handed out so far aren't compared bool same(network const &Other) const { return m_nodes == Other.m_nodes && m_lines == Other.m_lines; } norms::area_map areas; // the profiles of the norms by place node_id new_node() { return static_cast(++m_last_node); } line_id new_line() { return static_cast(++m_last_line); } vertex_id new_vertex() { return static_cast(++m_last_vertex); } // the end of the line meeting at its node, as the node keeps it end_ref const *end_of(line_id const Line, line_end const End) const; // how the end of the line meets its node, for laying the line end_condition condition(line const &Line, line_end const End) const; private: friend class transaction; std::map m_nodes; std::map m_lines; std::uint32_t m_last_node{0}; std::uint32_t m_last_line{0}; std::uint32_t m_last_vertex{0}; }; // the lines and nodes as they were before a change, or are after it: none where one didn't exist struct snapshot { std::map> lines; std::map> nodes; }; // a change of the network in the making: each line and node it touches is kept as it was the first time, so the change can be taken // back whole, or recorded for undoing and redoing class transaction { public: explicit transaction(network &Network) : m_network{Network} {} network const &view() const { return m_network; } network &ids() { return m_network; } line &change(line_id const Id); node &change(node_id const Id); line &add(line Line); node &add(node Node); void erase(line_id const Id); void erase(node_id const Id); std::set touched_lines() const; std::set touched_nodes() const; snapshot before() const { return m_before; } snapshot after() const; // everything as it was before the transaction void roll_back(); private: network &m_network; snapshot m_before; }; // puts a snapshot in place: the lines and the nodes it has as it has them void restore(network &Network, snapshot const &State); // the lines touched laid again between their nodes, and what depends on them: the nodes take the heading, the curvature, the cant and the // grade of the ends they hold (a node at a free end follows its line), and a line going on from a curve at a node whose curve changed is // laid again void settle(transaction &Change); // a line running on through the nodes of two ends: the lines in their order, each forward or backward struct chain_part { line_id line{line_id::none}; bool forward{true}; }; std::vector chain_of(network const &Network, line_id const Line); // the chain described as one line for the norms: the elements running on alike across the nodes as one, the ends open struct described_chain { norms::line line; std::vector starts; // the chainage each part starts at }; described_chain describe_chain(network const &Network, std::vector const &Chain); // what the norms found on a line changed struct notice { line_id line{line_id::none}; double chainage{0.0}; // along the line norms::finding finding; std::string text; }; struct verdict { std::vector reasons; // what breaks an invariant or a BLOCK limit std::vector notices; // WARN and PREFER bool fine() const { return reasons.empty(); } }; // the invariants of the touched lines and nodes, and the norms along the chains through the touched lines verdict check(network const &Network, std::set const &Lines, std::set const &Nodes); // the same over all of the network verdict check_all(network const &Network); // "cant deficiency 162 mm, the limit 150 mm (ST-T1-A6 tab. 9.4)" std::string finding_text(norms::finding const &Finding); } // namespace layout