spheres.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Fri, 05 Aug 2011 10:42:49 -0700
changeset 22 d4f4fe389914
parent 21 063f99363534
permissions -rw-r--r--
Removed qgllogo import

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, glutSolidSphere

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 Spheres(ViewerItem):
    def __init__(self, filename, parent = None):
        filename = filename.split(':')
        self.mult = float(filename[1]) if len(filename) > 1 else 1.
        filename = filename[0]
        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())

        self.wireframe = True

    def createMenu(self):
        menu = super(Spheres, self).createMenu()
        wireframeAction = menu.addAction("Wireframe")
        wireframeAction.setCheckable(True)
        wireframeAction.setChecked(self.wireframe)
        wireframeAction.command = lambda: self.wireframeCommand()
        return menu

    def wireframeCommand(self):
        self.wireframe = not self.wireframe

    def create_display_list(self):
        self.solid_display_list = glGenLists(1)
        glNewList(self.solid_display_list, GL_COMPILE)
        self.draw_spheres(wireframe = False)
        glEndList()
        self.wireframe_display_list = glGenLists(1)
        glNewList(self.wireframe_display_list, GL_COMPILE)
        self.draw_spheres(wireframe = True)
        glEndList()

    def draw_spheres(self, wireframe = True):
        for (c,r) in self.spheres:
            glTranslatef(c.x,c.y,c.z)
            if wireframe:
                glutWireSphere(r, 20, 20)
            else:
                glutSolidSphere(r, 50, 50)
            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)
        if self.wireframe:
            glCallList(self.wireframe_display_list)
        else:
            glCallList(self.solid_display_list)

    def vertices(self):
        for (c,r) in self.spheres:
            yield c

    def __iter__(self):
        return self.spheres

    def save_povray(self, f):
        cr,cg,cb,ca = self.color.getRgb()
        cr,cg,cb = cr/255., cg/255., cb/255.
        for (c,r) in self.spheres:
            f.write(povray.sphere(c,r,(cr,cg,cb)))

    def read_spheres(self, filename):
        self.spheres = []
        for block in line_blocks(filename, 1):
            block = block[0].split()
            c = Point(map(float, block[:3]))*self.mult
            r = float(block[3])*self.mult
            self.spheres.append((c,r))