Added saving of edges and spheres in povray
authorDmitriy Morozov <dmitriy@mrzv.org>
Tue, 21 Jun 2011 16:21:45 -0700
changeset 16 69f6b439b161
parent 15 30a851aa0675
child 17 9f4bf1795d51
Added saving of edges and spheres in povray
PyVEFViewer.py
edges.py
points.py
povray.py
spheres.py
--- a/PyVEFViewer.py	Fri Jun 03 12:56:27 2011 -0700
+++ b/PyVEFViewer.py	Tue Jun 21 16:21:45 2011 -0700
@@ -7,6 +7,7 @@
 from    edges           import Edges
 from    triangles       import Triangles
 from    spheres         import Spheres
+import  povray
 
 
 class VEFViewer(QGLViewer):
@@ -28,15 +29,10 @@
             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);
+        # Place light at camera position
+        cameraPos = self.camera().position()
+        pos = [cameraPos[0], cameraPos[1], cameraPos[2], 1.0]
+        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_POSITION, pos)
 
     def modelsContextMenu(self, position):
         item = self.model_list.itemAt(position)
@@ -59,29 +55,53 @@
             else:
                 self.camera().setType(Camera.ORTHOGRAPHIC)
             self.updateGL()
+        elif e.key() == QtCore.Qt.Key_S:
+            self.save_povray()
         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()
+        # Light setup
+        ogl.glDisable(ogl.GL_LIGHT0);
+        ogl.glEnable(ogl.GL_LIGHT1);
+
+        # Light default parameters
+        light_ambient  = [1.0, 1.0, 1.0, 1.0]
+        light_specular = [1.0, 1.0, 1.0, 1.0]
+        light_diffuse  = [1.0, 1.0, 1.0, 1.0]
 
-        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);
+        ogl.glLightf( ogl.GL_LIGHT1, ogl.GL_SPOT_EXPONENT, 3.0)
+        ogl.glLightf( ogl.GL_LIGHT1, ogl.GL_SPOT_CUTOFF,   10.0)
+        ogl.glLightf( ogl.GL_LIGHT1, ogl.GL_CONSTANT_ATTENUATION,  0.1)
+        ogl.glLightf( ogl.GL_LIGHT1, ogl.GL_LINEAR_ATTENUATION,    0.3)
+        ogl.glLightf( ogl.GL_LIGHT1, ogl.GL_QUADRATIC_ATTENUATION, 0.3)
+        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_AMBIENT,  light_ambient)
+        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_SPECULAR, light_specular)
+        ogl.glLightfv(ogl.GL_LIGHT1, ogl.GL_DIFFUSE,  light_diffuse)
 
 
     #def helpString(self):
     #    return helpstr
 
+    def save_povray(self):
+        filename = QtGui.QFileDialog.getSaveFileName(self, 'Save POV-Ray file', '.', 'POV-Ray files (*.pov)')
+        if not filename: return
+
+        pos = self.camera().position()
+        dir = self.camera().viewDirection()
+        up  = self.camera().upVector()
+        right  = self.camera().rightVector()
+        at  = pos + dir
+
+        with open(filename, 'w') as f:
+            f.write('#include "colors.inc"\n')
+            f.write('background { rgb 1 }\n')
+            f.write(povray.camera(pos, at))
+            f.write(povray.spotlight(pos, at))
+
+            for m in self.models():
+                if m.visible: m.save_povray(f)
+
     def read_points(self, points):
         for pts in points:
             self.model_list.addItem(Points(pts, self.model_list))
--- a/edges.py	Fri Jun 03 12:56:27 2011 -0700
+++ b/edges.py	Tue Jun 21 16:21:45 2011 -0700
@@ -9,6 +9,7 @@
 from        os.path     import basename
 from        itertools   import izip
 from        io          import line_blocks
+import      povray
 
 class Edges(ViewerItem):
     def __init__(self, filename, parent = None):
@@ -76,6 +77,15 @@
     def __iter__(self):
         return self.edges
 
+    def save_povray(self, f):
+        cr,cg,cb,ca = self.color.getRgb()
+        cr,cg,cb = cr/255., cg/255., cb/255.
+        for (u,v) in self.edges:
+            f.write(povray.cylinder(u,v,.5,(cr,cg,cb)))
+            f.write(povray.sphere(u,.5,(cr,cg,cb)))
+            f.write(povray.sphere(v,.5,(cr,cg,cb)))
+            f.write('\n')
+
     def read_edges(self, filename):
         self.edges = []
         for block in line_blocks(filename, 2):
--- a/points.py	Fri Jun 03 12:56:27 2011 -0700
+++ b/points.py	Tue Jun 21 16:21:45 2011 -0700
@@ -67,6 +67,9 @@
     def __iter__(self):
         return self.points
 
+    def save_povray(self, f):
+        pass
+
 def centerMinMax(it):
     count = 0
     center, min, max = Point(), Point(), Point()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/povray.py	Tue Jun 21 16:21:45 2011 -0700
@@ -0,0 +1,61 @@
+def cylinder(u,v,r,c):
+    return \
+'''cylinder
+{
+    <%(ux)f, %(uy)f, %(uz)f>,
+    <%(vx)f, %(vy)f, %(vz)f>,
+    %(r)f
+    texture { pigment { color rgb <%(cr)f, %(cg)f, %(cb)f> } }
+}\n''' % {  'ux': u.x,
+          'uy': u.y,
+          'uz': u.z,
+          'vx': v.x,
+          'vy': v.y,
+          'vz': v.z,
+          'r': r,
+          'cr': c[0],
+          'cg': c[1],
+          'cb': c[2] }
+
+def sphere(c,r,color):
+    return \
+'''sphere
+{
+    <%(x)f, %(y)f, %(z)f>, %(r)f
+    texture { pigment { color rgb <%(cr)f, %(cg)f, %(cb)f> } }
+}\n''' %  { 'x': c.x,
+          'y': c.y,
+          'z': c.z,
+          'r': r,
+          'cr': color[0],
+          'cg': color[1],
+          'cb': color[2] }
+
+
+def camera(pos, at):
+    return '''
+camera
+{
+    location <%(lx)f, %(ly)f, %(lz)f>
+    look_at <%(atx)f, %(aty)f, %(atz)f>
+}\n''' % { 'lx':  pos.x,
+           'ly':  pos.y,
+           'lz':  pos.z,
+           'atx': at.x,
+           'aty': at.y,
+           'atz': at.z }
+
+def spotlight(pos, at):
+    return '''
+light_source
+{
+    <%(lx)f, %(ly)f, %(lz)f> color White
+    spotlight
+    point_at <%(atx)f, %(aty)f, %(atz)f>
+}\n''' % { 'lx':  pos.x,
+           'ly':  pos.y,
+           'lz':  pos.z,
+           'atx': at.x,
+           'aty': at.y,
+           'atz': at.z }
+
--- a/spheres.py	Fri Jun 03 12:56:27 2011 -0700
+++ b/spheres.py	Tue Jun 21 16:21:45 2011 -0700
@@ -8,6 +8,7 @@
 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):
@@ -67,6 +68,12 @@
     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):