18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/editor/layout/layoutCommands.hpp
T
maj00r 555b77b11d feat(editor): the nodes of the track geometry and the commands editing the lines, kept whole or refused, one step to undo
Continues E2 (editor-tracks-domain.md §4 "Edycja = polecenie", §8): the lines meet at nodes, and every edit is a command which
proposes a new state, has what depends on it laid again, is checked and kept, or refused with its reasons and nothing changed.

- layoutNetwork: the node, its place (the only coordinates of the ends of the lines), the heading, the curvature, the cant and the
  grade, and the ends of the lines meeting there, each with its sense along the heading; the network of the nodes and the lines
  with the profiles of the norms by place and the identifiers handed out once, never again (an undone line made again is a new one).
  A transaction keeps each line and node the first time it touches it, so a change is rolled back whole or recorded.
- settle(): the lines touched laid again; the nodes take the values of their ends (a free end follows its line, a node keeps the
  curvature and the cant a line going on from it reads), a line going on from a node whose curve changed is laid again.
- check(): the invariants of the touched lines and nodes: laid, at their nodes, two ends at most on the two sides of a node (more
  is a turnout, E5), the heading the same within 1e-9 rad, the cant within 0.5 mm, the grade within the 2 per mille that needs no
  rounding; and the norms along the chains through the touched lines (the lines running on through the nodes as one line, the
  elements running on alike across a node as one, a jump of the curvature at a node a step of the deficiency the profile judges):
  BLOCK refuses, WARN and PREFER are told as notices.
- layoutCommands: create a line between two new nodes; move a vertex; set its radius, transitions or cant; split a line at a
  chainage (the track kept as it was: on a straight, in an arc, in a clothoid with a straight ramp, the parts going on from the new
  node at its curvature and cant; refused inside a vertical curve and inside a Bloss or cubic parabola transition); join two free
  ends with a line solved between them (a straight, a curve where the tangents meet, or two curves, the largest radius which fits
  with the cant and the transitions the norms prefer, a grade line meeting the grades of the ends); delete a line (a node left with
  no end goes). The model keeps the network and the steps: undo and redo restore the snapshots of the lines and nodes a command
  touched, one step a command.

Tests: the cases of each command, a refusal (a radius under the least, a cant over the greatest, curves overlapping, transitions
too long) leaving the state as it was, a WARN radius kept with its notice, undo and redo, the track the same along a line split on a
straight, in an arc and in a clothoid, a middle part deleted and joined again both ways round, a straight joining two ends on one
straight. P: 300 random edits each with the seeds 20261123, 7 and 450 (moved vertices, new radii, transitions and cants, splits,
joins, deletions, undo, redo): after each the invariants checked on their own (each element running on from the last, the cant
running on, the ends of the lines at their nodes to the bit, the heading, the cant and the grade running on through the nodes) and
no BLOCK along any chain; a refused command leaves the state as it was, an undo gives the state before the command.
2026-11-23 11:30:24 +01:00

125 lines
3.7 KiB
C++

/*
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/layoutNetwork.hpp"
#include <optional>
#include <string>
#include <variant>
#include <vector>
// The edits of the track geometry (editor-tracks-domain.md §4, "Edycja = polecenie"; §8): each one is a command which proposes a new
// state of the lines and the nodes it touches; what depends on them is laid again, the topology and the norms are checked (the module
// of the norms: BLOCK refuses, WARN and PREFER are told), and the change is kept whole or refused with the reasons, the state left as it
// was. Each one kept is one step to undo, the lines and nodes it touched restored as they were.
namespace layout
{
// a line laid between two new nodes, its vertices given with their radii, transitions and cants (the ids are given to them)
struct create_line
{
glm::dvec3 start{0.0};
glm::dvec3 end{0.0};
std::vector<vertex> vertices;
std::vector<grade_break> grades;
transition_shape transition{transition_shape::clothoid};
ramp_shape ramp{ramp_shape::linear};
norms::line_class category;
double chainage_start{0.0};
};
// the vertex moved in the plan
struct move_vertex
{
line_id line{line_id::none};
vertex_id vertex{vertex_id::none};
glm::dvec2 point{0.0};
};
// the radius, the transitions or the cant of the vertex, those given
struct set_vertex
{
line_id line{line_id::none};
vertex_id vertex{vertex_id::none};
std::optional<double> radius;
std::optional<double> transition_in;
std::optional<double> transition_out;
std::optional<double> cant;
};
// the line cut at the chainage by a new node: the part before it keeps the line, the part after it is a new line; the track stays as
// it was. Inside a curve the parts go on from the node at its curvature and cant; not inside a vertical curve, nor inside a transition
// curve which isn't a clothoid with a straight cant ramp, whose parts aren't curves of their kind
struct split_line
{
line_id line{line_id::none};
double chainage{0.0};
};
// a new line laid between two free ends, solved between the nodes: a straight where they face each other on one, a curve where their
// tangents meet, two curves otherwise. The radius 0 takes the largest the norms prefer which fits, the transition and the cant below 0
// the ones the norms prefer for the radius
struct join_ends
{
node_id from{node_id::none};
node_id to{node_id::none};
double radius{0.0};
double transition{-1.0};
double cant{-1.0};
};
// the line taken away; a node left with no end goes with it
struct delete_line
{
line_id line{line_id::none};
};
using command = std::variant<create_line, move_vertex, set_vertex, split_line, join_ends, delete_line>;
char const *command_name(command const &Command);
struct outcome
{
bool done{false};
std::vector<std::string> reasons; // why it was refused
std::vector<notice> notices; // what the norms warn of, or would prefer otherwise, on the lines changed
std::vector<line_id> lines; // made by it
std::vector<node_id> nodes;
};
// the lines and the nodes with the commands which edited them, to undo and redo one by one
class model
{
public:
model() = default;
explicit model(norms::area_map Areas) { m_network.areas = std::move(Areas); }
network const &state() const { return m_network; }
outcome execute(command const &Command);
bool undo();
bool redo();
std::size_t undoable() const { return m_undo.size(); }
std::size_t redoable() const { return m_redo.size(); }
private:
struct step
{
std::string name;
snapshot before;
snapshot after;
};
network m_network;
std::vector<step> m_undo;
std::vector<step> m_redo;
};
} // namespace layout