18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00
Files
maszyna/tests/unit/test_layout_editing.cpp
T
maj00r 6c2d467c5a feat(editor): the switches of the switch tool on the lines of the model its turnouts, inserted, moved, changed and taken out by its commands
Continues E4a (editor-tracks-domain.md §7, §8; stage E4b): with the editing on the model on, a switch dragged on a track of a line of
the model is a turnout of the catalogue of the model, and so on, each a command, refused with the reasons or one step of the history.
The switches of the old tracks are laid the old way.

- The drag of the switch tool, let go on a line of the model (on a straight or along a curve), is insert_turnout: the type of the
  catalogue the template is of (a template of the user's own isn't), its start where it was pressed, its chainage on the line
  (layout::chainage_of), the side of the drag its hand, the way it was dragged its direction. While it's dragged the panel tells in its
  red line why the model would refuse it, tried on a copy; let go, a refusal is told there with the value, the limit and the paragraph
  ("(9.6 pkt 16)"), nothing changed. Laid: its switch track made by the generator (t<n>), selected, the panel placed, its speed told.
- A turnout of the model selected: the inspector shows its type, side, speeds and bending, the templates of its kind of the catalogue
  (change_turnout), the other side (change_turnout of the hand), moved by a distance (move_turnout), taken out (remove_turnout). The
  slide of the switch tool moves it, another template changes it, Del takes it out (after the scenarios are looked through), each a
  command. Plan on a track of a turnout doesn't open: the turnout is edited through itself.
- A crossover dragged between two lines of the model is connect_tracks of the solver of the connections; refused, the solver's best
  proposal is told with the reasons ("Proposal: Rz 60E1-300-1:9 needs them 4.498 m apart").
- The tracks after a command: of a turnout one of whose tracks changed, its tracks of the simulator made again whole, kept by its main
  track (kind switch, the plain tracks of a double switch, a slip or a crossing with them); written with the scenery as the generator
  made them, recorded in the project with their kind and the file each went into (session::record_written of pieces of several files),
  found again when the map is opened, nothing taken for changed by hand. project::session::generate(turnout_id).

Self-test: modelturnoutside <track>; expect modelproposal <holds|lacks> <text>, expect handlines <count>; expect modelends checks the
paths of the switch tracks at the numbers of the nodes; a crossover of the connection command between two lines of the model goes to
the solver of the model. The cases m_8_turnout (a turnout dragged on, taken back and brought again, moved, given another type and side,
taken out by Del; one 2 m off the end of a cant ramp refused, nothing changed; saved and found after a reopen) and m_9_crossover (a
crossover between lines 4.75 m apart, one between lines 4 m apart refused with the proposal).

Tests: U: the chainage of a point on a line and its distance; the switch track of a turnout made by the session, the tracks of a line
written into two files recorded.

lang/editor_pl.po: the Polish of the new texts.
2026-11-23 18:48:19 +01:00

126 lines
4.9 KiB
C++

// the pieces of the model the editor's tools edit it through (editor/layout, stage E4b; editor-tracks-domain.md §8): the chainage of a point
// on a line, a vertex moved and changed in one command, a line laid on from the free end of another one sharing its node, the line alongside
// a line, its arcs concentric and the spacing in its curves widened; and the project session making the tracks of a turnout and recording
// the tracks of a line written into more files than one
#include "stdafx.h"
#include "doctest.h"
#include "layout_fixture.h"
#include "scratch.h"
#include "editor/layout/layoutTurnout.hpp"
#include "editor/project/projectSession.hpp"
#include <cmath>
#include <set>
namespace {
using namespace test::layout_fixture;
using layout::line_id;
using layout::node_id;
layout::create_line straight_along_z(double const X, double const From, double const To) {
layout::create_line result;
result.start = {X, 10.0, From};
result.end = {X, 10.0, To};
result.category = line_class();
return result;
}
line_id last_line(layout::model const &Model) {
REQUIRE_FALSE(Model.state().lines().empty());
return std::prev(Model.state().lines().end())->first;
}
// the free node of the line at the point
node_id node_at(layout::network const &Network, glm::dvec3 const &Point) {
for (auto const &[id, node] : Network.nodes()) {
if (glm::distance(node.position, Point) < 1e-9) {
return id;
}
}
FAIL("no node there");
return node_id::none;
}
} // namespace
TEST_CASE("layout editing: the chainage of a point of the plan on a line, and how far the point is from it") {
layout::model model;
REQUIRE(model.execute(three_curves()).done);
auto const &line{model.state().lines().begin()->second};
auto const &solved{line.solved};
for (double const at : {0.0, 12.5, 333.3, 0.5 * solved.length, solved.length - 0.25, solved.length}) {
auto const point{solved.position(at)};
double distance{-1.0};
auto const found{layout::chainage_of(solved, point, &distance)};
CHECK(found == doctest::Approx(at).epsilon(1e-6));
CHECK(distance < 1e-6);
// a point 2 m aside: the same chainage, 2 m off
auto const aside{point + layout::left_of(solved.tangent(at)) * 2.0};
if (at > 1.0 && at < solved.length - 1.0) {
CHECK(layout::chainage_of(solved, aside, &distance) == doctest::Approx(at).epsilon(1e-5));
CHECK(distance == doctest::Approx(2.0).epsilon(1e-6));
}
}
layout::solved_line const empty;
double distance{0.0};
CHECK(layout::chainage_of(empty, {0.0, 0.0}, &distance) == 0.0);
CHECK(std::isinf(distance));
}
TEST_CASE("project session: the switch track of a turnout made by the session, the tracks of a line written into two files recorded") {
test::scratch_folder folder{"layout_editing_session"};
folder.write("mapa.scn", "// a new scenery\n");
project::session session;
REQUIRE(session.open(folder.root(), "mapa.scn").done);
auto const laid{session.execute(straight_along_z(0.0, 0.0, 400.0))};
REQUIRE(laid.outcome.done);
auto const line{*laid.changed.begin()};
auto const types{layout::turnout_types()};
REQUIRE_FALSE(types.empty());
auto const inserted{session.execute(layout::insert_turnout{line, 100.0, "Rz 60E1-300-1:9", 1, 1})};
REQUIRE_MESSAGE(inserted.outcome.done, reasons_of(inserted.outcome));
REQUIRE(session.network().turnouts().size() == 1);
auto const &turnout{session.network().turnouts().begin()->second};
auto const made{session.generate(turnout.id)};
CHECK(made.errors.empty());
CHECK(made.text.find(" track switch ") != std::string::npos);
// a track of the turnout isn't made as a line of its own
CHECK_FALSE(session.generate(turnout.main).errors.empty());
// the tracks of the line written into two files, the switch track by its main track: all of them recorded, none taken for a change
// by hand
auto const pieces{session.generate(line).pieces};
REQUIRE_FALSE(pieces.empty());
std::vector<project::session::written_piece> written;
for (std::size_t i = 0; i < pieces.size(); ++i) {
written.push_back({pieces[i].name, pieces[i].text, "mapa.scn"});
}
// as a track of it written into another file of the map
written.push_back({pieces.front().name + "_b", pieces.front().text, "mapa_tory.scm"});
session.record_written(line, written);
session.record_written(turnout.main, {{made.name, made.text, "mapa_tory.scm", "switch"}});
std::set<std::string> files;
std::size_t switches{0};
for (auto const &node : session.project().generated) {
if (node.line == line) {
files.insert(node.file);
}
if (node.kind == "switch") {
++switches;
CHECK(node.line == turnout.main);
}
}
CHECK(files == std::set<std::string>{"mapa.scn", "mapa_tory.scm"});
CHECK(switches == 1);
// written again: the records of the line give way to the new ones, whatever file they were in
session.record_written(line, {{pieces.front().name, pieces.front().text, "mapa.scn"}});
std::size_t records{0};
for (auto const &node : session.project().generated) {
records += node.line == line ? 1 : 0;
}
CHECK(records == 1);
}