Added edges
authorDmitriy Morozov <dmitriy@mrzv.org>
Sat, 05 Mar 2011 15:58:56 -0800
changeset 4 d52927c0bf92
parent 3 6f1239040a06
child 5 b7757ccad2f4
Added edges
PyVEFViewer.py
ViewerItem.py
edges.py
points.py
--- a/PyVEFViewer.py	Sat Mar 05 15:27:02 2011 -0800
+++ b/PyVEFViewer.py	Sat Mar 05 15:58:56 2011 -0800
@@ -3,6 +3,7 @@
 import  OpenGL.GL as ogl
 
 from    points          import Points, centerMinMax, reduceCMM
+from    edges           import Edges
 
 
 class VEFViewer(QGLViewer):
@@ -66,8 +67,11 @@
     def read_points(self, points):
         for pts in points:
             self.model_list.addItem(Points(pts, self.model_list))
+
     def read_edges(self, edges):
-        pass
+        for edg in edges:
+            self.model_list.addItem(Edges(edg, self.model_list))
+
     def read_triangles(self, triangles):
         pass
 
--- a/ViewerItem.py	Sat Mar 05 15:27:02 2011 -0800
+++ b/ViewerItem.py	Sat Mar 05 15:58:56 2011 -0800
@@ -1,10 +1,11 @@
 from    PyQt4       import QtGui, QtCore
 
 class ViewerItem(QtGui.QListWidgetItem):
-    def __init__(self, name, parent = None):
+    def __init__(self, name, parent = None, color = None):
         super(ViewerItem, self).__init__(name, parent, QtGui.QListWidgetItem.UserType)
 
-        self.color = QtGui.QColor(255, 0, 255)
+        r,g,b = color if color else (0,0,0)
+        self.color = QtGui.QColor(r,g,b)
         self.visible = True
         self.setChecked()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/edges.py	Sat Mar 05 15:58:56 2011 -0800
@@ -0,0 +1,55 @@
+from        OpenGL.GL   import glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
+                               glBegin, glEnd, GL_LINES, glVertex3f, glColor3f, \
+                               glEnable, glDisable, GL_LIGHTING
+
+from        points      import Point, centerMinMax
+from        ViewerItem  import ViewerItem
+from        os.path     import basename
+from        itertools   import izip
+
+class Edges(ViewerItem):
+    def __init__(self, filename, parent = None):
+        super(Edges, self).__init__(basename(filename), parent, color = (0,0,255))
+
+        self.edges = []
+        points = []
+        with open(filename) as f:
+            for line in f:
+                if not line.strip() or line.startswith('#'): continue
+                points.append(Point(map(float, line.split())))
+
+            for (u,v) in izip(points[::2], points[1::2]):
+                self.edges.append((u,v))
+
+        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_edges()
+        glEndList()
+
+    def draw_edges(self):
+        glBegin(GL_LINES)
+        for (u,v) in self.edges:
+            glVertex3f(u.x, u.y, u.z)
+            glVertex3f(v.x, v.y, v.z)
+        glEnd()
+
+    def draw(self):
+        if not self.visible: return
+        r,g,b,a = self.color.getRgb()
+        glColor3f(r,g,b)
+        glDisable(GL_LIGHTING)
+        glCallList(self.display_list)
+        glEnable(GL_LIGHTING)
+
+    def vertices(self):
+        for (u,v) in self.edges:
+            yield u
+            yield v
+
+    def __iter__(self):
+        return self.edges
--- a/points.py	Sat Mar 05 15:27:02 2011 -0800
+++ b/points.py	Sat Mar 05 15:58:56 2011 -0800
@@ -37,7 +37,7 @@
 
 class Points(ViewerItem):
     def __init__(self, filename, parent = None):
-        super(Points, self).__init__(basename(filename), parent)
+        super(Points, self).__init__(basename(filename), parent, (255,0,255))
 
         self.points = []
         with open(filename) as f: