def cylinder(self, position1, position2, radius=1.0, material=None, name=None): if material is not None: color = self.materials[material] glColor3f(color[0], color[1], color[2]) z = Vector(0.0, 0.0, 1.0) d = position2 - position1 if d.length() < 1e-10: return d = d.normal() axis = z.cross(d) glPushMatrix() glTranslatef(position1[0], position1[1], position1[2]) if axis.length() > 0.0000001: axis = axis.normal() glRotatef(acos(z * d) * 180 / pi, axis[0], axis[1], axis[2]) else: if d[2] < 0: glRotatef(180, 1, 0, 0) gluCylinder(self.quadratic, radius, radius, (position2 - position1).length(), self.n, 2) glPopMatrix()
def testCylinder(self): p = ColladaPaint3D() f = StringIO() position1 = Vector(0, 0, 0) position2 = Vector(0, 0, 1) color = Vector(1, 0, 0) p.cylinder(position1, position2, 7, color) p.write(f)
def testColorMaterial(self): p = PovrayPaint3D() position = Vector(2, 3, 4) p.colorMaterial(color=Vector(1, 0, 0), name="colmat") f = StringIO() p.write(f) self.assert_("< 1.0" in f.getvalue()) self.assert_("colmat" in f.getvalue()) f.close()
def testPointLight(self): p = PovrayPaint3D() p.pointLight(Vector(2, 3, 4), Vector(1, 1, 1)) f = StringIO() p.write(f) self.assert_("light_source" in f.getvalue()) self.assert_("< 2.0" in f.getvalue()) self.assert_("rgb < 1.0" in f.getvalue()) f.close()
def orthographicCamera(self, position, look_at, right, up, name=None): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-right.length(), right.length(), -up.length(), up.length(), -100, 100) z = Vector(0.0, 0.0, 1.0) d = (look_at - position).normal() axis = z.cross(d) if axis.length() > 0.0000001: axis = axis.normal() glRotatef(z * d, axis[0], axis[1], axis[2]) glTranslatef(-position[0], -position[1], -position[2]) glMatrixMode(GL_MODELVIEW)
def testSphere(self): p = PovrayPaint3D() position = Vector(2, 3, 4) p.colorMaterial(Vector(1, 0, 0), name="colmat") p.sphere(position, 4, material="colmat") f = StringIO() p.write(f) self.assert_("sphere" in f.getvalue()) self.assert_("< 2.0" in f.getvalue()) self.assert_("colmat" in f.getvalue()) self.assert_("color rgb < 1.0" in f.getvalue()) f.close()
def export(self,data,attributes,path): paint=self.paint[0]() paint.width=attributes.width paint.height=attributes.height self.exportCamera(paint,attributes) paint.ambientLight(Vector(0.2,0.2,0.2)) paint.background(Vector(attributes.background)) paint.pointLight(self.transform(Vector(30,30,30),attributes),Vector(1,1,1)) paint.colorMaterial(attributes.cell_color,self.CELLMATERIAL) paint.colorMaterial(attributes.bond_color,self.BONDMATERIAL) paint.colorMaterial(attributes.arrow_color,self.ARROWMATERIAL) schedule(self.exportSequence(paint,data,attributes,path))
def testLine(self): f = StringIO() p = PovrayPaint3D() position1 = Vector(2, 3, 4) position2 = Vector(4, 5, 6) p.colorMaterial(Vector(1, 0, 0), name="colmat") p.line(position1, position2, 7, material="colmat") p.write(f) self.assert_("cylinder" in f.getvalue()) self.assert_("< 2.0" in f.getvalue()) self.assert_("< 4.0" in f.getvalue()) self.assert_("colmat" in f.getvalue()) self.assert_("color rgb < 1.0" in f.getvalue()) f.close()
def readStructure(self, Id, cstructure=False): s = Structure() l = self.fetchone( """SELECT calc_id,scale,comment,a11,a12,a13,a21,a22,a23,a31,a32,a33, species FROM #STRUCT WHERE id=%d""" % (Id)) (calc_id, scale, s.comment, s.basis[0][0], s.basis[0][1], s.basis[0][2], s.basis[1][0], s.basis[1][1], s.basis[1][2], s.basis[2][0], s.basis[2][1], s.basis[2][2], spec) = l s.scaling = [scale] s.setCartesian() l = self.fetchall( "SELECT specie,element,x,y,z FROM #STRUCTPOS WHERE structure_id=%d ORDER BY atomnumber" % (Id)) for spec, element, x, y, z in l: i = s.appendAtom(spec, Vector(x, y, z)) s.info.getRecordForAtom(i).element = str(element) l = self.fetchall( "SELECT x,y,z FROM #STRUCTCONSTRAINTS WHERE structure_id=%d ORDER BY atomnumber" % (Id)) if len(l): s.setSelective() for i in range(len(l)): s.selective[i] = [int(l[i][0]), int(l[i][1]), int(l[i][2])] if cstructure: s = p4vasp.cStructure.Structure(s) return s
def testPerspectiveCamera(self): p = PovrayPaint3D() camera_position = Vector(2, 3, 4) look_at = Vector(4, 5, 6) right = Vector(-1, 0, 0) up = Vector(0, 1, 0) p.perspectiveCamera(camera_position, look_at, right, up) f = StringIO() p.write(f) self.assert_("camera" in f.getvalue()) self.assert_("< 2.0" in f.getvalue()) self.assert_("< 4.0" in f.getvalue()) self.assert_("perspective" in f.getvalue()) f.close()
def readForce(self,structure_id): from matrix import Vector force=[] l=self.fetchall("SELECT x,y,z FROM #STRUCTFORCE WHERE structure_id=%d ORDER BY atomnumber"%(structure_id)) for x,y,z in l: force.append(Vector(x,y,z)) return force
def testAmbientLight(self): p = PovrayPaint3D() p.ambientLight(Vector(1, 0.5, 0)) f = StringIO() p.write(f) self.assert_("ambient_light" in f.getvalue()) self.assert_("color rgb < 1.0" in f.getvalue()) f.close()
def createElementMaterials(self,paint,structure): info=structure.info materials=[] for i in range(len(structure)): element=self.atomtypes.getRecordForElementSafe(info.getRecordForAtom(i).element,info.speciesIndex(i)) name=self.ELEMENTMATERIAL+"_%03d"%i color=Vector(element.red,element.green,element.blue) paint.colorMaterial(color,name) materials.append(name) return materials
def testBackground(self): p = PovrayPaint3D() p.background(Vector(1, 0.5, 0)) f = StringIO() p.write(f) self.assert_("background" in f.getvalue()) self.assert_("color rgb < 1.0" in f.getvalue()) f.close()
def exportCamera(self,paint,attributes): if attributes.perspective: look_at=self.transform(Vector(0,0,0),attributes) camera_position=self.transform(Vector(0,0,20),attributes) right=self.transformVector(Vector(-attributes.width*0.004,0,0),attributes) up=self.transformVector(Vector(0,attributes.height*0.004,0),attributes) paint.perspectiveCamera(camera_position,look_at,right,up) else: look_at=self.transform(Vector(0,0,0),attributes) camera_position=self.transform(Vector(0,0,30),attributes) right=self.transformVector(Vector(-attributes.width*0.04,0,0),attributes) up=self.transformVector(Vector(0,attributes.height*0.04,0),attributes) paint.orthographicCamera(camera_position,look_at,right,up)
def paintArrows(self,paint,structure,arrows,attributes,offset): info=structure.info materials=[] if arrows is not None: for i in range(min(len(structure),len(arrows))): paint.arrow( structure[i]+offset, structure[i]+offset+Vector(arrows[i])*attributes.arrow_scale, attributes.arrow_radius, attributes.arrowhead_radius, attributes.arrowhead_length, material=self.ARROWMATERIAL )
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ###################################################################### # DESCRIPTION: # ###################################################################### # paints a sphere mesh in povray - # # This is not realy useful - it is an example # ###################################################################### from p4vasp.paint3d.MeshTools import * from p4vasp.paint3d.PovrayPaint3D import * from p4vasp.matrix import Vector paint = ExtendedPovrayPaint3D() material=paint.colorMaterial(Vector(1,0,0),"MeshMaterial") paint.background(Vector(0,0,0)) paint.pointLight(position=Vector(10,10,10),color=Vector(1,1,1)) paint.orthographicCamera(look_at=Vector(0,0,0),position=(10,0,-10),right=Vector(0,8,0),up=Vector(0,0,6)) #paint.sphere(position=Vector(0,0,0),radius=1.0,material="MeshMaterial") coordinates,normals,triangles=sphereMesh(6) paint.mesh(coordinates,normals,triangles,material="MeshMaterial") paint.width=800 paint.height=600 paint.exportTo("povraysphere.pov")
def transform(self,position,attributes): m=Matrix(attributes.rotmat) offset=Vector([m[0][3],m[1][3],m[2][3]]) m=m.trans() return self.transformVector(position-offset,attributes)