18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 17:46:48 +01:00
Files
maszyna/editor/editorTrace.hpp
T
maj00r 656c5b4d75 feat(editor): the line traced on the photo: the straights and the curves recognised from the points clicked along the track
quirky-crafting-summit, Etap 1: the user clicks points along the middle of the track seen on the photo, on its straights and on its
curves, and the editor tells the straights from the curves itself. editor/editorTrace.*, the domain alone:

- Each point with the noise of its click, sigma: two pixels of the view at it, 5 cm at least. A run is a straight where its points keep
  within 3 sigma of one and no curve brings the sum of their squares down by more than the noise would (9 sigma^2 a parameter); else a
  curve, a gentle one too (R 5 to 30 km, nearly straight).
- The line is a chain from its start of straights, transitions and arcs fitted by Levenberg-Marquardt to the points, the distance of each
  point measured exactly (to an arc by its centre, to a transition by Newton on Gauss-Legendre), robust beyond 3 sigma. The shapes tried
  at a click grow from the one before: a curve opened on the straight, closed where the line leaves it, a radius more where the curvature
  changes (compound, up to three, its arcs 2.5 times apart at most, the transitions between them of the norms), a curve the other way
  after a straight or with none, the last curve begun afresh where the shape of before can't follow. The transitions in and out keep to
  the ones of the norms at the speed the radius allows unless the points show them shorter or longer (20 m a sigma).
- The line may start anywhere, inside a curve too (its heading from the fit, not from the first two points), or at a free end held: its
  heading and curvature, a curve going on from it, from a switch at its radius. And end at one (tracer::ended).
- Frozen: a curve with two points on the straight after it is fitted no more; its radii and its start stay, only its last arc and its
  transition out still turn the straight after it about its end till the next curve freezes. A click fits the open end alone, so it costs
  no more at the end of a long line. The radii of a curve freezing are chosen among 1, 2 and 3 once more.
- A click takes a budget of evaluations, the same for the same points: where it's spent, or no shape keeps to the points within the noise
  where the line did before, the line stays as it was and the point is told (misfit); two such running, the line goes on with the best of
  them. The same points always make the same line.
- The design of the line: a vertex where the straights either side of each curve cross, its radii, shares and transitions; a line
  beginning or ending within an arc goes on from it or into it (start_curved, end_curved). The norms of each curve asked at the speed of
  the line, else at a speed lower by steps of 10 km/h till it meets them (vertex::speed), the cant for that speed; refused only below the
  least radius of the kind of the track.
- spacing_to: the spacing a run of points keeps from a neighbouring line, parallel where it varies within their noise (for the stations
  traced on the photo, later).

fit_trace_stretch, which the plan named for the curves between straights, takes 0.2 to 3 s a stretch (189 ms for 10 points along a curve
of one radius, 3.1 s for 60 along one of three, RelWithDebInfo) and weighs no noise of the clicks: at 0.2 m it took a curve of three radii
for four. The chain is fitted on its own: 22 ms the slowest click of 60 points along a curve of three radii.

Tests: U, test_trace.cpp: lines of the norms sampled as clicks, sigma 0.1 and 0.2 m, fixed seeds: a straight; R 8 and 20 km over 300 to
1000 m nearly straight; one radius within 2 %; compound curves of 2 and 3 radii within 2 %; an S-curve; a line started inside a curve;
from a free end straight and curved, into one; the curves frozen unchanged by the clicks after them; a point clicked far off leaving the
line as it was; the same points the same line; a curve below the norms slowed down, one below the least radius refused; the worst click,
of 60 points along three radii, within 30 ms; a run parallel to a neighbour.
2026-11-24 18:12:01 +01:00

150 lines
6.8 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 <cstddef>
#include <optional>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include "editor/editorAlignment.hpp"
// The line traced on the aerial photo: the points clicked one after another along the middle of the track seen on the photo, each with
// the noise of its click, made into the straights and the curves the track runs along, the way the track was built: a straight where the
// points keep to one within the noise, else a curve, a very gentle one too (R 5 - 20 km, "nearly straight"), of one radius or compound,
// with its transitions and its cant as the norms ask for at the speed, and curves the other way with or without a straight between them.
// The line may start at a free end, its heading and its curvature held, or anywhere, inside a curve too.
//
// Each click fits the open end of the line only: a curve with two points clicked on the straight after it is frozen, the straight still
// turning about the end of its last arc until the next curve is frozen, so that what lies behind doesn't change and a click costs as much
// at the end of a long line as at its start. A click's fit takes a budget of evaluations of the line, the same for the same points: where
// it's spent, the fit doesn't converge or the line comes out worse than before the point, the line stays as it was and the point is told
// as one which doesn't fit. A curve sharper than the norms allow at the speed of the line is laid at the speed its radius and its
// transitions allow, the trains slowed down along it; only one below the least radius of the kind of the track is refused.
//
// The domain alone: no scene, no tracks, no user interface; the plan in metres, x and y of the plan (x and z of the scenery).
namespace trace
{
// the noise of a click: two pixels of the view at it, 5 cm at least
double click_sigma(double const MetresPerPixel);
// what the line is held to at its start or its end: the free end of a track it goes on from or into (later the diverging end of a turnout).
// The heading as the line runs, the curvature signed by the side it turns to as the line runs, positive to the left
struct hold
{
glm::dvec2 position{0.0};
glm::dvec2 heading{1.0, 0.0};
double curvature{0.0};
double cant{0.0}; // mm, of the track there, leaning into its turn
bool fixed{false}; // the diverging track of a switch: a curve goes on at its radius with no transition, a straight may begin right there
};
struct settings
{
double speed{100.0}; // km/h of the line
alignment::limits norms;
alignment::transition_shape shape{alignment::transition_shape::cubic_parabola};
double radius_least{0.0}; // m: a curve sharper is refused at any speed, the least radius of the norms for the kind of the track; 0 none
double speed_step{10.0}; // km/h: a curve below the norms is slowed down by these
double speed_least{10.0}; // km/h: the slowest a curve is laid for
std::size_t budget{20000}; // evaluations of the line a click's fit may take
bool operator==(settings const &) const = default;
};
// a curve of the line traced, one vertex of its design
struct curve
{
alignment::vertex vertex; // as the design has it, its speed among it where it's laid slower than the line
std::vector<double> radii; // of its arcs, the first one first
double speed{0.0}; // km/h it's laid for: the one of the line, or below it where its radius or its transitions don't meet the norms at that
bool restricted{false}; // slower than the line
bool refused{false}; // sharper than the least radius of the kind of the track
bool nearly_straight{false}; // R 5 km and more
bool open{false}; // the line ends within its last arc
bool inside{false}; // the line starts within its first arc
bool frozen{false};
double from{0.0}; // chainage of its start and of its end along the line laid
double to{0.0};
double sharpest() const;
bool compound() const { return radii.size() > 1; }
};
struct result
{
bool valid{false}; // a line to lay: two points clicked at least, the design computed
alignment::design design; // in the plan, the heights 0: its start and end, the vertices of its curves
alignment::result laid; // computed
std::vector<curve> curves; // one for each vertex of the design
std::vector<double> offsets; // of the points clicked from the line laid, positive to the left
std::vector<double> sigmas; // of the points
std::vector<char> misfit; // a point the line couldn't be fitted to: the line stayed as it was before it
std::vector<char> on_straight; // a point on a straight of the line, not on a curve
double rms{0.0};
double worst{0.0}; // m, the farthest point
bool refused{false}; // a curve below the least radius: not to be laid
std::string error; // why there's no line, or why it's refused
std::size_t frozen{0}; // curves frozen
// of the last click
double milliseconds{0.0};
std::size_t evaluations{0};
bool budget_spent{false};
};
// the line built click after click
class tracer
{
public:
tracer();
tracer(settings const &Settings, std::optional<hold> const &Start = {});
tracer(tracer const &Other);
tracer &operator=(tracer const &Other);
~tracer();
// a point clicked, with the noise of its click in metres
void add(glm::dvec2 const &Point, double const Sigma);
// the last point taken back, the line as it was before it
void pop();
std::size_t size() const;
std::vector<glm::dvec2> const &points() const;
settings const &options() const;
std::optional<hold> const &start() const;
// the line of the points clicked so far
result const &now() const;
// the same ending at End, a free end the last click reached (the end's position isn't among the points)
result ended(hold const &End) const;
private:
struct impl;
impl *m_impl{nullptr};
};
// the line of the points clicked one after another, as a tracer makes it
result traced(std::vector<glm::dvec2> const &Points, std::vector<double> const &Sigmas, settings const &Settings, std::optional<hold> const &Start = {});
// how far a run of the points of a line traced keeps from a neighbouring line (points along it, in order), each point measured square
// to the neighbour: the mean spacing, positive to the left of the neighbour as it runs, and how much it varies; a run keeping a constant
// spacing within the noise of its points is parallel
struct spacing
{
bool parallel{false};
double mean{0.0};
double spread{0.0}; // the root mean square of the departures from the mean
std::size_t points{0};
};
spacing spacing_to(std::vector<glm::dvec2> const &Points, std::vector<double> const &Sigmas, std::vector<glm::dvec2> const &Neighbour);
// the radii of the curve as the labels tell them: "850 m", "600/450 m", "9 km"
std::string radii_text(curve const &Curve);
} // namespace trace