Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: refactor of projection algorithm #10164

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion editoast/src/core/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use utoipa::ToSchema;

use crate::core::{AsCoreRequest, Json};
use crate::error::InternalError;
use crate::views::path::projection::Intersection;

editoast_common::schemas! {
IncompatibleConstraints,
Expand Down Expand Up @@ -156,7 +157,7 @@ pub enum PathfindingNotFound {

/// An oriented range on a track section.
/// `begin` is always less than `end`.
#[derive(Serialize, Deserialize, Clone, Debug, ToSchema, Hash, PartialEq, Eq)]
#[derive(Serialize, Deserialize, Clone, ToSchema, Hash, PartialEq, Eq)]
pub struct TrackRange {
/// The track section identifier.
#[schema(inline)]
Expand All @@ -171,6 +172,7 @@ pub struct TrackRange {

impl From<editoast_schemas::infra::DirectionalTrackRange> for TrackRange {
fn from(value: editoast_schemas::infra::DirectionalTrackRange) -> Self {
assert!(value.begin <= value.end);
TrackRange {
track_section: value.track,
begin: (value.begin * 1000.).round() as u64,
Expand All @@ -180,6 +182,17 @@ impl From<editoast_schemas::infra::DirectionalTrackRange> for TrackRange {
}
}

impl std::fmt::Debug for TrackRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "'{}+", self.track_section)?;
if matches!(self.direction, Direction::StartToStop) {
write!(f, "{}-{}'", self.begin, self.end)
} else {
write!(f, "{}-{}'", self.end, self.begin)
}
}
}

#[cfg(test)]
impl std::str::FromStr for TrackRange {
type Err = String;
Expand All @@ -200,6 +213,7 @@ impl std::str::FromStr for TrackRange {
let Ok(end) = end.parse() else {
return Err(format!("{end} in track range should be an integer"));
};
assert!(begin != end); // Impossible to determine direction in this case
let (begin, end, direction) = if begin < end {
(begin, end, Direction::StartToStop)
} else {
Expand All @@ -223,6 +237,7 @@ impl TrackRange {
end: u64,
direction: Direction,
) -> Self {
assert!(begin <= end);
Self {
track_section: track_section.as_ref().into(),
begin,
Expand Down Expand Up @@ -261,6 +276,23 @@ impl TrackRange {
pub fn length(&self) -> u64 {
self.end - self.begin
}

// Check if the 2 track ranges overlap.
// Intersection have non-null length and are offsets from the beginning
// of the `self` track range.
pub fn overlap(&self, track_range: &TrackRange) -> Option<Intersection> {
if self.track_section != track_range.track_section {
return None;
}
let begin = std::cmp::max(self.begin, track_range.begin);
let end = std::cmp::min(self.end, track_range.end);
// In case of equality, we don't consider it an overlap
if begin < self.end && end > self.begin {
Some(Intersection::from((begin, end)))
} else {
None
}
}
}

pub struct TrackRangeOffset<'a> {
Expand Down
Loading
Loading