diff --git a/editor/layout/layoutCommands.cpp b/editor/layout/layoutCommands.cpp index 37f46fc0c..146ce3000 100644 --- a/editor/layout/layoutCommands.cpp +++ b/editor/layout/layoutCommands.cpp @@ -150,6 +150,32 @@ std::optional crossing(glm::dvec2 const &A, glm::dvec2 const &AlongA return A + AlongA * first; } +// the joints of the old tracks of a line cut at the chainage: those past it go with the part after it; the old track cut in two keeps its +// name and its events on the part it ran into first, the other part a stretch of it which the generator names, with the rest of what it had +void split_old_tracks(std::vector const &All, double const Chainage, std::vector &Head, std::vector &Tail) +{ + Head.clear(); + Tail.clear(); + for (auto const &split : All) + { + if (split.chainage < Chainage - 1e-6) + Head.push_back(split); + else + Tail.push_back({std::max(0.0, split.chainage - Chainage), split.track}); + } + if (Head.empty() || (false == Tail.empty() && Tail.front().chainage <= 1e-6)) + return; + auto cut_off{Head.back().track}; + cut_off.continued = true; + if (Head.back().track.reversed) + { + Tail.insert(Tail.begin(), {0.0, Head.back().track}); + Head.back().track = cut_off; + } + else + Tail.insert(Tail.begin(), {0.0, cut_off}); +} + // the new node and the new line a split made struct split_made { @@ -290,6 +316,7 @@ reasons split_at(transaction &Change, line_id const Line, double const Chainage, else if (point.chainage > s) tail_line.shape.grades.push_back({point.chainage - s, point.elevation, point.vertical_radius}); } + split_old_tracks(original.hard_splits, s, head_line.hard_splits, tail_line.hard_splits); // the node at the end of the line holds the end of the new one now auto &last{Change.change(original.end)}; for (auto &end : last.ends) @@ -354,6 +381,8 @@ bool merge_at(transaction &Change, node_id const Node) merged.end_continues = second->end_continues; for (auto const &point : second->shape.grades) merged.shape.grades.push_back({point.chainage + a.length, point.elevation, point.vertical_radius}); + for (auto const &split : second->hard_splits) + merged.hard_splits.push_back({split.chainage + a.length, split.track}); auto const &before{first->shape.vertices}; auto const &after{second->shape.vertices}; std::vector> candidates; diff --git a/editor/layout/layoutGenerator.cpp b/editor/layout/layoutGenerator.cpp index 845e0f302..f427736db 100644 --- a/editor/layout/layoutGenerator.cpp +++ b/editor/layout/layoutGenerator.cpp @@ -48,6 +48,25 @@ std::string number_text(float const Value) return std::format("{}", Value); } +std::string point_text(glm::dvec3 const &Point) +{ + return std::format("{} {} {}", number_text(Point.x), number_text(Point.y), number_text(Point.z)); +} + +// a path as an entry has it: the start, its roll, the control vectors, the end, its roll, the radius +std::string path_text(segment_data const &Path) +{ + auto const &p{Path.points}; + return std::format("{} {} {} {} {} {} {}", point_text(p[segment_data::point::start]), number_text(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(Path.rolls[1]), number_text(Path.radius)); +} + +// the entry of a plain track of the piece: its name, its length, the surface, its path and the attributes +std::string entry_text(generated_piece const &Piece, std::string_view const Surface, std::string_view const Attributes) +{ + return std::format("node -1 0 {} track normal {:.3f} {} {} {} endtrack\n", Piece.name, std::abs(Piece.to - Piece.from), Surface, path_text(Piece.path), Attributes); +} + class cutter { public: @@ -138,12 +157,15 @@ class cutter result.push_back(stretch.from); result.push_back(stretch.to); } + auto const hard{hard_points()}; + result.insert(result.end(), hard.begin(), hard.end()); std::ranges::sort(result); std::erase_if(result, [&](double const Chainage) { return Chainage < 0.0 || Chainage > m_solved.length; }); auto const last{std::unique(result.begin(), result.end(), [](double const A, double const B) { return std::abs(A - B) < 1e-9; })}; result.erase(last, result.end()); result.front() = 0.0; result.back() = m_solved.length; + auto const is_hard = [&result, &hard](std::size_t const At) { return At == 0 || At + 1 == result.size() || std::ranges::find(hard, result[At]) != hard.end(); }; for (std::size_t i = 0; i + 1 < result.size() && result.size() > 2;) { if (result[i + 1] - result[i] >= kShortestPiece) @@ -151,12 +173,28 @@ class cutter ++i; continue; } - // the short stretch goes to its neighbour: the break after it goes, or the one before it at the end - result.erase(result.begin() + static_cast(i + 1 + 1 < result.size() ? i + 1 : i)); + // the short stretch goes to its neighbour: the break after it goes, or the one before it; a joint of an old track stays, the + // short piece with it where both ends are + if (false == is_hard(i + 1)) + result.erase(result.begin() + static_cast(i + 1)); + else if (false == is_hard(i)) + result.erase(result.begin() + static_cast(i)); + else + ++i; } return result; } + // the joints of the old tracks inside the line + std::vector hard_points() const + { + std::vector result; + for (auto const &split : m_line.hard_splits) + if (split.chainage > 0.0 && split.chainage < m_solved.length) + result.push_back(split.chainage); + return result; + } + // the element the chainage lies in, the curve of a span all in one element const *only_element(double const From, double const To) const { @@ -367,28 +405,74 @@ class cutter piece.to = To.chainage; piece.path = path; piece.deviation = off; - auto const &p{path.points}; - auto const point = [](glm::dvec3 const &Point) { return std::format("{} {} {}", number_text(Point.x), number_text(Point.y), number_text(Point.z)); }; - piece.text = std::format("node -1 0 {} track normal {:.3f} {} {} {} {} {} {} {} {} velocity {} endtrack\n", piece.name, To.chainage - From.chainage, m_options.surface, point(p[segment_data::point::start]), - number_text(path.rolls[0]), point(p[segment_data::point::control1]), point(p[segment_data::point::control2]), point(p[segment_data::point::end]), number_text(path.rolls[1]), - number_text(path.radius), number_text(m_line.category.vmax)); + piece.text = entry_text(piece, m_options.surface, std::format("velocity {}", number_text(m_line.category.vmax))); Into.push_back(std::move(piece)); } std::map m_counts; }; -std::string point_text(glm::dvec3 const &Point) +// the attributes of an old track without its events, for the pieces of its stretch which don't carry its name +std::string without_events(std::string_view const Attributes) { - return std::format("{} {} {}", number_text(Point.x), number_text(Point.y), number_text(Point.z)); + static constexpr std::array events{"event0", "event1", "event2", "eventall0", "eventall1", "eventall2"}; + std::string result; + std::size_t at{0}; + bool skip{false}; + while (at < Attributes.size()) + { + auto const start{Attributes.find_first_not_of(' ', at)}; + if (start == std::string_view::npos) + break; + auto const end{std::min(Attributes.find(' ', start), Attributes.size())}; + auto const word{Attributes.substr(start, end - start)}; + at = end; + if (skip) + skip = false; + else if (std::ranges::find(events, word) != events.end()) + skip = true; + else + result += (result.empty() ? "" : " ") + std::string{word}; + } + return result; } -// a path as an entry has it: the start, its roll, the control vectors, the end, its roll, the radius -std::string path_text(segment_data const &Path) +// the pieces of the stretches of the old tracks named and written as each old track was: its name on the piece it ran into first, with +// its events, _2... on the others, its surface and its attributes, the path the way it ran; a piece out of any stretch as the generator +// names and writes it +void keep_old_tracks(line const &Line, std::vector &Pieces, generator_options const &Options) { - auto const &p{Path.points}; - return std::format("{} {} {} {} {} {} {}", point_text(p[segment_data::point::start]), number_text(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(Path.rolls[1]), number_text(Path.radius)); + auto const &splits{Line.hard_splits}; + auto const stretch_of = [&splits](generated_piece const &Piece) { + auto const after{std::ranges::upper_bound(splits, Piece.from + 1e-6, {}, &hard_split::chainage)}; + return after == splits.begin() ? splits.size() : static_cast(after - splits.begin()) - 1; + }; + std::size_t first{0}; + while (first < Pieces.size()) + { + auto const stretch{stretch_of(Pieces[first])}; + auto last{first + 1}; + while (last < Pieces.size() && stretch_of(Pieces[last]) == stretch) + ++last; + if (stretch < splits.size()) + { + auto const &old{splits[stretch].track}; + for (auto i = first; i < last; ++i) + { + auto &piece{Pieces[i]}; + auto const order{old.reversed ? last - 1 - i : i - first}; + if (false == old.name.empty() && false == old.continued) + piece.name = order == 0 ? old.name : std::format("{}_{}", old.name, order + 1); + if (old.reversed) + piece.path = geometry::reversed(piece.path); + auto attributes{order == 0 && false == old.continued ? old.attributes : without_events(old.attributes)}; + if (old.attributes.empty()) + attributes = std::format("velocity {}", number_text(Line.category.vmax)); + piece.text = entry_text(piece, old.surface.empty() ? Options.surface : old.surface, attributes); + } + } + first = last; + } } // the path the pieces make, from the start of the first to the end of the last, for the versions of the simulator which know only the path: @@ -516,13 +600,10 @@ generated_line generate(network const &Network, line_id const Line, generator_op result.errors.push_back(std::format("line {} is a track of turnout {}, made with it", number(Line), number(owner->turnout))); return result; } - if (auto const hard{Options.hard_splits.find(Line)}; hard != Options.hard_splits.end() && false == hard->second.empty()) - { - result.errors.push_back(std::format("line {}: the joints of old tracks as hard points of the pieces come with stage E6", number(Line))); - return result; - } cutter pieces{Network, *owner, Options}; result.pieces = pieces.pieces(result.errors); + if (false == owner->hard_splits.empty()) + keep_old_tracks(*owner, result.pieces, Options); return result; } diff --git a/editor/layout/layoutGenerator.hpp b/editor/layout/layoutGenerator.hpp index bafc99d93..1fb21c6d8 100644 --- a/editor/layout/layoutGenerator.hpp +++ b/editor/layout/layoutGenerator.hpp @@ -27,16 +27,14 @@ http://mozilla.org/MPL/2.0/. // 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. +// +// A line taken over from old tracks (stage E6) keeps their joints as hard points of the pieces (line::hard_splits, §10 "stare złącza jako +// twarde punkty podziału"): the pieces end at each, and the pieces of the stretch of an old track carry its name, its surface and its +// attributes (the speed, the events, the isolated circuits), each written the way the old track ran; where the stretch takes more than one +// piece, the one the old track ran into first keeps the name and the events, the others are _2, _3... with the rest. namespace layout { -// a joint of an old track the pieces are to end at, the name of the old track kept for the piece before it (stage E6) -struct hard_split -{ - double chainage{0.0}; - std::string name; -}; - struct generator_options { std::string prefix{"l"}; // of the names of the tracks: _ @@ -46,9 +44,6 @@ struct generator_options // what an entry has between the type and the path: the width, the friction, the sound distance, the quality, the damage, the // environment and the visibility with the materials std::string surface{"1.435 0.15 25 20 0 flat vis rail_screw_used1 4 tpbps-new2 0.2 0.5 1.1"}; - // the joints of the old tracks as hard points of the pieces, by line: stage E6 (§10, "stare złącza jako twarde punkty podziału"). - // Not yet: a line given any is refused, so nothing is made which would lose the names and the events of the old tracks - std::map> hard_splits; }; struct generated_piece diff --git a/editor/layout/layoutNetwork.hpp b/editor/layout/layoutNetwork.hpp index acbb4ce10..d7b315646 100644 --- a/editor/layout/layoutNetwork.hpp +++ b/editor/layout/layoutNetwork.hpp @@ -60,6 +60,29 @@ struct node bool operator==(node const &) const = default; }; +// an old track a line was taken over from (stage E6, editor-tracks-domain.md §5 and §10): its name and what its entry had besides the path, +// whether it ran against the line. The pieces the generator makes of the stretch of the line it covered carry them, so the names, the events +// and the isolated circuits the scenarios rely on stay where they were +struct old_track +{ + std::string name; // empty: one of no name (none), the pieces named by the generator + std::string surface; // the words between the length and the path: the width, the friction, ... the visibility and the materials + std::string attributes; // the words after the path up to endtrack: the speed, the events, the isolated circuits and the rest + bool reversed{false}; // it ran against the line + bool continued{false}; // a part of an old track a split cut off: named by the generator, its events left with the other part + + bool operator==(old_track const &) const = default; +}; + +// a joint of an old track as a hard point of the pieces: where along the line the stretch of the old track begins; the pieces end there +struct hard_split +{ + double chainage{0.0}; + old_track track; + + bool operator==(hard_split const &) const = default; +}; + struct line { line_id id{line_id::none}; @@ -73,6 +96,8 @@ struct line 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 + // the old tracks it was taken over from, by the chainage each begins at, in order; none for a line laid in the editor + std::vector hard_splits; // derived: the line laid between its nodes solved_line solved; diff --git a/editor/project/projectFile.cpp b/editor/project/projectFile.cpp index fdc6342a1..52b5cf724 100644 --- a/editor/project/projectFile.cpp +++ b/editor/project/projectFile.cpp @@ -344,6 +344,9 @@ std::string write(document const &Project) put(std::format("vertex {} at {} {} radius {} in {} out {} cant {}", layout::number(vertex.id), number(vertex.point.x), number(vertex.point.y), number(vertex.radius), number(vertex.transition_in), number(vertex.transition_out), number(vertex.cant))); for (auto const &grade : line.shape.grades) put(std::format("grade {} {} {}", number(grade.chainage), number(grade.elevation), number(grade.vertical_radius))); + for (auto const &split : line.hard_splits) + put(std::format("old {} {} reversed {} continued {} surface {} attributes {}", number(split.chainage), word(split.track.name), split.track.reversed ? 1 : 0, split.track.continued ? 1 : 0, + word(split.track.surface), word(split.track.attributes))); put("end"); } put("turnouts"); @@ -585,6 +588,22 @@ read_result read(std::string_view const Text) words.finish(); line->shape.grades.push_back(grade); } + else if (keyword == "old") + { + layout::hard_split split; + split.chainage = words.real("the chainage"); + split.track.name = unword(words.next("the name")); + words.expect("reversed"); + split.track.reversed = words.choice("reversed", std::array{"0", "1"}) == 1; + words.expect("continued"); + split.track.continued = words.choice("continued", std::array{"0", "1"}) == 1; + words.expect("surface"); + split.track.surface = unword(words.next("the surface")); + words.expect("attributes"); + split.track.attributes = unword(words.next("the attributes")); + words.finish(); + line->hard_splits.push_back(std::move(split)); + } else if (keyword == "end") { words.finish(); diff --git a/editor/project/projectFile.hpp b/editor/project/projectFile.hpp index db7a6dd4f..594bd0ff6 100644 --- a/editor/project/projectFile.hpp +++ b/editor/project/projectFile.hpp @@ -35,6 +35,9 @@ http://mozilla.org/MPL/2.0/. // turnout profile // vertex at radius in out cant // grade +// old reversed <0|1> continued <0|1> surface attributes an old track the line was taken over from +// (E6): where its stretch begins, its name, whether it ran against the line, whether it's +// a part a split cut off, the words of its entry before the path and after it // end // turnouts // turnout type hand <1|-1> direction <1|-1> start main diverging chainage diff --git a/tests/unit/test_layout_generator.cpp b/tests/unit/test_layout_generator.cpp index 04db8f61a..08f8052d3 100644 --- a/tests/unit/test_layout_generator.cpp +++ b/tests/unit/test_layout_generator.cpp @@ -8,6 +8,7 @@ #include "layout_fixture.h" #include "track_text.h" #include "editor/layout/layoutGenerator.hpp" +#include "editor/project/projectFile.hpp" #include #include @@ -302,16 +303,48 @@ TEST_CASE("layout generator: the names come from the ids of the line and its ver CHECK(layout::generate(model.state(), id).text() == after.text()); } -TEST_CASE("layout generator: the joints of old tracks as hard points of the pieces are stage E6, a line given them isn't made") { +TEST_CASE("layout generator: the joints of old tracks as hard points of the pieces, their names, attributes and the way they ran kept") { auto const model{laid(three_curves())}; auto const id{model.state().lines().begin()->first}; - layout::generator_options options; - options.hard_splits[id] = {{350.0, "old_track"}}; - auto const made{layout::generate(model.state(), id, options)}; - CHECK(made.pieces.empty()); - REQUIRE(made.errors.size() == 1); - CHECK(made.errors.front().find("E6") != std::string::npos); - std::vector errors; - CHECK(layout::generate_all(model.state(), {}, errors).find("endtrack") != std::string::npos); - CHECK(errors.empty()); + auto network{model.state()}; + auto &line{const_cast(*network.find(id))}; + auto const length{line.solved.length}; + // three old tracks: one of a name with its events and circuit, one of none, one of a name which ran against the line + layout::old_track first{"tor_a", "1.435 0.25 25.0 20 0 flat vis rail 4 bed 0.2 0.5 1.1", "velocity 60 event1 ev_a isolated obw_a", false, false}; + layout::old_track second{"", "", "velocity 70 event2 ev_b", false, false}; + layout::old_track third{"tor_c", "", "velocity 80 event1 ev_c", true, false}; + line.hard_splits = {{0.0, first}, {350.0, second}, {length - 100.0, third}}; + auto const made{layout::generate(network, id)}; + REQUIRE(made.errors.empty()); + // the pieces end at the joints + auto const ends_at = [&](double const Chainage) { return std::ranges::any_of(made.pieces, [&](layout::generated_piece const &Piece) { return std::abs(Piece.to - Chainage) < 1e-9; }); }; + CHECK(ends_at(350.0)); + CHECK(ends_at(length - 100.0)); + std::map texts; + for (auto const &piece : made.pieces) { + CHECK(texts.emplace(piece.name, piece.text).second); + } + // the first old track: its name on its first piece with all of its attributes, the others numbered on without its events + REQUIRE(texts.contains("tor_a")); + CHECK(texts["tor_a"].find("1.435 0.25 25.0 20 0 flat vis rail 4 bed 0.2 0.5 1.1") != std::string::npos); + CHECK(texts["tor_a"].ends_with(" velocity 60 event1 ev_a isolated obw_a endtrack\n")); + for (auto const &[name, text] : texts) { + if (name.starts_with("tor_a_")) { + CHECK(text.ends_with(" velocity 60 isolated obw_a endtrack\n")); + } + } + // of no name: named by the generator, its events on the first piece only + auto const events_b{std::ranges::count_if(texts, [](auto const &Each) { return Each.second.find("event2 ev_b") != std::string::npos; })}; + CHECK(events_b == 1); + // the last one ran against the line: its piece written from the end of the line, the named one where it started + REQUIRE(texts.contains("tor_c")); + auto const named_c{std::ranges::find_if(made.pieces, [](layout::generated_piece const &Piece) { return Piece.name == "tor_c"; })}; + CHECK(std::abs(named_c->to - length) < 1e-9); + CHECK(named_c->path.points[segment_data::point::start] == network.find(line.end)->position); + // written and read back as the line has them + project::document project; + project.network = network; + auto const read{project::read(project::write(project))}; + REQUIRE(read.project.has_value()); + CHECK(read.project->network.find(id)->hard_splits == line.hard_splits); }