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

Refactor: Replace connected nodes with edges #42

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
39 changes: 39 additions & 0 deletions yaramo/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@ def get_signals_with_direction_in_order(self, direction: "SignalDirection") -> L
result.sort(key=lambda x: x.distance_edge, reverse=(direction == SignalDirection.GEGEN))
return result

def get_opposite_node(self, node: "Node") -> "Node":
"""Returns the opposite Node of the given Node

Parameters
----------
node : Node
The Node to get the opposite Node of

Returns
-------
Node
The opposite Node
"""

if self.node_a.uuid == node.uuid:
return self.node_b
return self.node_a

def get_next_geo_node(self, node: "Node") -> "GeoNode":
"""Returns the next GeoNode on Edgeof the given Top Node

Parameters
----------
geo_node : Node
The Top Node to get the next GeoNode of

Returns
-------
GeoNode
The next GeoNode
"""

if self.node_a.uuid == node.uuid:
return self.intermediate_geo_nodes[1]
if self.node_b.uuid == node.uuid:
return self.intermediate_geo_nodes[-2]
else:
return None

def to_serializable(self):
"""See the description in the BaseElement class.

Expand Down
83 changes: 61 additions & 22 deletions yaramo/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def __init__(self, turnout_side=None, **kwargs):
"""

super().__init__(**kwargs)
self.connected_on_head = None
self.connected_on_left = None
self.connected_on_right = None
self.connected_edge_on_head = None
self.connected_edge_on_right = None
self.connected_edge_on_left = None
self.maximum_speed_on_left = None
self.maximum_speed_on_right = None
self.connected_nodes: list["Node"] = []
self.connected_edges: list["Edge"] = []
self.geo_node: GeoNode = None
self.turnout_side: str = turnout_side

Expand All @@ -52,17 +52,43 @@ def maximum_speed(self, node_a: "Node", node_b: "Node"):
else:
return None

def set_connection_head(self, node: "Node"):
self.connected_on_head = node
self.connected_nodes.append(node)
@property
def connected_nodes(self):
return [edge.get_opposite_node(self) for edge in self.connected_edges]

def set_connection_left(self, node: "Node"):
self.connected_on_left = node
self.connected_nodes.append(node)
@property
def connected_on_head(self):
return self.connected_edge_on_head.get_opposite_node(self)

def set_connection_right(self, node: "Node"):
self.connected_on_right = node
self.connected_nodes.append(node)
@property
def connected_on_left(self):
return self.connected_edge_on_left.get_opposite_node(self)

@property
def connected_on_right(self):
return self.connected_edge_on_right.get_opposite_node(self)

def set_connection_head_edge(self, edge: "Edge"):
self.connected_edge_on_head = edge
self.connected_edges.append(edge)

def set_connection_left_edge(self, edge: "Edge"):

self.connected_edge_on_left = edge
self.connected_edges.append(edge)

def set_connection_right_edge(self, edge: "Edge"):
self.connected_edge_on_right = edge
self.connected_edges.append(edge)

def remove_edge_to_node(self, node: "Node"):
"""Removes the edge to the given node and removes the node from the connected_nodes list."""
edge = self.get_edge_to_node(node)
self.connected_edges.remove(edge)

def get_edge_to_node(self, node):
"""Returns the edge to the given neighbor node."""
self.connected_edges.filter(lambda edge: edge.get_opposite_node(self).uuid == node.uuid)

def get_possible_followers(self, source):
"""Returns the Nodes that could follow (head, left, right) when comming from a source Node connected to this Node."""
Expand Down Expand Up @@ -125,7 +151,9 @@ def is_above_line_between_points(head_point: GeoPoint, branching_point: GeoPoint
)
if cur_arc < current_max_arc:
missing_index = sum(range(len(self.connected_nodes))) - (i + j)
self.connected_on_head = self.connected_nodes[missing_index]
self.connected_edge_on_head = self.get_edge_to_node(
self.connected_nodes[missing_index]
)
other_a = self.connected_nodes[i]
other_b = self.connected_nodes[j]
current_max_arc = cur_arc
Expand All @@ -135,21 +163,32 @@ def is_above_line_between_points(head_point: GeoPoint, branching_point: GeoPoint
side_b = is_above_line_between_points(self.connected_on_head.geo_node.geo_point, self.geo_node.geo_point, other_b.geo_node.geo_point)

# If they're on two separate sides we know which is left and right
if(side_a != side_b):
if (side_a):
self.connected_on_left, self.connected_on_right = other_a, other_b
if side_a != side_b:
if side_a:
self.connected_edge_on_left = self.get_edge_to_node(other_a)
self.connected_edge_on_right = self.get_edge_to_node(other_b)
else:
self.connected_on_right, self.connected_on_left = other_a, other_b
self.connected_edge_on_left = self.get_edge_to_node(other_b)
self.connected_edge_on_right = self.get_edge_to_node(other_a)
# If they're both above or below that line, we make the node that branches further away the left or right node,
# depending on the side they're on (left if both above)
else:
arc_a = get_arc_between_nodes(self.connected_on_head, other_a)
arc_b = get_arc_between_nodes(self.connected_on_head, other_b)
if(arc_a > arc_b):
self.connected_on_right, self.connected_on_left = (other_a, other_b) if side_a else (other_b, other_a)
if arc_a > arc_b:
self.connected_edge_on_left = (
self.get_edge_to_node(other_a) if side_a else self.get_edge_to_node(other_b)
)
self.connected_edge_on_right = (
self.get_edge_to_node(other_b) if side_a else self.get_edge_to_node(other_a)
)
antonykamp marked this conversation as resolved.
Show resolved Hide resolved
else:
self.connected_on_left, self.connected_on_right = (other_a, other_b) if side_a else (other_b, other_a)

self.connected_edge_on_left = (
self.get_edge_to_node(other_a) if side_a else self.get_edge_to_node(other_b)
)
self.connected_edge_on_right = (
self.get_edge_to_node(other_b) if side_a else self.get_edge_to_node(other_a)
)

def to_serializable(self):
"""See the description in the BaseElement class.
Expand Down