From f83afa8a96485fc5a58522da0244ba0098e099d3 Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:30:12 +0100 Subject: [PATCH] feat(editor): the elements and the line of the track geometry, a module of their own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first part of E2 (editor-tracks-domain.md §2 - §4): the geometry of a line computed in one place, editor/layout, standing on its own with no scene, no tracks and no user interface. Nothing of the editor uses it yet; the route design keeps its own solver. - layoutElements: the elements of a line, the straight, the arc, the clothoid (the curvature changing evenly, from any curvature to any other), the Bloss curve (3t^2 - 2t^3) and the cubic parabola (y = x^3 / 6RL by the abscissa, the length along it its own): the length, the curvature along, the point and the heading at a distance, a point offset from the axis, the shift of the arc. The local forms of the clothoid and the cubic parabola and the blossom of a Bezier curve are the ones of editorAlignment.cpp, moved here word for word; the alignment calls them, so the two agree to the bit and its tests run as before. - layoutLine: the line as a tangent polygon between the points of its two end nodes (it has no coordinates of its own there), its vertices with the radius, the transitions in and out and the cant, a grade line of breaks with their vertical curves, the shape of the transitions and of the cant ramps (linear or Bloss). solve() lays it: each curve fitted in the frame of its tangent point from its deflection (the arc what the transitions leave of it), the straights what the tangents leave of the sides, every element placed from the polygon so nothing drifts; the cant held along the arcs, ramped along the transitions, a step refused; the heights and the grades by the breaks. An end can go on from the curvature and the cant its node holds (a line split inside a curve): its curve begins right at the node. What can't be laid is an error with the vertex and the metres: curves overlapping, a curve starting before the start, transitions turning more than the tangents, a cant step, a vertical curve past the end. - described(): the line as the norms module reads it (elements, cant points, grade elements), a Bloss ramp seen by eight points so its steeper middle counts. preferred_curve(): the cant and the transition length the profile prefers for a radius at the line class, the deficiency, the excess and the greatest cant of PREFER, else of WARN, the transitions long enough for dD/ds, dD/dt and dI/dt. - norms::line_class compares by its values (operator==), the line keeping it compares whole. Tests (U): the clothoid, the cubic parabola, the Bloss curve and the arc against the series of the textbooks (the end point, the length of the parabola, the shift: L^2/24R and L^2/40R), the point as the integral of the heading, the circles alongside an arc, the curve of a vertex by its tangent length (R + p) tan(a/2) + x_m. --- CMakeLists.txt | 2 + editor/editorAlignment.cpp | 70 +--- editor/layout/layoutElements.cpp | 302 +++++++++++++++ editor/layout/layoutElements.hpp | 112 ++++++ editor/layout/layoutLine.cpp | 582 ++++++++++++++++++++++++++++ editor/layout/layoutLine.hpp | 197 ++++++++++ editor/norms/norms.hpp | 2 + tests/unit/CMakeLists.txt | 3 + tests/unit/test_layout_elements.cpp | 159 ++++++++ 9 files changed, 1365 insertions(+), 64 deletions(-) create mode 100644 editor/layout/layoutElements.cpp create mode 100644 editor/layout/layoutElements.hpp create mode 100644 editor/layout/layoutLine.cpp create mode 100644 editor/layout/layoutLine.hpp create mode 100644 tests/unit/test_layout_elements.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bcc8090d..4c74c7216 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -216,6 +216,8 @@ set(SOURCES "editor/norms/normsLimits.cpp" "editor/norms/normsEngine.cpp" "editor/norms/normsLine.cpp" +"editor/layout/layoutElements.cpp" +"editor/layout/layoutLine.cpp" "editor/editorNormsReport.cpp" "editor/editorProfile.cpp" "editor/editorGauge.cpp" diff --git a/editor/editorAlignment.cpp b/editor/editorAlignment.cpp index aef5e31b9..5ee67d176 100644 --- a/editor/editorAlignment.cpp +++ b/editor/editorAlignment.cpp @@ -11,6 +11,7 @@ http: #include "editor/editorAlignment.hpp" #include "editor/editorFormat.hpp" #include "editor/editorGeometry.hpp" +#include "editor/layout/layoutElements.hpp" #include "utilities/translation.h" #include @@ -44,64 +45,15 @@ double ceil_to(double const Value, double const Step) return std::ceil(Value / Step - 1e-9) * Step; } -struct local_point -{ - double x{0.0}; - double y{0.0}; - double angle{0.0}; -}; +using layout::blossom; +using layout::local_point; +using layout::spiral_point; local_point transition_point(transition_shape const Shape, double const Length, double const Radius, double const Parameter) { - if (Length <= 0.0 || Radius <= 0.0) - return {}; if (Shape == transition_shape::cubic_parabola) - { - auto const x{Length * Parameter}; - return {x, x * x * x / (6.0 * Radius * Length), std::atan(x * x / (2.0 * Radius * Length))}; - } - auto const distance{Length * Parameter}; - auto const angle = [&](double const S) { return S * S / (2.0 * Radius * Length); }; - // Simpson over 64 steps at most keeps a transition of a radian within a millimetre, however long - int const steps{std::clamp(static_cast(std::ceil(distance / 0.5)) * 2, 8, 64)}; - double const h{distance / steps}; - double x{0.0}; - double y{0.0}; - for (int i = 0; i <= steps; ++i) - { - double const weight = (i == 0 || i == steps) ? 1.0 : (i % 2 ? 4.0 : 2.0); - auto const a{angle(i * h)}; - x += weight * std::cos(a); - y += weight * std::sin(a); - } - return {x * h / 3.0, y * h / 3.0, angle(distance)}; -} - -local_point spiral_point(double const Start, double const End, double const Length, double const Distance) -{ - if (Distance <= 0.0) - return {}; - // an arc, or a straight, has its point at once - if (Start == End) - { - auto const angle{Start * Distance}; - if (std::abs(angle) < 1e-9) - return {Distance, 0.0, 0.0}; - return {std::sin(angle) / Start, (1.0 - std::cos(angle)) / Start, angle}; - } - auto const heading = [&](double const S) { return Start * S + (End - Start) * S * S / (2.0 * std::max(Length, 1e-9)); }; - int const steps{std::clamp(static_cast(std::ceil(Distance / 0.5)) * 2, 8, 64)}; - double const h{Distance / steps}; - double x{0.0}; - double y{0.0}; - for (int i = 0; i <= steps; ++i) - { - double const weight = (i == 0 || i == steps) ? 1.0 : (i % 2 ? 4.0 : 2.0); - auto const a{heading(i * h)}; - x += weight * std::cos(a); - y += weight * std::sin(a); - } - return {x * h / 3.0, y * h / 3.0, heading(Distance)}; + return layout::parabola_point(Length, Radius, Parameter); + return layout::clothoid_point(Length, Radius, Parameter); } struct compound_curve @@ -257,16 +209,6 @@ double signed_cant(result const &Result, element const &Element, double const Di return span < 1e-9 ? next->y : previous->y + (next->y - previous->y) * (chainage - previous->x) / span; } -glm::dvec2 blossom(std::array const &Points, double const A, double const B, double const C) -{ - glm::dvec2 r[3], s[2]; - for (int i = 0; i < 3; ++i) - r[i] = glm::mix(Points[i], Points[i + 1], A); - for (int i = 0; i < 2; ++i) - s[i] = glm::mix(r[i], r[i + 1], B); - return glm::mix(s[0], s[1], C); -} - std::size_t element_pieces(element const &Element, transition_shape const Shape, int const Transitions, double const Arcangle) { if (Element.length < 1e-3) diff --git a/editor/layout/layoutElements.cpp b/editor/layout/layoutElements.cpp new file mode 100644 index 000000000..8b11cf416 --- /dev/null +++ b/editor/layout/layoutElements.cpp @@ -0,0 +1,302 @@ +/* +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/layout/layoutElements.hpp" + +#include +#include + +namespace layout +{ + +namespace +{ + +double constexpr kPi{3.14159265358979323846}; + +// the heading turned along a curve integrated into the point of its frame, by Simpson's rule over steps of about a quarter of a metre, +// 64 at most: a transition of a radian stays within a millimetre however long, a usual one within a micrometre +template local_point integrated(double const Distance, Heading const &Angle) +{ + int const steps{std::clamp(static_cast(std::ceil(Distance / 0.5)) * 2, 8, 64)}; + double const h{Distance / steps}; + double x{0.0}; + double y{0.0}; + for (int i = 0; i <= steps; ++i) + { + double const weight = (i == 0 || i == steps) ? 1.0 : (i % 2 ? 4.0 : 2.0); + auto const a{Angle(i * h)}; + x += weight * std::cos(a); + y += weight * std::sin(a); + } + return {x * h / 3.0, y * h / 3.0, Angle(Distance)}; +} + +double sign_of(double const Value) +{ + return Value < 0.0 ? -1.0 : 1.0; +} + +glm::dvec2 rotated(glm::dvec2 const &Vector, double const Angle) +{ + auto const c{std::cos(Angle)}; + auto const s{std::sin(Angle)}; + return {Vector.x * c - Vector.y * s, Vector.x * s + Vector.y * c}; +} + +// the cubic parabola of the abscissa X and the radius: the slope and the curvature at the abscissa, the radius taken as it is +double parabola_slope(double const X, double const Radius, double const At) +{ + return At * At / (2.0 * Radius * X); +} + +double parabola_curvature(double const X, double const Radius, double const At) +{ + auto const slope{parabola_slope(X, Radius, At)}; + return At / (Radius * X) / std::pow(1.0 + slope * slope, 1.5); +} + +// the abscissa of the cubic parabola at the length along it +double parabola_abscissa(double const X, double const Radius, double const Length) +{ + auto x{std::min(Length, X)}; + for (int i = 0; i < 40; ++i) + { + auto const slope{parabola_slope(X, Radius, x)}; + auto const step{(parabola_length(X, Radius, x) - Length) / std::sqrt(1.0 + slope * slope)}; + x = std::clamp(x - step, 0.0, X); + if (std::abs(step) < 1e-12) + break; + } + return x; +} + +// the cubic parabola from the tangent, turning to the side of the radius, at the length along it +local_point parabola_entering(double const X, double const Radius, double const Length) +{ + auto const side{sign_of(Radius)}; + auto const x{parabola_abscissa(X, std::abs(Radius), Length)}; + auto const point{parabola_point(X, std::abs(Radius), X > 0.0 ? x / X : 0.0)}; + return {point.x, side * point.y, side * point.angle}; +} + +} // namespace + +local_point parabola_point(double const Length, double const Radius, double const Parameter) +{ + if (Length <= 0.0 || Radius <= 0.0) + return {}; + auto const x{Length * Parameter}; + return {x, x * x * x / (6.0 * Radius * Length), std::atan(x * x / (2.0 * Radius * Length))}; +} + +local_point clothoid_point(double const Length, double const Radius, double const Parameter) +{ + if (Length <= 0.0 || Radius <= 0.0) + return {}; + auto const distance{Length * Parameter}; + auto const angle = [&](double const S) { return S * S / (2.0 * Radius * Length); }; + return integrated(distance, angle); +} + +local_point spiral_point(double const Start, double const End, double const Length, double const Distance) +{ + if (Distance <= 0.0) + return {}; + // an arc, or a straight, has its point at once + if (Start == End) + { + auto const angle{Start * Distance}; + if (std::abs(angle) < 1e-9) + return {Distance, 0.0, 0.0}; + return {std::sin(angle) / Start, (1.0 - std::cos(angle)) / Start, angle}; + } + auto const heading = [&](double const S) { return Start * S + (End - Start) * S * S / (2.0 * std::max(Length, 1e-9)); }; + return integrated(Distance, heading); +} + +double bloss_share(double const T) +{ + return T * T * (3.0 - 2.0 * T); +} + +local_point bloss_point(double const Start, double const End, double const Length, double const Distance) +{ + if (Distance <= 0.0) + return {}; + if (Start == End) + return spiral_point(Start, End, Length, Distance); + auto const length{std::max(Length, 1e-9)}; + // the heading is the integral of the curvature: Start s + (End - Start) L (t^3 - t^4 / 2) + auto const heading = [&](double const S) { + auto const t{S / length}; + return Start * S + (End - Start) * length * (t * t * t - 0.5 * t * t * t * t); + }; + return integrated(Distance, heading); +} + +glm::dvec2 blossom(std::array const &Points, double const A, double const B, double const C) +{ + glm::dvec2 r[3], s[2]; + for (int i = 0; i < 3; ++i) + r[i] = glm::mix(Points[i], Points[i + 1], A); + for (int i = 0; i < 2; ++i) + s[i] = glm::mix(r[i], r[i + 1], B); + return glm::mix(s[0], s[1], C); +} + +double parabola_length(double const X, double const Radius, double const Upto) +{ + if (Upto <= 0.0 || X <= 0.0 || Radius == 0.0) + return std::max(0.0, Upto); + auto const radius{std::abs(Radius)}; + int constexpr steps{64}; + double const h{Upto / steps}; + double length{0.0}; + for (int i = 0; i <= steps; ++i) + { + double const weight = (i == 0 || i == steps) ? 1.0 : (i % 2 ? 4.0 : 2.0); + auto const slope{parabola_slope(X, radius, i * h)}; + length += weight * std::sqrt(1.0 + slope * slope); + } + return length * h / 3.0; +} + +double element::curvature(double const Distance) const +{ + auto const t{length > 0.0 ? std::clamp(Distance / length, 0.0, 1.0) : 1.0}; + switch (kind) + { + case element_kind::straight: return 0.0; + case element_kind::arc: return curvature_start; + case element_kind::clothoid: return curvature_start + (curvature_end - curvature_start) * t; + case element_kind::bloss: return curvature_start + (curvature_end - curvature_start) * bloss_share(t); + case element_kind::cubic_parabola: + { + auto const along{entering ? std::clamp(Distance, 0.0, length) : length - std::clamp(Distance, 0.0, length)}; + auto const radius{std::abs(parabola_radius)}; + return sign_of(parabola_radius) * parabola_curvature(parabola_x, radius, parabola_abscissa(parabola_x, radius, along)); + } + } + return 0.0; +} + +local_point element::local(double const Distance) const +{ + auto const d{std::clamp(Distance, 0.0, length)}; + switch (kind) + { + case element_kind::straight: return {d, 0.0, 0.0}; + case element_kind::arc: return spiral_point(curvature_start, curvature_start, length, d); + case element_kind::clothoid: return spiral_point(curvature_start, curvature_end, length, d); + case element_kind::bloss: return bloss_point(curvature_start, curvature_end, length, d); + case element_kind::cubic_parabola: + { + if (entering) + return parabola_entering(parabola_x, parabola_radius, d); + // out of the curve: the parabola turning the other way, run backwards from its curved end. The frame of the start looks back + // along it, half a turn from its heading there + auto const whole{parabola_entering(parabola_x, -parabola_radius, length)}; + auto const here{parabola_entering(parabola_x, -parabola_radius, length - d)}; + auto const turned{rotated(glm::dvec2{here.x - whole.x, here.y - whole.y}, -(whole.angle + kPi))}; + return {turned.x, turned.y, here.angle - whole.angle}; + } + } + return {}; +} + +glm::dvec2 element::position(double const Distance) const +{ + auto const point{local(Distance)}; + return origin + direction * point.x + left_of(direction) * point.y; +} + +glm::dvec2 element::tangent(double const Distance) const +{ + return rotated(direction, local(Distance).angle); +} + +glm::dvec2 element::offset_point(double const Distance, double const Offset) const +{ + return position(Distance) + left_of(tangent(Distance)) * Offset; +} + +double element::shift() const +{ + if (kind == element_kind::cubic_parabola) + { + auto const end{parabola_entering(parabola_x, std::abs(parabola_radius), length)}; + auto const radius{std::abs(parabola_radius)}; + return end.y - radius * (1.0 - std::cos(end.angle)); + } + if (kind != element_kind::clothoid && kind != element_kind::bloss) + return 0.0; + if ((curvature_start == 0.0) == (curvature_end == 0.0)) + return 0.0; + // the one from the tangent of the same curve: the shift is the same at either end + auto const curved{std::abs(curvature_start == 0.0 ? curvature_end : curvature_start)}; + auto const end{kind == element_kind::clothoid ? spiral_point(0.0, curved, length, length) : bloss_point(0.0, curved, length, length)}; + return end.y - (1.0 - std::cos(end.angle)) / curved; +} + +element make_straight(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Length) +{ + element result; + result.kind = element_kind::straight; + result.origin = Origin; + result.direction = Direction; + result.length = Length; + return result; +} + +element make_arc(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Curvature, double const Length) +{ + element result; + result.kind = element_kind::arc; + result.role = element_role::arc; + result.origin = Origin; + result.direction = Direction; + result.length = Length; + result.curvature_start = Curvature; + result.curvature_end = Curvature; + return result; +} + +element make_spiral(element_kind const Kind, glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Start, double const End, double const Length) +{ + element result; + result.kind = Kind; + result.role = std::abs(End) >= std::abs(Start) ? element_role::transition_in : element_role::transition_out; + result.origin = Origin; + result.direction = Direction; + result.length = Length; + result.curvature_start = Start; + result.curvature_end = End; + return result; +} + +element make_parabola(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const X, double const Radius, bool const Entering) +{ + element result; + result.kind = element_kind::cubic_parabola; + result.role = Entering ? element_role::transition_in : element_role::transition_out; + result.origin = Origin; + result.direction = Direction; + result.parabola_x = X; + result.parabola_radius = Radius; + result.entering = Entering; + result.length = parabola_length(X, Radius, X); + auto const end{sign_of(Radius) * parabola_curvature(X, std::abs(Radius), X)}; + result.curvature_start = Entering ? 0.0 : end; + result.curvature_end = Entering ? end : 0.0; + return result; +} + +} // namespace layout diff --git a/editor/layout/layoutElements.hpp b/editor/layout/layoutElements.hpp new file mode 100644 index 000000000..6110f1441 --- /dev/null +++ b/editor/layout/layoutElements.hpp @@ -0,0 +1,112 @@ +/* +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 +#include + +// The elements of a line of the track geometry (editor-tracks-domain.md §2, "Element"): the straight, the circular arc and the +// transition curves (clothoid, cubic parabola, Bloss), each with its length along it, its curvature along it, its position and heading +// at a distance from its start and the points offset from it. The plan is the scenery's x and z, a curvature above 0 turns left, to +// the side geometry::cross() measures positive. The local forms of the spirals are the ones the route design has always used +// (editorAlignment.cpp calls them), so the two agree to the bit. +namespace layout +{ + +// a point of a curve in the frame of its start: along the tangent, to its left, and the heading turned so far +struct local_point +{ + double x{0.0}; + double y{0.0}; + double angle{0.0}; +}; + +// the cubic parabola y = x^3 / (6 R L) at the share Parameter of its abscissa Length, turning towards the radius +local_point parabola_point(double const Length, double const Radius, double const Parameter); +// the clothoid from the tangent to the radius over the length, at the share Parameter of the length +local_point clothoid_point(double const Length, double const Radius, double const Parameter); +// a curve whose curvature changes evenly from Start to End over the length (a clothoid, an arc where they're alike), at Distance along it +local_point spiral_point(double const Start, double const End, double const Length, double const Distance); +// the Bloss curve from the curvature Start to End over the length, at Distance along it: the curvature follows 3t^2 - 2t^3 +local_point bloss_point(double const Start, double const End, double const Length, double const Distance); +// the point of the cubic Bezier curve of the control points at the parameters A, B and C (de Casteljau's blossom) +glm::dvec2 blossom(std::array const &Points, double const A, double const B, double const C); + +// 3t^2 - 2t^3, the share of the change a Bloss curve or a Bloss cant ramp has made at the share t of its length +double bloss_share(double const T); + +enum class element_kind +{ + straight, + arc, + clothoid, + bloss, + cubic_parabola +}; + +// what the element is in the curve of a vertex, for the names of the tracks made of it +enum class element_role +{ + straight, + transition_in, + arc, + transition_out +}; + +struct element +{ + element_kind kind{element_kind::straight}; + element_role role{element_role::straight}; + glm::dvec2 origin{0.0}; // the start + glm::dvec2 direction{0.0, 1.0}; // the heading at the start, unit + double chainage{0.0}; // of the start, along the line + double length{0.0}; // m along the curve + double curvature_start{0.0}; // 1/m, signed, above 0 turning left + double curvature_end{0.0}; + // a cubic parabola: the abscissa of all of it along its tangent and the radius it leads to (signed); from the tangent into the + // curve, or out of the curve onto the tangent + double parabola_x{0.0}; + double parabola_radius{0.0}; + bool entering{true}; + // the id of the vertex whose curve it's a part of; a straight, the one it leads to, 0 for the one leading to the end of the line + unsigned vertex{0}; + + bool operator==(element const &) const = default; + + double end_chainage() const { return chainage + length; } + // the curvature at the distance from the start + double curvature(double const Distance) const; + // the point at the distance from the start, in the frame of the start + local_point local(double const Distance) const; + glm::dvec2 position(double const Distance) const; + glm::dvec2 tangent(double const Distance) const; + // the point Offset to the left of the axis at the distance, as a track alongside would run + glm::dvec2 offset_point(double const Distance, double const Offset) const; + // a transition from a straight to an arc or from an arc to a straight: how far the arc is moved in from the tangent, the shift p of + // the arc carried on from its curved end; 0 for the other elements + double shift() const; +}; + +// the left of a heading, a quarter turn towards where a curvature above 0 turns +inline glm::dvec2 left_of(glm::dvec2 const &Direction) +{ + return {-Direction.y, Direction.x}; +} + +element make_straight(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Length); +element make_arc(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Curvature, double const Length); +// a clothoid or a Bloss curve from the curvature Start to End +element make_spiral(element_kind const Kind, glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const Start, double const End, double const Length); +// the cubic parabola of the abscissa X leading to the signed radius, from the tangent (Entering) or onto it +element make_parabola(glm::dvec2 const &Origin, glm::dvec2 const &Direction, double const X, double const Radius, bool const Entering); +// the length along a cubic parabola of the abscissa X and the radius, to the abscissa Upto +double parabola_length(double const X, double const Radius, double const Upto); + +} // namespace layout diff --git a/editor/layout/layoutLine.cpp b/editor/layout/layoutLine.cpp new file mode 100644 index 000000000..fd49f3898 --- /dev/null +++ b/editor/layout/layoutLine.cpp @@ -0,0 +1,582 @@ +/* +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/layout/layoutLine.hpp" +#include "editor/editorGeometry.hpp" + +#include +#include +#include +#include + +namespace layout +{ + +namespace +{ + +double constexpr kPi{3.14159265358979323846}; +double constexpr kSame{1e-6}; // m: a straight shorter than this is none, a curve within this of its node begins at it +double constexpr kCantStep{0.5}; // mm: a smaller change isn't a step of the cant (the norms engine takes it alike) +double constexpr kRailBase{1500.0}; // mm between the middles of the rail heads, the base of the roll + +double sign_of(double const Value) +{ + return Value < 0.0 ? -1.0 : 1.0; +} + +// the elements of the curve after one another from the frame of the tangent point: the next starts where the last ended +void chain(std::vector &Elements, element Element, glm::dvec2 &Origin, glm::dvec2 &Direction) +{ + Element.origin = Origin; + Element.direction = Direction; + Origin = Element.position(Element.length); + Direction = glm::normalize(Element.tangent(Element.length)); + Elements.push_back(Element); +} + +element in_frame(element Element, glm::dvec2 const &Origin, glm::dvec2 const &Direction) +{ + auto const left{left_of(Direction)}; + Element.origin = Origin + Direction * Element.origin.x + left * Element.origin.y; + Element.direction = glm::normalize(Direction * Element.direction.x + left * Element.direction.y); + return Element; +} + +std::optional tier_value(norms::thresholds const &Limits) +{ + for (auto const *bound : {&Limits.prefer, &Limits.warn, &Limits.block}) + if (bound->has_value()) + return (*bound)->value; + return std::nullopt; +} + +} // namespace + +curve_fit fit_curve(transition_shape const Shape, double const Deflection, double const Radius, double const In, double const Out, double const From, double const To) +{ + curve_fit result; + auto const in{std::max(0.0, In)}; + auto const out{std::max(0.0, Out)}; + if (std::abs(Deflection) < 1e-9 && in == 0.0 && out == 0.0) + return result; // the tangents go on alike: no curve + if (Radius < 1.0) + { + result.error = std::format("the radius {:.1f} m is too small", Radius); + return result; + } + auto const side{sign_of(Deflection)}; + auto const curvature{side / Radius}; + auto const kind{Shape == transition_shape::bloss ? element_kind::bloss : element_kind::clothoid}; + auto const parabola{Shape == transition_shape::cubic_parabola}; + if (parabola && ((in > 0.0 && From != 0.0) || (out > 0.0 && To != 0.0))) + { + result.error = "a cubic parabola runs between a straight and an arc only"; + return result; + } + // how far the transitions turn: a clothoid and a Bloss curve by the mean of their curvatures, a cubic parabola by its end slope + auto const turning = [&](double const Length, double const Other) { + if (Length <= 0.0) + return 0.0; + return parabola ? side * std::atan(Length / (2.0 * Radius)) : 0.5 * (Other + curvature) * Length; + }; + auto const arcangle{Deflection - turning(in, From) - turning(out, To)}; + auto arc{arcangle / curvature}; + if (arc < -1e-9) + { + result.error = std::format("the transition curves turn {:.4f} rad, more than the {:.4f} rad the tangents do", std::abs(Deflection - arcangle), std::abs(Deflection)); + return result; + } + arc = std::max(0.0, arc); + result.arc = arc; + glm::dvec2 origin{0.0}; + glm::dvec2 direction{1.0, 0.0}; + if (in > 0.0) + chain(result.elements, parabola ? make_parabola({}, {1.0, 0.0}, in, side * Radius, true) : make_spiral(kind, {}, {1.0, 0.0}, From, curvature, in), origin, direction); + if (arc > kSame) + chain(result.elements, make_arc({}, {1.0, 0.0}, curvature, arc), origin, direction); + if (out > 0.0) + chain(result.elements, parabola ? make_parabola({}, {1.0, 0.0}, out, side * Radius, false) : make_spiral(kind, {}, {1.0, 0.0}, curvature, To, out), origin, direction); + for (auto &element : result.elements) + { + element.role = &element == &result.elements.front() && in > 0.0 ? element_role::transition_in : &element == &result.elements.back() && out > 0.0 ? element_role::transition_out : element_role::arc; + } + // the tangent points: where the tangent at the start and the one at the end cross + glm::dvec2 const leaving{std::cos(Deflection), std::sin(Deflection)}; + double first{0.0}; + double second{0.0}; + if (false == geometry::lines_cross({0.0, 0.0}, {1.0, 0.0}, origin, leaving, first, second)) + { + result.error = "the curve ends heading as it starts"; + return result; + } + result.tangent_in = first; + result.tangent_out = -second; + return result; +} + +std::size_t solved_line::element_at(double const Chainage) const +{ + if (elements.empty()) + return 0; + auto const next{std::upper_bound(elements.begin(), elements.end(), Chainage, [](double const Value, element const &Element) { return Value < Element.chainage; })}; + return next == elements.begin() ? 0 : static_cast(std::prev(next) - elements.begin()); +} + +glm::dvec2 solved_line::position(double const Chainage) const +{ + auto const &e{elements[element_at(Chainage)]}; + return e.position(Chainage - e.chainage); +} + +glm::dvec2 solved_line::tangent(double const Chainage) const +{ + auto const &e{elements[element_at(Chainage)]}; + return e.tangent(Chainage - e.chainage); +} + +double solved_line::curvature(double const Chainage) const +{ + auto const &e{elements[element_at(Chainage)]}; + return e.curvature(Chainage - e.chainage); +} + +double solved_line::cant_at(double const Chainage) const +{ + if (cant.empty()) + return 0.0; + auto const next{std::upper_bound(cant.begin(), cant.end(), Chainage, [](double const Value, cant_stretch const &Stretch) { return Value < Stretch.from; })}; + auto const &stretch{next == cant.begin() ? cant.front() : *std::prev(next)}; + auto const span{stretch.to - stretch.from}; + auto const t{span > 0.0 ? std::clamp((Chainage - stretch.from) / span, 0.0, 1.0) : 1.0}; + auto const share{stretch.shape == ramp_shape::bloss ? bloss_share(t) : t}; + return stretch.start + (stretch.end - stretch.start) * share; +} + +double solved_line::elevation(double const Chainage) const +{ + if (profile.size() < 2) + return profile.empty() ? 0.0 : profile.front().elevation; + auto const s{std::clamp(Chainage, 0.0, length)}; + std::size_t segment{0}; + while (segment + 2 < profile.size() && s > profile[segment + 1].chainage) + ++segment; + auto const &from{profile[segment]}; + auto const &to{profile[segment + 1]}; + double height{from.elevation + (to.elevation - from.elevation) * (s - from.chainage) / (to.chainage - from.chainage)}; + for (std::size_t i = 1; i + 1 < profile.size(); ++i) + { + auto const &point{profile[i]}; + auto const distance{std::abs(s - point.chainage)}; + if (point.tangent <= 0.0 || distance >= point.tangent) + continue; + auto const before{(point.elevation - profile[i - 1].elevation) / (point.chainage - profile[i - 1].chainage)}; + auto const after{(profile[i + 1].elevation - point.elevation) / (profile[i + 1].chainage - point.chainage)}; + auto const offset{(point.tangent - distance) * (point.tangent - distance) / (2.0 * point.vertical_radius)}; + height += after > before ? offset : -offset; + } + return height; +} + +double solved_line::grade(double const Chainage) const +{ + if (profile.size() < 2) + return 0.0; + auto const s{std::clamp(Chainage, 0.0, length)}; + std::size_t segment{0}; + while (segment + 2 < profile.size() && s > profile[segment + 1].chainage) + ++segment; + auto const &from{profile[segment]}; + auto const &to{profile[segment + 1]}; + double slope{(to.elevation - from.elevation) / (to.chainage - from.chainage)}; + for (std::size_t i = 1; i + 1 < profile.size(); ++i) + { + auto const &point{profile[i]}; + auto const distance{s - point.chainage}; + if (point.tangent <= 0.0 || std::abs(distance) >= point.tangent) + continue; + auto const before{(point.elevation - profile[i - 1].elevation) / (point.chainage - profile[i - 1].chainage)}; + auto const after{(profile[i + 1].elevation - point.elevation) / (profile[i + 1].chainage - point.chainage)}; + auto const change{(point.tangent - std::abs(distance)) / point.vertical_radius * (distance < 0.0 ? 1.0 : -1.0)}; + slope += after > before ? change : -change; + } + return slope; +} + +glm::dvec3 solved_line::point(double const Chainage) const +{ + auto const plan{position(Chainage)}; + return {plan.x, elevation(Chainage), plan.y}; +} + +solved_line solve(line_shape const &Shape, end_condition const &Start, end_condition const &End) +{ + solved_line result; + auto const &vertices{Shape.vertices}; + auto const count{vertices.size()}; + std::vector polygon{geometry::plan_of(Start.position)}; + for (auto const &vertex : vertices) + polygon.push_back(vertex.point); + polygon.push_back(geometry::plan_of(End.position)); + auto const vertex_name = [&](std::size_t const Index) { return std::format("vertex {}", number(vertices[Index].id)); }; + + std::vector directions; + std::vector lengths; + for (std::size_t i = 0; i + 1 < polygon.size(); ++i) + { + auto const length{glm::distance(polygon[i], polygon[i + 1])}; + if (length < 1e-3) + { + result.errors.push_back(i == 0 ? std::format("{} lies on the start of the line", count > 0 ? vertex_name(0) : "the end") + : std::format("{} lies on {}", vertex_name(i - 1), i < count ? vertex_name(i) : "the end of the line")); + return result; + } + lengths.push_back(length); + directions.push_back((polygon[i + 1] - polygon[i]) / length); + } + if (count == 0 && (Start.continues || End.continues) && (Start.curvature != 0.0 || End.curvature != 0.0 || Start.cant != 0.0 || End.cant != 0.0)) + { + result.errors.emplace_back("a line going on from a curve at its end needs a vertex for the curve"); + return result; + } + + // the curves of the vertices, each in the frame of its first tangent point + std::vector curves(count); + std::vector cants(count); + for (std::size_t k = 0; k < count; ++k) + { + auto const &vertex{vertices[k]}; + auto const deflection{geometry::signed_angle(directions[k], directions[k + 1])}; + if (std::abs(deflection) > kPi - 1e-6) + { + result.errors.push_back(std::format("the line turns back at {}", vertex_name(k))); + return result; + } + if (vertex.cant < 0.0) + { + result.errors.push_back(std::format("{}: a cant leaning out of the turn", vertex_name(k))); + return result; + } + auto const from{k == 0 && Start.continues ? Start.curvature : 0.0}; + auto const to{k + 1 == count && End.continues ? End.curvature : 0.0}; + curves[k] = fit_curve(Shape.transition, deflection, vertex.radius, vertex.transition_in, vertex.transition_out, from, to); + if (false == curves[k].error.empty()) + { + result.errors.push_back(std::format("{}: {}", vertex_name(k), curves[k].error)); + return result; + } + if (curves[k].elements.empty() && vertex.cant > 0.0) + { + result.errors.push_back(std::format("{}: a cant where the line doesn't turn", vertex_name(k))); + return result; + } + cants[k] = sign_of(deflection) * vertex.cant; + } + + // the straights between the curves: what the tangents leave of the sides of the polygon + std::vector straights(count + 1); + for (std::size_t i = 0; i <= count; ++i) + { + auto const taken{(i > 0 ? curves[i - 1].tangent_out : 0.0) + (i < count ? curves[i].tangent_in : 0.0)}; + straights[i] = lengths[i] - taken; + if (straights[i] < -kSame) + { + if (i == 0) + result.errors.push_back(std::format("{}: the curve starts {:.3f} m before the start of the line", vertex_name(0), -straights[i])); + else if (i == count) + result.errors.push_back(std::format("{}: the curve ends {:.3f} m past the end of the line", vertex_name(count - 1), -straights[i])); + else + result.errors.push_back(std::format("the curves of {} and {} overlap by {:.3f} m", vertex_name(i - 1), vertex_name(i), -straights[i])); + return result; + } + auto const held{(i == 0 && Start.continues) || (i == count && End.continues)}; + if (held && straights[i] > kSame) + { + result.errors.push_back(std::format("{}: the curve has to {} right at the node, where the track goes on from a curve; it's {:.3f} m off", vertex_name(i == 0 ? 0 : count - 1), + i == 0 ? "begin" : "end", straights[i])); + return result; + } + } + + // the elements one after another, each placed from the points of the polygon: the straights along its sides, the curves from their + // tangent points, so nothing drifts along the line + double chainage{0.0}; + auto const push = [&](element Element) { + Element.chainage = chainage; + chainage += Element.length; + result.elements.push_back(Element); + }; + for (std::size_t i = 0; i <= count; ++i) + { + if (straights[i] > kSame) + { + auto const from{i == 0 ? polygon[0] : polygon[i] + directions[i] * curves[i - 1].tangent_out}; + auto straight{make_straight(from, directions[i], straights[i])}; + straight.vertex = i < count ? number(vertices[i].id) : 0; + push(straight); + } + if (i == count) + break; + // a curve beginning at the start node begins there to the bit + auto const tangent_point{i == 0 && straights[0] <= kSame ? polygon[0] : polygon[i + 1] - directions[i] * curves[i].tangent_in}; + for (auto const &local : curves[i].elements) + { + auto placed{in_frame(local, tangent_point, directions[i])}; + placed.vertex = number(vertices[i].id); + push(placed); + } + } + result.length = chainage; + if (result.elements.empty()) + { + result.errors.emplace_back("the line has no length"); + return result; + } + + // the cant: held along the arcs, ramped along the transitions, 0 along the straights; a change with no transition to ramp it along + // is a step, which isn't allowed + auto cant{Start.continues ? Start.cant : 0.0}; + auto const stretch = [&](double const From, double const To, double const Begin, double const Finish, ramp_shape const Ramp) { + if (To - From > 0.0) + result.cant.push_back({From, To, Begin, Finish, Ramp}); + }; + std::size_t curve{0}; + for (std::size_t i = 0; i < result.elements.size(); ++i) + { + auto const &e{result.elements[i]}; + if (e.role == element_role::straight) + { + if (std::abs(cant) > kCantStep) + { + result.errors.push_back(std::format("a cant of {:.0f} mm on the straight at {:.1f} m", std::abs(cant), e.chainage)); + return result; + } + stretch(e.chainage, e.end_chainage(), 0.0, 0.0, ramp_shape::linear); + cant = 0.0; + continue; + } + while (curve + 1 < count && number(vertices[curve].id) != e.vertex) + ++curve; + auto const target{cants[curve]}; + auto const after{curve + 1 == count && End.continues ? End.cant : 0.0}; + switch (e.role) + { + case element_role::transition_in: + stretch(e.chainage, e.end_chainage(), cant, target, Shape.ramp); + cant = target; + break; + case element_role::arc: + if (std::abs(cant - target) > kCantStep) + { + result.errors.push_back(std::format("{}: the cant steps by {:.0f} mm where the arc begins, with no transition curve to ramp it along", vertex_name(curve), std::abs(cant - target))); + return result; + } + stretch(e.chainage, e.end_chainage(), target, target, ramp_shape::linear); + cant = target; + break; + case element_role::transition_out: + if (std::abs(cant - target) > kCantStep) + { + result.errors.push_back(std::format("{}: the cant steps by {:.0f} mm in the curve", vertex_name(curve), std::abs(cant - target))); + return result; + } + stretch(e.chainage, e.end_chainage(), target, after, Shape.ramp); + cant = after; + break; + default: break; + } + // the last element of a curve with no transition out: what follows starts at the cant the curve leaves + auto const last_of_curve{i + 1 == result.elements.size() || result.elements[i + 1].vertex != e.vertex || result.elements[i + 1].role == element_role::straight}; + if (last_of_curve && std::abs(cant - after) > kCantStep) + { + result.errors.push_back(std::format("{}: the cant steps by {:.0f} mm where the curve ends, with no transition curve to ramp it along", vertex_name(curve), std::abs(cant - after))); + return result; + } + if (last_of_curve) + cant = after; + } + auto const end_cant{End.continues ? End.cant : 0.0}; + if (std::abs(cant - end_cant) > kCantStep) + { + result.errors.push_back(std::format("the cant steps by {:.0f} mm at the end of the line", std::abs(cant - end_cant))); + return result; + } + + // the grade line: the heights of the nodes at the ends, the breaks between them rounded with their vertical curves + result.profile.push_back({0.0, Start.position.y, 0.0, 0.0}); + for (auto const &point : Shape.grades) + { + if (point.chainage <= result.profile.back().chainage + 1e-3 || point.chainage >= result.length - 1e-3) + { + result.errors.push_back(std::format("the grade break at {:.1f} m isn't between the one before it and the end of the line, {:.1f} m long", point.chainage, result.length)); + return result; + } + if (point.vertical_radius <= 0.0) + { + result.errors.push_back(std::format("the grade break at {:.1f} m has no vertical radius", point.chainage)); + return result; + } + result.profile.push_back({point.chainage, point.elevation, point.vertical_radius, 0.0}); + } + result.profile.push_back({result.length, End.position.y, 0.0, 0.0}); + for (std::size_t i = 1; i + 1 < result.profile.size(); ++i) + { + auto &point{result.profile[i]}; + auto const before{(point.elevation - result.profile[i - 1].elevation) / (point.chainage - result.profile[i - 1].chainage)}; + auto const after{(result.profile[i + 1].elevation - point.elevation) / (result.profile[i + 1].chainage - point.chainage)}; + point.tangent = point.vertical_radius * std::abs(after - before) * 0.5; + } + for (std::size_t i = 0; i + 1 < result.profile.size(); ++i) + { + auto const &a{result.profile[i]}; + auto const &b{result.profile[i + 1]}; + if (a.tangent + b.tangent > b.chainage - a.chainage + 1e-9) + { + result.errors.push_back(std::format("the vertical curves at {:.1f} m and {:.1f} m overlap by {:.2f} m", a.chainage, b.chainage, a.tangent + b.tangent - (b.chainage - a.chainage))); + return result; + } + } + + auto const &first{result.elements.front()}; + auto const &last{result.elements.back()}; + result.start = {Start.position, directions.front(), first.curvature(0.0), result.cant_at(0.0), result.grade(0.0)}; + result.end = {End.position, directions.back(), last.curvature(last.length), result.cant.empty() ? 0.0 : result.cant.back().end, result.grade(result.length)}; + return result; +} + +norms::line described(solved_line const &Line, norms::line_class const &Class, std::string Name) +{ + norms::line result; + result.name = std::move(Name); + for (auto const &e : Line.elements) + { + norms::element element; + element.kind = e.kind == element_kind::straight ? norms::element_kind::straight : e.kind == element_kind::arc ? norms::element_kind::arc : norms::element_kind::transition; + element.start = e.chainage; + element.length = e.length; + element.curvature_start = e.curvature(0.0); + element.curvature_end = e.curvature(e.length); + element.speed = Class.vmax; + auto const from{e.position(0.0)}; + auto const to{e.position(e.length)}; + element.from = {from.x, from.y}; + element.to = {to.x, to.y}; + result.elements.push_back(element); + } + auto const add = [&](double const Chainage, double const Cant) { + if (false == result.cant.empty() && std::abs(result.cant.back().chainage - Chainage) < 1e-9 && std::abs(result.cant.back().cant - Cant) < 1e-9) + return; + result.cant.push_back({Chainage, Cant}); + }; + for (auto const &stretch : Line.cant) + { + // a Bloss ramp as eight even pieces, so its steeper middle is seen + auto const pieces{stretch.shape == ramp_shape::bloss && stretch.start != stretch.end ? 8 : 1}; + for (int i = 0; i <= pieces; ++i) + { + auto const t{static_cast(i) / pieces}; + auto const share{stretch.shape == ramp_shape::bloss ? bloss_share(t) : t}; + add(stretch.from + (stretch.to - stretch.from) * t, stretch.start + (stretch.end - stretch.start) * share); + } + } + auto const &profile{Line.profile}; + for (std::size_t i = 0; i + 1 < profile.size(); ++i) + { + auto const &a{profile[i]}; + auto const &b{profile[i + 1]}; + auto const grade{(b.elevation - a.elevation) / (b.chainage - a.chainage)}; + if (i > 0 && a.tangent > 0.0) + { + auto const before{(a.elevation - profile[i - 1].elevation) / (a.chainage - profile[i - 1].chainage)}; + result.grades.push_back({a.chainage - a.tangent, 2.0 * a.tangent, before, grade}); + } + auto const from{a.chainage + a.tangent}; + auto const to{b.chainage - b.tangent}; + if (to - from > 1e-9) + result.grades.push_back({from, to - from, grade, grade}); + } + return result; +} + +curve_norms preferred_curve(norms::profile_id const Profile, norms::line_class const &Class, double const Radius) +{ + curve_norms result; + norms::query where; + where.speed = Class.vmax; + where.radius = Radius; + where.load = Class.load; + where.kind = Class.kind; + where.track = Class.track; + where.features = Class.features; + auto const limit = [&](norms::parameter const What) { return tier_value(norms::limits(Profile, What, where)); }; + auto const speed{Class.vmax}; + auto const slow{Class.vmin > 0.0 ? Class.vmin : norms::slow_speed(Class.kind, Class.vmax)}; + // the cant within the deficiency, the excess and the greatest cant of the preferred values, else within the ones which only warn, + // else the most the cant may be: the regular cant where it's among them, rounded to 5 mm + auto const deficiency_limits{norms::limits(Profile, norms::parameter::cant_deficiency, where)}; + auto const excess_limits{norms::limits(Profile, norms::parameter::cant_excess, where)}; + auto const cant_limits{norms::limits(Profile, norms::parameter::cant, where)}; + auto const regular{norms::regular_cant(speed, Radius)}; + double cant{0.0}; + bool found{false}; + for (auto const tier : {&norms::thresholds::prefer, &norms::thresholds::warn, &norms::thresholds::block}) + { + auto low{0.0}; + auto high{1e9}; + if (auto const &bound{deficiency_limits.*tier}) + low = std::max(low, norms::least_cant(speed, Radius, bound->value)); + if (auto const &bound{excess_limits.*tier}) + high = std::min(high, norms::greatest_cant(slow, Radius, bound->value)); + if (auto const &bound{cant_limits.*tier}) + high = std::min(high, bound->value); + if (low > high) + { + cant = high; + continue; + } + cant = std::clamp(regular, low, high); + auto const rounded{std::round(cant / 5.0) * 5.0}; + cant = rounded > high ? std::floor(high / 5.0) * 5.0 : rounded < low ? std::ceil(low / 5.0) * 5.0 : rounded; + cant = std::clamp(cant, low, high); + found = true; + break; + } + cant = std::max(0.0, found ? cant : std::floor(cant / 5.0) * 5.0); + // a cant given at all is at least the least one: that, or none where the curve does as well without + if (auto const least{limit(norms::parameter::cant_min)}; least && cant > 0.0 && cant < *least) + { + auto const needed{deficiency_limits.prefer ? norms::least_cant(speed, Radius, deficiency_limits.prefer->value) : 0.0}; + cant = needed > 0.0 || cant >= 0.5 * *least ? std::ceil(*least / 5.0) * 5.0 : 0.0; + } + result.cant = cant; + auto const metres{speed / 3.6}; + auto const deficiency{std::max(0.0, norms::equilibrium_cant(speed, Radius) - cant)}; + double transition{0.0}; + if (auto const gradient{limit(norms::parameter::cant_gradient)}; gradient && *gradient > 0.0) + transition = std::max(transition, cant / *gradient); + if (auto const rate{limit(norms::parameter::cant_rate)}; rate && *rate > 0.0) + transition = std::max(transition, metres * cant / *rate); + if (auto const rate{limit(norms::parameter::deficiency_rate)}; rate && *rate > 0.0) + transition = std::max(transition, metres * deficiency / *rate); + if (auto const shortest{limit(norms::parameter::transition_length)}; shortest && cant > 0.0) + transition = std::max(transition, *shortest); + result.transition = std::ceil(transition - 1e-9); + result.radius_prefer = limit(norms::parameter::radius).value_or(0.0); + auto const block{norms::limits(Profile, norms::parameter::radius, where).block}; + result.radius_block = block ? block->value : 0.0; + return result; +} + +double roll_of(double const Cant) +{ + return glm::degrees(std::asin(std::clamp(Cant / kRailBase, -1.0, 1.0))); +} + +} // namespace layout diff --git a/editor/layout/layoutLine.hpp b/editor/layout/layoutLine.hpp new file mode 100644 index 000000000..28c24e193 --- /dev/null +++ b/editor/layout/layoutLine.hpp @@ -0,0 +1,197 @@ +/* +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/layoutElements.hpp" +#include "editor/norms/norms.hpp" + +#include +#include +#include +#include + +// The line of the track geometry (editor-tracks-domain.md §2 and §4, "Linia"): the axis of a track between two nodes, given as a tangent +// polygon of vertices between them, each with its radius, its transition curves and its cant, and a grade line of breaks with vertical +// curves; solved into the elements one after another, the cant and the heights as functions of the chainage. The ends of the line are +// its nodes: it has no coordinates of its own there, the polygon starts and ends at the points of the nodes. +namespace layout +{ + +// the identifiers of the domain: given once, never given again, 0 for none +enum class node_id : std::uint32_t +{ + none = 0 +}; +enum class line_id : std::uint32_t +{ + none = 0 +}; +enum class vertex_id : std::uint32_t +{ + none = 0 +}; + +inline std::uint32_t number(node_id const Id) { return static_cast(Id); } +inline std::uint32_t number(line_id const Id) { return static_cast(Id); } +inline std::uint32_t number(vertex_id const Id) { return static_cast(Id); } + +enum class transition_shape +{ + clothoid, + cubic_parabola, // the abscissa along the tangent is the transition length given + bloss +}; + +// how the cant changes along a transition curve +enum class ramp_shape +{ + linear, + bloss +}; + +// a break of the tangents: where they meet, the radius of the arc between them, the transition curves into it and out of it, its cant +struct vertex +{ + vertex_id id{vertex_id::none}; + glm::dvec2 point{0.0}; // plan, the scenery's x and z + double radius{600.0}; // m + double transition_in{0.0}; // m + double transition_out{0.0}; + double cant{0.0}; // mm, leaning into the turn + + bool operator==(vertex const &) const = default; +}; + +// a break of the grade line: at the chainage, the height there, the radius of the vertical curve rounding it +struct grade_break +{ + double chainage{0.0}; + double elevation{0.0}; + double vertical_radius{5000.0}; + + bool operator==(grade_break const &) const = default; +}; + +// the cant, mm, signed: above 0 leaning left as the line runs, from the start of the stretch to its end, a ramp or a stretch held +struct cant_stretch +{ + double from{0.0}; + double to{0.0}; + double start{0.0}; + double end{0.0}; + ramp_shape shape{ramp_shape::linear}; + + bool operator==(cant_stretch const &) const = default; +}; + +// the grade line: the breaks between the ends, each with the tangent of its vertical curve +struct profile_point +{ + double chainage{0.0}; + double elevation{0.0}; + double vertical_radius{0.0}; + double tangent{0.0}; // m from the break to either end of its vertical curve + + bool operator==(profile_point const &) const = default; +}; + +// the values of the line at an end, as the line runs from its start to its end +struct end_values +{ + glm::dvec3 position{0.0}; + glm::dvec2 heading{0.0, 1.0}; + double curvature{0.0}; + double cant{0.0}; // mm, signed + double grade{0.0}; + + bool operator==(end_values const &) const = default; +}; + +// how the curve next to an end of the line meets its node: as the node holds it (the end goes on from a curve, the line was split +// inside one), or from a straight +struct end_condition +{ + glm::dvec3 position{0.0}; + bool continues{false}; + double curvature{0.0}; // as the line runs, of the node where the end continues + double cant{0.0}; +}; + +struct line_shape +{ + std::vector vertices; + std::vector grades; + transition_shape transition{transition_shape::clothoid}; + ramp_shape ramp{ramp_shape::linear}; + + bool operator==(line_shape const &) const = default; +}; + +// the line solved: the elements, the cant and the grade line along it; errors where it can't be laid as given +struct solved_line +{ + std::vector elements; + std::vector cant; + std::vector profile; + double length{0.0}; + end_values start; + end_values end; + std::vector errors; + + bool valid() const { return errors.empty() && false == elements.empty(); } + bool operator==(solved_line const &) const = default; + + // the element at the chainage, the later one at a boundary + std::size_t element_at(double const Chainage) const; + glm::dvec2 position(double const Chainage) const; + glm::dvec2 tangent(double const Chainage) const; + double curvature(double const Chainage) const; + // mm, signed; where a stretch begins, its own value + double cant_at(double const Chainage) const; + double elevation(double const Chainage) const; + double grade(double const Chainage) const; + glm::dvec3 point(double const Chainage) const; +}; + +// the line laid between the end conditions; the vertices and the breaks as given, the ends from the nodes +solved_line solve(line_shape const &Shape, end_condition const &Start, end_condition const &End); + +// the shape of a curve of a vertex in the frame of the tangent point where it starts, heading along x: the elements, and how far the +// tangent points are from the vertex +struct curve_fit +{ + std::vector elements; + double tangent_in{0.0}; + double tangent_out{0.0}; + double arc{0.0}; + std::string error; +}; +// Deflection: signed, above 0 turning left. From and To: the curvatures the transitions start at and end at, signed +curve_fit fit_curve(transition_shape const Shape, double const Deflection, double const Radius, double const In, double const Out, double const From = 0.0, double const To = 0.0); + +// the line as the norms describe it (norms::line), the class of the line telling the speed of its elements +norms::line described(solved_line const &Line, norms::line_class const &Class, std::string Name = {}); + +// the cant, the transition length and the radius the norms prefer for a curve of the radius at the line class, by the profile: +// the regular cant within the preferred deficiency and greatest cant, the transitions long enough for the preferred ramp gradient, +// cant rate and deficiency rate, rounded up to whole metres +struct curve_norms +{ + double cant{0.0}; + double transition{0.0}; + double radius_prefer{0.0}; + double radius_block{0.0}; +}; +curve_norms preferred_curve(norms::profile_id const Profile, norms::line_class const &Class, double const Radius); + +// mm of the cant as degrees of the roll of the track: on the base of the rail heads, 1500 mm +double roll_of(double const Cant); + +} // namespace layout diff --git a/editor/norms/norms.hpp b/editor/norms/norms.hpp index 64b5ab983..ca712b298 100644 --- a/editor/norms/norms.hpp +++ b/editor/norms/norms.hpp @@ -112,6 +112,8 @@ struct line_class track_kind track{track_kind::main}; double load{5.0}; // Tg a year, for the cant excess (ST-T1-A6 tab. 9.8) unsigned features{0}; // platform, station, ballastless, suburban, tilting + + bool operator==(line_class const &) const = default; }; // the speed of the slow trains of a line of the traffic and the highest speed given: the line type of ST-T1-A6 tab. 8.1 with the diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index a54c53bbe..7457daee1 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -56,6 +56,9 @@ add_executable(${UNIT_TESTS_NAME} "${CMAKE_SOURCE_DIR}/editor/norms/normsEngine.cpp" "${CMAKE_SOURCE_DIR}/editor/norms/normsLine.cpp" "${CMAKE_SOURCE_DIR}/editor/editorNormsReport.cpp" + # the domain of the track geometry: the elements, the lines and the nodes, the commands editing them, the tracks made of them + "${CMAKE_SOURCE_DIR}/editor/layout/layoutElements.cpp" + "${CMAKE_SOURCE_DIR}/editor/layout/layoutLine.cpp" ${UNIT_TEST_CASES}) # the core library brings the game's build settings along diff --git a/tests/unit/test_layout_elements.cpp b/tests/unit/test_layout_elements.cpp new file mode 100644 index 000000000..72561852d --- /dev/null +++ b/tests/unit/test_layout_elements.cpp @@ -0,0 +1,159 @@ +// the elements of the lines of the track geometry (editor/layout, editor-tracks-domain.md §2): the clothoid, the cubic parabola, the +// Bloss curve and the arc — the length, the curvature along, the point and the heading at a distance, the shift of the arc, a point +// offset from the axis — against the series and the formulas of the textbooks; the curve of a vertex between its tangents + +#include "stdafx.h" +#include "doctest.h" +#include "editor/layout/layoutLine.hpp" + +#include + +namespace { + +using layout::element_kind; + +double constexpr kPi{3.14159265358979323846}; + +glm::dvec2 const kOrigin{0.0, 0.0}; +glm::dvec2 const kAlong{1.0, 0.0}; + +} // namespace + +TEST_CASE("layout elements: a clothoid from a straight to R 600 over 120 m, its length, curvature, end and shift by the series") { + double const R{600.0}; + double const L{120.0}; + auto const e{layout::make_spiral(element_kind::clothoid, kOrigin, kAlong, 0.0, 1.0 / R, L)}; + CHECK(e.length == L); + CHECK(e.curvature(0.0) == 0.0); + CHECK(e.curvature(L / 2.0) == doctest::Approx(0.5 / R).epsilon(1e-12)); + CHECK(e.curvature(L) == doctest::Approx(1.0 / R).epsilon(1e-12)); + // x = L - L^5 / (40 A^4) + L^9 / (3456 A^8), y = L^3 / (6 A^2) - L^7 / (336 A^6) + L^11 / (42240 A^10), A^2 = R L + auto const a2{R * L}; + auto const x{L - std::pow(L, 5) / (40.0 * a2 * a2) + std::pow(L, 9) / (3456.0 * std::pow(a2, 4))}; + auto const y{std::pow(L, 3) / (6.0 * a2) - std::pow(L, 7) / (336.0 * std::pow(a2, 3)) + std::pow(L, 11) / (42240.0 * std::pow(a2, 5))}; + auto const end{e.position(L)}; + CHECK(end.x == doctest::Approx(x).epsilon(1e-9)); + CHECK(end.y == doctest::Approx(y).epsilon(1e-7)); + CHECK(e.local(L).angle == doctest::Approx(L / (2.0 * R)).epsilon(1e-12)); + auto const heading{e.tangent(L)}; + CHECK(std::atan2(heading.y, heading.x) == doctest::Approx(L / (2.0 * R)).epsilon(1e-12)); + // the shift of the arc, p = L^2 / 24R - L^4 / 2688R^3 + CHECK(e.shift() == doctest::Approx(L * L / (24.0 * R) - std::pow(L, 4) / (2688.0 * std::pow(R, 3))).epsilon(1e-5)); + // the same clothoid run out of the curve: the same shift, the curvature falling evenly + auto const out{layout::make_spiral(element_kind::clothoid, kOrigin, kAlong, -1.0 / R, 0.0, L)}; + CHECK(out.shift() == doctest::Approx(e.shift()).epsilon(1e-12)); + CHECK(out.curvature(L / 4.0) == doctest::Approx(-0.75 / R).epsilon(1e-12)); +} + +TEST_CASE("layout elements: a clothoid between two arcs turns by the mean of their curvatures") { + auto const e{layout::make_spiral(element_kind::clothoid, {10.0, 20.0}, glm::normalize(glm::dvec2{1.0, 1.0}), 1.0 / 1000.0, 1.0 / 400.0, 60.0)}; + CHECK(e.local(60.0).angle == doctest::Approx(0.5 * (1.0 / 1000.0 + 1.0 / 400.0) * 60.0).epsilon(1e-12)); + // the point at the distance is the integral of the heading: a few metres at a time by the chord of each + glm::dvec2 point{10.0, 20.0}; + int constexpr steps{6000}; + for (int i = 0; i < steps; ++i) { + auto const middle{(i + 0.5) * 60.0 / steps}; + point += e.tangent(middle) * (60.0 / steps); + } + CHECK(glm::distance(point, e.position(60.0)) < 1e-6); +} + +TEST_CASE("layout elements: a cubic parabola of 100 m to R 500, its end, length, heading, shift and its run out of the curve") { + double const R{500.0}; + double const X{100.0}; + auto const in{layout::make_parabola(kOrigin, kAlong, X, R, true)}; + // the length along it, X (1 + X^2 / 40R^2 - X^4 / 1152R^4) to the order kept + CHECK(in.length == doctest::Approx(X * (1.0 + X * X / (40.0 * R * R) - std::pow(X, 4) / (1152.0 * std::pow(R, 4)))).epsilon(1e-8)); + auto const end{in.position(in.length)}; + CHECK(end.x == doctest::Approx(X).epsilon(1e-9)); + CHECK(end.y == doctest::Approx(X * X / (6.0 * R)).epsilon(1e-9)); + CHECK(in.local(in.length).angle == doctest::Approx(std::atan(X / (2.0 * R))).epsilon(1e-12)); + // the curvature at its end falls short of 1/R by the slope there + CHECK(in.curvature(in.length) == doctest::Approx(1.0 / R / std::pow(1.0 + std::pow(X / (2.0 * R), 2), 1.5)).epsilon(1e-9)); + CHECK(in.curvature(0.0) == 0.0); + // the shift: X^2 / 24R + 3X^4 / 128R^3 to the second order + CHECK(in.shift() == doctest::Approx(X * X / (24.0 * R) + 3.0 * std::pow(X, 4) / (128.0 * std::pow(R, 3))).epsilon(1e-3)); + CHECK(in.shift() == doctest::Approx(X * X / (6.0 * R) - R * (1.0 - std::cos(std::atan(X / (2.0 * R))))).epsilon(1e-9)); + // run out of the curve onto the tangent, turning left as well: the mirror of the one into it + auto const out{layout::make_parabola(kOrigin, kAlong, X, R, false)}; + CHECK(out.length == doctest::Approx(in.length).epsilon(1e-12)); + auto const angle{std::atan(X / (2.0 * R))}; + auto const beyond{out.position(out.length)}; + CHECK(beyond.x == doctest::Approx(X * std::cos(angle) + end.y * std::sin(angle)).epsilon(1e-9)); + CHECK(beyond.y == doctest::Approx(X * std::sin(angle) - end.y * std::cos(angle)).epsilon(1e-9)); + CHECK(out.local(out.length).angle == doctest::Approx(angle).epsilon(1e-12)); + CHECK(out.curvature(0.0) == doctest::Approx(in.curvature(in.length)).epsilon(1e-12)); + CHECK(out.curvature(out.length) == doctest::Approx(0.0)); + // turning right, the same mirrored + auto const right{layout::make_parabola(kOrigin, kAlong, X, -R, true)}; + CHECK(right.position(right.length).y == doctest::Approx(-end.y).epsilon(1e-12)); + CHECK(right.curvature(right.length) == doctest::Approx(-in.curvature(in.length)).epsilon(1e-12)); +} + +TEST_CASE("layout elements: a Bloss curve from a straight to R 400 over 100 m: its curvature, heading and a shift of 3/5 of the clothoid's") { + double const R{400.0}; + double const L{100.0}; + auto const e{layout::make_spiral(element_kind::bloss, kOrigin, kAlong, 0.0, 1.0 / R, L)}; + CHECK(e.curvature(L / 2.0) == doctest::Approx(0.5 / R).epsilon(1e-12)); + CHECK(e.curvature(L / 4.0) == doctest::Approx((3.0 / 16.0 - 2.0 / 64.0) / R).epsilon(1e-12)); + CHECK(e.curvature(L) == doctest::Approx(1.0 / R).epsilon(1e-12)); + // it turns as far as a clothoid of its length + CHECK(e.local(L).angle == doctest::Approx(L / (2.0 * R)).epsilon(1e-12)); + // the shift of a Bloss curve, L^2 / 40R, is 3/5 of the one of a clothoid, L^2 / 24R + CHECK(e.shift() == doctest::Approx(L * L / (40.0 * R)).epsilon(1e-3)); + auto const clothoid{layout::make_spiral(element_kind::clothoid, kOrigin, kAlong, 0.0, 1.0 / R, L)}; + CHECK(e.shift() / clothoid.shift() == doctest::Approx(0.6).epsilon(2e-3)); + // its point is the integral of its heading, as for the clothoid + glm::dvec2 point{0.0}; + int constexpr steps{6000}; + for (int i = 0; i < steps; ++i) + point += e.tangent((i + 0.5) * L / steps) * (L / steps); + CHECK(glm::distance(point, e.position(L)) < 1e-6); +} + +TEST_CASE("layout elements: an arc keeps its radius and its centre, points offset from it lie on the circles alongside") { + double const R{300.0}; + auto const e{layout::make_arc({5.0, 7.0}, {0.0, 1.0}, -1.0 / R, 200.0)}; + // turning right from heading +z: the centre to the right, at +x + glm::dvec2 const centre{5.0 + R, 7.0}; + for (double s = 0.0; s <= 200.0; s += 25.0) { + CAPTURE(s); + CHECK(glm::distance(e.position(s), centre) == doctest::Approx(R).epsilon(1e-12)); + CHECK(e.curvature(s) == -1.0 / R); + // a track 4.5 m to the left runs on the circle 4.5 m larger + CHECK(glm::distance(e.offset_point(s, 4.5), centre) == doctest::Approx(R + 4.5).epsilon(1e-12)); + } + CHECK(e.local(200.0).angle == doctest::Approx(-200.0 / R).epsilon(1e-12)); + CHECK(e.shift() == 0.0); + auto const straight{layout::make_straight({1.0, 2.0}, {0.6, 0.8}, 50.0)}; + CHECK(glm::distance(straight.offset_point(10.0, 2.0), glm::dvec2{1.0 + 6.0 - 1.6, 2.0 + 8.0 + 1.2}) < 1e-12); +} + +TEST_CASE("layout elements: the curve of a vertex, R 800 and clothoids of 90 m, by the tangent length of the textbooks") { + double const R{800.0}; + double const L{90.0}; + double const deflection{0.4}; + auto const fit{layout::fit_curve(layout::transition_shape::clothoid, deflection, R, L, L)}; + REQUIRE(fit.error.empty()); + REQUIRE(fit.elements.size() == 3); + // T = (R + p) tan(alpha / 2) + x_m, x_m = L / 2 - L^3 / 240 R^2 + auto const p{fit.elements.front().shift()}; + auto const xm{L / 2.0 - std::pow(L, 3) / (240.0 * R * R)}; + CHECK(fit.tangent_in == doctest::Approx((R + p) * std::tan(deflection / 2.0) + xm).epsilon(1e-6)); + CHECK(fit.tangent_out == doctest::Approx(fit.tangent_in).epsilon(1e-9)); + CHECK(fit.arc == doctest::Approx(R * deflection - L).epsilon(1e-12)); + // the elements run on from one another + for (std::size_t i = 1; i < fit.elements.size(); ++i) { + auto const &a{fit.elements[i - 1]}; + auto const &b{fit.elements[i]}; + CHECK(glm::distance(a.position(a.length), b.origin) < 1e-12); + CHECK(a.curvature(a.length) == doctest::Approx(b.curvature(0.0)).epsilon(1e-12)); + } + // too long transitions for the deflection + CHECK_FALSE(layout::fit_curve(layout::transition_shape::clothoid, 0.1, R, L, L).error.empty()); + // a curve turning right is the mirror + auto const right{layout::fit_curve(layout::transition_shape::clothoid, -deflection, R, L, L)}; + CHECK(right.tangent_in == doctest::Approx(fit.tangent_in).epsilon(1e-12)); + CHECK(right.elements.back().position(right.elements.back().length).y == doctest::Approx(-fit.elements.back().position(fit.elements.back().length).y).epsilon(1e-12)); + (void)kPi; +}