Skip to content

Commit bce4e82

Browse files
Split: Fix labeling of element with two-color-problem in focus
1 parent a564ff2 commit bce4e82

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

yaramo/operations/split.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _get_new_geo_node_same_type(_old_geo_node: GeoNode, x: float, y: float) -> G
188188
def _label_elements(
189189
topology: Topology,
190190
new_end_nodes: Dict[Edge, Tuple[Node, Node]],
191-
node_label_assignments: Node | Dict[Node, Label],
191+
node_label_assignments: Dict[Node, Label],
192192
) -> Tuple[Dict[Node, Label], Dict[Edge, Label], Dict[Signal, Label]]:
193193
node_labels: Dict[Node, Label] = {}
194194
edge_labels: Dict[Edge, Label] = {}
@@ -218,19 +218,52 @@ def _dfs(_start_node: Node, _label):
218218
for node, label in node_label_assignments.items():
219219
_dfs(node, label)
220220

221-
for end_node_pair in new_end_nodes.values():
222-
node_a = end_node_pair[0]
223-
node_b = end_node_pair[1]
221+
def _get_any_unlabeled_end_node_pair():
222+
for pair in new_end_nodes.values():
223+
if pair[0] not in node_labels and pair[1] not in node_labels:
224+
return pair
225+
return None
226+
227+
if not node_labels:
228+
# Nothing labeled so far, start with any pair
229+
if not new_end_nodes:
230+
raise ValueError("No end nodes but also nothing labeled. Split not possible")
231+
any_end_node_pair = _get_any_unlabeled_end_node_pair()
232+
_dfs(any_end_node_pair[0], Label.A_Topology)
233+
_dfs(any_end_node_pair[1], Label.B_Topology)
234+
235+
def _get_next_end_node_pair():
236+
for pair in new_end_nodes.values():
237+
_node_a = pair[0]
238+
_node_b = pair[1]
239+
if (_node_a in node_labels and _node_b not in node_labels) or (_node_a not in node_labels and _node_b in node_labels):
240+
return pair
241+
return _get_any_unlabeled_end_node_pair()
242+
243+
next_pair = _get_next_end_node_pair()
244+
while next_pair is not None:
245+
node_a = next_pair[0]
246+
node_b = next_pair[1]
224247
if node_a not in node_labels and node_b not in node_labels:
225248
_dfs(node_a, Label.A_Topology)
226249
_dfs(node_b, Label.B_Topology)
227250
elif node_a in node_labels and node_b in node_labels:
228251
if node_labels[node_a] == node_labels[node_b]:
229-
raise ValueError("Split edges do not fully split. Split not possible.")
252+
raise ValueError("Split edges do not fully split or is not colorable with two colors. Split not possible.")
230253
elif node_a in node_labels:
231254
_dfs(node_b, Label.get_opposite_label(node_labels[node_a]))
232255
elif node_b in node_labels:
233256
_dfs(node_a, Label.get_opposite_label(node_labels[node_b]))
257+
next_pair = _get_next_end_node_pair()
258+
259+
# Verify all end node pairs have different labels
260+
for pair in new_end_nodes.values():
261+
node_a = pair[0]
262+
node_b = pair[1]
263+
if node_labels[node_a] == node_labels[node_b]:
264+
raise ValueError(
265+
"Split edges do not fully split or is not colorable with two colors. Split not possible."
266+
)
234267

235268
return node_labels, edge_labels, signal_labels
236269

0 commit comments

Comments
 (0)