summaryrefslogtreecommitdiff
path: root/lib/graph.py
diff options
context:
space:
mode:
authormdroe <mdroe@stsci.edu>2012-06-07 12:01:08 -0400
committermdroe <mdroe@stsci.edu>2012-06-07 12:01:08 -0400
commit40e93ce69ab8b6181362981250cde7de6f4033ea (patch)
treeefd32029b415dab38cebecb86734775d0cbb8ccf /lib/graph.py
parent4648aef9a8f5ac40d43b0c6618d65b72c11d2a2c (diff)
downloadstsci.sphere-40e93ce69ab8b6181362981250cde7de6f4033ea.tar.gz
Avoid adding duplicated nodes and edges when building the graph. These can look like cut lines and get removed in weird ways, so it is better to just not create duplicates in the first place.
git-svn-id: http://svn.stsci.edu/svn/ssb/stsci_python/stsci_python/branches/sphere@17219 fe389314-cf27-0410-b35b-8c050e845b92 Former-commit-id: 51d0e8176c7eecdea00e478ef7efaa77c08aab70
Diffstat (limited to 'lib/graph.py')
-rw-r--r--lib/graph.py48
1 files changed, 42 insertions, 6 deletions
diff --git a/lib/graph.py b/lib/graph.py
index 58b918d..d64074a 100644
--- a/lib/graph.py
+++ b/lib/graph.py
@@ -156,6 +156,26 @@ class Graph:
except IndexError:
raise ValueError("Following from disconnected node")
+ def equals(self, other):
+ """
+ Returns True if the other edge is between the same two nodes.
+
+ Parameters
+ ----------
+ other : `Edge` instance
+
+ Returns
+ -------
+ equals : bool
+ """
+ if (self._nodes[0].equals(other._nodes[0]) and
+ self._nodes[1].equals(other._nodes[1])):
+ return True
+ if (self._nodes[1].equals(other._nodes[0]) and
+ self._nodes[0].equals(other._nodes[1])):
+ return True
+ return False
+
def __init__(self, polygons):
"""
@@ -234,9 +254,17 @@ class Graph:
node : `Node` instance
The new node
"""
- node = self.Node(point, source_polygons)
- self._nodes.add(node)
- return node
+ new_node = self.Node(point, source_polygons)
+
+ # Don't add nodes that already exist. Update the existing
+ # node's source_polygons list to include the new polygon.
+ for node in self._nodes:
+ if node.equals(new_node):
+ node._source_polygons.update(source_polygons)
+ return node
+
+ self._nodes.add(new_node)
+ return new_node
def remove_node(self, node):
"""
@@ -279,9 +307,17 @@ class Graph:
assert A in self._nodes
assert B in self._nodes
- edge = self.Edge(A, B, source_polygons)
- self._edges.add(edge)
- return edge
+ new_edge = self.Edge(A, B, source_polygons)
+
+ # Don't add any edges that already exist. Update the edge's
+ # source polygons list to include the new polygon.
+ for edge in self._edges:
+ if edge.equals(new_edge):
+ edge._source_polygons.update(source_polygons)
+ return edge
+
+ self._edges.add(new_edge)
+ return new_edge
def remove_edge(self, edge):
"""