From cf94a39e9f5e16c27d1995869356d2d3bbbe6804 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Fri, 20 Nov 2026 03:43:44 +0100 Subject: [PATCH] refactor(ai): one span test for the pantographs and the AI, a wire query and the stop solver (#357) Step 1 of 2 for the AI stopping with its pantograph past the end of the overhead wire. Nothing changes in what the vehicles or the AI do. - vehicle/aiwire.{h,cpp} (core): span_contact(), the geometry of a pantograph against a span of the wire, moved out of basic_cell::update_traction with the same operations in the same order; reaches(); wire_shortfall(), how far short of its target a train stops to keep its rear pantograph under the wire (3 m margin, 2 m steps, up to 300 m back, 0 when nothing is found); stops_under_wire(), which stops this applies to. - basic_cell::update_traction calls span_contact(). - basic_cell/basic_section/basic_region::wire_over(): is there a wire over a point of the track within reach of a pantograph. Not called yet. - tests/unit/test_ai_wire.cpp, including a check that span_contact() gives the very numbers of the code it replaced. --- CMakeLists.txt | 1 + scene/scene.cpp | 80 +++++++++--- scene/scene.h | 13 ++ tests/unit/test_ai_wire.cpp | 254 ++++++++++++++++++++++++++++++++++++ vehicle/aiwire.cpp | 78 +++++++++++ vehicle/aiwire.h | 75 +++++++++++ 6 files changed, 485 insertions(+), 16 deletions(-) create mode 100644 tests/unit/test_ai_wire.cpp create mode 100644 vehicle/aiwire.cpp create mode 100644 vehicle/aiwire.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 78abf46a2..c78515f1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -557,6 +557,7 @@ set(CORE_SOURCES "scripting/scriptscreenkernels.cpp" "scripting/scriptscreendrawing.cpp" "simulation/simulationtestrules.cpp" +"vehicle/aiwire.cpp" "vehicle/blackbox.cpp" "utilities/physicslog.cpp" "vehicle/enginestart.cpp" diff --git a/scene/scene.cpp b/scene/scene.cpp index b31c1749a..eed3f6438 100644 --- a/scene/scene.cpp +++ b/scene/scene.cpp @@ -22,6 +22,7 @@ http://mozilla.org/MPL/2.0/. #include "scene/sceneterrain.h" #include "rendering/renderer.h" #include "widgets/map_objects.h" +#include "vehicle/aiwire.h" namespace scene { @@ -57,25 +58,15 @@ basic_cell::update_traction( TDynamicObject *Vehicle, int const Pantographindex for( auto *traction : m_directories.traction ) { - // współczynniki równania parametrycznego - auto const paramfrontdot = glm::dot( traction->vParametric, vFront ); - auto const fRaParam = - -( glm::dot( traction->pPoint1, vFront ) - glm::dot( pantographposition, vFront ) ) - / ( paramfrontdot != 0.0 ? - paramfrontdot : - 0.001 ); // div0 trap - - if( fRaParam < -0.001 - || fRaParam > 1.001 ) { continue; } + // the span against the pantograph, the geometry the AI uses to find the wire as well + auto const contact = ai_wire::span_contact( + traction->pPoint1, traction->vParametric, pantographposition, vFront, vUp, vLeft, pantograph->fWidth ); // jeśli tylko jest w przedziale, wyznaczyć odległość wzdłuż wektorów vUp i vLeft - // punkt styku płaszczyzny z drutem (dla generatora łuku el.) - auto const vStyk = traction->pPoint1 + fRaParam * traction->vParametric; - // wektor musi się mieścić w przedziale ruchu pantografu - auto const vGdzie = vStyk - pantographposition; - auto fVertical = glm::dot( vGdzie, vUp ); + if( false == contact.within ) { continue; } + auto fVertical = contact.vertical; if( fVertical >= 0.0 ) { // jeśli ponad pantografem (bo może łapać druty spod wiaduktu) - auto const fHorizontal = std::abs( glm::dot( vGdzie, vLeft ) ) - pantograph->fWidth; + auto const fHorizontal = contact.horizontal; if (Global.bEnableTraction && fVertical < pantograph->PantWys - 0.15 ) { @@ -120,6 +111,24 @@ basic_cell::update_traction( TDynamicObject *Vehicle, int const Pantographindex } } +// checks whether a wire of the cell, not broken, is within reach of a pantograph at specified point +bool +basic_cell::wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double const Halfwidth, double const Minheight, double const Maxheight ) const { + + for( auto const *traction : m_directories.traction ) { + if( ( traction->DamageFlag & 128 ) != 0 ) { + // a broken wire powers nothing + continue; + } + if( ai_wire::reaches( + ai_wire::span_contact( traction->pPoint1, traction->vParametric, Point, Front, Up, Left, Halfwidth ), + Minheight, Maxheight ) ) { + return true; + } + } + return false; +} + // legacy method, updates sounds and polls event launchers within radius around specified point void basic_cell::update_events() { @@ -785,6 +794,22 @@ basic_section::update_traction( TDynamicObject *Vehicle, int const Pantographind } } +// checks whether a wire of the section's cells around specified point is within reach of a pantograph at the point +bool +basic_section::wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double const Halfwidth, double const Minheight, double const Maxheight ) const { + + auto const radius { EU07_CELLSIZE * 0.5 }; // the same area of interest the pantographs look for their wire in + + for( auto const &cell : m_cells ) { + // we reject early cells which aren't within our area of interest + if( glm::length2( cell.area().center - Point ) < sq( cell.area().radius + radius ) + && cell.wire_over( Point, Front, Up, Left, Halfwidth, Minheight, Maxheight ) ) { + return true; + } + } + return false; +} + // legacy method, polls event launchers within radius around specified point void basic_section::update_events( glm::dvec3 const &Location, float const Radius ) { @@ -1168,6 +1193,29 @@ basic_region::update_traction( TDynamicObject *Vehicle, int const Pantographinde } } +// checks whether there's a contact wire over specified point, within reach of a pantograph there +bool +basic_region::wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, double const Halfwidth, double const Minheight, double const Maxheight ) const { + + // the vertical up and the horizontal left of the heading. the tilt of the track in curves is left out + auto const up { glm::dvec3 { 0.0, 1.0, 0.0 } }; + auto const left { glm::cross( up, Front ) }; + auto const leftlength { glm::length( left ) }; + if( leftlength <= 0.0 ) { + // no heading along the ground + return false; + } + // a list of its own, not the shared scratchpad of the region + std::vector sectionlist; + sections( Point, EU07_CELLSIZE * 0.5, sectionlist ); + for( auto const *section : sectionlist ) { + if( section->wire_over( Point, Front, up, left / leftlength, Halfwidth, Minheight, Maxheight ) ) { + return true; + } + } + return false; +} + // checks whether specified file is a valid region data file bool basic_region::is_scene( std::string const &Scenariofile ) const { diff --git a/scene/scene.h b/scene/scene.h index 1c3f8064c..b8256e257 100644 --- a/scene/scene.h +++ b/scene/scene.h @@ -109,6 +109,10 @@ public: // legacy method, finds and assigns traction piece to specified pantograph of provided vehicle void update_traction( TDynamicObject *Vehicle, int const Pantographindex ); + // checks whether a wire, not broken, is within reach of a pantograph at specified point: the plane of the pantograph's + // movement, normal to Front, crosses it over the slide of half width Halfwidth, between Minheight and Maxheight along Up + bool + wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double const Halfwidth, double const Minheight, double const Maxheight ) const; // legacy method, polls event launchers within radius around specified point void update_events(); @@ -320,6 +324,10 @@ public: // legacy method, finds and assigns traction piece to specified pantograph of provided vehicle void update_traction( TDynamicObject *Vehicle, int const Pantographindex ); + // checks whether a wire, not broken, is within reach of a pantograph at specified point: the plane of the pantograph's + // movement, normal to Front, crosses it over the slide of half width Halfwidth, between Minheight and Maxheight along Up + bool + wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double const Halfwidth, double const Minheight, double const Maxheight ) const; // legacy method, updates sounds and polls event launchers within radius around specified point void update_events( glm::dvec3 const &Location, float const Radius ); @@ -446,6 +454,11 @@ public: // legacy method, finds and assigns traction piece to specified pantograph of provided vehicle void update_traction( TDynamicObject *Vehicle, int const Pantographindex ); + // checks whether there's a contact wire, not broken, over specified point of a track with heading Front, within reach of + // a pantograph there: over its slide of half width Halfwidth, between Minheight and Maxheight above the point. the + // geometry is the one the pantographs find their wire with (ai_wire::span_contact), with the vertical up + bool + wire_over( glm::dvec3 const &Point, glm::dvec3 const &Front, double const Halfwidth, double const Minheight, double const Maxheight ) const; // legacy method, polls event launchers around camera void update_events(); diff --git a/tests/unit/test_ai_wire.cpp b/tests/unit/test_ai_wire.cpp new file mode 100644 index 000000000..070f8618f --- /dev/null +++ b/tests/unit/test_ai_wire.cpp @@ -0,0 +1,254 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/aiwire.h" + +#include +#include +#include + +// where the overhead wire is for the AI driver (vehicle/aiwire.h): a pantograph against a span of the wire, and how far +// short of its target a train stops to keep its pantograph under the wire. the scene query over these +// (basic_region::wire_over) needs the scene, which the unit tests don't link + +namespace { + +// a vehicle running along +z on level track, the pantograph's base point 4 m above the origin +glm::dvec3 const front{0.0, 0.0, 1.0}; +glm::dvec3 const up{0.0, 1.0, 0.0}; +glm::dvec3 const left{1.0, 0.0, 0.0}; +glm::dvec3 const pantograph{0.0, 4.0, 0.0}; +double const halfwidth{0.5}; + +// a 100 m span along the track, at height Height and Offset to the side, its middle at Middle along the track +ai_wire::contact along(double const Offset, double const Height, double const Middle = 0.0) { + return ai_wire::span_contact({Offset, Height, Middle - 50.0}, {0.0, 0.0, 100.0}, pantograph, front, up, left, + halfwidth); +} + +// the wire over the route up to Last metres from the head of the train +auto wire_to(double const Last) { + return [Last](double const Distance) { return Distance <= Last; }; +} + +} // namespace + +TEST_CASE("ai wire: a span centred over the pantograph is on the slide") { + auto const contact{along(0.0, 6.0)}; + + CHECK(contact.within); + CHECK(contact.vertical == doctest::Approx(2.0)); + CHECK(contact.horizontal == doctest::Approx(-halfwidth)); + CHECK(ai_wire::reaches(contact, 1.0, 3.0)); +} + +TEST_CASE("ai wire: a span at the very edge of the slide still touches it, one beyond it doesn't") { + auto const edge{along(halfwidth, 6.0)}; + CHECK(edge.horizontal == doctest::Approx(0.0)); + CHECK(ai_wire::reaches(edge, 1.0, 3.0)); + // either side + CHECK(ai_wire::reaches(along(-halfwidth, 6.0), 1.0, 3.0)); + + auto const beyond{along(halfwidth + 0.1, 6.0)}; + CHECK(beyond.within); + CHECK(beyond.horizontal == doctest::Approx(0.1)); + CHECK_FALSE(ai_wire::reaches(beyond, 1.0, 3.0)); +} + +TEST_CASE("ai wire: the wire is reached only within the given heights") { + auto const contact{along(0.0, 6.0)}; + + CHECK_FALSE(ai_wire::reaches(contact, 3.0, 9.0)); // too low: below the lowest height + CHECK_FALSE(ai_wire::reaches(contact, 0.0, 1.5)); // too high: a bridge's wire above the line + CHECK(ai_wire::reaches(contact, 2.0, 2.0)); // the limits are included +} + +TEST_CASE("ai wire: a span below the pantograph is crossed but out of reach") { + auto const contact{along(0.0, 3.0)}; + + CHECK(contact.within); + CHECK(contact.vertical == doctest::Approx(-1.0)); + CHECK_FALSE(ai_wire::reaches(contact, 0.0, 9.0)); +} + +TEST_CASE("ai wire: a span ahead of or behind the pantograph isn't crossed") { + auto const ahead{along(0.0, 6.0, 60.0)}; + CHECK_FALSE(ahead.within); + CHECK_FALSE(ai_wire::reaches(ahead, 0.0, 9.0)); + + CHECK_FALSE(along(0.0, 6.0, -60.0).within); + // the ends of the span have the tolerance of the vehicles: 0.001 of the span's length + CHECK(along(0.0, 6.0, 50.05).within); + CHECK(along(0.0, 6.0, -50.05).within); + CHECK_FALSE(along(0.0, 6.0, 50.2).within); +} + +TEST_CASE("ai wire: a span skewed against the track is met where it crosses the plane of the pantograph") { + // the zigzag of the wire: from 0.3 m to the right to 0.3 m to the left over 100 m + auto const zigzag{ai_wire::span_contact({-0.3, 6.0, -50.0}, {0.6, 0.0, 100.0}, pantograph + glm::dvec3{0, 0, 25.0}, + front, up, left, halfwidth)}; + CHECK(zigzag.within); + CHECK(zigzag.horizontal == doctest::Approx(0.15 - halfwidth)); + CHECK(ai_wire::reaches(zigzag, 1.0, 3.0)); + + // a sloping span from a tunnel portal, 1 m lower at its far end: the height where the plane crosses it + auto const sloping{ai_wire::span_contact({0.0, 6.0, -50.0}, {0.0, -1.0, 100.0}, pantograph + glm::dvec3{0, 0, 25.0}, + front, up, left, halfwidth)}; + CHECK(sloping.vertical == doctest::Approx(1.25)); + + // a span of a wire crossing the line at a right angle lies in the plane of the pantograph over the track, and is + // out of reach a step along the track (the division by zero trap) + auto const across{ai_wire::span_contact({-5.0, 6.0, 0.0}, {10.0, 0.0, 0.0}, pantograph, front, up, left, halfwidth)}; + CHECK(across.within); + CHECK(std::isfinite(across.vertical)); + CHECK_FALSE(ai_wire::reaches(across, 0.0, 9.0)); // its end, 5 m to the side + CHECK_FALSE(ai_wire::span_contact({-5.0, 6.0, 0.0}, {10.0, 0.0, 0.0}, pantograph + glm::dvec3{0, 0, 1.0}, front, up, + left, halfwidth) + .within); +} + +TEST_CASE("ai wire: the direction of travel doesn't change what the pantograph meets") { + auto const forward{along(0.3, 6.0)}; + auto const backward{ai_wire::span_contact({0.3, 6.0, -50.0}, {0.0, 0.0, 100.0}, pantograph, -front, up, -left, + halfwidth)}; + + CHECK(backward.within); + CHECK(backward.vertical == forward.vertical); + CHECK(backward.horizontal == forward.horizontal); +} + +TEST_CASE("ai wire: the span test gives the very numbers of the code the vehicles had before it was moved here") { + // the math of basic_cell::update_traction before it called span_contact, as it was there + struct legacy { + bool within; + double vertical; + double horizontal; + }; + auto const legacy_contact{[](glm::dvec3 const &pPoint1, glm::dvec3 const &vParametric, + glm::dvec3 const &pantographposition, glm::dvec3 const &vFront, glm::dvec3 const &vUp, + glm::dvec3 const &vLeft, double const fWidth) { + auto const paramfrontdot = glm::dot(vParametric, vFront); + auto const fRaParam = + -(glm::dot(pPoint1, vFront) - glm::dot(pantographposition, vFront)) / (paramfrontdot != 0.0 ? paramfrontdot : 0.001); + if (fRaParam < -0.001 || fRaParam > 1.001) { + return legacy{false, 0.0, 0.0}; + } + auto const vStyk = pPoint1 + fRaParam * vParametric; + auto const vGdzie = vStyk - pantographposition; + auto const fVertical = glm::dot(vGdzie, vUp); + auto const fHorizontal = std::abs(glm::dot(vGdzie, vLeft)) - fWidth; + return legacy{true, fVertical, fHorizontal}; + }}; + + // pseudo-random vehicles and spans around a point far from the origin, as in a real scenery + std::uint32_t seed{12345}; + auto const next{[&seed](double const Low, double const High) { + seed = seed * 1664525u + 1013904223u; + return Low + (High - Low) * (seed >> 8) / double(1u << 24); + }}; + int within{0}; + for (int sample = 0; sample < 2000; ++sample) { + auto const heading{next(0.0, 6.283185307179586)}; + auto const cant{next(-0.1, 0.1)}; + glm::dvec3 const vFront{std::sin(heading), next(-0.03, 0.03), std::cos(heading)}; + glm::dvec3 const vUp{std::sin(cant) * std::cos(heading), std::cos(cant), -std::sin(cant) * std::sin(heading)}; + auto const vLeft{glm::cross(vUp, vFront)}; + glm::dvec3 const position{next(-20000.0, 20000.0), next(100.0, 300.0), next(-20000.0, 20000.0)}; + auto const pantographposition{position + vUp * 4.2}; + auto const spanheading{heading + next(-0.05, 0.05)}; + glm::dvec3 const vParametric{60.0 * std::sin(spanheading), next(-0.5, 0.5), 60.0 * std::cos(spanheading)}; + auto const pPoint1{position + glm::dvec3{next(-1.0, 1.0), next(5.0, 7.0), next(-1.0, 1.0)} - vParametric * next(-0.2, 1.2)}; + auto const fWidth{next(0.4, 0.7)}; + + auto const old{legacy_contact(pPoint1, vParametric, pantographposition, vFront, vUp, vLeft, fWidth)}; + auto const now{ai_wire::span_contact(pPoint1, vParametric, pantographposition, vFront, vUp, vLeft, fWidth)}; + CAPTURE(sample); + REQUIRE(now.within == old.within); + if (old.within) { + ++within; + // bit for bit + REQUIRE(now.vertical == old.vertical); + REQUIRE(now.horizontal == old.horizontal); + } + } + // both kinds were tried + CHECK(within > 200); + CHECK(within < 1800); +} + +TEST_CASE("ai wire: a stop with the wire over the pantograph stays where it is") { + auto const everywhere{[](double) { return true; }}; + CHECK(ai_wire::wire_shortfall(everywhere, 20.0, 130.0) == 0.0); + + // the wire ends between the pantographs: the rear one the train relies on is under it, with the margin ahead + CHECK(ai_wire::wire_shortfall(wire_to(118.0), 20.0, 130.0) == 0.0); + // gaps behind the pantograph don't matter + auto const gap_behind{[](double const Distance) { return Distance <= 90.0 || Distance >= 95.0; }}; + CHECK(ai_wire::wire_shortfall(gap_behind, 20.0, 130.0) == 0.0); +} + +TEST_CASE("ai wire: a stop past the end of the wire is moved back to put the pantograph under the wire") { + // the head stops at 130 m, the pantograph 20 m behind it at 110 m; the wire ends at 100 m. 2 m steps back to the + // first place with the wire over the pantograph and 3 m ahead of it: 96 m + CHECK(ai_wire::wire_shortfall(wire_to(100.0), 20.0, 130.0) == doctest::Approx(14.0)); + // the wire ends right at the pantograph: no margin ahead + CHECK(ai_wire::wire_shortfall(wire_to(110.0), 20.0, 130.0) == doctest::Approx(4.0)); + // a gap in the wire at the pantograph: the stop goes before the gap + auto const gap{[](double const Distance) { return Distance <= 107.0 || Distance >= 112.0; }}; + CHECK(ai_wire::wire_shortfall(gap, 20.0, 130.0) == doctest::Approx(6.0)); +} + +TEST_CASE("ai wire: the margin of wire ahead of the pantograph") { + // exactly the margin ahead: fine + CHECK(ai_wire::wire_shortfall(wire_to(113.0), 20.0, 130.0) == 0.0); + // a little less: one step back + CHECK(ai_wire::wire_shortfall(wire_to(112.9), 20.0, 130.0) == doctest::Approx(2.0)); + // other margins and steps + CHECK(ai_wire::wire_shortfall(wire_to(100.0), 20.0, 130.0, 0.0, 1.0) == doctest::Approx(10.0)); + CHECK(ai_wire::wire_shortfall(wire_to(100.0), 20.0, 130.0, 5.0, 1.0) == doctest::Approx(15.0)); +} + +TEST_CASE("ai wire: without the wire within the walk back the stop stays where it is") { + auto const nowhere{[](double) { return false; }}; + CHECK(ai_wire::wire_shortfall(nowhere, 20.0, 130.0) == 0.0); + + // the pantograph at 110 m: the wire ends more than 300 m back + CHECK(ai_wire::wire_shortfall(wire_to(-200.0), 20.0, 130.0) == 0.0); + // the last place looked at is 300 m back + CHECK(ai_wire::wire_shortfall(wire_to(-187.0), 20.0, 130.0) == doctest::Approx(300.0)); + CHECK(ai_wire::wire_shortfall(wire_to(-187.0), 20.0, 130.0, 3.0, 2.0, 298.0) == 0.0); +} + +TEST_CASE("ai wire: a stop without a coverage or a step to walk with stays where it is") { + CHECK(ai_wire::wire_shortfall({}, 20.0, 130.0) == 0.0); + CHECK(ai_wire::wire_shortfall(wire_to(100.0), 20.0, 130.0, 3.0, 0.0) == 0.0); + CHECK(ai_wire::wire_shortfall(wire_to(100.0), 20.0, 130.0, 3.0, -2.0) == 0.0); +} + +TEST_CASE("ai wire: the walk back asks for the places the 2 m steps give") { + std::vector asked; + auto const recording{[&asked](double const Distance) { + asked.push_back(Distance); + return Distance <= 100.0; + }}; + ai_wire::wire_shortfall(recording, 20.0, 130.0); + + // the pantograph's place and the margin ahead, then the steps back; the margin is asked only where the place is + // under the wire + std::vector const expected{110.0, 108.0, 106.0, 104.0, 102.0, 100.0, 103.0, 98.0, 101.0, 96.0, 99.0}; + CHECK(asked == expected); +} + +TEST_CASE("ai wire: which stops keep the pantograph under the wire") { + using ai_wire::stop_kind; + // the stops of a train that runs with the wire + CHECK(ai_wire::stops_under_wire(true, true, false, stop_kind::track_end)); + CHECK(ai_wire::stops_under_wire(true, true, false, stop_kind::timetable_stop)); + // a signal is a wait, the vehicle to couple to may stand without the wire + CHECK_FALSE(ai_wire::stops_under_wire(true, true, false, stop_kind::signal)); + CHECK_FALSE(ai_wire::stops_under_wire(true, true, false, stop_kind::coupling)); + // without a current collector, with the voltage made up, where the scenery says to coast or lower the pantographs + CHECK_FALSE(ai_wire::stops_under_wire(false, true, false, stop_kind::track_end)); + CHECK_FALSE(ai_wire::stops_under_wire(true, false, false, stop_kind::track_end)); + CHECK_FALSE(ai_wire::stops_under_wire(true, true, true, stop_kind::timetable_stop)); +} diff --git a/vehicle/aiwire.cpp b/vehicle/aiwire.cpp new file mode 100644 index 000000000..999d69075 --- /dev/null +++ b/vehicle/aiwire.cpp @@ -0,0 +1,78 @@ +/* +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 "vehicle/aiwire.h" + +#include + +namespace ai_wire { + +contact span_contact(glm::dvec3 const &Point1, glm::dvec3 const &Parametric, glm::dvec3 const &Point, + glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double const Halfwidth) { + // Winger 170204: the parameter of the span's point in the plane of the pantograph's movement (the plane normal to + // Front through Point). the operations and their order are those the vehicles always had, so they find the same wire + auto const paramfrontdot = glm::dot(Parametric, Front); + auto const parameter = -(glm::dot(Point1, Front) - glm::dot(Point, Front)) / (paramfrontdot != 0.0 ? paramfrontdot : 0.001); + + contact result; + if (parameter < -0.001 || parameter > 1.001) { + return result; + } + // the point where the wire crosses the plane, against the pantograph + auto const crossing = Point1 + parameter * Parametric; + auto const offset = crossing - Point; + result.within = true; + result.vertical = glm::dot(offset, Up); + result.horizontal = std::abs(glm::dot(offset, Left)) - Halfwidth; + return result; +} + +bool reaches(contact const &Contact, double const Minheight, double const Maxheight) { + return Contact.within && Contact.horizontal <= 0.0 && Contact.vertical >= Minheight && Contact.vertical <= Maxheight; +} + +double wire_shortfall(std::function const &Covered, double const Pantograph, double const Stop, + double const Margin, double const Step, double const Walkback) { + if (!Covered || Step <= 0.0) { + return 0.0; + } + auto const fine{[&](double const Place) { return Covered(Place) && Covered(Place + Margin); }}; + // the pantograph at the target of the head + auto const target{Stop - Pantograph}; + if (fine(target)) { + return 0.0; + } + // the places back are counted from the target, not added up, so the steps don't drift + auto const steps{static_cast(std::floor(Walkback / Step + 1e-9))}; + for (int step = 1; step <= steps; ++step) { + auto const back{step * Step}; + if (fine(target - back)) { + return back; + } + } + return 0.0; +} + +bool stops_under_wire(bool const Collector, bool const Livetraction, bool const Overheadrestricted, stop_kind const Kind) { + if (!Collector || !Livetraction || Overheadrestricted) { + return false; + } + switch (Kind) { + case stop_kind::track_end: + case stop_kind::timetable_stop: + return true; + case stop_kind::signal: + case stop_kind::coupling: + default: + return false; + } +} + +} // namespace ai_wire diff --git a/vehicle/aiwire.h b/vehicle/aiwire.h new file mode 100644 index 000000000..89ccb1ecc --- /dev/null +++ b/vehicle/aiwire.h @@ -0,0 +1,75 @@ +/* +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 + +// where the overhead contact wire is, for the AI driver to stop its train with the pantograph under the wire, not past +// the end of it. the geometry of a pantograph against a span of the wire is the one the vehicles use to find their wire +// (basic_cell::update_traction), so the AI sees the wire where the pantographs do +namespace ai_wire { + +// a pantograph against one span of the wire +struct contact { + // the plane of the pantograph's movement (across the vehicle, at the pantograph) cuts the span, between its ends + bool within{false}; + // height of the wire above the pantograph's base point, along the up vector. negative for a wire below it + double vertical{0.0}; + // sideways distance of the wire from the working part of the slide: |offset along the left vector| less the slide's + // half width. the wire is on the slide when it's not positive + double horizontal{0.0}; +}; + +// the pantograph with its base point at Point, on a vehicle whose front, up and left vectors are Front, Up and Left, and +// a slide of half width Halfwidth, against the span from Point1 along Parametric (the span's other end is Point1 + +// Parametric). for a span parallel to the plane of the pantograph's movement the distance to the plane is divided by +// 0.001 in place of 0 (the division by zero trap the vehicles always had), which puts the crossing outside the span +// unless the span lies in the plane +contact span_contact(glm::dvec3 const &Point1, glm::dvec3 const &Parametric, glm::dvec3 const &Point, + glm::dvec3 const &Front, glm::dvec3 const &Up, glm::dvec3 const &Left, double Halfwidth); + +// the span is a wire the pantograph can reach: it crosses the plane of the pantograph's movement on the slide, between +// Minheight and Maxheight above the pantograph's base point. the heights keep out the wires of the bridges above the +// line and of the lines below it +bool reaches(contact const &Contact, double Minheight, double Maxheight); + +// the default margin of wire ahead of the pantograph at a stop, the step and the farthest distance back the stop is +// looked for under the wire, in metres +double const stop_margin{3.0}; +double const stop_step{2.0}; +double const stop_walk_back{300.0}; + +// how far short of its target a train has to stop so its pantograph stays under the wire, in metres along the route. +// the distances are along the route from the head of the train: Stop the target of the head, Pantograph the pantograph +// the train relies on (the rearmost one it raises) behind the head. Covered tells whether there's a wire over the +// route at the given distance. the pantograph at the stop is fine when there's wire over it and Margin ahead of it; +// otherwise it's moved back by Step at a time, up to Walkback, to the first place it's fine at. 0 when it's fine at the +// target, and when no place within Walkback is (not an electrified stretch, or a gap too long to tell about: stopping +// short won't help there) +double wire_shortfall(std::function const &Covered, double Pantograph, double Stop, + double Margin = stop_margin, double Step = stop_step, double Walkback = stop_walk_back); + +// what makes the train stop at a place +enum class stop_kind { + track_end, // the end of the track, e.g. a buffer stop, in train or shunting mode + timetable_stop, // a stop of the timetable (W4 indicator) + signal, // a signal at stop + coupling, // a vehicle to couple to or to uncouple from +}; + +// the train stops with its pantograph under the wire. not when it runs without the wire: without a current collector, +// with the voltage made up (livetraction off), on a stretch the scenery tells to coast or to run with the pantographs +// down. not for a signal (a wait, the wire ending before a signal is a fault of the scenery) and not for a coupling +// (the vehicle to couple to may stand on a siding without the wire) +bool stops_under_wire(bool Collector, bool Livetraction, bool Overheadrestricted, stop_kind Kind); + +} // namespace ai_wire