Added triangles
authorDmitriy Morozov <dmitriy@mrzv.org>
Sat, 05 Mar 2011 22:52:03 -0800
changeset 5 b7757ccad2f4
parent 4 d52927c0bf92
child 6 6ce97bd3e70f
Added triangles
PyVEFViewer.py
edges.py
points.py
triangles.py
--- a/PyVEFViewer.py	Sat Mar 05 15:58:56 2011 -0800
+++ b/PyVEFViewer.py	Sat Mar 05 22:52:03 2011 -0800
@@ -4,6 +4,7 @@
 
 from    points          import Points, centerMinMax, reduceCMM
 from    edges           import Edges
+from    triangles       import Triangles
 
 
 class VEFViewer(QGLViewer):
@@ -39,6 +40,7 @@
         item = self.model_list.itemAt(position)
         if item:
             item.contextMenu(self.model_list.mapToGlobal(position))
+        self.updateGL()
 
     def modelDoubleClicked(self, item):
         item.toggleVisible()
@@ -73,7 +75,8 @@
             self.model_list.addItem(Edges(edg, self.model_list))
 
     def read_triangles(self, triangles):
-        pass
+        for tri in triangles:
+            self.model_list.addItem(Triangles(tri, self.model_list))
 
     def set_list(self, model_list):
         self.model_list = model_list
--- a/edges.py	Sat Mar 05 15:58:56 2011 -0800
+++ b/edges.py	Sat Mar 05 22:52:03 2011 -0800
@@ -42,9 +42,7 @@
         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:
--- a/points.py	Sat Mar 05 15:58:56 2011 -0800
+++ b/points.py	Sat Mar 05 22:52:03 2011 -0800
@@ -63,9 +63,7 @@
         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 __iter__(self):
         return self.points
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/triangles.py	Sat Mar 05 22:52:03 2011 -0800
@@ -0,0 +1,91 @@
+from        OpenGL.GL   import glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
+                               glBegin, glEnd, GL_LINES, glVertex3f, glColor3f, \
+                               glEnable, glDisable, GL_LIGHTING, GL_TRIANGLES
+
+from        points      import Point, centerMinMax
+from        ViewerItem  import ViewerItem
+from        os.path     import basename
+from        itertools   import izip
+
+class Triangles(ViewerItem):
+    def __init__(self, filename, parent = None):
+        super(Triangles, self).__init__(basename(filename), parent, color = (200,0,200))
+
+        self.triangles = []
+        lines = []
+        with open(filename) as f:
+            for line in f:
+                if not line.strip() or line.startswith('#'): continue
+                lines.append(line)
+
+            for i in xrange(0,len(lines),7):
+                u = Point(map(float, lines[i+2].split()[1:4]))
+                v = Point(map(float, lines[i+3].split()[1:4]))
+                w = Point(map(float, lines[i+4].split()[1:4]))
+                self.triangles.append((u,v,w))
+
+        self.create_display_lists()
+        self.center, self.min, self.max = centerMinMax(self.vertices())
+        self.wireframe = False
+
+    def createMenu(self):
+        menu = super(Triangles, self).createMenu()
+        wireframeAction = menu.addAction("Wireframe")
+        wireframeAction.command = lambda: self.wireframeCommand()
+        return menu
+
+    def wireframeCommand(self):
+        self.wireframe = not self.wireframe
+
+    def create_display_lists(self):
+        self.triangles_display_list = glGenLists(1)
+        glNewList(self.triangles_display_list, GL_COMPILE)
+        self.draw_triangles()
+        glEndList()
+        self.wireframe_display_list = glGenLists(1)
+        glNewList(self.wireframe_display_list, GL_COMPILE)
+        self.draw_wireframe()
+        glEndList()
+
+    def draw_triangles(self):
+        glBegin(GL_TRIANGLES)
+        for (u,v,w) in self.triangles:
+            # front
+            glVertex3f(u.x, u.y, u.z)
+            glVertex3f(v.x, v.y, v.z)
+            glVertex3f(w.x, w.y, w.z)
+            # back
+            glVertex3f(u.x, u.y, u.z)
+            glVertex3f(w.x, w.y, w.z)
+            glVertex3f(v.x, v.y, v.z)
+        glEnd()
+
+    def draw_wireframe(self):
+        glBegin(GL_LINES)
+        for (u,v,w) in self.triangles:
+            glVertex3f(u.x, u.y, u.z)
+            glVertex3f(v.x, v.y, v.z)
+            glVertex3f(v.x, v.y, v.z)
+            glVertex3f(w.x, w.y, w.z)
+            glVertex3f(w.x, w.y, w.z)
+            glVertex3f(u.x, u.y, u.z)
+        glEnd()
+
+    def draw(self):
+        if not self.visible: return
+        r,g,b,a = self.color.getRgb()
+        glColor3f(r,g,b)
+        if self.wireframe:
+            glCallList(self.wireframe_display_list)
+        else:
+            glCallList(self.triangles_display_list)
+            
+
+    def vertices(self):
+        for (u,v,w) in self.triangles:
+            yield u
+            yield v
+            yield w
+
+    def __iter__(self):
+        return self.triangles