Permalink
Browse files

DiagramGrid: Distinguish between welding failures and restarts.

The loop in the constructor DiagramGrid now can distinguish between the
situations when _weld_triangle has not found any welding and when it has
corrected the fringe and requires a restart of the process.  This makes
the test added in the previous commit pass.
  • Loading branch information...
1 parent 2be0f5c commit 8d3e1937b6e0df33b3665f09615ad8d2f72b7d57 @scolobb committed Jun 28, 2012
Showing with 18 additions and 6 deletions.
  1. +18 −6 sympy/categories/diagram_drawing.py
@@ -367,11 +367,18 @@ def _choose_target_cell(pt1, pt2, edge, obj, skeleton, grid):
else:
return None
+ # The possible return values of ``_weld_triangle``.
+ _WELDING_FAILURE = 1
+ _RESTART = 2
+
@staticmethod
def _weld_triangle(triangles, fringe, grid, skeleton):
"""
If possible, welds a triangle to the fringe and returns the
- welded triangle. Otherwise returns None.
+ welded triangle. Otherwise returns ``_WELDING_FAILURE``. If
+ this method encounters a degenerate situation and corrects the
+ fringe such that a restart of the search is required, it
+ returns ``_RESTART``.
"""
for tri in triangles:
welding_edge = DiagramGrid._find_triangle_welding(
@@ -401,7 +408,7 @@ def _weld_triangle(triangles, fringe, grid, skeleton):
# on the actual fringe. Correct the
# fringe and go on.
fringe.remove((a, b))
- break
+ return DiagramGrid._RESTART
elif a[0] == b[0]:
# A horizontal edge. Suppose a triangle can be
# built in the downward direction.
@@ -424,7 +431,7 @@ def _weld_triangle(triangles, fringe, grid, skeleton):
# This edge is not in the fringe, remove it
# and restart.
fringe.remove((a, b))
- break
+ return DiagramGrid._RESTART
elif a[1] == b[1]:
# A vertical edge. Suppose a triangle can be built to
@@ -447,7 +454,7 @@ def _weld_triangle(triangles, fringe, grid, skeleton):
# This edge is not in the fringe, remove it
# and restart.
fringe.remove((a, b))
- break
+ return DiagramGrid._RESTART
# We now know where to place the other vertex of the
# triangle.
@@ -466,7 +473,7 @@ def _weld_triangle(triangles, fringe, grid, skeleton):
return tri
- return None
+ return DiagramGrid._WELDING_FAILURE
@staticmethod
def _triangle_key(tri, triangle_sizes):
@@ -596,7 +603,12 @@ def __init__(self, diagram):
triangle = DiagramGrid._weld_triangle(
triangles, fringe, grid, skeleton)
- if not triangle:
+ if triangle == DiagramGrid._RESTART:
+ # ``_weld_triangle`` wants to have the search for
+ # welding restarted.
+ continue
+
+ if triangle == DiagramGrid._WELDING_FAILURE:
# No more weldings found. Try to attach triangles by
# vertices.
new_obj = DiagramGrid._grow_pseudopod(

0 comments on commit 8d3e193

Please sign in to comment.