18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 10:46:45 +01:00

fix(editor): track edits keep the joints, the norms, the records and the saved speed together

- The cant typed in the inspector goes on into the track joined at that end, as the gizmo and the
  joints tool do it; the neighbours go into the same undo step. The joint stepped before.
- A track laid on from a canted joint takes the cant off along the ramp the norms ask for at the
  speed (`alignment::cant_ramp_length`), not at a fixed 1.5 mm a metre whatever the speed and the
  norms: 120 mm at 100 km/h over 120 m, not 80 m.
- Deleting a track of the line under the route tool keeps the laying norms. They went back to the
  defaults, and every later lay, extend and join used those.
- The gauge is a paste group of its own, off by default: pasting how a track rides or sounds no
  longer changes its gauge.
- The speed of a turntable set in the editor is the one saved. The editor changed the speed the
  turntable runs at, while the file keeps the limit it was loaded with.
- Deleting a set of tracks does what Del does with each: a double switch, a slip and a crossover go
  whole, the records of the switches go with them, and the model and events of a turntable too. The
  records stayed behind and on saving were given to another switch, which then showed a wrong type.
- Deleting a switch of a crossover takes the crossover out whole, as the connection tools do: the
  outer track of a crossover on a curve comes down by its lift and the record of the connection
  goes. Alone, the switch left the outer track up for good, and the connection to the next switch
  given its name. A switch bent out of the shape of its record is found by its name.

Tests: a unit test of the norm ramp. The other changes are in the editor's UI, which the unit tests
don't reach.
This commit is contained in:
maj00r
2026-11-23 01:31:37 +01:00
parent f15ea40575
commit c813c03865
9 changed files with 211 additions and 22 deletions
+5 -3
View File
@@ -1292,6 +1292,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri
} trace;
};
void render_route_ui();
// lets go of the line under the route tool, a track of it deleted; the norms set for laying stay
void route_drop();
bool render_route_band();
bool render_route_parameters();
bool render_route_vertices();
@@ -2535,7 +2537,7 @@ class editor_mode : public application_mode, private editor_track::observer, pri
void copy_track_style(TTrack const &Track);
std::size_t paste_track_style(std::vector<TTrack *> const &Tracks);
// which groups of the properties are pasted or put on the tracks by a preset, in the order of the groups of editormodetrackprops.cpp
using track_groups = std::array<bool, 11>;
using track_groups = std::array<bool, 12>;
// the values of the properties of the chosen groups put on the tracks, in one step of the undo. returns: the number of the
// tracks changed, none when no value is of the chosen groups
std::optional<std::size_t> apply_track_values(std::vector<std::pair<std::string, std::string>> const &Values, track_groups const &Groups, std::vector<TTrack *> const &Tracks, char const *What);
@@ -2610,8 +2612,8 @@ class editor_mode : public application_mode, private editor_track::observer, pri
std::string from;
std::vector<std::pair<std::string, std::string>> values;
// ticked at first by their groups: speed, environment, overhead line, track circuits, rails, trackbed, its shape, sleepers, ride and
// sound, frogs, sleeper placement
track_groups groups{false, false, false, false, true, true, true, true, false, true, true};
// sound, frogs, sleeper placement, gauge (not by default: the gauge of the track pasted onto stays)
track_groups groups{false, false, false, false, true, true, true, true, false, true, true, false};
std::map<std::string, bool> ticks; // changed by hand, by the keys of the properties
bool picking{false}; // the next click on a track in the view takes its properties
bool reveal{false}; // its section of the inspector opens, a track having been taken
+15 -7
View File
@@ -649,16 +649,16 @@ namespace
// the cant of a track joined goes on into the track laid, whatever shape that one has: its difference fades along the pieces from the
// joint, over as many of them as the cant takes at the ramp a transition curve has at the least, or over all of them where they're shorter
void lead_cant(std::vector<segment_data> &Pieces, double const Roll, double const Gauge, bool const From_start)
void lead_cant(std::vector<segment_data> &Pieces, double const Roll, alignment::limits const &Norms, double const Speed, bool const From_start)
{
double constexpr ramp_slope{1.5}; // mm of the cant to a metre
if (Pieces.empty())
return;
auto const here{From_start ? Pieces.front().rolls[0] : Pieces.back().rolls[1]};
auto const difference{Roll - static_cast<double>(here)};
if (std::abs(difference) < 1e-3)
return;
auto const wanted{Gauge * std::abs(std::sin(glm::radians(difference))) / ramp_slope};
// the ramp the norms ask for at the speed, as a transition curve has it; over all the pieces where they're shorter
auto const wanted{alignment::cant_ramp_length(Speed, Norms.gauge * std::abs(std::sin(glm::radians(difference))), Norms)};
std::vector<double> lengths;
auto reach{0.0};
for (std::size_t i = 0; i < Pieces.size(); ++i)
@@ -730,7 +730,7 @@ std::vector<segment_data> editor_mode::extend_pieces() const
auto pieces{eased.has_value() && false == eased->pieces.empty() ? eased->pieces : editor_track::layout_pieces(m_track_layout, tool.point, tool.direction, tool.grade, tool.mouse, radius)};
follow_ground(pieces, tool.point.y, tool.grade, tool.mouse.y, std::numeric_limits<double>::quiet_NaN(), tool.track);
if (tool.track != nullptr)
lead_cant(pieces, -editor_track::joint_roll(*tool.track, {tool.path, tool.atend ? editor_track::point_kind::end : editor_track::point_kind::start}), laying_norms().gauge, true);
lead_cant(pieces, -editor_track::joint_roll(*tool.track, {tool.path, tool.atend ? editor_track::point_kind::end : editor_track::point_kind::start}), laying_norms(), design_speed(tool.track, nullptr), true);
return pieces;
}
@@ -1189,7 +1189,7 @@ std::vector<segment_data> editor_mode::lay_pieces(std::vector<glm::dvec3> const
Length += bezier{piece}.plan_length();
if (pieces.empty())
Error = STR_C("The track can't be laid through these points");
lead_cant(pieces, editor_track::joint_roll(*End.track, End.point), laying_norms().gauge, false);
lead_cant(pieces, editor_track::joint_roll(*End.track, End.point), laying_norms(), design_speed(End.track, nullptr), false);
return pieces;
}
m_join_last = {};
@@ -1285,9 +1285,9 @@ std::vector<segment_data> editor_mode::lay_pieces(std::vector<glm::dvec3> const
else
ground_follow::follow_points(pieces, plan.knots, {Points.front().y, grade0, Points.back().y, grade1});
if (tool.start.track != nullptr)
lead_cant(pieces, -editor_track::joint_roll(*tool.start.track, tool.start.point), design.norms.gauge, true);
lead_cant(pieces, -editor_track::joint_roll(*tool.start.track, tool.start.point), design.norms, design.speed, true);
if (End.track != nullptr)
lead_cant(pieces, editor_track::joint_roll(*End.track, End.point), design.norms.gauge, false);
lead_cant(pieces, editor_track::joint_roll(*End.track, End.point), design.norms, design.speed, false);
if (Plan != nullptr)
*Plan = std::move(plan);
return pieces;
@@ -2304,6 +2304,14 @@ track_hover editor_mode::split_spot() const
return track_under_cursor(selected, 20.0f);
}
void editor_mode::route_drop()
{
// the lay, extend and join tools take their norms from here: emptied with the line, they'd go back to the defaults unseen
auto const norms{m_route.design.norms};
m_route = {};
m_route.design.norms = norms;
}
alignment::limits editor_mode::laying_norms() const
{
auto norms{m_route.design.norms};
+69 -5
View File
@@ -1589,7 +1589,25 @@ void editor_mode::render_path_ui()
if (std::array<float, 2> cants{lift(path.rolls[0]), lift(path.rolls[1])}; ImGui::DragFloat2("##roll", cants.data(), 0.5f, -388.0f, 388.0f, "%.0f") && std::isfinite(cants[0]) && std::isfinite(cants[1]))
path.rolls = {roll(cants[0]), roll(cants[1])};
item_tooltip("Cant at the start and at the end: how much higher one rail is, + raises the left rail looking along the path");
finish_edit(rebuild::geometry);
// the cant typed at an end goes on into the track joined there, as with the gizmo and the joints tool: the joint
// would step otherwise. the neighbours go into the same undo step
if (ImGui::IsItemActivated())
m_track_field_before = before;
if (ImGui::IsItemDeactivatedAfterEdit())
{
std::vector<std::pair<TTrack *, editor_track::state>> states{{track, m_track_field_before}};
std::vector<TTrack *> changed{track};
auto const joints{editor_track::joints(*track)};
for (auto const &joint : joints)
if (std::find(changed.begin(), changed.end(), joint.other) == changed.end())
{
states.emplace_back(joint.other, editor_track::capture(*joint.other));
changed.push_back(joint.other);
}
editor_track::match_rolls(*track, joints);
push_track_snapshot(std::move(states));
editor_track::commit(changed);
}
field_row("Radius");
if (auto radius{path.radius}; ImGui::InputFloat("##radius", &radius, 0.0f, 0.0f, "%.1f") && std::isfinite(radius))
path.radius = radius;
@@ -1711,7 +1729,7 @@ void editor_mode::delete_selected_track()
push_track_snapshot({}, created, {set->first, set->link, set->second});
drives_deleted({set->first, set->second});
if (std::any_of(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), [&](TTrack const *Each) { return Each == set->first || Each == set->link || Each == set->second; }))
m_route = {};
route_drop();
m_node = nullptr;
ui()->set_node(nullptr);
m_track_point = {};
@@ -1742,16 +1760,62 @@ void editor_mode::delete_selected_track()
push_track_snapshot({}, created, slip->parts);
drives_deleted(slip->switches);
if (std::any_of(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), [&](TTrack const *Each) { return std::find(slip->parts.begin(), slip->parts.end(), Each) != slip->parts.end(); }))
m_route = {};
route_drop();
m_node = nullptr;
ui()->set_node(nullptr);
m_track_point = {};
straight_refresh();
return;
}
// retired first, so the infrastructure it carried and moves elsewhere is in the same undo step
// a switch of a crossover takes the whole crossover out, as the connection tools do: its switches leave their main tracks, the
// insert goes and the outer track of a crossover on a curve comes down by the lift it went up by. Alone, the switch would leave
// the record of the connection behind, the outer track up, and the next switch given its name would take the connection over
if (auto const crossover{crossover_of(*track)})
{
for (auto const *part : {crossover->first, crossover->second, crossover->insert})
if (false == part->Dynamics.empty())
{
ui()->set_status(STR_C("The track can't be deleted while vehicles stand on it"), true);
return;
}
profile_library_load();
auto library{m_profiles};
std::vector<std::pair<TTrack *, editor_track::state>> states;
std::vector<TTrack *> created;
std::vector<TTrack *> removed;
TTrack *first{nullptr};
TTrack *second{nullptr};
if (false == take_out_crossover(*crossover, states, created, removed, first, second))
{
ui()->set_status(STR_C("The crossover there can't be taken out"), true);
return;
}
switch_store::follow();
auto status{std::string{STR_C("The crossover is taken out whole, its switches replaced by their main tracks")}};
if (auto const note{designs_trimmed(removed)}; false == note.empty())
status += "\n" + note;
auto const chained{std::any_of(removed.begin(), removed.end(), [&](TTrack const *Each) { return std::find(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), Each) != m_route.chain.tracks.end(); })};
push_track_snapshot(std::move(states), std::move(created), removed);
drives_deleted(removed);
records_to_history();
if (crossover->record.lifted())
profiles_to_history(std::move(library));
if (chained)
route_drop();
ui()->set_status(status, false);
m_node = nullptr;
ui()->set_node(nullptr);
m_track_point = {};
straight_refresh();
return;
}
// retired first, so the infrastructure it carried and moves elsewhere is in the same undo step. A switch bent out of the shape of
// its record isn't found by the shape, so by its name
if (auto const known{switch_store::of(*track)})
switch_store::forget(known->name);
else if (track->eType == tt_Switch)
if (auto const named{switch_store::named(*track)})
switch_store::forget(named->name);
editor_track::retire(*track);
// a switch leaves its main track behind, so the line isn't broken, unless Shift asks for all of it
std::vector<TTrack *> created;
@@ -1775,7 +1839,7 @@ void editor_mode::delete_selected_track()
m_history.back().instances.push_back(instance);
}
if (std::find(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), track) != m_route.chain.tracks.end())
m_route = {};
route_drop();
m_node = nullptr;
ui()->set_node(nullptr);
m_track_point = {};
+4 -3
View File
@@ -206,12 +206,13 @@ enum group : int
ride_sound,
frogs,
sleeper_placement, // spacing and shift of the sleepers and of the trackbed under them, apart from their models
gauge, // of its own: pasting how a track rides or sounds doesn't change its gauge
group_count
};
char const *const kGroupLabels[group_count] = {"Speed", "Environment", "Overhead line", "Track circuits", "Rails", "Trackbed", "Shape of the trackbed", "Sleepers", "Ride and sound", "Frog and check rails", "Sleeper placement"};
char const *const kGroupLabels[group_count] = {"Speed", "Environment", "Overhead line", "Track circuits", "Rails", "Trackbed", "Shape of the trackbed", "Sleepers", "Ride and sound", "Frog and check rails", "Sleeper placement", "Gauge"};
// the groups as the presets keep them in their file, by names which don't change with the order of the groups
char const *const kGroupKeys[group_count] = {"speed", "environment", "overhead", "circuits", "rails", "trackbed", "bedshape", "sleepers", "ride", "frogs", "sleeperplacement"};
char const *const kGroupKeys[group_count] = {"speed", "environment", "overhead", "circuits", "rails", "trackbed", "bedshape", "sleepers", "ride", "frogs", "sleeperplacement", "gauge"};
struct track_property
{
@@ -712,7 +713,7 @@ std::vector<track_property> make_properties()
return true;
};
}
add_float(list, "gauge", section::ride, group::ride_sound, "Gauge (m)", "Gauge of the track, 1.435 m for the standard gauge", &TTrack::fTrackWidth, 3, 0.01f, true);
add_float(list, "gauge", section::ride, group::gauge, "Gauge (m)", "Gauge of the track, 1.435 m for the standard gauge", &TTrack::fTrackWidth, 3, 0.01f, true);
list.back().applies = any;
return list;
}
+81 -4
View File
@@ -1939,22 +1939,99 @@ void editor_mode::track_set_delete()
{
track_set_prune();
std::vector<TTrack *> removed;
// of crossovers taken out: the main tracks of their switches laid, the outer track brought down
std::vector<std::pair<TTrack *, editor_track::state>> states;
std::vector<TTrack *> created;
bool crossovers{false};
bool lifted{false};
profile_library_load();
auto library{m_profiles};
std::size_t kept{0};
auto const gone = [&](TTrack const *Track) { return std::find(removed.begin(), removed.end(), Track) != removed.end(); };
auto const blocked = [](TTrack const *Track) { return false == Track->Dynamics.empty() || false == scene::Layers.editable(Track); };
// as Del does with each: a double switch, a slip and a crossover go whole, the records of the switches go with them
for (auto *track : m_track_set)
{
if (false == track->Dynamics.empty() || false == scene::Layers.editable(track))
if (gone(track))
continue;
if (blocked(track))
{
++kept;
continue;
}
if (auto const set{double_of(*track)})
{
if (blocked(set->first) || blocked(set->link) || blocked(set->second))
{
++kept;
continue;
}
if (auto const known{switch_store::named(*set->first)})
switch_store::forget(known->name);
for (auto *part : {set->first, set->link, set->second})
if (false == gone(part))
{
editor_track::retire(*part);
removed.push_back(part);
}
continue;
}
if (auto const slip{slip_of(*track)})
{
if (std::any_of(slip->parts.begin(), slip->parts.end(), blocked))
{
++kept;
continue;
}
slip_taken_out(*slip, false);
for (auto *part : slip->parts)
if (false == gone(part))
removed.push_back(part);
continue;
}
if (auto const crossover{crossover_of(*track)})
{
if (blocked(crossover->first) || blocked(crossover->second) || blocked(crossover->insert))
{
++kept;
continue;
}
TTrack *first{nullptr};
TTrack *second{nullptr};
if (take_out_crossover(*crossover, states, created, removed, first, second))
{
crossovers = true;
lifted = lifted || crossover->record.lifted();
continue;
}
}
if (auto const known{switch_store::of(*track)})
switch_store::forget(known->name);
else if (track->eType == tt_Switch)
if (auto const named{switch_store::named(*track)})
switch_store::forget(named->name);
editor_track::retire(*track);
removed.push_back(track);
if (std::find(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), track) != m_route.chain.tracks.end())
m_route = {};
}
if (std::any_of(removed.begin(), removed.end(), [&](TTrack const *Each) { return std::find(m_route.chain.tracks.begin(), m_route.chain.tracks.end(), Each) != m_route.chain.tracks.end(); }))
route_drop();
if (crossovers)
switch_store::follow();
auto const designs{designs_trimmed(removed)};
push_track_snapshot({}, {}, removed);
push_track_snapshot(std::move(states), std::move(created), removed);
drives_deleted(removed);
// the model and the events of a turntable can't stay without its track
for (auto const *track : removed)
if (track->eType == tt_Table)
for (auto const instance : turntable_includes(*track))
{
scene::Layers.removed(instance, true);
m_history.back().instances.push_back(instance);
}
if (crossovers)
records_to_history();
if (lifted)
profiles_to_history(std::move(library));
m_track_set.clear();
m_node = nullptr;
ui()->set_node(nullptr);
+5
View File
@@ -1348,6 +1348,11 @@ glm::dvec3 along_end(glm::dvec3 const &Point, glm::dvec2 const &Direction, doubl
} // namespace
double cant_ramp_length(double const Speed, double const Cant, limits const &Limits)
{
return ramp_length(Speed, std::abs(Cant), Limits);
}
std::pair<double, double> end_curvatures(element const &Element)
{
auto const own{Element.radius > 0.0 ? 1.0 / Element.radius : 0.0};
+3
View File
@@ -134,6 +134,9 @@ struct recommendation
};
recommendation recommend(double const Speed, double const Radius, limits const &Limits);
double transition_length(double const Speed, double const Radius, double const Cant, limits const &Limits);
// the length of a straight cant ramp the norms ask for to change the cant by Cant [mm] at Speed [km/h]: slow enough in time and
// along the track [m], 0 for no change
double cant_ramp_length(double const Speed, double const Cant, limits const &Limits);
// the transition curve from the curvature and the cant of one arc to those of the next one, 0 for a straight; the curvature positive
// turning one way, the cant with the same sign as the curvature it leans into
double transition_between(double const Speed, double const Curvature0, double const Cant0, double const Curvature1, double const Cant1, limits const &Limits);
+5
View File
@@ -1226,11 +1226,16 @@ double editor_track::velocity(TTrack const &Track)
{
if (Track.SwitchExtension && Track.eType == tt_Switch)
return Track.SwitchExtension->fVelocity;
// a turntable keeps the limit of its file where a switch does, and it's that one which is saved
if (Track.SwitchExtension && Track.eType == tt_Table && Track.SwitchExtension->fVelocity != -1.0f)
return Track.SwitchExtension->fVelocity;
return Track.fVelocity;
}
void editor_track::velocity(TTrack &Track, double const Velocity)
{
if (Track.SwitchExtension && Track.eType == tt_Table)
Track.SwitchExtension->fVelocity = static_cast<float>(Velocity);
if (Track.SwitchExtension && Track.eType == tt_Switch)
{
Track.SwitchExtension->fVelocity = static_cast<float>(Velocity);
+24
View File
@@ -1162,3 +1162,27 @@ TEST_CASE("alignment: a curve traced on from a switch goes on at its radius, not
CHECK(two.vertices.front().arcs.front().radius == doctest::Approx(600.0).epsilon(0.1));
CHECK(two.rms < 0.1);
}
TEST_CASE("alignment: the cant ramp the norms ask for, which a track laid on from a canted joint takes its cant off along") {
alignment::limits const norms;
// no change, no ramp
CHECK(alignment::cant_ramp_length(100.0, 0.0, norms) == doctest::Approx(0.0));
// 120 mm at 100 km/h: along the track 0.12 * 10 * 100 = 120 m, longer than in time (100 * 120 / (3.6 * 35) = 95.2 m). The lay tool
// used to take any cant off at a fixed 1.5 mm a metre, over 80 m here, whatever the speed and the norms
CHECK(alignment::cant_ramp_length(100.0, 120.0, norms) == doctest::Approx(120.0));
// the cant taken off or put on alike
CHECK(alignment::cant_ramp_length(100.0, -120.0, norms) == doctest::Approx(120.0));
// slower, shorter: 0.12 * 10 * 40 = 48 m, to whole 5 m
CHECK(alignment::cant_ramp_length(40.0, 120.0, norms) == doctest::Approx(50.0));
// the norms of the route tool count: a gentler factor leaves the time the longer, 95.2 m to 100 m
auto gentle{norms};
gentle.ramp_factor = 5.0;
CHECK(alignment::cant_ramp_length(100.0, 120.0, gentle) == doctest::Approx(100.0));
// the ramp is never shorter at a higher speed
double previous{0.0};
for (double speed = 20.0; speed <= 200.0; speed += 10.0) {
auto const length{alignment::cant_ramp_length(speed, 80.0, norms)};
CHECK(length >= previous);
previous = length;
}
}