This class defined a network for trains. More...
#include <network.h>
Public Member Functions | |
Network () | |
Default constructor. | |
Network (const string &nodesFile, const string &linksFile, std::string netName="") | |
Constructor for the Network class. | |
Network (Vector< tuple< int, double, double, std::string, double, double > > nodesRecords, Vector< tuple< int, int, int, double, int, double, double, int, double, bool, std::string, std::string, double > > linksRecords, std::string netName="") | |
Constructor for the Network class. | |
Network (Vector< std::shared_ptr< NetNode > > theNodes, Vector< std::shared_ptr< NetLink > > theLinks, std::string netName="") | |
Constructor for the Network class. | |
std::map< int, std::shared_ptr< NetNode > > | defineNodes () |
Define nodes. | |
Vector< int > | getSimulatorTrainPath (Vector< int > userDefinedTrainPath) |
Gets simulator train path. | |
tuple< double, double, double, double, double > | getNetworkStats () |
Gets the network statistics. | |
std::shared_ptr< NetNode > | getNodeByID (int &id) |
Gets node by identifier. | |
double | getDistanceBetweenTwoNodes (std::shared_ptr< Train > train, std::shared_ptr< NetNode > node1, std::shared_ptr< NetNode > node2) |
Computes the distance between two nodes on a given train's path. | |
bool | isConflictZone (std::shared_ptr< Train > train, std::shared_ptr< NetNode > node1, std::shared_ptr< NetNode > node2) |
Checks if there's a conflict zone between two nodes for the given train. | |
bool | DistanceToEndOfAllLinkTrainsIsLarge (const std::shared_ptr< NetLink > link, const std::shared_ptr< Train > train) |
Checks if the distance to the end of all leading trains on a link is larger than a certain threshold (2 units in this case). | |
pair< double, double > | getPositionbyTravelledDistance (std::shared_ptr< Train > train, double &travelledDistance) |
Computes the position of a train based on its travelled distance along a path. | |
std::pair< double, double > | normalize (std::pair< double, double > vec) |
Normalize a 2D vector. | |
Vector< std::shared_ptr< NetSignal > > | getSignalsByCurrentNodeList (const std::vector< std::shared_ptr< NetNode > > nodeList) |
Fetches the network signals associated with the list of given nodes. | |
std::shared_ptr< NetLink > | getLinkByStartandEndNodeID (const std::shared_ptr< Train > train, int startID, int endID, bool calcExact=true) |
Fetches the network link by start and end node identifiers. | |
Vector< std::shared_ptr< NetLink > > | getLinksByStartandEndNode (std::shared_ptr< NetNode > startNode, std::shared_ptr< NetNode > endNode) |
Retrieves the network links between a specified start node and end node. | |
double | getFullPathLength (std::shared_ptr< Train > train) |
Calculates the total length of the train's path. | |
std::shared_ptr< NetLink > | getLinkByStartNodeID (const std::shared_ptr< Train > train, int startNodeID) |
Retrieves a network link originating from a specified start node. | |
std::shared_ptr< NetLink > | getFirstTrainLink (const std::shared_ptr< Train > train) |
Retrieves the first link in a train's path. | |
Vector< double > | generateCumLinksLengths (std::shared_ptr< Train > train) |
Generates a vector containing the cumulative lengths of the train's path. | |
std::shared_ptr< NetNode > | getPreviousNodeByDistance (std::shared_ptr< Train > train, double travelledDistance, int &previousNodeID) |
Retrieves the previous node in the train's path given a travelled distance. | |
std::shared_ptr< NetLink > | getLinkFromDistance (std::shared_ptr< Train > train, double &travelledDistance, int &previousNodeID) |
Retrieves the link in the train's path that includes a specified travelled distance. | |
int | getSimulatorNodeIDByUserID (int oldID) |
Translates a user-provided node identifier to a simulator-specific node identifier. | |
double | getDistanceToSpecificNodeByTravelledDistance (std::shared_ptr< Train > train, double &travelledDistance, int &nodeID) |
Calculates the distance to a specific node given a travelled distance. | |
bool | ccw (const std::pair< double, double > &A, const std::pair< double, double > &B, const std::pair< double, double > &C) |
Checks if three points make a counter-clockwise turn. | |
bool | twoLinesIntersect (const std::pair< double, double > &A, const std::pair< double, double > &B, const std::pair< double, double > &C, const std::pair< double, double > &D) |
Checks if two lines intersect. | |
std::pair< Vector< std::shared_ptr< NetNode > >, double > | shortestPathSearch (int startNodeID, int targetNodeID) |
Performs a shortest path search between two nodes. | |
Public Attributes | |
std::string | networkName |
Holds the name of the network. | |
std::map< int, std::shared_ptr< NetNode > > | nodes |
The nodes mapped by its simulator id. | |
Vector< std::shared_ptr< NetLink > > | links |
The links. | |
Vector< std::shared_ptr< NetSignal > > | networkSignals |
The signals. | |
Friends | |
ostream & | operator<< (ostream &ostr, Network &stud) |
Stream insertion operator. | |
This class defined a network for trains.
|
inline |
|
inline |
Constructor for the Network class.
Initializes a new instance of the Network class using node and link files.
nodesFile | A string representing the path to the nodes file. |
linksFile | A string representing the path to the links file. |
netName | A string for the network name. If not provided, the network name will be generated from the linksFile name. |
|
inline |
Constructor for the Network class.
Initializes a new instance of the Network class using pre-loaded nodes and links data.
nodesRecords | A vector of tuples representing the nodes data. |
linksRecords | A vector of tuples representing the links data. |
netName | A string for the network name. If not provided, the network name will be "Unnamed Network". |
|
inline |
Constructor for the Network class.
Initializes a new instance of the Network class using shared pointers to NetNode and NetLink.
theNodes | A vector of shared pointers to NetNode objects. |
theLinks | A vector of shared pointers to NetLink objects. |
netName | A string for the network name. If not provided, the network name will be "Unnamed Network". |
|
inline |
Checks if three points make a counter-clockwise turn.
Given three points A, B, C, it returns true if they are listed in counterclockwise order and false if they are listed in clockwise order or are colinear. The points are pairs of coordinates (x,y) in a 2D plane.
A | The first point as a pair (x,y). |
B | The second point as a pair (x,y). |
C | The third point as a pair (x,y). |
|
inline |
|
inline |
Checks if the distance to the end of all leading trains on a link is larger than a certain threshold (2 units in this case).
Generates a vector containing the cumulative lengths of the train's path.
The function iterates over the train's path and for each node, calculates the cumulative distance from the start node to this node. The result is stored in a vector which is then returned.
train | A shared pointer to the train object. |
|
inline |
Computes the distance between two nodes on a given train's path.
train | A shared_ptr to a Train object representing the train for which to compute the distance. |
node1 | A shared_ptr to the first NetNode object. |
node2 | A shared_ptr to the second NetNode object. |
|
inline |
Calculates the distance to a specific node given a travelled distance.
This function retrieves the cumulative distance to the specified node and subtracts the travelled distance. The result is the remaining distance to the specified node.
train | The train object. |
travelledDistance | The distance that has been travelled. |
nodeID | The identifier of the node. |
|
inline |
Retrieves the first link in a train's path.
This function retrieves the first node from the train's path and finds the link from that node to the next node in the path.
train | The train whose first link is to be retrieved. |
std::runtime_error | If the train's path is empty. |
|
inline |
Calculates the total length of the train's path.
This function iterates over the train's path, adding the length of each link between nodes to compute the total path length.
train | The train whose path length will be calculated. |
std::out_of_range | If trainPath contains a node not connected to the previous node. |
|
inline |
Fetches the network link by start and end node identifiers.
Checks all the links between the two nodes and returns the one that has the train, or the link with the least cost if calcExact is false. If calcExact is true, it will try to find a link where the train can travel without colliding with other trains.
train | A shared pointer to the Train object for which the link is to be found. |
startID | The start node identifier. |
endID | The end node identifier. |
calcExact | (Optional) Flag to calculate exact link, default is true. |
|
inline |
Retrieves a network link originating from a specified start node.
This function finds the next node in the train's path after the specified start node, then retrieves the link from the start node to the next node.
train | The train that is being processed. |
startNodeID | The identifier of the start node. |
std::out_of_range | If startNodeID is not in trainPath or it is the last node in the path. |
|
inline |
Retrieves the link in the train's path that includes a specified travelled distance.
This function iterates over the train's path until it finds the node whose cumulative distance is greater than the travelled distance. The function then retrieves the link from the previous node to this one.
train | The train object. |
travelledDistance | The distance that has been travelled. |
previousNodeID | An input/output parameter that will store the ID of the previous node. |
|
inline |
Retrieves the network links between a specified start node and end node.
This function will search the linked nodes of the start node and return all links that point to the provided end node.
startNode | The starting node of the links. |
endNode | The end node of the links. |
std::out_of_range | If the endNode is not a neighbor of the startNode. |
|
inline |
Gets the network statistics.
|
inline |
Gets node by identifier.
std::runtime_error | Raised when a runtime error condition occurs. |
[in,out] | id | The identifier. |
|
inline |
Computes the position of a train based on its travelled distance along a path.
This function calculates the position of a train on its path given the travelled distance. The path is represented as a sequence of links, each with its own length. The function iterates over these links to find the one on which the train is currently located, and then calculates the train's position on this link.
If the travelled distance is 0 or less, the function returns the coordinates of the first node on the path. If the travelled distance is equal to or greater than the total path length, it returns the coordinates of the last node. Otherwise, the function determines on which link the train is located and calculates the precise position.
train | A shared_ptr to the Train object for which to compute the position. This object contains the train's path, the total path length, and the cumulative lengths of the individual links. |
travelledDistance | The distance the train has travelled along its path. Must be non-negative and not exceed the total path length. |
|
inline |
Retrieves the previous node in the train's path given a travelled distance.
This function iterates over the train's path until it finds the node whose cumulative distance is greater than the travelled distance. The function then retrieves the node prior to this one.
train | The train object. |
travelledDistance | The distance that has been travelled. |
previousNodeID | An input/output parameter that will store the ID of the previous node. |
|
inline |
Fetches the network signals associated with the list of given nodes.
Iterates over each node and checks the node's signals. If a signal's current node matches the current iterating node, the signal is added to a list that is returned after the completion of the function.
nodeList | Reference to a vector of shared pointers to NetNode objects. |
|
inline |
Translates a user-provided node identifier to a simulator-specific node identifier.
This function iterates over the network nodes until it finds a node with a user ID that matches the provided oldID. The function then returns the simulator-specific ID of this node.
oldID | The user-provided identifier. |
std::runtime_error | If a node with the provided user ID cannot be found. |
Gets simulator train path.
userDefinedTrainPath | Full path list of the user defined train file. |
|
inline |
Checks if there's a conflict zone between two nodes for the given train.
A conflict zone exists when there's only one link between nodes.
train | A shared_ptr to a Train object. |
node1 | A shared_ptr to the first NetNode object. |
node2 | A shared_ptr to the second NetNode object. |
|
inline |
Normalize a 2D vector.
This function takes a 2D vector represented as a std::pair of doubles and normalizes it. Normalizing a vector adjusts its length to be 1, but keeps its direction the same. If the input vector has length 0, the function will return the same vector.
vec | The vector to be normalized, represented as a std::pair of doubles. The first element of the pair represents the x component of the vector, and the second element represents the y component. |
|
inline |
Performs a shortest path search between two nodes.
This function implements a Dijkstra-like shortest path algorithm. It takes the start node and target node IDs, then calculates the shortest path between them by traversing the connected nodes and minimizing the overall path length. It returns a pair, where the first element is a vector of nodes representing the shortest path, and the second element is the total length of this path.
startNodeID | The identifier for the start node. |
targetNodeID | The identifier for the target node. |
|
inline |
Checks if two lines intersect.
This function takes four points A, B, C, D representing two lines (AB and CD). It determines whether these lines intersect by checking if the two points of one line straddle the other line.
A | The first point of the first line as a pair (x,y). |
B | The second point of the first line as a pair (x,y). |
C | The first point of the second line as a pair (x,y). |
D | The second point of the second line as a pair (x,y). |
|
friend |
Stream insertion operator.
[in,out] | ostr | The ostr. |
[in,out] | stud | The stud. |
std::string Network::networkName |
Holds the name of the network.
std::map<int, std::shared_ptr<NetNode> > Network::nodes |
The nodes mapped by its simulator id.