Struct transit_grid::graphs::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 NodeId
s and NodeIndex
es (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>
impl<R: Copy, T: CoordNum> PhysicalGraph<R, T>
sourcepub fn index_to_id(&self, index: NodeIndex) -> Option<&NodeId>
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
- TheNodeIndex
to be converted.
Returns
NodeId
- The correspondingNodeId
of the providedNodeIndex
.
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));
sourcepub fn id_to_index(&self, id: NodeId) -> Option<&NodeIndex>
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
- TheNodeId
to be converted.
Returns
NodeIndex
- The correspondingNodeIndex
of the providedNodeId
.
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);
sourcepub fn add_transit_node(&mut self, node: TransitNode<R>) -> NodeIndex
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);
sourcepub fn add_transit_edge(&mut self, edge: TransitEdge<T>) -> EdgeIndex
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);
sourcepub fn get_transit_edge(
&self,
node1: NodeId,
node2: NodeId
) -> Option<&TransitEdge<T>>
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
- TheNodeId
of the first node.node2
- TheNodeId
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
ornode2
are not valid node IDs in the graph. - If there is no edge between
node1
andnode2
.
sourcepub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>
pub fn get_transit_edge_by_id(&self, edge_id: EdgeId) -> Option<&TransitEdge<T>>
Returns a reference to the TransitEdge
with the specified EdgeId
.
sourcepub fn repair_edge(&mut self, node1: NodeId, node2: NodeId)where
R: EuclideanDistance<T, Coord<T>>,
pub fn repair_edge(&mut self, node1: NodeId, node2: NodeId)where R: EuclideanDistance<T, Coord<T>>,
Repairs a physical edge in the PhysicalGraph
based on its nodes’ locations.
Arguments
edge
- TheTransitEdge
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>
impl<R: Clone, T: Clone + CoordNum> Clone for PhysicalGraph<R, T>
source§fn clone(&self) -> PhysicalGraph<R, T>
fn clone(&self) -> PhysicalGraph<R, T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more