Set center and radius in ComplexViewer3D dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 07 Jun 2012 21:36:53 -0700
branchdev
changeset 265 66a311c0d597
parent 264 55700750a464
child 266 38cf887cb139
Set center and radius in ComplexViewer3D
bindings/python/dionysus/viewer/complex3d.py
--- a/bindings/python/dionysus/viewer/complex3d.py	Thu Jun 07 21:21:11 2012 -0700
+++ b/bindings/python/dionysus/viewer/complex3d.py	Thu Jun 07 21:36:53 2012 -0700
@@ -2,6 +2,7 @@
 from    PyGLWidget  import PyGLWidget
 from    OpenGL.GL   import *
 from    dionysus    import Simplex
+from    math        import sqrt
 
 class ComplexViewer3D(PyGLWidget):
     def __init__(self, points, complex = None, values = None):
@@ -19,6 +20,22 @@
             self.values = [0]*len(self.points)
         self.maxval, self.minval = max(self.values), min(self.values)
 
+        center, radius = self.center_radius()
+        self.set_radius(radius)
+        self.set_center(center)
+
+    def center_radius(self):
+        c = [0,0,0]
+        for p in self.points:
+            for i in xrange(3): c[i] += p[i]
+        for i in xrange(3): c[i] /= len(self.points)
+
+        r = 0
+        for p in self.points:
+            d = sqrt((p[0] - c[0])**2 + (p[1] - c[1])**2 + (p[2] - c[2])**2)
+            if d > r: r = d
+        return c,r
+
     def paintGL(self):
         PyGLWidget.paintGL(self)
         self.complex.sort(lambda s1, s2: -cmp(s1.dimension(), s2.dimension()))