Added spheres.py
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 05 May 2011 08:43:19 -0700
changeset 12 2d50a82295ab
parent 11 d2b9ee185b38
child 13 a4e763e563c5
Added spheres.py
PyVEFViewer.py
ViewerItem.py
points.py
spheres.py
--- a/PyVEFViewer.py	Thu Mar 24 15:15:07 2011 -0700
+++ b/PyVEFViewer.py	Thu May 05 08:43:19 2011 -0700
@@ -6,6 +6,7 @@
 from    points          import Points, centerMinMax, reduceCMM
 from    edges           import Edges
 from    triangles       import Triangles
+from    spheres         import Spheres
 
 
 class VEFViewer(QGLViewer):
@@ -93,6 +94,10 @@
         for tri in triangles:
             self.model_list.addItem(Triangles(tri, self.model_list))
 
+    def read_spheres(self, spheres):
+        for sph in spheres:
+            self.model_list.addItem(Spheres(sph, self.model_list))
+
     def read_from_extension(self, fn):
         if   ext(fn) == '.vrt' or ext(fn) == '.pts':
             self.read_points([fn])
@@ -100,6 +105,8 @@
             self.read_edges([fn])
         elif ext(fn) == '.stl':
             self.read_triangles([fn])
+        elif ext(fn) == '.sph':
+            self.read_spheres([fn])
         else:
             return False
 
@@ -113,12 +120,12 @@
 
     def normalize_view(self):
         self.scene_parameters()
-        
+
         self.setSceneBoundingBox(Vec(self.min.x, self.min.y, self.min.z), \
                                  Vec(self.max.x, self.max.y, self.max.z))
         self.setSceneCenter(Vec(self.center.x, self.center.y, self.center.z))
-        
-    def show_entire_scene(self):        
+
+    def show_entire_scene(self):
         self.camera().showEntireScene()
 
     def scene_parameters(self):
--- a/ViewerItem.py	Thu Mar 24 15:15:07 2011 -0700
+++ b/ViewerItem.py	Thu May 05 08:43:19 2011 -0700
@@ -18,7 +18,7 @@
             self.setCheckState(QtCore.Qt.Checked)
         else:
             self.setCheckState(QtCore.Qt.Unchecked)
-        
+
     def createMenu(self):
         menu = QtGui.QMenu()
         colorAction = menu.addAction("Color")
--- a/points.py	Thu Mar 24 15:15:07 2011 -0700
+++ b/points.py	Thu May 05 08:43:19 2011 -0700
@@ -31,7 +31,7 @@
 
     def min(self, p):
         return Point((min(self.x, p.x), min(self.y, p.y), min(self.z, p.z)))
-    
+
     def max(self, p):
         return Point((max(self.x, p.x), max(self.y, p.y), max(self.z, p.z)))
 
@@ -75,7 +75,7 @@
         center += p
         min = p.min(min)
         max = p.max(max)
-    if count: center /= count        
+    if count: center /= count
 
     return center, min, max
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spheres.py	Thu May 05 08:43:19 2011 -0700
@@ -0,0 +1,53 @@
+from        OpenGL.GL   import glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
+                               glBegin, glEnd, GL_LINES, glVertex3f, glColor3f, \
+                               glEnable, glDisable, GL_LIGHTING, GL_POINTS
+
+from        points      import Point, centerMinMax
+from        ViewerItem  import ViewerItem
+from        os.path     import basename
+from        itertools   import izip
+from        io          import line_blocks
+
+class Spheres(ViewerItem):
+    def __init__(self, filename, parent = None):
+        super(Spheres, self).__init__(basename(filename), parent, color = (0,0,255))
+
+        self.read_spheres(filename)
+
+        self.create_display_list()
+        self.center, self.min, self.max = centerMinMax(self.vertices())
+
+
+    def create_display_list(self):
+        self.display_list = glGenLists(1)
+        glNewList(self.display_list, GL_COMPILE)
+        self.draw_spheres()
+        glEndList()
+
+    def draw_spheres(self):
+        # TODO
+        glBegin(GL_POINTS)
+        for (c,r) in self.spheres:
+            glVertex3f(c.x, c.y, c.z)
+        glEnd()
+
+    def draw(self):
+        if not self.visible: return
+        r,g,b,a = self.color.getRgb()
+        glColor3f(r,g,b)
+        glCallList(self.display_list)
+
+    def vertices(self):
+        for (c,r) in self.spheres:
+            yield c
+
+    def __iter__(self):
+        return self.spheres
+
+    def read_spheres(self, filename):
+        self.spheres = []
+        for block in line_blocks(filename, 1):
+            block = block[0].split()
+            c = Point(map(float, block[:3]))
+            r = float(block[3])
+            self.spheres.append((c,r))