Skip to main content

PhysicalGraph

Struct PhysicalGraph 

Source
pub struct PhysicalGraph<R, T: CoordNum> {
    pub graph: UnGraph<TransitNode<R>, TransitEdge<T>, u32>,
    /* private fields */
}
Expand description

Represents the physical layout of the transit network.

PhysicalGraph is an undirected graph where each node represents a transit node (a point in the transit network where a vehicle can stop) and each edge represents a transit edge (a path between two transit nodes). The PhysicalGraph uses the UnGraph structure from the petgraph crate to internally represent this data. The PhysicalGraph maintains mappings between NodeIds and NodeIndexes (from petgraph), allowing for efficient conversion between the two.

§Examples

Creating a new PhysicalGraph and adding a TransitNode:

use transit_grid::core::TransitNode;
use transit_grid::prelude::PhysicalGraph;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
graph.add_transit_node(node);

Adding a TransitEdge to the PhysicalGraph:

use transit_grid::core::{TransitNode, TransitEdge};
use transit_grid::prelude::PhysicalGraph;
use geo::{coord, Coord, LineString};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };
let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);
let edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:0.0, y:0.0 }, coord! { x:1.0, y:1.0 }]),
};
graph.add_transit_edge(edge);

Fields§

§graph: UnGraph<TransitNode<R>, TransitEdge<T>, u32>

Underlying undirected graph.

Implementations§

Source§

impl<R: Copy, T: CoordNum> PhysicalGraph<R, T>

Source

pub fn new() -> Self

Creates a new, empty PhysicalGraph.

Source

pub fn index_to_id(&self, index: NodeIndex) -> Option<&NodeId>

Converts a NodeIndex to a NodeId.

This method provides a way to map from the petgraph’s NodeIndex to the NodeId used in the TransitNode.

§Arguments
  • index - The NodeIndex to be converted.
§Returns
  • NodeId - The corresponding NodeId of the provided NodeIndex.
§Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node_index = graph.add_transit_node(node);
let node_id = graph.index_to_id(node_index);
assert_eq!(node_id, Some(&1));
Source

pub fn id_to_index(&self, id: NodeId) -> Option<&NodeIndex>

Converts a NodeId to a NodeIndex.

This method provides a way to map from a NodeId used in the TransitNode to the petgraph’s NodeIndex.

§Arguments
  • id - The NodeId to be converted.
§Returns
  • NodeIndex - The corresponding NodeIndex of the provided NodeId.
§Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node_index = graph.add_transit_node(node);
let queried_index = graph.id_to_index(1);
assert_eq!(Some(&node_index), queried_index);
Source

pub fn add_transit_node(&mut self, node: TransitNode<R>) -> NodeIndex

Adds a TransitNode to the PhysicalGraph.

§Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::TransitNode;
use geo::{coord, Coord};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
graph.add_transit_node(node);
Source

pub fn add_transit_edge(&mut self, edge: TransitEdge<T>) -> EdgeIndex

Adds a TransitEdge to the PhysicalGraph.

§Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::{TransitNode, TransitEdge};
use geo::{coord, Coord, LineString};
use petgraph::csr::IndexType;

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };

let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);

let edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:0.0, y:0.0 }, coord! { x:1.0, y:1.0 }]),
};

graph.add_transit_edge(edge);
Source

pub fn get_transit_edge( &self, node1: NodeId, node2: NodeId, ) -> Option<&TransitEdge<T>>

Returns a reference to the TransitEdge connecting the two nodes specified by node1 and node2.

§Arguments
  • node1 - The NodeId of the first node.
  • node2 - The NodeId of the second node.
§Returns

A reference to the TransitEdge connecting node1 and node2. This function will panic if there is no edge between the nodes.

§Panics

This function will panic in the following cases:

  • If node1 or node2 are not valid node IDs in the graph.
  • If there is no edge between node1 and node2.
Source

pub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>

Returns a reference to the TransitEdge with the specified EdgeId.

Source

pub fn repair_edge(&mut self, node1: NodeId, node2: NodeId)
where Euclidean: Distance<T, R, Coord<T>>,

Repairs a physical edge in the PhysicalGraph based on its nodes’ locations.

§Arguments
  • edge - The TransitEdge to be repaired.
§Example
use transit_grid::prelude::PhysicalGraph;
use transit_grid::core::{TransitNode, TransitEdge};
use geo::{coord, Coord, LineString};

let mut graph: PhysicalGraph<Coord, f64> = PhysicalGraph::new();
let node1 = TransitNode { id: 1, location: coord! { x:0.0, y:0.0 } };
let node2 = TransitNode { id: 2, location: coord! { x:1.0, y:1.0 } };

let node1_id = graph.add_transit_node(node1);
let node2_id = graph.add_transit_node(node2);

let mut edge = TransitEdge {
    id: 1,
    source: 1,
    target: 2,
    length: 1.0,
    path: LineString(vec![coord! { x:1.0, y:1.0 }, coord! { x:0.0, y:0.0 }]),  // Note that the direction is initially reversed
};

graph.add_transit_edge(edge.clone());
graph.repair_edge(1, 2);
let edge = graph.get_transit_edge(1, 2).unwrap();
assert_eq!(
   edge.path,
   LineString(vec![Coord { x: 0.0, y: 0.0 }, Coord { x: 1.0, y: 1.0 }])
);

// After repair, the edge path should be from 0,0 to 1,1
//assert_eq!(edge.path.0.first().unwrap(), &coord! { x:0.0, y:0.0 });
//assert_eq!(edge.path.0.last().unwrap(), &coord! { x:1.0, y:1.0 });

Trait Implementations§

Source§

impl<R: Clone, T: Clone + CoordNum> Clone for PhysicalGraph<R, T>

Source§

fn clone(&self) -> PhysicalGraph<R, T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Debug, T: Debug + CoordNum> Debug for PhysicalGraph<R, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: Copy, T: CoordNum> Default for PhysicalGraph<R, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<R, T> Freeze for PhysicalGraph<R, T>

§

impl<R, T> RefUnwindSafe for PhysicalGraph<R, T>

§

impl<R, T> Send for PhysicalGraph<R, T>
where R: Send, T: Send,

§

impl<R, T> Sync for PhysicalGraph<R, T>
where R: Sync, T: Sync,

§

impl<R, T> Unpin for PhysicalGraph<R, T>
where R: Unpin, T: Unpin,

§

impl<R, T> UnsafeUnpin for PhysicalGraph<R, T>

§

impl<R, T> UnwindSafe for PhysicalGraph<R, T>
where R: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

§

fn is_within(&self, b: &G2) -> bool