mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 22:07:55 +01:00
c3b987abb2
* 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.
* test(editor): self-test scripts that fail, a runner, and a case for each fix of the track editing
The editor's self-test scripts only logged: nothing made a run fail. Now:
- `expect` commands check the state and log `SELFTEST PASS` or `SELFTEST FAIL ... got X, expected Y`:
the cant at a track end, the joint at an end (gap, angle, cant step), the records of a switch and
the connections naming it, speed, gauge, a track present or removed, the laying norms' gauge,
heights and lifts against a mark, whether a track is in the line under the route tool, and the
includes of a turntable. A command no group takes counts as a failure, so a mistyped one can't
make the checks after it pass.
- At `quit` the result is logged, and a script whose expectations failed ends the editor with exit
code 2 (a failed start stays 1; the simulator's test mode is unchanged).
- The commit of the inspector's cant field is `track_cant_commit`, which the inspector and the
`trackcant` command both call; `routegauge` sets the laying norms as the route tool does, and
`tracksetdelete` deletes a set of tracks by name.
- `tests/editor/run.py` runs each script of `tests/editor/cases` on a copy of the fixture scenery
in the game folder, with a wall-time limit and a memory cap, reads the exit code and the
`SELFTEST` lines, and deletes the copy. A `#> reopen` line restarts the editor on the saved
scenery. See `tests/editor/README.md`.
- One case per fix of the track editing quick fixes (inspector cant, norms after deleting, the
gauge paste group, turntable speed saved, deleting a set, a set with a double switch, a slip
and a crossover, deleting a switch of a crossover, a bent switch). All pass; with the fixes
reverted all fail.
* feat(editor): the track tool panels keep their fields, results and buttons within the panel
- The card of the tool: after a long gesture the action goes under it, at its indent, when what is
left of the row is narrower than 8 font sizes or the longest word of the action. It was squeezed
into a sliver and broken in the middle of its words.
- A click on the title row of the card folds it to that row (icon, name, family, key, and a small
triangle marker) and opens it again. One flag for all the tools, kept with the editor settings
(`track_card_folded` in eu07_editor.ini), open by default: the card took 250-300 px at the top
of every tool and repeats the hint bar of the view.
- The grade labels of the vertical profile, on the graph and in the view, write the per mille as
the panel does (the translated "per mille", the per mille sign in Polish), not "o/oo".
- The track analysis window: the thresholds of the speed check and of the joints keep to their
widest name and a field of 10 font sizes where the window is wider than that, as docked across
the bottom (`begin_fields_fitted`). The results are tables with each column as wide as what is in
it: the speed check with its column names kept at the top as the list scrolls, the joints with
the kind, the value and the two tracks next to each other. A click on a row shows it as before.
- `same_line_if_fits` puts the next button beside the one before only when it fits in what is left
of the line, at the start of the next line otherwise. Used where the route design puts buttons
side by side, for "Forget the design" in the profile panel, which was cut off at the edge, and for
the buttons of a switch in the inspector.
- The laying options: the speed, the curve radius and the longest path are fields with the name on
the left in two columns (`begin_fields`/`field_row`), as in the inspector, the route and the
profile, the chips of the quick values under them. The segmented choices give the short names
less of the room when a long one doesn't fit in its even share, and go on in a second bar when
they don't fit side by side: "Through the clicked points" overflowed its third of the bar.
* fix(editor): a switch laid gets the speed of its diverging track, one breaking a hard rule of ST-T1-A6 isn't laid
- Every switch laid writes the speed of its diverging track (`velocity`): the switch tool on a
straight, into a curve, from a free end and linked, a double switch, the switches of a slip, a
switch given another template of the catalogue, and a switch moved along the curve it's bent
into. A switch of the catalogue had no limit before, one copied from a switch of the scenery kept
that one's, one laid without a track had the laying speed on both its tracks. The limit is for the
diverging track alone (`velocity -V`); the own limit of the track it goes into stays for both
where it's the lower. The switch tool says the speed and what gives it.
- The speed is formula 9.9 of PKP PLK ST-T1-A6 the other way round, V = sqrt((I + D) r / 11.8), with
the normal cant deficiency of the frog of table 9.7 (110 mm a fixed frog, 130 mm a movable nose),
rounded down to the steps of the speed limits and no more than table 11.2 gives the basic switch.
For the fixed frogs the formula gives table 11.2 itself (190 40, 300 50, 500 60, 760 80, 1200
100 km/h); the 2500-1:26.5 with a movable nose stays 130. A switch bent into a curve takes the
radius of its diverging track from table 11.8 (one-sided r" = (Rr - t^2)/(R + r), two-sided
(Rr + t^2)/(R - r)) and the cant of the line with its sign: leaning against the diverging track it
takes speed away, where the speed check counted it as helping. Rz 60E1-190-1:9 bent out of the
curve R 600 m with 60 mm: 30 km/h, not 40; the 60E1-500-1:12 of the review, out of R 1200 m with
60 mm: 60 km/h at its fixed frog (54 at I = 100 mm, the 0.65 m/s2 the editor took), not 107.
- A switch breaking a hard rule isn't laid, the tracks left as they were, and the switch tool says
why, with the value, the limit and the paragraph: the cant changing within it (11.4 pkt 7), a cant
ramp ending within it or nearer than 6 m (9.6 pkt 16-17; one ending right at an end of the switch
is let be, as the snapping of the start offers it), a double switch, a slip or a crossing bent
(11.4 pkt 4), the curvature of its diverging or its main track turning from one side to the other
(9.7 pkt 12, KZW II). They were warnings shown after the switch was laid, and on a straight the
cant wasn't looked at at all. The line is looked at 10 m past the ends of the curve or the
straight too, so the ramp on the straight before a curve counts. The previews show the refusals.
- A switch moved along its straight or the curve it's bent into, a double switch moved along its
straight, and a switch given another template go through the same checks; one breaking a rule
stays as it is.
- Warnings still: 1:9.403 and steeper than 1:9 bent (11.4 pkt 4c-d), a rule of the running and the
main tracks, which the editor doesn't tell from the side ones, as the warning now says; the limits
of table 11.9 and a cant over 100 mm in a bent switch (table 9.6), as before.
Tests: unit tests of tables 11.8 and 11.9 (the least main radius bends the diverging track to the
least radius the table gives), table 9.7, the speeds of table 11.2 by formula 9.9, the speed of a
bent switch with the cant with its sign (the example of the review), the cant over a switch on a
straight and a double switch bent. Editor cases `q2_1_switch_speed`, `q2_2_switch_refusals` and
`q2_3_switch_move`, with `expect status` (the status of the switch tool) and `expect steps` (the
undo steps), the fixture with a straight taking a cant on and a compound curve without cant. All
pass; with the changes of the editor reverted all three fail.
* test(editor): golden round trips of the tracks and of the //$ lines, the cant at the joints
The safety net of the track editor before its domain model changes (E0 of
editor-tracks-tests.md): what the editor reads from the scenery files and
writes back is read, written and read again in tests/unit, with no game data.
Tracks (test_track_text.cpp, data/tracks/). Entries written the way the game
writes them, read by TTrack::Load() and written by export_as_text(), come out
byte for byte the same, and read again give the same track field by field:
plain tracks with all their attributes (events, speed, isolated, overhead,
vradius, rail profile, friction cell), a curve with cant, radius, uneven,
buckle, cwr and sleeper models, a bridge, switches with trackbed, frogs,
movable nose, blades, uneven of both paths and timbers, a turntable, paths of
pieces, the speeds +40, -40, 0.5, 0 and none of tracks, switches and
turntables, a river, a tributary, a road and a crossing. What the game writes
otherwise than given is checked as it is (table as turn, mountain as
mountains, the length of the path, a switch heard from 10 m, the events in
order, names in lower case, model paths with forward slashes, the texture
length, rolls as floats, no overhead and no vradius left out).
The seam: TTrack built from text with no scene. The engine stubs of the tests
had none of it, so the tests link the few sources reading and writing a track
takes (world/Track.cpp, Segment.cpp, TrackUneven.cpp, MemCell.cpp,
scene/scenenode.cpp, utilities/Timer.cpp, rendering/geometrybank.cpp) and
track_stubs.cpp stands in for the rest: a renderer keeping the materials by
their names, and the scene, events, vehicles and the geometry of the rails,
which reading and writing never reach and which stop the test with their name
if one does. It tests the game's own Load() and export, where a parser of its
own would have meant rewriting Load(), and changes no code of the game. The
stub of basic_node::radius_() goes, scenenode.cpp brings the real one. The
rails don't buckle in the tests (TrackBuckle.cpp needs the environment), the
buckle declared is read and written either way; material names go through a
stand-in, the game's material manager isn't tested here.
The //$ lines (test_editor_records.cpp, data/records/). Their readers and
writers were members of editor_mode, srk_tool and the stores, with the scene
around them; they move as they were into editor/editorRecords.cpp, the one
place each kind is read and written, which the stores call (and which the
migration to the project file of E3 will need apart anyway). The methods of
scene::layer_context move into scene/scenelayercontext.cpp so it links alone.
Each kind, h r k p t w b s c o z, read from golden lines and written back comes
out the same, in the coordinates of the file and of a file placed at an
origin; //$e, the descriptions of the templates, read and saved by
editor_includes in a template in a scratch folder, leaves the file as it was.
Cant (test_cant.cpp). The paths an alignment lays roll by asin(cant/gauge),
signed with the turn, back to the cant of the line at every end; a curve the
other way rolls the other way. At a joint editor_track::match_rolls() gives
the joined track the roll for the travel across it, turned where the paths
face or stand back to back, and the track written keeps it. The inspector's
own conversion on 1500 mm stays in its lambdas here; named, as
editor_mode::cant_of_roll()/roll_of_cant(), it comes with the quick fixes.
Known bugs found, tests of the change each (doctest::should_fail, passing
until fixed, then reported to drop the decorator), data/tracks/known_lost.scn:
- angle1/angle2 are read and dropped: not written back.
- fouling1/fouling2 are read and dropped: not written back.
- name@circuit: the circuit is taken out of the name with the letter before
the @ ("tor1@iso1" becomes track "tor"; substr(0, i - 1)).
- the attributes of the entry (gauge, friction, texture sizes, speed...) are
written to 6 significant digits, a float has 9: more digits are lost on the
first save.
None in the //$ lines.
Unit tests: 957 cases before, 974 after, all passing.
* test(editor): a mistyped track fails the self-test, indented comments are skipped, case 7b checks the switch got bent
- A blank line or an indented `#` comment in a script is no longer a command no group takes.
- `trackcant`, `routegauge` and `tracksetdelete` with a track that isn't there count as a failed expectation.
- `expect shaped <switch> <yes|no>`: 7b checks that the gizmo drag bent the switch out of its record before deleting it.
- The ramp-length loop of test_alignment counts in an integer (Sonar S2193).
* test(editor): the entries known to be lost are checked apart from should_fail, the cant test says where the inspector conversion is tested
- A normal test checks that known_lost.scn and each of its entries are there and read, so the should_fail tests can't pass for
data gone missing.
- The comment of test_cant.cpp said the tests of cant_of_roll()/roll_of_cant() are here; they are the self-test case
1_inspector_cant.txt, as the unit tests don't link the editor.
1024 lines
37 KiB
C++
1024 lines
37 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "editor/editorKilometrage.hpp"
|
|
#include "editor/editorRecords.hpp"
|
|
#include "editor/editorAlignmentStore.hpp"
|
|
#include "editor/editorFormat.hpp"
|
|
#include "editor/editorGeometry.hpp"
|
|
#include "editor/editorTrack.hpp"
|
|
|
|
#include "world/Track.h"
|
|
#include "simulation/simulation.h"
|
|
#include "utilities/Logs.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iterator>
|
|
#include <limits>
|
|
#include <map>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <unordered_set>
|
|
|
|
namespace kilometrage
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
using geometry::plan_distance;
|
|
using geometry::plan_of;
|
|
|
|
std::string const kMark{"//$k"};
|
|
double const kReach{60000.0}; // m, of the tracks followed each way from a mark
|
|
double const kStep{2.0}; // m between the points of an axis
|
|
double const kOnAxis{12.0}; // m, of a mark from the axis of its line
|
|
double const kNear{6.0}; // m, of a mark from the track it stands on
|
|
double const kBetweenLow{3.0}; // m between the axes of the tracks of a double-track line
|
|
double const kBetweenHigh{6.5};
|
|
double const kCell{10.0}; // m, of the squares the track beside is sorted into
|
|
double const kApart{15.0}; // m between the tracks, of a double-track line on two formations
|
|
double const kBlend{100.0}; // m, the axis takes to go over from between the tracks to one of them
|
|
double const kLonger{1.0}; // m, of the track beside longer than the track of the line, for the axis to follow it
|
|
double const kDetour{20000.0}; // m, of the tracks followed each way from a via back onto the line
|
|
|
|
std::vector<mark> g_marks;
|
|
std::vector<via> g_vias;
|
|
state g_base; // as it was before the changes not yet in the undo history
|
|
bool g_changed{false};
|
|
bool g_loaded{false};
|
|
std::size_t g_edits{0};
|
|
std::map<std::string, std::optional<axis>> g_axes;
|
|
std::size_t g_revision{0};
|
|
|
|
void drop_axes()
|
|
{
|
|
g_axes.clear();
|
|
++g_revision;
|
|
}
|
|
|
|
using span = editor_track::route_span;
|
|
|
|
// the lines and their format: editor_records::read_kilometrage() and kilometrage_lines()
|
|
void store(scene::layer_handle const Layer)
|
|
{
|
|
if (false == scene::Layers.valid(Layer))
|
|
return;
|
|
std::vector<mark> marks;
|
|
std::copy_if(g_marks.begin(), g_marks.end(), std::back_inserter(marks), [&](mark const &Entry) { return Entry.layer == Layer; });
|
|
std::vector<via> vias;
|
|
std::copy_if(g_vias.begin(), g_vias.end(), std::back_inserter(vias), [&](via const &Entry) { return Entry.layer == Layer; });
|
|
scene::Layers.mark(Layer, kMark, editor_records::kilometrage_lines(marks, vias, scene::Layers.layer(Layer).context_insert()));
|
|
}
|
|
|
|
void changed(std::set<scene::layer_handle> const &Layers)
|
|
{
|
|
g_changed = true;
|
|
drop_axes();
|
|
for (auto const layer : Layers)
|
|
store(layer);
|
|
}
|
|
|
|
std::vector<span> flipped(std::vector<span> Spans)
|
|
{
|
|
std::reverse(Spans.begin(), Spans.end());
|
|
for (auto &entry : Spans)
|
|
entry.forward = false == entry.forward;
|
|
return Spans;
|
|
}
|
|
|
|
std::size_t index_of(std::vector<span> const &Spans, TTrack const *Track)
|
|
{
|
|
auto const found{std::find_if(Spans.begin(), Spans.end(), [&](span const &Entry) { return Entry.track == Track; })};
|
|
return found == Spans.end() ? Spans.size() : static_cast<std::size_t>(found - Spans.begin());
|
|
}
|
|
|
|
// the run on from the end of the span the way it goes, along the main paths of the switches
|
|
std::vector<span> onward(span const &Last)
|
|
{
|
|
auto const run{editor_track::run_route(*Last.track, kReach)};
|
|
auto const at{index_of(run.spans, Last.track)};
|
|
if (at == run.spans.size())
|
|
return {};
|
|
if (run.spans[at].forward == Last.forward)
|
|
return {run.spans.begin() + at + 1, run.spans.end()};
|
|
return flipped({run.spans.begin(), run.spans.begin() + at});
|
|
}
|
|
|
|
// the spans with each track once, the first time it comes
|
|
std::vector<span> once(std::vector<span> const &Spans)
|
|
{
|
|
std::vector<span> result;
|
|
std::unordered_set<TTrack const *> seen;
|
|
for (auto const &entry : Spans)
|
|
if (seen.insert(entry.track).second)
|
|
result.push_back(entry);
|
|
return result;
|
|
}
|
|
|
|
// the route runs along the track of the via, along the path of a switch it stands on
|
|
bool runs_by(editor_track::route const &Route, std::size_t const Via)
|
|
{
|
|
int path{0};
|
|
auto const *track{track_near(g_vias[Via].point, kNear, path)};
|
|
return track != nullptr && std::any_of(Route.spans.begin(), Route.spans.end(), [&](span const &Entry) { return Entry.track == track && Entry.path == path; });
|
|
}
|
|
|
|
// the route led along the tracks of the vias: from the track of each the run along the straight paths of the switches both ways
|
|
// back onto the route takes the place of the stretch of the route between where it comes back; where it comes back one way only,
|
|
// as at a junction, the route goes on along it from there
|
|
void through(editor_track::route &Route, std::vector<std::size_t> const &Vias)
|
|
{
|
|
for (auto const index : Vias)
|
|
{
|
|
int path{0};
|
|
auto *track{track_near(g_vias[index].point, kNear, path)};
|
|
if (track == nullptr || runs_by(Route, index))
|
|
continue;
|
|
auto const run{editor_track::run_route(*track, kDetour, path)};
|
|
auto const at{index_of(run.spans, track)};
|
|
if (at == run.spans.size())
|
|
continue;
|
|
auto const none{Route.spans.size()};
|
|
auto const on = [&](std::size_t const Index) { return index_of(Route.spans, run.spans[Index].track); };
|
|
std::optional<std::size_t> low;
|
|
for (auto i = at; i-- > 0;)
|
|
if (on(i) != none)
|
|
{
|
|
low = i;
|
|
break;
|
|
}
|
|
std::optional<std::size_t> high;
|
|
for (auto i = at + 1; i < run.spans.size(); ++i)
|
|
if (on(i) != none)
|
|
{
|
|
high = i;
|
|
break;
|
|
}
|
|
auto const part = [&](std::size_t const From, std::size_t const To) { return std::vector<span>{run.spans.begin() + From, run.spans.begin() + To}; };
|
|
auto const head = [&](std::size_t const To) { return std::vector<span>{Route.spans.begin(), Route.spans.begin() + To}; };
|
|
auto const tail = [&](std::size_t const From) { return std::vector<span>{Route.spans.begin() + From, Route.spans.end()}; };
|
|
auto const joined = [](std::vector<std::vector<span>> const &Parts) {
|
|
std::vector<span> result;
|
|
for (auto const &entry : Parts)
|
|
result.insert(result.end(), entry.begin(), entry.end());
|
|
return result;
|
|
};
|
|
std::vector<span> spans;
|
|
if (low && high)
|
|
{
|
|
auto const from{on(*low)};
|
|
auto const to{on(*high)};
|
|
if (from == to)
|
|
continue;
|
|
spans = from < to ? joined({head(from), part(*low, *high + 1), tail(to + 1)}) : joined({head(to), flipped(part(*low, *high + 1)), tail(from + 1)});
|
|
}
|
|
else if (low)
|
|
{
|
|
auto const from{on(*low)};
|
|
spans = run.spans[*low].forward == Route.spans[from].forward ? joined({head(from), part(*low, run.spans.size())}) : joined({flipped(part(*low, run.spans.size())), tail(from + 1)});
|
|
}
|
|
else if (high)
|
|
{
|
|
auto const to{on(*high)};
|
|
spans = run.spans[*high].forward == Route.spans[to].forward ? joined({part(0, *high + 1), tail(to + 1)}) : joined({head(to), flipped(part(0, *high + 1))});
|
|
}
|
|
else
|
|
continue;
|
|
Route.spans = once(spans);
|
|
}
|
|
}
|
|
|
|
double plan_gap(std::vector<editor_track::route_sample> const &Samples, glm::dvec3 const &Point)
|
|
{
|
|
auto best{std::numeric_limits<double>::max()};
|
|
for (auto const &sample : Samples)
|
|
best = std::min(best, plan_distance(sample.position, Point));
|
|
return best;
|
|
}
|
|
|
|
// the track beside the line: of each sample of the line the sample of it beside, where they run side by side
|
|
struct pairing
|
|
{
|
|
editor_track::route route; // of the track beside
|
|
std::vector<editor_track::route_sample> partner; // along the track beside, empty for none
|
|
std::vector<int> match; // of each sample, -1 where the track beside isn't there
|
|
std::vector<double> across; // to the sample beside, along the normal of the sample
|
|
};
|
|
|
|
pairing pair_with(std::vector<editor_track::route_sample> const &Samples, TTrack &Beside, std::vector<std::size_t> const &Vias)
|
|
{
|
|
pairing result;
|
|
result.match.assign(Samples.size(), -1);
|
|
result.across.assign(Samples.size(), 0.0);
|
|
result.route = editor_track::run_route(Beside, kReach);
|
|
through(result.route, Vias);
|
|
result.partner = editor_track::sample_route(result.route, kStep);
|
|
auto const &partner{result.partner};
|
|
auto const cell = [](glm::dvec3 const &Point) { return std::make_pair(static_cast<long>(std::floor(Point.x / kCell)), static_cast<long>(std::floor(Point.z / kCell))); };
|
|
std::map<std::pair<long, long>, std::vector<std::size_t>> grid;
|
|
for (std::size_t j = 0; j < partner.size(); ++j)
|
|
grid[cell(partner[j].position)].push_back(j);
|
|
for (std::size_t i = 0; i < Samples.size(); ++i)
|
|
{
|
|
auto const &sample{Samples[i]};
|
|
glm::dvec2 const normal{-sample.direction.y, sample.direction.x};
|
|
auto const [cx, cz]{cell(sample.position)};
|
|
auto best{std::numeric_limits<double>::max()};
|
|
for (auto x = cx - 1; x <= cx + 1; ++x)
|
|
for (auto z = cz - 1; z <= cz + 1; ++z)
|
|
{
|
|
auto const found{grid.find({x, z})};
|
|
if (found == grid.end())
|
|
continue;
|
|
for (auto const j : found->second)
|
|
{
|
|
auto const offset{plan_of(partner[j].position) - plan_of(sample.position)};
|
|
auto const across{glm::dot(offset, normal)};
|
|
auto const along{glm::dot(offset, sample.direction)};
|
|
if (std::abs(across) < kBetweenLow || std::abs(across) > kBetweenHigh || std::abs(along) >= kStep * 1.5 || std::abs(glm::dot(sample.direction, partner[j].direction)) <= 0.97 || std::abs(along) >= best)
|
|
continue;
|
|
best = std::abs(along);
|
|
result.match[i] = static_cast<int>(j);
|
|
result.across[i] = across;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// the track beside comes back onto the line between the stretches it runs beside it, or both ways, as a track of a station or a
|
|
// passing loop does: not the other track
|
|
bool looped(pairing const &Pairs, std::unordered_set<TTrack const *> const &Own)
|
|
{
|
|
auto low{Pairs.partner.size()};
|
|
std::size_t high{0};
|
|
for (auto const match : Pairs.match)
|
|
if (match >= 0)
|
|
{
|
|
low = std::min(low, static_cast<std::size_t>(match));
|
|
high = std::max(high, static_cast<std::size_t>(match));
|
|
}
|
|
auto const on_line = [&](editor_track::route_sample const &Sample) { return Sample.span < Pairs.route.spans.size() && Own.count(Pairs.route.spans[Sample.span].track) > 0; };
|
|
if (low >= Pairs.partner.size())
|
|
return false;
|
|
auto const first{Pairs.partner.begin() + static_cast<std::ptrdiff_t>(low)};
|
|
auto const last{Pairs.partner.begin() + static_cast<std::ptrdiff_t>(high) + 1};
|
|
return std::any_of(first, last, on_line) || (std::any_of(Pairs.partner.begin(), first, on_line) && std::any_of(last, Pairs.partner.end(), on_line));
|
|
}
|
|
|
|
// the track beside the line, looked for by the first mark and then every 500 m along the line: a track the line doesn't run
|
|
// through, the spacing of a double-track line away, beside it for 100 m at least
|
|
pairing pair_up(std::vector<editor_track::route_sample> const &Samples, editor_track::route const &Route, std::size_t const Origin, std::vector<std::size_t> const &Vias)
|
|
{
|
|
std::unordered_set<TTrack const *> own;
|
|
for (auto const &entry : Route.spans)
|
|
own.insert(entry.track);
|
|
std::unordered_set<TTrack const *> tried;
|
|
auto const step{static_cast<std::size_t>(500.0 / kStep)};
|
|
for (std::size_t k = 0; k * step < Samples.size() && tried.size() < 4; ++k)
|
|
for (auto const way : {1, -1})
|
|
{
|
|
if (k == 0 && way < 0)
|
|
continue;
|
|
auto const index{static_cast<long>(Origin) + way * static_cast<long>(k * step)};
|
|
if (index < 0 || index >= static_cast<long>(Samples.size()))
|
|
continue;
|
|
auto const &at{Samples[static_cast<std::size_t>(index)]};
|
|
glm::dvec2 const left{-at.direction.y, at.direction.x};
|
|
for (auto const side : {1.0, -1.0})
|
|
{
|
|
auto const probe{plan_of(at.position) + left * (side * (kBetweenLow + kBetweenHigh) * 0.5)};
|
|
auto *track{track_near({probe.x, at.position.y, probe.y}, 1.6)};
|
|
if (track == nullptr || own.count(track) > 0 || false == tried.insert(track).second)
|
|
continue;
|
|
auto result{pair_with(Samples, *track, Vias)};
|
|
if (std::count_if(result.match.begin(), result.match.end(), [](int const Match) { return Match >= 0; }) * kStep >= 100.0 && false == looped(result, own))
|
|
return result;
|
|
}
|
|
}
|
|
pairing none;
|
|
none.match.assign(Samples.size(), -1);
|
|
none.across.assign(Samples.size(), 0.0);
|
|
return none;
|
|
}
|
|
|
|
// the axis: between the tracks where they run side by side, along the longer one where they part and come together again
|
|
// (PKP GK-1 6.1.12), the other one getting the km by the share of its length, on the track of the line where the other one ends
|
|
void compose(axis &Result, std::vector<editor_track::route_sample> const &Samples, pairing const &Pairs)
|
|
{
|
|
using sample = editor_track::route_sample;
|
|
auto const emit = [&](glm::dvec3 const &Point, glm::dvec2 const &Direction) {
|
|
Result.chainage.push_back(Result.points.empty() ? 0.0 : Result.chainage.back() + plan_distance(Result.points.back(), Point));
|
|
Result.points.push_back(Point);
|
|
Result.directions.push_back(Direction);
|
|
};
|
|
auto const shifted = [](glm::dvec3 const &Point, glm::dvec2 const &Direction, double const Offset) {
|
|
auto const planar{plan_of(Point) + glm::dvec2{-Direction.y, Direction.x} * Offset};
|
|
return glm::dvec3{planar.x, Point.y, planar.y};
|
|
};
|
|
std::vector<std::size_t> matched;
|
|
for (std::size_t i = 0; i < Samples.size(); ++i)
|
|
if (Pairs.match[i] >= 0)
|
|
matched.push_back(i);
|
|
if (matched.empty())
|
|
{
|
|
for (auto const &entry : Samples)
|
|
emit(entry.position, entry.direction);
|
|
return;
|
|
}
|
|
Result.between = true;
|
|
auto const half = [&](std::size_t const Index) { return Pairs.across[Index] * 0.5; };
|
|
// from between the tracks over to one of them, within kBlend of the ends of the stretch apart
|
|
auto const eased = [&](double const From, double const To, double const Low, double const High) {
|
|
return From < To ? Low * (1.0 - std::clamp(From / kBlend, 0.0, 1.0)) : High * (1.0 - std::clamp(To / kBlend, 0.0, 1.0));
|
|
};
|
|
auto const first{matched.front()};
|
|
for (std::size_t i = 0; i < first; ++i)
|
|
emit(shifted(Samples[i].position, Samples[i].direction, half(first) * std::clamp(1.0 - (Samples[first].chainage - Samples[i].chainage) / kBlend, 0.0, 1.0)), Samples[i].direction);
|
|
std::optional<axis::side> pending;
|
|
std::vector<double> shares;
|
|
for (std::size_t k = 0; k < matched.size(); ++k)
|
|
{
|
|
auto const a{matched[k]};
|
|
emit(shifted(Samples[a].position, Samples[a].direction, half(a)), Samples[a].direction);
|
|
if (pending)
|
|
{
|
|
pending->to = Result.chainage.back();
|
|
for (auto const share : shares)
|
|
pending->chainage.push_back(pending->from + share * (pending->to - pending->from));
|
|
Result.sides.push_back(std::move(*pending));
|
|
pending.reset();
|
|
}
|
|
if (k + 1 == matched.size())
|
|
break;
|
|
auto const b{matched[k + 1]};
|
|
if (b == a + 1)
|
|
continue;
|
|
// the tracks apart between a and b: the track beside from the sample by a to the one by b, turned along the line
|
|
auto const ja{Pairs.match[a]};
|
|
auto const jb{Pairs.match[b]};
|
|
std::vector<sample> beside;
|
|
for (auto j = ja; j != jb; j += ja < jb ? 1 : -1)
|
|
{
|
|
auto entry{Pairs.partner[static_cast<std::size_t>(j)]};
|
|
if (ja > jb)
|
|
entry.direction = -entry.direction;
|
|
beside.push_back(entry);
|
|
}
|
|
beside.push_back(Pairs.partner[static_cast<std::size_t>(jb)]);
|
|
if (ja > jb)
|
|
beside.back().direction = -beside.back().direction;
|
|
std::vector<double> along{0.0};
|
|
for (std::size_t q = 1; q < beside.size(); ++q)
|
|
along.push_back(along.back() + plan_distance(beside[q - 1].position, beside[q].position));
|
|
auto const own_length{Samples[b].chainage - Samples[a].chainage};
|
|
auto const beside_length{along.back()};
|
|
auto const at_share = [&](double const Share) {
|
|
auto const target{Share * beside_length};
|
|
auto const next{std::min<std::size_t>(beside.size() - 1, static_cast<std::size_t>(std::lower_bound(along.begin(), along.end(), target) - along.begin()))};
|
|
if (next == 0)
|
|
return beside.front().position;
|
|
auto const span{along[next] - along[next - 1]};
|
|
return glm::mix(beside[next - 1].position, beside[next].position, span > 1e-9 ? (target - along[next - 1]) / span : 0.0);
|
|
};
|
|
auto widest{0.0};
|
|
for (auto i = a + 1; i < b; ++i)
|
|
widest = std::max(widest, plan_distance(Samples[i].position, at_share((Samples[i].chainage - Samples[a].chainage) / own_length)));
|
|
if (widest <= kApart || own_length < 1e-6 || beside_length < 1e-6)
|
|
{
|
|
// spread a little, as through a station: between them still
|
|
for (auto i = a + 1; i < b; ++i)
|
|
{
|
|
auto const t{(Samples[i].chainage - Samples[a].chainage) / own_length};
|
|
emit(shifted(Samples[i].position, Samples[i].direction, half(a) + (half(b) - half(a)) * t), Samples[i].direction);
|
|
}
|
|
continue;
|
|
}
|
|
pending = axis::side{};
|
|
shares.clear();
|
|
pending->from = Result.chainage.back();
|
|
if (beside_length > own_length + kLonger)
|
|
{
|
|
for (std::size_t q = 1; q + 1 < beside.size(); ++q)
|
|
emit(shifted(beside[q].position, beside[q].direction, -eased(along[q], beside_length - along[q], half(a), half(b))), beside[q].direction);
|
|
pending->length = own_length;
|
|
for (auto i = a + 1; i < b; ++i)
|
|
{
|
|
pending->points.push_back(Samples[i].position);
|
|
pending->along.push_back(Samples[i].chainage - Samples[a].chainage);
|
|
shares.push_back(pending->along.back() / own_length);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (auto i = a + 1; i < b; ++i)
|
|
emit(shifted(Samples[i].position, Samples[i].direction, eased(Samples[i].chainage - Samples[a].chainage, Samples[b].chainage - Samples[i].chainage, half(a), half(b))), Samples[i].direction);
|
|
pending->length = beside_length;
|
|
for (std::size_t q = 1; q + 1 < beside.size(); ++q)
|
|
{
|
|
pending->points.push_back(beside[q].position);
|
|
pending->along.push_back(along[q]);
|
|
shares.push_back(along[q] / beside_length);
|
|
}
|
|
}
|
|
}
|
|
auto const last{matched.back()};
|
|
for (auto i = last + 1; i < Samples.size(); ++i)
|
|
emit(shifted(Samples[i].position, Samples[i].direction, half(last) * std::clamp(1.0 - (Samples[i].chainage - Samples[last].chainage) / kBlend, 0.0, 1.0)), Samples[i].direction);
|
|
}
|
|
|
|
std::optional<axis> build(std::string const &Line)
|
|
{
|
|
std::vector<std::size_t> own;
|
|
for (std::size_t index = 0; index < g_marks.size(); ++index)
|
|
if (g_marks[index].line == Line)
|
|
own.push_back(index);
|
|
if (own.empty())
|
|
return std::nullopt;
|
|
std::vector<TTrack *> tracks;
|
|
for (auto const index : own)
|
|
tracks.push_back(track_near(g_marks[index].point, kNear));
|
|
auto const start{std::find_if(tracks.begin(), tracks.end(), [](TTrack const *Track) { return Track != nullptr; })};
|
|
if (start == tracks.end())
|
|
return std::nullopt;
|
|
auto *first{*start};
|
|
auto const first_mark{own[static_cast<std::size_t>(start - tracks.begin())]};
|
|
std::vector<std::size_t> passes;
|
|
for (std::size_t index = 0; index < g_vias.size(); ++index)
|
|
if (g_vias[index].line == Line)
|
|
passes.push_back(index);
|
|
auto route{editor_track::run_route(*first, kReach)};
|
|
through(route, passes);
|
|
if (route.spans.empty())
|
|
return std::nullopt;
|
|
// the marks the main paths of the switches lead away from: the line goes their way
|
|
std::set<std::size_t> tried;
|
|
std::vector<editor_track::route_sample> samples;
|
|
pairing pairs;
|
|
auto const origin_of = [&](std::vector<editor_track::route_sample> const &Samples) {
|
|
std::size_t origin{0};
|
|
auto nearest{std::numeric_limits<double>::max()};
|
|
for (std::size_t i = 0; i < Samples.size(); ++i)
|
|
if (auto const distance{plan_distance(Samples[i].position, g_marks[first_mark].point)}; distance < nearest)
|
|
{
|
|
nearest = distance;
|
|
origin = i;
|
|
}
|
|
return origin;
|
|
};
|
|
for (std::size_t round = 0; round <= own.size(); ++round)
|
|
{
|
|
samples = editor_track::sample_route(route, kStep);
|
|
// the track beside goes along the vias the line doesn't
|
|
std::vector<std::size_t> beside;
|
|
std::copy_if(passes.begin(), passes.end(), std::back_inserter(beside), [&](std::size_t const Via) { return false == runs_by(route, Via); });
|
|
pairs = pair_up(samples, route, origin_of(samples), beside);
|
|
std::size_t away{own.size()};
|
|
for (std::size_t k = 0; k < own.size() && away == own.size(); ++k)
|
|
if (tracks[k] != nullptr && tried.count(k) == 0 && plan_gap(samples, g_marks[own[k]].point) > kOnAxis && plan_gap(pairs.partner, g_marks[own[k]].point) > kOnAxis)
|
|
away = k;
|
|
if (away == own.size())
|
|
break;
|
|
tried.insert(away);
|
|
editor_track::route found;
|
|
std::string error;
|
|
if (false == editor_track::find_route(first, tracks[away], found, error) || found.spans.size() < 2)
|
|
continue;
|
|
auto const at{index_of(route.spans, first)};
|
|
if (at == route.spans.size())
|
|
break;
|
|
std::vector<span> spans;
|
|
if (found.spans.front().forward == route.spans[at].forward)
|
|
{
|
|
spans.assign(route.spans.begin(), route.spans.begin() + at);
|
|
spans.insert(spans.end(), found.spans.begin(), found.spans.end());
|
|
auto const more{onward(found.spans.back())};
|
|
spans.insert(spans.end(), more.begin(), more.end());
|
|
}
|
|
else
|
|
{
|
|
spans = flipped(onward(found.spans.back()));
|
|
auto const back{flipped(found.spans)};
|
|
spans.insert(spans.end(), back.begin(), back.end());
|
|
spans.insert(spans.end(), route.spans.begin() + at + 1, route.spans.end());
|
|
}
|
|
route.spans = once(spans);
|
|
through(route, passes);
|
|
}
|
|
if (samples.size() < 2)
|
|
return std::nullopt;
|
|
axis result;
|
|
result.line = Line;
|
|
compose(result, samples, pairs);
|
|
for (auto const &entry : route.spans)
|
|
result.paths.emplace_back(entry.track, entry.path);
|
|
auto const paired{std::any_of(pairs.match.begin(), pairs.match.end(), [](int const Match) { return Match >= 0; })};
|
|
if (paired)
|
|
for (auto const &entry : pairs.route.spans)
|
|
result.paths.emplace_back(entry.track, entry.path);
|
|
for (auto const index : passes)
|
|
if (false == runs_by(route, index) && (false == paired || false == runs_by(pairs.route, index)))
|
|
result.blocked.push_back(index);
|
|
for (auto const index : own)
|
|
{
|
|
axis::anchor anchor;
|
|
anchor.mark = index;
|
|
anchor.chainage = chainage_at(result, g_marks[index].point, anchor.gap);
|
|
if (anchor.gap > kOnAxis)
|
|
{
|
|
result.astray.push_back(index);
|
|
continue;
|
|
}
|
|
anchor.before = g_marks[index].km;
|
|
anchor.after = g_marks[index].beyond.value_or(g_marks[index].km);
|
|
result.anchors.push_back(anchor);
|
|
}
|
|
if (result.anchors.empty())
|
|
return std::nullopt;
|
|
std::sort(result.anchors.begin(), result.anchors.end(), [](axis::anchor const &A, axis::anchor const &B) { return A.chainage < B.chainage; });
|
|
// the km rise the way the marks grow, the way the only one says
|
|
auto const &low{result.anchors.front()};
|
|
auto const &high{result.anchors.back()};
|
|
if (result.anchors.size() > 1 && std::abs(high.before - low.before) > 1e-6)
|
|
result.growth = high.before > low.before ? 1 : -1;
|
|
else
|
|
result.growth = glm::dot(g_marks[low.mark].way, direction_on(result, low.chainage)) >= 0.0 ? 1 : -1;
|
|
return result;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void reset()
|
|
{
|
|
g_marks.clear();
|
|
g_vias.clear();
|
|
g_base = {};
|
|
drop_axes();
|
|
g_changed = false;
|
|
g_loaded = false;
|
|
}
|
|
|
|
void load()
|
|
{
|
|
if (g_loaded || scene::Layers.empty())
|
|
return;
|
|
g_loaded = true;
|
|
g_marks.clear();
|
|
g_vias.clear();
|
|
drop_axes();
|
|
for (auto const layer : scene::Layers.marked_layers(kMark))
|
|
editor_records::read_kilometrage(scene::Layers.marked(layer, kMark), scene::Layers.layer(layer).context_insert(), layer, g_marks, g_vias);
|
|
g_base = current();
|
|
g_changed = false;
|
|
if (false == g_marks.empty())
|
|
WriteLog("Editor: " + std::to_string(g_marks.size()) + " kilometrage mark(s) and " + std::to_string(g_vias.size()) + " via(s) read from the scenery", logtype::generic);
|
|
}
|
|
|
|
std::vector<mark> const &marks()
|
|
{
|
|
load();
|
|
return g_marks;
|
|
}
|
|
|
|
std::vector<via> const &vias()
|
|
{
|
|
load();
|
|
return g_vias;
|
|
}
|
|
|
|
state current()
|
|
{
|
|
return {g_marks, g_vias};
|
|
}
|
|
|
|
void put(mark Entry)
|
|
{
|
|
load();
|
|
auto *track{track_near(Entry.point, kNear)};
|
|
Entry.layer = track != nullptr ? alignment_store::layer_of(*track) : scene::Layers.active();
|
|
if (false == scene::Layers.valid(Entry.layer))
|
|
Entry.layer = 1;
|
|
auto const layer{Entry.layer};
|
|
g_marks.push_back(std::move(Entry));
|
|
changed({layer});
|
|
}
|
|
|
|
void change(std::size_t const Index, mark Entry)
|
|
{
|
|
load();
|
|
if (Index >= g_marks.size())
|
|
return;
|
|
Entry.layer = g_marks[Index].layer;
|
|
g_marks[Index] = std::move(Entry);
|
|
changed({g_marks[Index].layer});
|
|
}
|
|
|
|
void remove(std::size_t const Index)
|
|
{
|
|
load();
|
|
if (Index >= g_marks.size())
|
|
return;
|
|
auto const layer{g_marks[Index].layer};
|
|
g_marks.erase(g_marks.begin() + static_cast<std::ptrdiff_t>(Index));
|
|
changed({layer});
|
|
}
|
|
|
|
void put(via Entry)
|
|
{
|
|
load();
|
|
int path{0};
|
|
auto *track{track_near(Entry.point, kNear, path)};
|
|
Entry.layer = track != nullptr ? alignment_store::layer_of(*track) : scene::Layers.active();
|
|
if (false == scene::Layers.valid(Entry.layer))
|
|
Entry.layer = 1;
|
|
auto const layer{Entry.layer};
|
|
g_vias.push_back(std::move(Entry));
|
|
changed({layer});
|
|
}
|
|
|
|
void remove_via(std::size_t const Index)
|
|
{
|
|
load();
|
|
if (Index >= g_vias.size())
|
|
return;
|
|
auto const layer{g_vias[Index].layer};
|
|
g_vias.erase(g_vias.begin() + static_cast<std::ptrdiff_t>(Index));
|
|
changed({layer});
|
|
}
|
|
|
|
std::optional<state> take_changes()
|
|
{
|
|
if (false == g_changed)
|
|
return std::nullopt;
|
|
auto before{std::move(g_base)};
|
|
g_base = current();
|
|
g_changed = false;
|
|
return before;
|
|
}
|
|
|
|
void settle()
|
|
{
|
|
g_base = current();
|
|
g_changed = false;
|
|
}
|
|
|
|
void restore(state State)
|
|
{
|
|
std::set<scene::layer_handle> layers;
|
|
for (auto const *list : {&g_marks, &State.marks})
|
|
for (auto const &entry : *list)
|
|
layers.insert(entry.layer);
|
|
for (auto const *list : {&g_vias, &State.vias})
|
|
for (auto const &entry : *list)
|
|
layers.insert(entry.layer);
|
|
g_marks = std::move(State.marks);
|
|
g_vias = std::move(State.vias);
|
|
changed(layers);
|
|
settle();
|
|
}
|
|
|
|
void follow(std::size_t const Edits)
|
|
{
|
|
if (Edits == g_edits)
|
|
return;
|
|
g_edits = Edits;
|
|
drop_axes();
|
|
}
|
|
|
|
std::size_t revision()
|
|
{
|
|
return g_revision;
|
|
}
|
|
|
|
std::vector<std::string> lines()
|
|
{
|
|
load();
|
|
std::vector<std::string> result;
|
|
for (auto const &entry : g_marks)
|
|
if (std::find(result.begin(), result.end(), entry.line) == result.end())
|
|
result.push_back(entry.line);
|
|
std::sort(result.begin(), result.end());
|
|
return result;
|
|
}
|
|
|
|
axis const *axis_of(std::string const &Line)
|
|
{
|
|
load();
|
|
auto found{g_axes.find(Line)};
|
|
if (found == g_axes.end())
|
|
found = g_axes.emplace(Line, build(Line)).first;
|
|
return found->second.has_value() ? &*found->second : nullptr;
|
|
}
|
|
|
|
double km_on(axis const &Axis, double const Chainage)
|
|
{
|
|
auto const &anchors{Axis.anchors};
|
|
if (anchors.empty())
|
|
return 0.0;
|
|
auto const growth{static_cast<double>(Axis.growth)};
|
|
// the km of an anchor on the side of the lower chainage, and of the higher one
|
|
auto const lower = [&](axis::anchor const &Anchor) { return Axis.growth > 0 ? Anchor.before : Anchor.after; };
|
|
auto const higher = [&](axis::anchor const &Anchor) { return Axis.growth > 0 ? Anchor.after : Anchor.before; };
|
|
if (Chainage <= anchors.front().chainage)
|
|
return lower(anchors.front()) - growth * (anchors.front().chainage - Chainage) / 1000.0;
|
|
if (Chainage >= anchors.back().chainage)
|
|
return higher(anchors.back()) + growth * (Chainage - anchors.back().chainage) / 1000.0;
|
|
auto const next{std::upper_bound(anchors.begin(), anchors.end(), Chainage, [](double const Value, axis::anchor const &Anchor) { return Value < Anchor.chainage; })};
|
|
auto const &to{*next};
|
|
auto const &from{*std::prev(next)};
|
|
auto const span{to.chainage - from.chainage};
|
|
if (span < 1e-6)
|
|
return higher(from);
|
|
return higher(from) + (lower(to) - higher(from)) * (Chainage - from.chainage) / span;
|
|
}
|
|
|
|
double km_on(axis const &Axis, axis::side const &Side, double const Along)
|
|
{
|
|
auto const low{km_on(Axis, Side.from + 1e-6)};
|
|
auto const high{km_on(Axis, Side.to - 1e-6)};
|
|
auto const length{Side.length};
|
|
if (length < 1e-6 || Side.to - Side.from < 1e-6)
|
|
return low;
|
|
// along the rising km: from the km the stretch starts at, the irregular hectometre before the one it ends at
|
|
auto const rising{Axis.growth > 0};
|
|
auto const start{rising ? low : high};
|
|
auto const end{rising ? high : low};
|
|
auto const s{std::clamp(rising ? Along : length - Along, 0.0, length)};
|
|
auto const scale{std::abs(end - start) / ((Side.to - Side.from) / 1000.0)}; // km of the axis to a km of the scenery
|
|
auto const regular{start + scale * length / 1000.0};
|
|
auto const last{(std::ceil(std::min(regular, end) * 10.0 - 1e-9) - 1.0) / 10.0};
|
|
if (end <= start || last <= start || scale <= 1e-9)
|
|
return start + (end - start) * s / length;
|
|
auto const at{(last - start) / scale * 1000.0};
|
|
if (s <= at || at >= length)
|
|
return start + scale * s / 1000.0;
|
|
return last + (end - last) * (s - at) / (length - at);
|
|
}
|
|
|
|
double chainage_of(axis const &Axis, glm::dvec3 const &Point, double &Gap)
|
|
{
|
|
Gap = std::numeric_limits<double>::max();
|
|
auto result{0.0};
|
|
auto const point{plan_of(Point)};
|
|
for (std::size_t i = 1; i < Axis.points.size(); ++i)
|
|
{
|
|
auto const a{plan_of(Axis.points[i - 1])};
|
|
auto const along{plan_of(Axis.points[i]) - a};
|
|
auto const length2{glm::dot(along, along)};
|
|
auto const t{length2 > 1e-12 ? std::clamp(glm::dot(point - a, along) / length2, 0.0, 1.0) : 0.0};
|
|
if (auto const gap{glm::distance(point, a + along * t)}; gap < Gap)
|
|
{
|
|
Gap = gap;
|
|
result = Axis.chainage[i - 1] + (Axis.chainage[i] - Axis.chainage[i - 1]) * t;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
double chainage_at(axis const &Axis, glm::dvec3 const &Point, double &Gap)
|
|
{
|
|
auto result{chainage_of(Axis, Point, Gap)};
|
|
auto const point{plan_of(Point)};
|
|
for (auto const &entry : Axis.sides)
|
|
for (std::size_t i = 1; i < entry.points.size(); ++i)
|
|
{
|
|
auto const a{plan_of(entry.points[i - 1])};
|
|
auto const along{plan_of(entry.points[i]) - a};
|
|
auto const length2{glm::dot(along, along)};
|
|
auto const t{length2 > 1e-12 ? std::clamp(glm::dot(point - a, along) / length2, 0.0, 1.0) : 0.0};
|
|
if (auto const gap{glm::distance(point, a + along * t)}; gap < Gap)
|
|
{
|
|
Gap = gap;
|
|
result = entry.chainage[i - 1] + (entry.chainage[i] - entry.chainage[i - 1]) * t;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
glm::dvec3 point_on(axis const &Axis, double const Chainage)
|
|
{
|
|
if (Axis.points.empty())
|
|
return glm::dvec3{0.0};
|
|
auto const after{std::lower_bound(Axis.chainage.begin(), Axis.chainage.end(), Chainage)};
|
|
if (after == Axis.chainage.begin())
|
|
return Axis.points.front();
|
|
if (after == Axis.chainage.end())
|
|
return Axis.points.back();
|
|
auto const i{static_cast<std::size_t>(after - Axis.chainage.begin())};
|
|
auto const step{Axis.chainage[i] - Axis.chainage[i - 1]};
|
|
return glm::mix(Axis.points[i - 1], Axis.points[i], step > 1e-9 ? (Chainage - Axis.chainage[i - 1]) / step : 0.0);
|
|
}
|
|
|
|
glm::dvec2 direction_on(axis const &Axis, double const Chainage)
|
|
{
|
|
if (Axis.directions.empty())
|
|
return {0.0, 1.0};
|
|
auto const after{std::lower_bound(Axis.chainage.begin(), Axis.chainage.end(), Chainage)};
|
|
auto const i{std::min(Axis.directions.size() - 1, static_cast<std::size_t>(after - Axis.chainage.begin()))};
|
|
return Axis.directions[i];
|
|
}
|
|
|
|
bool km_at(std::string const &Line, glm::dvec3 const &Point, double &Km, double const Reach)
|
|
{
|
|
auto const *found{axis_of(Line)};
|
|
if (found == nullptr)
|
|
return false;
|
|
double gap{0.0};
|
|
auto const chainage{chainage_of(*found, Point, gap)};
|
|
auto const point{plan_of(Point)};
|
|
axis::side const *apart{nullptr};
|
|
auto along{0.0};
|
|
for (auto const &entry : found->sides)
|
|
for (std::size_t i = 1; i < entry.points.size(); ++i)
|
|
{
|
|
auto const a{plan_of(entry.points[i - 1])};
|
|
auto const ab{plan_of(entry.points[i]) - a};
|
|
auto const length2{glm::dot(ab, ab)};
|
|
auto const t{length2 > 1e-12 ? std::clamp(glm::dot(point - a, ab) / length2, 0.0, 1.0) : 0.0};
|
|
if (auto const distance{glm::distance(point, a + ab * t)}; distance < gap)
|
|
{
|
|
gap = distance;
|
|
apart = &entry;
|
|
along = entry.along[i - 1] + (entry.along[i] - entry.along[i - 1]) * t;
|
|
}
|
|
}
|
|
if (gap > Reach)
|
|
return false;
|
|
Km = apart != nullptr ? km_on(*found, *apart, along) : km_on(*found, chainage);
|
|
return true;
|
|
}
|
|
|
|
std::string line_at(glm::dvec3 const &Point, double const Reach)
|
|
{
|
|
std::string result;
|
|
auto nearest{Reach};
|
|
for (auto const &line : lines())
|
|
if (auto const *found{axis_of(line)}; found != nullptr)
|
|
{
|
|
double gap{0.0};
|
|
chainage_at(*found, Point, gap);
|
|
if (gap < nearest)
|
|
{
|
|
nearest = gap;
|
|
result = line;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int growth_at(std::string const &Line, glm::dvec3 const &Point, glm::dvec2 const &Way, double const Reach)
|
|
{
|
|
auto const *found{axis_of(Line)};
|
|
if (found == nullptr)
|
|
return 0;
|
|
double gap{0.0};
|
|
auto const chainage{chainage_at(*found, Point, gap)};
|
|
if (gap > Reach)
|
|
return 0;
|
|
return glm::dot(direction_on(*found, chainage) * static_cast<double>(found->growth), Way) >= 0.0 ? 1 : -1;
|
|
}
|
|
|
|
std::vector<hectometre> hectometres(axis const &Axis)
|
|
{
|
|
std::vector<hectometre> result;
|
|
std::vector<double> jumps;
|
|
for (auto const &anchor : Axis.anchors)
|
|
if (std::abs(anchor.after - anchor.before) >= 1e-6)
|
|
jumps.push_back(anchor.chainage);
|
|
auto const along = [&](std::vector<glm::dvec3> const &Points, std::vector<double> const &Km, std::vector<double> const &Chainage) {
|
|
for (std::size_t i = 1; i < Points.size(); ++i)
|
|
{
|
|
auto const from{std::min(Chainage[i - 1], Chainage[i])};
|
|
auto const to{std::max(Chainage[i - 1], Chainage[i])};
|
|
if (std::any_of(jumps.begin(), jumps.end(), [&](double const Jump) { return Jump >= from - 1e-6 && Jump <= to + 1e-6; }))
|
|
continue;
|
|
auto const k0{Km[i - 1]};
|
|
auto const k1{Km[i]};
|
|
if (std::abs(k1 - k0) < 1e-9)
|
|
continue;
|
|
auto way{plan_of(Points[i] - Points[i - 1])};
|
|
way = glm::length(way) > 1e-9 ? glm::normalize(way) : glm::dvec2{0.0, 1.0};
|
|
auto const first{static_cast<int>(std::floor(std::min(k0, k1) * 10.0 + 1e-9)) + 1};
|
|
auto const last{static_cast<int>(std::floor(std::max(k0, k1) * 10.0 + 1e-9))};
|
|
for (auto number = first; number <= last && number < first + 10; ++number)
|
|
result.push_back({glm::mix(Points[i - 1], Points[i], (number / 10.0 - k0) / (k1 - k0)), k1 > k0 ? way : -way, number});
|
|
}
|
|
};
|
|
// at a jump the post of the km on from it, as the posts of a line built anew begin there, else of the km the jump is reached at
|
|
for (auto const &anchor : Axis.anchors)
|
|
{
|
|
if (std::abs(anchor.after - anchor.before) < 1e-6)
|
|
continue;
|
|
auto const round = [](double const Km) { return std::abs(Km * 10.0 - std::round(Km * 10.0)) < 1e-4; };
|
|
auto const km{round(anchor.after) ? anchor.after : anchor.before};
|
|
if (round(km))
|
|
result.push_back({point_on(Axis, anchor.chainage), direction_on(Axis, anchor.chainage) * static_cast<double>(Axis.growth), static_cast<int>(std::lround(km * 10.0))});
|
|
}
|
|
std::vector<double> km;
|
|
for (auto const chainage : Axis.chainage)
|
|
km.push_back(km_on(Axis, chainage));
|
|
along(Axis.points, km, Axis.chainage);
|
|
for (auto const &side : Axis.sides)
|
|
{
|
|
km.clear();
|
|
for (auto const distance : side.along)
|
|
km.push_back(km_on(Axis, side, distance));
|
|
along(side.points, km, side.chainage);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
TTrack *track_near(glm::dvec3 const &Point, double const Reach)
|
|
{
|
|
TTrack *best{nullptr};
|
|
auto nearest{Reach};
|
|
for (auto *track : simulation::Paths.sequence())
|
|
{
|
|
if (track == nullptr || track->m_editorremoved || (track->iCategoryFlag & 1) == 0 || false == editor_track::is_supported(*track) || track->m_paths.empty())
|
|
continue;
|
|
auto const &ends{track->m_paths.front().points};
|
|
auto const chord{plan_distance(ends[segment_data::point::start], ends[segment_data::point::end])};
|
|
if (std::min(plan_distance(ends[segment_data::point::start], Point), plan_distance(ends[segment_data::point::end], Point)) > chord * 0.8 + nearest + 1.0)
|
|
continue;
|
|
auto const on{editor_track::point_at(*track, editor_track::nearest_parameter(*track, Point))};
|
|
if (auto const distance{plan_distance(on, Point)}; distance < nearest)
|
|
{
|
|
nearest = distance;
|
|
best = track;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
int path_near(TTrack const &Track, glm::dvec3 const &Point, glm::dvec3 &On)
|
|
{
|
|
int result{0};
|
|
auto nearest{std::numeric_limits<double>::max()};
|
|
On = Point;
|
|
for (int path = 0; path < static_cast<int>(Track.m_paths.size()); ++path)
|
|
{
|
|
geometry::bezier const curve{Track.m_paths[path]};
|
|
auto previous{curve.point(0.0)};
|
|
for (int i = 1; i <= 64; ++i)
|
|
{
|
|
auto const next{curve.point(i / 64.0)};
|
|
auto const ab{plan_of(next - previous)};
|
|
auto const length2{glm::dot(ab, ab)};
|
|
auto const t{length2 > 1e-12 ? std::clamp(glm::dot(plan_of(Point - previous), ab) / length2, 0.0, 1.0) : 0.0};
|
|
auto const at{glm::mix(previous, next, t)};
|
|
// where the paths of a switch run together, the first one
|
|
if (auto const distance{plan_distance(at, Point)}; distance < nearest - 1e-3)
|
|
{
|
|
nearest = distance;
|
|
result = path;
|
|
On = at;
|
|
}
|
|
previous = next;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
TTrack *track_near(glm::dvec3 const &Point, double const Reach, int &Path)
|
|
{
|
|
TTrack *best{nullptr};
|
|
auto nearest{Reach};
|
|
Path = 0;
|
|
for (auto *track : simulation::Paths.sequence())
|
|
{
|
|
if (track == nullptr || track->m_editorremoved || (track->iCategoryFlag & 1) == 0 || false == editor_track::is_supported(*track) || track->m_paths.empty())
|
|
continue;
|
|
auto const &ends{track->m_paths.front().points};
|
|
auto const chord{plan_distance(ends[segment_data::point::start], ends[segment_data::point::end])};
|
|
if (std::min(plan_distance(ends[segment_data::point::start], Point), plan_distance(ends[segment_data::point::end], Point)) > chord * 0.8 + nearest + 1.0)
|
|
continue;
|
|
glm::dvec3 on{0.0};
|
|
auto const path{path_near(*track, Point, on)};
|
|
if (auto const distance{plan_distance(on, Point)}; distance < nearest)
|
|
{
|
|
nearest = distance;
|
|
best = track;
|
|
Path = path;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
std::string text(double const Km)
|
|
{
|
|
auto result{format("%.3f", Km)};
|
|
std::replace(result.begin(), result.end(), '.', ',');
|
|
return result;
|
|
}
|
|
|
|
} // namespace kilometrage
|