Skip to content

Commit de0fac3

Browse files
Merge branch 'main' into detect-implausible-switches
2 parents 6671580 + 1ab283f commit de0fac3

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

test/node_test.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,74 @@ def create_node(x, y):
1111

1212

1313
def coords_str(node: Node):
14-
return f'({node.geo_node.geo_point.x}, {node.geo_node.geo_point.y})'
14+
return f"({node.geo_node.geo_point.x}, {node.geo_node.geo_point.y})"
1515

1616

1717
def test_anschluss():
1818
base_scenarios = [
1919
# Base scenarios are defined in "grids", so look at them like
2020
# looking at a layout (h=head, s=switch, l=left, r=right).
21-
22-
( # north to south
21+
( # north to south
2322
(" h "),
2423
(" s "),
2524
(" "),
2625
("r l"),
2726
),
28-
( # northeast to southwest:
27+
( # northeast to southwest:
2928
(" h"),
3029
(" s "),
3130
(" "),
3231
("rl "),
3332
),
34-
( # east to west:
33+
( # east to west:
3534
("r "),
3635
(" sh"),
3736
("l "),
3837
),
39-
( # southeast to northwest:
38+
( # southeast to northwest:
4039
("lr "),
4140
(" "),
4241
(" s "),
4342
(" h"),
4443
),
45-
( # south to north:
44+
( # south to north:
4645
("l r"),
4746
(" "),
4847
(" s "),
4948
(" h "),
5049
),
51-
( # southwest to northeast:
50+
( # southwest to northeast:
5251
(" lr"),
5352
(" "),
5453
(" s "),
5554
("h "),
5655
),
57-
( # west to east:
56+
( # west to east:
5857
(" l"),
5958
("hs "),
6059
(" r"),
6160
),
62-
( # northwest to southeast:
61+
( # northwest to southeast:
6362
("h "),
6463
(" s "),
6564
(" "),
6665
(" rl"),
6766
),
68-
( # curved north to south:
67+
( # curved north to south:
6968
("h "),
7069
("s "),
7170
(" "),
7271
(" rl"),
7372
),
74-
( # curved east to west:
73+
( # curved east to west:
7574
("r "),
7675
("l "),
7776
(" sh"),
7877
),
79-
( # branch east to west
78+
( # branch east to west
8079
("r "),
8180
("lsh"),
8281
),
83-
8482
]
8583

8684
# base scenarios will be "moved around" by some offsets:
@@ -114,10 +112,14 @@ def test_anschluss():
114112

115113
print(
116114
"scenario:",
117-
"\n head ", coords_str(head),
118-
"\n switch", coords_str(switch),
119-
"\n left ", coords_str(left),
120-
"\n right ", coords_str(right)
115+
"\n head ",
116+
coords_str(head),
117+
"\n switch",
118+
coords_str(switch),
119+
"\n left ",
120+
coords_str(left),
121+
"\n right ",
122+
coords_str(right),
121123
)
122124

123125
# connect all nodes to the switch
@@ -127,20 +129,23 @@ def test_anschluss():
127129
switch.calc_anschluss_of_all_nodes()
128130

129131
# assert ``calc_anschluss_of_all_nodes`` did what it should
130-
assert switch.connected_on_head == head, 'head node ' \
131-
f'{coords_str(switch.connected_on_head)} incorrect'
132-
assert switch.connected_on_left == left, 'left node ' \
133-
f'{coords_str(switch.connected_on_left)} incorrect'
134-
assert switch.connected_on_right == right, 'right node ' \
135-
f'{coords_str(switch.connected_on_right)} incorrect'
132+
assert switch.connected_on_head == head, (
133+
"head node " f"{coords_str(switch.connected_on_head)} incorrect"
134+
)
135+
assert switch.connected_on_left == left, (
136+
"left node " f"{coords_str(switch.connected_on_left)} incorrect"
137+
)
138+
assert switch.connected_on_right == right, (
139+
"right node " f"{coords_str(switch.connected_on_right)} incorrect"
140+
)
136141

137142

138143
def test_implausible_anschluss():
139144
"""Assert that detection of "Anschluss" (head, left, right) raises
140145
an exception on really implausible geographies."""
141-
head = create_node(0, 0) # layout:
142-
switch = create_node(2, 2) # l
143-
left = create_node(1, 3) # s r
146+
head = create_node(0, 0) # layout:
147+
switch = create_node(2, 2) # l
148+
left = create_node(1, 3) # s r
144149
right = create_node(3, 2) # h
145150

146151
switch.connected_nodes.extend((head, left, right))

yaramo/node.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import sys
12
from enum import Enum
23
from itertools import permutations
3-
from math import sin, cos, atan2
4-
import sys
4+
from math import atan2, cos, sin
55

66
from yaramo.base_element import BaseElement
77
from yaramo.geo_node import GeoNode
@@ -109,8 +109,7 @@ def calc_anschluss_of_all_nodes(self):
109109

110110
almost_zero = sys.float_info.epsilon
111111

112-
def get_rad_between_nodes(node_a: GeoPoint,
113-
node_b: GeoPoint) -> float:
112+
def get_rad_between_nodes(node_a: GeoPoint, node_b: GeoPoint) -> float:
114113
"""
115114
Returns the angle of an (maybe imaginary) line between
116115
:param:`node_a` and :param:`node_b`.
@@ -132,7 +131,7 @@ def get_rad_between_nodes(node_a: GeoPoint,
132131
continue
133132

134133
right_angle_abs = get_rad_between_nodes(self, right)
135-
right_angle_rel = right_angle_abs- head_angle_abs
134+
right_angle_rel = right_angle_abs - head_angle_abs
136135
if cos(right_angle_rel) <= almost_zero:
137136
# right turn more than (or almost) 90°
138137
continue

0 commit comments

Comments
 (0)