Added viewer.show_complex_2D + center points in the persistence diagram dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Tue, 05 Jun 2012 17:53:08 -0700
branchdev
changeset 258 bb5bc5eff779
parent 257 d69a9e11205e
child 259 84c100980206
Added viewer.show_complex_2D + center points in the persistence diagram
bindings/python/dionysus/__init__.py
bindings/python/dionysus/viewer/__init__.py
bindings/python/dionysus/viewer/complex.py
bindings/python/dionysus/viewer/diagram.py
--- a/bindings/python/dionysus/__init__.py	Tue Jun 05 17:04:10 2012 -0700
+++ b/bindings/python/dionysus/__init__.py	Tue Jun 05 17:53:08 2012 -0700
@@ -3,7 +3,6 @@
 from    zigzag      import *
 from    adaptor     import *
 import  circular
-import  viewer
 
 def init_with_none(self, iter, data = None):        # convenience: data defaults to None
     self._cpp_init_(iter, data)
--- a/bindings/python/dionysus/viewer/__init__.py	Tue Jun 05 17:04:10 2012 -0700
+++ b/bindings/python/dionysus/viewer/__init__.py	Tue Jun 05 17:53:08 2012 -0700
@@ -1,1 +1,2 @@
 from diagram import *
+from complex import *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bindings/python/dionysus/viewer/complex.py	Tue Jun 05 17:53:08 2012 -0700
@@ -0,0 +1,57 @@
+from    PyQt4       import QtGui, QtCore
+
+class ComplexViewer(QtGui.QGraphicsView):
+    def __init__(self, complex, points):
+        super(QtGui.QGraphicsView, self).__init__()
+
+        self.complex = [s for s in complex]
+        self.points = points
+
+        self.setRenderHint(QtGui.QPainter.Antialiasing)
+        self.scene = QtGui.QGraphicsScene(self)
+        self.setScene(self.scene)
+
+        minx = min(p[0] for p in points)
+        miny = min(p[1] for p in points)
+        maxx = max(p[0] for p in points)
+        maxy = max(p[1] for p in points)
+
+        radius = min(maxx - minx, maxy - miny)/100
+        self.scene.setSceneRect(minx - 10*radius, miny - 10*radius, (maxx - minx) + 20*radius, (maxy - miny) + 20*radius)
+
+        self.complex.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension()))
+        for s in self.complex:
+            vertices = [v for v in s.vertices]
+            if s.dimension() == 0:
+                p = points[vertices[0]]
+                item = QtGui.QGraphicsEllipseItem(p[0] - radius/2,p[1] - radius/2,radius,radius)
+                # TODO: vertex colors
+                color = QtCore.Qt.red
+                item.setBrush(QtGui.QBrush(color))
+                item.setPen(QtGui.QPen(color))
+            elif s.dimension() == 1:
+                p0 = points[vertices[0]]
+                p1 = points[vertices[1]]
+                item = QtGui.QGraphicsLineItem(p0[0], p0[1], p1[0], p1[1])
+            else:
+                pts = [QtCore.QPointF(points[v][0], points[v][1]) for v in vertices]
+                item = QtGui.QGraphicsPolygonItem(QtGui.QPolygonF(pts))
+                item.setBrush(QtCore.Qt.blue)
+
+            self.scene.addItem(item)
+
+        # Flip y-axis
+        self.scale(1,-1)
+
+        # Set the correct view
+        rect = self.scene.itemsBoundingRect()
+        self.fitInView(rect, QtCore.Qt.KeepAspectRatio)
+
+# TODO: cycle
+def show_complex_2D(complex, points):
+    app = QtGui.QApplication([])
+    view = ComplexViewer(complex, points)
+    view.show()
+    view.raise_()
+    app.exec_()
+
--- a/bindings/python/dionysus/viewer/diagram.py	Tue Jun 05 17:04:10 2012 -0700
+++ b/bindings/python/dionysus/viewer/diagram.py	Tue Jun 05 17:53:08 2012 -0700
@@ -2,7 +2,7 @@
 
 class DiagramPoint(QtGui.QGraphicsEllipseItem):
     def __init__(self,x,y,radius, p, viewer):
-        super(QtGui.QGraphicsEllipseItem, self).__init__(x,y,radius, radius)
+        super(QtGui.QGraphicsEllipseItem, self).__init__(x - radius/2,y - radius/2,radius, radius)
         self.p = p
         self.viewer = viewer