edges.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Tue, 21 Jun 2011 16:24:27 -0700
changeset 17 9f4bf1795d51
parent 16 69f6b439b161
child 18 7d5e8fdf69f2
permissions -rw-r--r--
Transposed y and z in povray

from        OpenGL.GL   import glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
                               glBegin, glEnd, GL_LINES, glVertex3f, glColor3f, \
                               glEnable, glDisable, GL_LIGHTING
from        OpenGL.GLE  import glePolyCylinder
from        OpenGL.GLE  import *

from        points      import Point, centerMinMax
from        ViewerItem  import ViewerItem
from        os.path     import basename
from        itertools   import izip
from        io          import line_blocks
import      povray

class Edges(ViewerItem):
    def __init__(self, filename, parent = None):
        super(Edges, self).__init__(basename(filename), parent, color = (0,0,255))

        self.read_edges(filename)

        self.create_display_list()
        self.center, self.min, self.max = centerMinMax(self.vertices())
        self.pipes = False

    def createMenu(self):
        menu = super(Edges, self).createMenu()
        pipesAction = menu.addAction("Pipes")
        pipesAction.setCheckable(True)
        pipesAction.setChecked(self.pipes)
        pipesAction.command = lambda: self.pipesCommand()
        return menu

    def pipesCommand(self):
        self.pipes = not self.pipes

    def create_display_list(self):
        self.display_list = glGenLists(1)
        glNewList(self.display_list, GL_COMPILE)
        self.draw_edges()
        glEndList()
        self.display_list_pipes = glGenLists(1)
        glNewList(self.display_list_pipes, GL_COMPILE)
        self.draw_pipes()
        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_pipes(self):
        r,g,b,a = self.color.getRgb()
        glColor3f(r,g,b)
        for (u,v) in self.edges:
            glePolyCylinder([(u.x, u.y, u.z),
                             (u.x, u.y, u.z),
                             (v.x,v.y,v.z),
                             (v.x,v.y,v.z),],
                            None,
                            .5)

    def draw(self):
        if not self.visible: return
        r,g,b,a = self.color.getRgb()
        glColor3f(r,g,b)
        if not self.pipes:
            glCallList(self.display_list)
        else:
            glCallList(self.display_list_pipes)

    def vertices(self):
        for (u,v) in self.edges:
            yield u
            yield v

    def __iter__(self):
        return self.edges

    def save_povray(self, f):
        cr,cg,cb,ca = self.color.getRgb()
        cr,cg,cb = cr/255., cg/255., cb/255.
        for (u,v) in self.edges:
            f.write(povray.cylinder(u,v,.5,(cr,cg,cb)))
            f.write(povray.sphere(u,.5,(cr,cg,cb)))
            f.write(povray.sphere(v,.5,(cr,cg,cb)))
            f.write('\n')

    def read_edges(self, filename):
        self.edges = []
        for block in line_blocks(filename, 2):
            u = Point(map(float, block[0].split()))
            v = Point(map(float, block[1].split()))
            self.edges.append((u,v))