18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

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.
This commit is contained in:
jerrrrycho
2026-11-20 03:43:44 +01:00
committed by GitHub
parent 4a52b5affc
commit cf94a39e9f
6 changed files with 485 additions and 16 deletions

78
vehicle/aiwire.cpp Normal file
View File

@@ -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 <cmath>
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<bool(double)> 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<int>(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

75
vehicle/aiwire.h Normal file
View File

@@ -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 <functional>
#include <glm/glm.hpp>
// 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<bool(double)> 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