edges.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Sat, 05 Mar 2011 23:06:27 -0800
changeset 6 6ce97bd3e70f
parent 5 b7757ccad2f4
child 7 cbb51ef4dd6d
permissions -rw-r--r--
Added color icons

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)
        glCallList(self.display_list)

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

    def __iter__(self):
        return self.edges