Skip to content

Commit

Permalink
fix: use neighbor for angle calculation
Browse files Browse the repository at this point in the history
fixes Bug: Wrong calculation of "Anschluss" if connected edges are long and not straight simulate-digital-rail#41
  • Loading branch information
antonykamp committed Apr 18, 2023
1 parent 35afe3b commit 51f8a77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions yaramo/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ def get_next_geo_node(self, node: "Node") -> "GeoNode":
GeoNode
The next GeoNode
"""

if len(self.intermediate_geo_nodes) < 2:
return self.get_opposite_node(node).geo_node
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
return None

def to_serializable(self):
"""See the description in the BaseElement class.
Expand Down
23 changes: 18 additions & 5 deletions yaramo/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ def calc_anschluss_of_all_nodes(self):
"""Calculates and sets the 'Anschluss' or connection side of the connected_nodes based on their geo-location."""

def get_arc_between_nodes(_node_a: "Node", _node_b: "Node"):
_a = _node_a.geo_node.get_distance_to_other_geo_node(self.geo_node)
_b = self.geo_node.get_distance_to_other_geo_node(_node_b.geo_node)
_c = _node_a.geo_node.get_distance_to_other_geo_node(_node_b.geo_node)
_neighbor_to_a = self.get_edge_to_node(_node_a).get_next_geo_node(self)
_neighbor_to_b = self.get_edge_to_node(_node_b).get_next_geo_node(self)
_a = _neighbor_to_a.get_distance_to_other_geo_node(self.geo_node)
_b = self.geo_node.get_distance_to_other_geo_node(_neighbor_to_b)
_c = _neighbor_to_a.get_distance_to_other_geo_node(_neighbor_to_b)

return math.degrees(math.acos((_a * _a + _b * _b - _c * _c) / (2.0 * _a * _b)))

Expand All @@ -164,9 +166,20 @@ def is_above_line_between_points(head_point: GeoPoint, branching_point: GeoPoint
other_b = self.connected_nodes[j]
current_max_arc = cur_arc

_neighbor_to_head = self.connected_edge_on_head.get_next_geo_node(self)
_neighbor_to_a = self.get_edge_to_node(other_a).get_next_geo_node(self)
_neighbor_to_b = self.get_edge_to_node(other_b).get_next_geo_node(self)
# Check on which side of the line between the head connection and this node the other nodes are
side_a = is_above_line_between_points(self.connected_on_head.geo_node.geo_point, self.geo_node.geo_point, other_a.geo_node.geo_point)
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)
side_a = is_above_line_between_points(
_neighbor_to_head.geo_point,
self.geo_node.geo_point,
_neighbor_to_a.geo_point,
)
side_b = is_above_line_between_points(
_neighbor_to_head.geo_point,
self.geo_node.geo_point,
_neighbor_to_b.geo_node.geo_point,
)

# If they're on two separate sides we know which is left and right
if side_a != side_b:
Expand Down

0 comments on commit 51f8a77

Please sign in to comment.