PyVEFViewer.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Tue, 22 Mar 2011 14:17:09 -0700
changeset 10 bc10701b4081
parent 9 a500b28a675d
child 11 d2b9ee185b38
permissions -rw-r--r--
Added file opening

from    PyQGLViewer     import *
from    PyQt4           import QtGui, QtCore
import  OpenGL.GL as ogl
from    os.path         import splitext; ext = lambda fn: splitext(fn)[1]

from    points          import Points, centerMinMax, reduceCMM
from    edges           import Edges
from    triangles       import Triangles


class VEFViewer(QGLViewer):
    def __init__(self, parent = None):
        super(VEFViewer, self).__init__(parent)
        self.setStateFileName('.PyVEFViewer.xml')

    def models(self):
        for i in xrange(self.model_list.count()):
            yield self.model_list.item(i)

    def draw(self):
        self.lights()
        for mod in self.models():
            mod.draw()

    def drawWithNames(self):
        for i,mod in enumerate(self.models()):
            mod.drawWithNames(i)

    def lights(self):
        # GL_LIGHT0
        ogl.glLightfv(ogl.GL_LIGHT0, ogl.GL_POSITION, (self.center.x, self.center.y, self.center.z, 1.0));

        # GL_LIGHT1
        camera_pos = self.camera().position();
        camera_dir = self.camera().viewDirection();
        light_pos1 = (camera_pos.x, camera_pos.y, camera_pos.z, 1.0);
        light_spot_dir1 = (camera_dir.x, camera_dir.y, camera_dir.z);
        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_POSITION, light_pos1);

    def modelsContextMenu(self, position):
        item = self.model_list.itemAt(position)
        if item:
            item.contextMenu(self.model_list.mapToGlobal(position))
        self.updateGL()

    def modelDoubleClicked(self, item):
        item.toggleVisible()
        self.updateGL()

    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_O:
            filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file')
            self.read_from_extension(str(filename))
            self.normalize_view()
        else:
            super(VEFViewer, self).keyPressEvent(e)

    def init(self):
        # ogl.glMaterialf(ogl.GL_FRONT_AND_BACK, ogl.GL_SHININESS, 50.0)
        # specular_color = [ 0.8, 0.8, 0.8, 1.0 ]
        # ogl.glMaterialfv(ogl.GL_FRONT_AND_BACK, ogl.GL_SPECULAR,  specular_color)
        # self.restoreStateFromFile()
        # self.help()

        ogl.glShadeModel(ogl.GL_SMOOTH);
        ogl.glEnable(ogl.GL_COLOR_MATERIAL);
        ogl.glEnable(ogl.GL_NORMALIZE);
        ogl.glEnable(ogl.GL_LINE_SMOOTH);
        ogl.glEnable(ogl.GL_POINT_SMOOTH);
        ogl.glLightModeli(ogl.GL_LIGHT_MODEL_TWO_SIDE, ogl.GL_TRUE);
        ogl.glPointSize(2.0);
        ogl.glEnable(ogl.GL_CULL_FACE);


    #def helpString(self):
    #    return helpstr

    def read_points(self, points):
        for pts in points:
            self.model_list.addItem(Points(pts, self.model_list))

    def read_edges(self, edges):
        for edg in edges:
            self.model_list.addItem(Edges(edg, self.model_list))

    def read_triangles(self, triangles):
        for tri in triangles:
            self.model_list.addItem(Triangles(tri, self.model_list))

    def read_from_extension(self, fn):
        if   ext(fn) == '.vrt' or ext(fn) == '.pts':
            self.read_points([fn])
        elif ext(fn) == '.edg':
            self.read_edges([fn])
        elif ext(fn) == '.stl':
            self.read_triangles([fn])
        else:
            return False

        return True

    def set_list(self, model_list):
        self.model_list = model_list
        self.model_list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.model_list.customContextMenuRequested.connect(self.modelsContextMenu)
        self.model_list.itemDoubleClicked.connect(self.modelDoubleClicked)

    def normalize_view(self):
        self.scene_parameters()
        
        self.setSceneBoundingBox(Vec(self.min.x, self.min.y, self.min.z), \
                                 Vec(self.max.x, self.max.y, self.max.z))
        self.setSceneCenter(Vec(self.center.x, self.center.y, self.center.z))
        
    def show_entire_scene(self):        
        self.camera().showEntireScene()

    def scene_parameters(self):
        self.center, self.min, self.max = reduceCMM((mod.center, mod.min, mod.max) for mod in self.models())