spheres.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu, 05 May 2011 09:24:41 -0700
changeset 13 a4e763e563c5
parent 12 2d50a82295ab
child 14 9e82d4394fc3
permissions -rw-r--r--
Draw spheres using glutWiredSphere

from        OpenGL.GL   import glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
                               glBegin, glEnd, GL_LINES, glVertex3f, glColor3f, \
                               glEnable, glDisable, GL_LIGHTING, GL_POINTS, glTranslatef
from        OpenGL.GLUT import glutInit, glutWireSphere

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)

        glutInit()
        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):
        for (c,r) in self.spheres:
            glTranslatef(c.x,c.y,c.z)
            glutWireSphere(r, 20, 20)
            glTranslatef(-c.x,-c.y,-c.z)

    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))