diff --git a/editor/layout/layoutAnchors.cpp b/editor/layout/layoutAnchors.cpp index 86fc8b9b2..a4cf79271 100644 --- a/editor/layout/layoutAnchors.cpp +++ b/editor/layout/layoutAnchors.cpp @@ -323,6 +323,19 @@ void anchor_again(network const &Network, anchor &Anchor, placement const &Where Anchor.points[i] = point_along(*found); } +// the anchor's line is gone, or ends before the anchor or one of its points now: split, its head part keeping its id (split_at, +// insert_turnout), the anchor is on the line beyond it. Kept at its chainage, it was clamped onto the end of the head part +bool lost(network const &After, anchor const &Anchor) +{ + auto const *line{After.find(Anchor.line)}; + if (line == nullptr) + return true; + if (false == line->solved.valid()) + return false; + auto const beyond = [line](anchor_point const &Point) { return Point.chainage > line->solved.length + 1e-6; }; + return beyond(Anchor.at) || std::ranges::any_of(Anchor.points, beyond); +} + bool same_place(placement const &A, placement const &B) { if (glm::distance(A.location, B.location) > 1e-4 || std::abs(circular(A.yaw - B.yaw)) > 1e-6 || A.points.size() != B.points.size()) @@ -375,7 +388,7 @@ std::vector follow(network const &Before, network const &After, std::vect { auto &each{Anchors[i]}; auto const was{place(Before, each)}; - if (After.find(each.line) == nullptr && was.has_value()) + if (was.has_value() && lost(After, each)) anchor_again(After, each, *was); auto const now{place(After, each)}; if (now.has_value() && (false == was.has_value() || false == same_place(*was, *now))) diff --git a/editor/layout/layoutAnchors.hpp b/editor/layout/layoutAnchors.hpp index db26787fb..48c895937 100644 --- a/editor/layout/layoutAnchors.hpp +++ b/editor/layout/layoutAnchors.hpp @@ -92,9 +92,9 @@ struct placement }; std::optional place(network const &Network, anchor const &Anchor); -// the anchors following a change of the network from Before to After: an anchor whose line is there still keeps its chainage (on the -// length the line has now); one whose line is gone (split, joined, taken by a turnout) is anchored again where it stood before, to the line -// there now. The anchors whose objects move, with where to, in the order of the anchors; the anchors updated +// the anchors following a change of the network from Before to After: an anchor whose line is there still keeps its chainage; one whose +// line is gone (joined, taken by a turnout), or ends before it now (split, the head part keeping the id), is anchored again where it stood +// before, to the line there now. The anchors whose objects move, with where to, in the order of the anchors; the anchors updated struct moved { std::size_t anchor{0}; diff --git a/editor/layout/layoutCommands.cpp b/editor/layout/layoutCommands.cpp index 186998962..7a54ab976 100644 --- a/editor/layout/layoutCommands.cpp +++ b/editor/layout/layoutCommands.cpp @@ -411,6 +411,10 @@ 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}); + // the part of a line with no old track where it starts stays out of the old tracks: else the last old track of the first line ran + // on over it, its name and its isolated circuit with it + if (false == first->hard_splits.empty() && (second->hard_splits.empty() || second->hard_splits.front().chainage > 1e-6)) + merged.hard_splits.push_back({a.length, old_track{}}); for (auto const &split : second->hard_splits) merged.hard_splits.push_back({split.chainage + a.length, split.track}); auto const &before{first->shape.vertices}; @@ -1558,18 +1562,21 @@ outcome model::execute(command const &Command, edit_policy const Policy) std::optional earlier; if (Policy == edit_policy::no_new_block) earlier = m_network; - // the line laid as it was, for its joints of old tracks to follow it - auto const line{reshaped(Command)}; - std::optional before; - if (auto const *shaped{line.has_value() ? m_network.find(*line) : nullptr}; shaped != nullptr && false == shaped->hard_splits.empty()) - before = shaped->solved; + // the lines with joints of old tracks laid as they were, for the joints to follow them: the one the command reshapes, and the ones + // settle() lays again with it, a line going on from the diverging end of a turnout on it say + std::map before; + if (reshaped(Command).has_value()) + for (auto const &[id, each] : m_network.lines()) + if (false == each.hard_splits.empty()) + before.emplace(id, each.solved); transaction change{m_network}; auto why{std::visit([&](auto const &Each) { return propose(change, result, Each); }, Command)}; if (why.empty()) { settle(change); - if (before.has_value()) - follow_joints(change, *line, *before); + for (auto const id : change.touched_lines()) + if (auto const laid{before.find(id)}; laid != before.end()) + follow_joints(change, id, laid->second); 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()) { diff --git a/editor/layout/layoutGenerator.cpp b/editor/layout/layoutGenerator.cpp index 29853c21d..7be9bde1a 100644 --- a/editor/layout/layoutGenerator.cpp +++ b/editor/layout/layoutGenerator.cpp @@ -18,6 +18,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include namespace layout @@ -471,9 +472,32 @@ std::string without_events(std::string_view const Attributes) // 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 +// the letters made small, as the simulator makes the names +std::string small_letters(std::string_view const Text) +{ + std::string result; + std::ranges::transform(Text, std::back_inserter(result), [](char const C) { return static_cast(std::tolower(static_cast(C))); }); + return result; +} + +// the name of a piece of an old track after its first one, _, the number on past the names taken: by an old track of the +// line called so, or by a piece named before +std::string piece_name(std::string const &Name, std::size_t Number, std::set &Taken) +{ + auto name{std::format("{}_{}", Name, Number)}; + while (Taken.contains(small_letters(name))) + name = std::format("{}_{}", Name, ++Number); + Taken.insert(small_letters(name)); + return name; +} + void keep_old_tracks(line const &Line, std::vector &Pieces, generator_options const &Options) { auto const &splits{Line.hard_splits}; + std::set taken; + for (auto const &split : splits) + if (false == split.track.name.empty()) + taken.insert(small_letters(split.track.name)); 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; @@ -495,7 +519,7 @@ void keep_old_tracks(line const &Line, std::vector &Pieces, gen // 0 at the Point1 of the old track 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); + piece.name = order == 0 ? old.name : piece_name(old.name, order + 1, taken); if (old.reversed) piece.path = geometry::reversed(piece.path); auto attributes{old.continued ? without_events(old.attributes) : attributes_of_piece(old.attributes, {true, order == 0, order + 1 == count})}; diff --git a/editor/layout/layoutLegacy.cpp b/editor/layout/layoutLegacy.cpp index 0b9ba82a6..2216cea7f 100644 --- a/editor/layout/layoutLegacy.cpp +++ b/editor/layout/layoutLegacy.cpp @@ -164,7 +164,7 @@ double legacy_track::velocity() const std::istringstream words{attributes}; std::string word; while (words >> word) - if (word == "velocity") + if (lowered(word) == "velocity") { double value{0.0}; if (words >> value) @@ -304,7 +304,7 @@ takeover_plan plan_takeover(std::vector const &Tracks, std::vector auto const &track{Tracks[chain.links[k].track]}; fastest = std::max(fastest, std::abs(track.velocity())); old_track old; - old.name = track.name == "none" ? std::string{} : track.name; + old.name = lowered(track.name) == "none" ? std::string{} : track.name; old.surface = track.surface; old.attributes = track.attributes; old.reversed = false == chain.links[k].forward; diff --git a/editor/project/projectFile.cpp b/editor/project/projectFile.cpp index d710ff6f8..192c2cbfd 100644 --- a/editor/project/projectFile.cpp +++ b/editor/project/projectFile.cpp @@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include #include #include #include @@ -706,6 +707,9 @@ read_result read(std::string_view const Text) words.expect("attributes"); split.track.attributes = unword(words.next("the attributes")); words.finish(); + // the joints along the line in their order: the generator looks them up so + if (false == words.failed() && (false == std::isfinite(split.chainage) || split.chainage < 0.0 || (false == line->hard_splits.empty() && split.chainage < line->hard_splits.back().chainage))) + words.fail(std::format("the old track {} at {} m is off the line, or out of the order of the ones before it", split.track.name, split.chainage)); line->hard_splits.push_back(std::move(split)); } else if (keyword == "end") diff --git a/tests/unit/test_takeover_scenery.cpp b/tests/unit/test_takeover_scenery.cpp index 35312bbd0..bb4911358 100644 --- a/tests/unit/test_takeover_scenery.cpp +++ b/tests/unit/test_takeover_scenery.cpp @@ -292,3 +292,58 @@ TEST_CASE("takeover scenery: the anchors of the lineside objects, with their con CHECK(read.project->anchors.size() == anchors.size()); CHECK(project::write(*read.project) == project::write(document)); } + +TEST_CASE("takeover scenery: an object past where its line is split goes onto the part beyond, not onto the end of the part before") { + // a straight of 500 m, a signal 3 m beside it at 300 m; split at 100 m, the part before it keeping the id of the line + layout::model model; + layout::create_line straight; + straight.start = {0.0, 100.0, 0.0}; + straight.end = {500.0, 100.0, 0.0}; + straight.category.vmax = 80.0; + auto const made{model.execute(straight)}; + REQUIRE(made.done); + auto const line{made.lines.front()}; + layout::anchor signal; + signal.id = 1; + signal.kind = "model"; + signal.name = "sem"; + signal.line = line; + signal.at.chainage = 300.0; + signal.at.offset = 3.0; + auto const was{layout::place(model.state(), signal)}; + REQUIRE(was.has_value()); + std::vector anchors{signal}; + auto const before{model.state()}; + REQUIRE(model.execute(layout::split_line{line, 100.0}).done); + auto const moved{layout::follow(before, model.state(), anchors)}; + // where it stood, along the part beyond the split: clamped to the end of the part before, it went 200 m back + CHECK(moved.empty()); + CHECK(anchors.front().line != line); + CHECK(anchors.front().at.chainage == doctest::Approx(200.0).epsilon(1e-6)); + auto const now{layout::place(model.state(), anchors.front())}; + REQUIRE(now.has_value()); + CHECK(glm::distance(now->location, was->location) < 1e-6); +} + +TEST_CASE("takeover scenery: the joints of the old tracks read back on their line and in their order") { + layout::model model; + layout::create_line straight; + straight.start = {0.0, 100.0, 0.0}; + straight.end = {500.0, 100.0, 0.0}; + straight.category.vmax = 80.0; + REQUIRE(model.execute(straight).done); + project::document document; + document.network = model.state(); + auto const text{project::write(document)}; + auto const with = [&text](std::string const &Joints) { + auto changed{text}; + auto const at{changed.find("\nend\n")}; + REQUIRE(at != std::string::npos); + changed.insert(at + 1, Joints); + return project::read(changed); + }; + CHECK(with("old 0 a reversed 0 continued 0 surface - attributes -\nold 120 b reversed 0 continued 0 surface - attributes -\n").project.has_value()); + CHECK_FALSE(with("old 120 a reversed 0 continued 0 surface - attributes -\nold 20 b reversed 0 continued 0 surface - attributes -\n").project.has_value()); + CHECK_FALSE(with("old -5 a reversed 0 continued 0 surface - attributes -\n").project.has_value()); + CHECK_FALSE(with("old nan a reversed 0 continued 0 surface - attributes -\n").project.has_value()); +}