18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 12:57:06 +01:00

feat(editor): the //$r switches of an old scenery on the lines of its alignments made turnouts of its project

E5, what E3 left (editor-tracks-domain.md §9 "Migracja"): the //$r records were kept in the project file as a block a file; the switches
among them which the domain can lay now become its turnouts when the map is first opened.

- A switch or a double switch of the editor becomes a turnout where its record names a type of the catalogue (its type, rail, radius, ratio
  and a, b of an ordinary or a symmetric one; the first of the catalogue's order where a movable nose is the only difference, which the
  record doesn't keep), its start lies on a line made of an alignment within a centimetre and its direction along the line or against it,
  and its main track is bent as the record says the line is there (straight, or the radius within 5 %). It's laid into the line by the
  command insert_turnout, the hand of the record, with edit_policy::no_new_block: the old line may break the norms where nobody touched
  it, the turnout may add no BLOCK finding to it.
- Its record goes on its own into the project with the main track of the turnout (as an //$h record keeps the line it became); the rest of
  the block stays as written, the comments with it, why each switch didn't become a turnout with it: a slip or a crossing (their records
  don't tell the side of the crossing track), a connection (the solver of the connections lays it anew), a lifted outer track, no type of
  the catalogue, no line under it, a bending other than the line's, or the reasons the command refused it for. A block none of whose
  switches became one is kept as before, with the reasons.

Tests: U: an alignment of data/records with five //$r lines: two switches on its straight, along it and against it, to the left and to
the right, made turnouts of their types at their starts (the structure whole, no reason); one beside the line, one of no type of the
catalogue and a connection kept in the block with the comment, each with why; the two records each with the main track of its turnout;
the project written and read back the same. The records of data/records and of the map of data/project/migration (no switch on a line)
kept whole as before. Unit tests 1096 -> 1097.
This commit is contained in:
maj00r
2026-11-23 14:49:14 +01:00
parent efcc28b0ed
commit 84dca7cccb
3 changed files with 254 additions and 2 deletions
+179
View File
@@ -12,6 +12,9 @@ http://mozilla.org/MPL/2.0/.
#include "editor/editorAlignment.hpp"
#include "editor/editorGeometry.hpp"
#include "editor/editorRecords.hpp"
#include "editor/editorSwitchDesign.hpp"
#include "editor/layout/layoutCommands.hpp"
#include "editor/layout/layoutTurnout.hpp"
#include "editor/project/sceneryText.hpp"
#include <algorithm>
@@ -19,6 +22,7 @@ http://mozilla.org/MPL/2.0/.
#include <format>
#include <fstream>
#include <iterator>
#include <limits>
#include <optional>
namespace project
@@ -121,6 +125,174 @@ struct profile_candidate
std::string why;
};
// the //$r lines of a file, the switches of the editor: kept as one record till they become turnouts
struct switch_block
{
std::size_t legacy{0};
std::string file;
scene::layer_context context;
std::vector<std::string> lines;
};
double constexpr kOnLine{0.01}; // m: the start of a switch lies on a line within this
double constexpr kAlong{1e-3}; // of the sine: its direction runs along the line within this
// the type of the catalogue the switch of the editor is of: its kind, type, rail, radius, ratio and a (and b of an ordinary one) as its record
// gives them, the first of them in the order of the catalogue; empty where none is
std::string catalogue_type(switch_store::record const &Record)
{
for (auto const &entry : switch_design::catalogue_entries())
{
if (entry.type != Record.type || entry.rail != Record.rail || std::abs(entry.ratio - Record.ratio) > 0.01 || std::abs(entry.a - Record.a) > 0.002)
continue;
if (entry.type != "Skrz" && std::abs(entry.radius - Record.radius) > 0.5)
continue;
if ((entry.type == "Rz" || entry.type == "Rls") && std::abs(entry.b - Record.b) > 0.002)
continue;
return entry.name;
}
return {};
}
// the line of its own the switch starts on, along it or against it: its chainage there
struct switch_place
{
layout::line_id line{layout::line_id::none};
double chainage{0.0};
int direction{1};
};
std::optional<switch_place> place_of(layout::network const &Network, switch_store::record const &Record)
{
glm::dvec2 const start{Record.start.x, Record.start.z};
for (auto const &[id, line] : Network.lines())
{
if (line.turnout != layout::turnout_id::none || false == line.solved.valid())
continue;
auto const &solved{line.solved};
auto s{0.0};
auto nearest{std::numeric_limits<double>::max()};
auto const count{std::max(2, static_cast<int>(std::ceil(solved.length / 2.0)))};
for (int i = 0; i <= count; ++i)
if (auto const d{glm::distance(solved.position(solved.length * i / count), start)}; d < nearest)
{
nearest = d;
s = solved.length * i / count;
}
for (int i = 0; i < 20; ++i)
s = std::clamp(s + glm::dot(start - solved.position(s), glm::normalize(solved.tangent(s))), 0.0, solved.length);
auto const tangent{glm::normalize(solved.tangent(s))};
if (glm::distance(solved.position(s), start) > kOnLine || std::abs(geometry::cross(tangent, Record.direction)) > kAlong)
continue;
return switch_place{id, s, glm::dot(tangent, Record.direction) > 0.0 ? 1 : -1};
}
return std::nullopt;
}
// the switch of the editor made a turnout of the domain where it can be: of a type of the catalogue, on a line of the domain, bent as that
// line is; the main track of the turnout, or none and why not
layout::line_id switch_turnout(layout::network &Network, switch_store::record const &Record, std::string &Why)
{
if (Record.kind == "connection")
{
Why = "a connection of switches, which the solver of the connections lays anew";
return layout::line_id::none;
}
if (Record.lifted())
{
Why = "its outer track lifted, which the domain has no place for";
return layout::line_id::none;
}
auto const type{catalogue_type(Record)};
if (type.empty())
{
Why = std::format("no {} of the catalogue of {} R {:.0f} 1:{:.3f} a {:.3f} b {:.3f}", Record.type, Record.rail, Record.radius, Record.ratio, Record.a, Record.b);
return layout::line_id::none;
}
if (Record.hand != 'L' && Record.hand != 'R')
{
Why = std::format("{}: the side it leaves to isn't in its record", type);
return layout::line_id::none;
}
auto const place{place_of(Network, Record)};
if (false == place.has_value())
{
Why = std::format("{}: its start lies on no line of the domain, along it", type);
return layout::line_id::none;
}
// bent as its record says, the line under it bent so
auto const *line{Network.find(place->line)};
auto const length{layout::turnout_type_of(type)->length};
auto const curvature{std::abs(line->solved.curvature(std::clamp(place->chainage + place->direction * 0.5 * length, 0.0, line->solved.length)))};
if ((Record.main_radius == 0.0) != (curvature < 1e-6) || (Record.main_radius != 0.0 && std::abs(1.0 / curvature - std::abs(Record.main_radius)) > 0.05 * std::abs(Record.main_radius)))
{
Why = std::format("{}: bent to R {:.0f} m as written, where its line runs at R {:.0f} m", type, Record.main_radius, curvature > 1e-9 ? 1.0 / curvature : 0.0);
return layout::line_id::none;
}
layout::model model{Network};
auto const made{model.execute(layout::insert_turnout{place->line, place->chainage, type, Record.hand == 'L' ? 1 : -1, place->direction}, layout::edit_policy::no_new_block)};
if (false == made.done)
{
Why = type + ":";
for (auto const &reason : made.reasons)
Why += " " + reason + ";";
return layout::line_id::none;
}
Network = model.state();
return Network.find(made.turnouts.front())->main;
}
// the switches of the blocks of //$r lines made turnouts: each one which became one a record of its own with its main track, the rest kept
// as it was in the block, why not with it
void migrate_switches(document &Project, std::vector<switch_block> const &Blocks, std::vector<std::string> &Notes)
{
// from the last block on, the records of one put in place of it
for (auto block{Blocks.rbegin()}; block != Blocks.rend(); ++block)
{
std::vector<std::string> kept;
std::vector<std::string> whys;
std::vector<legacy_record> made;
for (auto const &text : block->lines)
{
auto const records{editor_records::read_switches({text}, block->context, null_handle)};
if (records.empty())
{
kept.push_back(text);
continue;
}
std::string why;
auto const main{switch_turnout(Project.network, records.front(), why)};
if (main == layout::line_id::none)
{
kept.push_back(text);
whys.push_back(std::format("{}: {}", records.front().name, why));
continue;
}
made.push_back({'r', block->file, main, {}, {text}});
Notes.push_back(std::format("{}: switch {} became turnout {}", block->file, records.front().name, layout::number(Project.network.find(main)->turnout)));
}
auto &legacy{Project.legacy};
auto const at{legacy.begin() + static_cast<std::ptrdiff_t>(block->legacy)};
auto const switches_left{std::ranges::any_of(kept, [&](std::string const &Text) { return false == editor_records::read_switches({Text}, block->context, null_handle).empty(); })};
if (made.empty() || switches_left)
{
at->lines = kept;
for (auto const &why : whys)
at->why += (at->why.empty() ? "" : "; ") + why;
if (false == whys.empty())
Notes.push_back(std::format("{}: switches kept as they were: {}", block->file, at->why));
legacy.insert(at + 1, made.begin(), made.end());
}
else
{
// nothing but the comments left of the block: they go with the first switch which became a turnout
made.front().lines.insert(made.front().lines.begin(), kept.begin(), kept.end());
*at = made.front();
legacy.insert(at + 1, made.begin() + 1, made.end());
}
}
}
// what in the design the domain hasn't a place for, or lays otherwise; empty if nothing
std::string unfit(alignment_candidate const &Candidate)
{
@@ -329,6 +501,7 @@ migration migrate(std::vector<marked_file> const &Files, norms::area_map const &
project.network.areas = Areas;
std::vector<alignment_candidate> alignments;
std::vector<profile_candidate> profiles;
std::vector<switch_block> switches;
// the records kept as they were, each kind of each file; the alignments and the grade lines one by one
for (auto const &file : Files)
@@ -367,7 +540,11 @@ migration migrate(std::vector<marked_file> const &Files, norms::area_map const &
profiles.push_back(std::move(candidate));
}
else
{
if (mark == 'r')
switches.push_back({project.legacy.size(), file.file, file.context, lines});
project.legacy.push_back({mark, file.file, layout::line_id::none, {}, lines});
}
}
for (auto const &[mark, lines] : file.lines)
{
@@ -474,6 +651,8 @@ migration migrate(std::vector<marked_file> const &Files, norms::area_map const &
project.legacy[profile.legacy].why = profile.why;
result.notes.push_back(std::format("{}: a grade line kept as it was: {}", project.legacy[profile.legacy].file, profile.why));
}
// the switches on the lines made of the alignments become their turnouts (E5)
migrate_switches(project, switches, result.notes);
return result;
}
+7 -2
View File
@@ -28,8 +28,13 @@ http://mozilla.org/MPL/2.0/.
// kept as it was, with the line it became: the points clicked, the old norms, the pieces, the fit have no place in the domain.
// //$p a grade line designed along such a line: its breaks with their vertical curves as the breaks of the line, the heights of its
// ends as those of the nodes. Not one with a break with no vertical curve, or along no line of an alignment. Kept as it was too.
// //$r switches (E5), //$k the kilometrage of the railway lines, //$t the tracks fitted, //$w the tracks sharing a grade line, //$b
// the objects bound to the tracks (E6), //$s the stations (E7): no place in the domain yet, kept as they were.
// //$r a switch, a double switch of the editor on a line made of an alignment, of a type of the catalogue (its type, rail, radius, ratio,
// a and b), along the line or against it, bent as its record says the line is: a turnout laid into the line, as the command lays
// it, the norms not refusing what the old line breaks already; its record kept on its own with the main track of the turnout.
// Not a slip nor a crossing (their records don't tell the side of the crossing track), a connection (the solver of the
// connections lays it anew), a lifted one, one of no type of the catalogue or on no line: kept in the block of the file, why with it.
// //$k the kilometrage of the railway lines, //$t the tracks fitted, //$w the tracks sharing a grade line, //$b the objects bound to
// the tracks (E6), //$s the stations (E7): no place in the domain yet, kept as they were.
// //$c, //$o, //$z the camera, the orthophoto and the area of the downloads: what the editor keeps of itself in the scenery, not the
// track geometry; not taken.
namespace project
+68
View File
@@ -9,10 +9,12 @@
#include "editor/editorAlignment.hpp"
#include "editor/editorRecords.hpp"
#include "editor/project/projectMigration.hpp"
#include "layout_fixture.h"
#include <filesystem>
#include <fstream>
#include <iterator>
#include <map>
#include <string>
namespace {
@@ -245,3 +247,69 @@ TEST_CASE("project migration: a map opened for the first time gets its project o
CHECK(project::same(*again.project.project, project));
round_trip(project);
}
TEST_CASE("project migration: the //$r switches on the lines made of the alignments become turnouts, the rest kept as written with why") {
// the first alignment of data/records: a straight north for 570 m, then a curve to the east; switches on its straight both ways, one
// beside it, one of no type of the catalogue and a connection
auto alignment{file_text(std::string{EU07_UNIT_TESTS_DATA} + "/records/alignment.scn")};
// up to the end of its record, not to its ends
auto const end{alignment.find("//$h end\r\n") != std::string::npos ? alignment.find("//$h end\r\n") : alignment.find("//$h end\n")};
REQUIRE(end != std::string::npos);
alignment = alignment.substr(0, end + 8) + "\n";
std::vector<std::string> const switches{
"switch z1 Rz 60E1 500.000 12.000 L 20.797 20.797 0.000 parts 1 z1 start 0.0000 0.0000 200.0000 dir 0.000000000 1.000000000 km - number - source designed",
"switch z2 Rz 60E1 190.000 9.000 R 10.523 16.615 0.000 parts 1 z2 start 0.0000 0.0000 450.0000 dir 0.000000000 -1.000000000 km - number - source designed",
"switch z3 Rz 60E1 500.000 12.000 L 20.797 20.797 0.000 parts 1 z3 start 50.0000 0.0000 300.0000 dir 0.000000000 1.000000000 km - number - source designed",
"switch z4 Rz 60E1 777.000 13.000 L 1.000 1.000 0.000 parts 1 z4 start 0.0000 0.0000 100.0000 dir 0.000000000 1.000000000 km - number - source designed",
"connection c1 Rz 60E1 500.000 12.000 R 20.797 20.797 0.000 parts 2 c3 c4 start 0.0000 0.0000 350.0000 dir 0.000000000 1.000000000 km - number - source designed",
};
std::string text{alignment + "//$r # switches placed in the track editor, the simulation skips these lines\n"};
for (auto const &line : switches) {
text += "//$r " + line + "\n";
}
auto const migrated{project::migrate({{"switches.scn", {}, project::marked_lines(text)}})};
CAPTURE(notes_of(migrated.notes));
auto const &project{migrated.project};
auto const &network{project.network};
REQUIRE(network.turnouts().size() == 2);
// the old design breaks the norms in its curve, which the turnouts may not add to (edit_policy::no_new_block)
test::layout_fixture::check_structure(network);
CHECK(layout::check_all(network).reasons.empty());
// the two on the straight: their types, hands and ways along the line, their starts where the records put them
std::map<std::string, layout::turnout const *> by_type;
for (auto const &[id, turnout] : network.turnouts()) {
by_type[turnout.type] = &turnout;
}
REQUIRE(by_type.contains("Rz 60E1-500-1:12"));
REQUIRE(by_type.contains("Rz 60E1-190-1:9"));
auto const &first{*by_type.at("Rz 60E1-500-1:12")};
auto const &second{*by_type.at("Rz 60E1-190-1:9")};
CHECK(first.hand == 1);
CHECK(first.direction == 1);
CHECK(second.hand == -1);
CHECK(second.direction == -1);
CHECK(glm::distance(geometry::plan_of(network.find(first.start)->position), glm::dvec2{0.0, 200.0}) < 1e-9);
CHECK(glm::distance(geometry::plan_of(network.find(second.start)->position), glm::dvec2{0.0, 450.0}) < 1e-9);
// each a record of its own with its main track; the rest of the block kept as written, why with it
std::vector<project::legacy_record const *> records;
for (auto const &record : project.legacy) {
if (record.mark == 'r') {
records.push_back(&record);
}
}
REQUIRE(records.size() == 3);
auto const &kept{*records[0]};
CHECK(kept.line == line_id::none);
CHECK(kept.lines == std::vector<std::string>{"# switches placed in the track editor, the simulation skips these lines", switches[2], switches[3], switches[4]});
CAPTURE(kept.why);
CHECK(kept.why.find("z3: Rz 60E1-500-1:12: its start lies on no line of the domain") != std::string::npos);
CHECK(kept.why.find("z4: no Rz of the catalogue") != std::string::npos);
CHECK(kept.why.find("c1: a connection of switches") != std::string::npos);
CHECK(records[1]->lines == std::vector<std::string>{switches[0]});
CHECK(records[1]->line == first.main);
CHECK(records[2]->lines == std::vector<std::string>{switches[1]});
CHECK(records[2]->line == second.main);
// all of the lines of the block in the project, written and read back the same
CHECK(editor_records::read_switches(kept.lines, {}, 0).size() == 3);
round_trip(project);
}