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