import math

class Triangle:

    edges = [('A','B'),('B','C'),('C','A')]
    
    def __init__(self, a, b, c):
        self.vertices = {}
        self.vertices['A'] = a
        self.vertices['B'] = b
        self.vertices['C'] = c

    def povout(self):
        "Simulate writing edges, vertices to Povray"
        def pt(point):
            return "<%s, %s, %s>" % point

        for e in self.edges:
            v0 = self.vertices[e[0]]
            v1 = self.vertices[e[1]]            
            print "cylinder {%s , %s 0.02}" % (pt(v0), pt(v1))

        for v in self.vertices.values():
            print "sphere {%s 0.02}" % (pt(v))

    def length(self, p0, p1):
        "Distance forumla applied to user-supplied vertices"
        v0 = self.vertices[p0]
        v1 = self.vertices[p1]
        
        return math.sqrt((v0[0] - v1[0])**2 +
                         (v0[1] - v1[1])**2 +
                         (v0[2] - v1[1])**2 )

    def __repr__(self):
        return "Triangle: %s" % self.vertices.values()

class Tri(Triangle):

    def povout(self, thefile):
        "Write edges to Povray file"

        def pt(point):
            return "<%s, %s, %s>" % point

        for e in self.edges:
            v0 = self.vertices[e[0]]
            v1 = self.vertices[e[1]]            
            thefile.write("cylinder {%s , %s 0.02 }\n" % (pt(v0), pt(v1)))

        for v in self.vertices.values():
            thefile.write("sphere {%s 0.02 }\n" % (pt(v)))

class Povray:

    filepath = "C:/Documents and Settings/Kirby/My Documents/FreeGeek/PythonicGeom/"
    
    def __init__(self, filename):
        self.filename = self.filepath + filename
        print self.filename
                
    def header(self):
        print "Writing header..."
        thefile = file(self.filename,'w')
        thefile.write("""
// Persistence of Vision Ray Tracer Scene Description File
// File: .pov
// Vers: 3.5
// Desc:
// Date:
// Auth:

// create a regular point light source
light_source {
  0*x                  // light's position (translated below)
  color rgb <1,1,1>    // light's color
  translate <-20, 40, -20>
}     

// set a color of the background (sky)
background { color rgb <0.1, 0.3, 0.8> }
// perspective (default) camera
camera {
  location  <0.0, 2.0, -10.0>
  look_at   <0.0, 0.0,  0.0>
  right     x*image_width/image_height
}\n
""")
        
    def body(self, object):
        print "Writing object..."
        thefile = file(self.filename,'a')
        object.povout(thefile)

def test1():
    t = Triangle((0,0,0),(3,0,0),(0,4,0))
    t
    t.length('A','B')
    t.length('A','C')
    t.length('B','C')
    t.povout()
    
def test2():
    t = Tri((0,0,0),(3,0,0),(0,4,0))
    outfile = Povray("test2.pov")
    outfile.header()
    outfile.body(t)
    for i in file(Povray.filepath + "test2.pov"):
        print i,

def test3():
    t1 = Tri((0,0,0),(3,0,0),(0,4,0))
    t2 = Tri((0,0,0),(-3,0,0),(0,4,0))
    t3 = Tri((0,0,0),(-3,0,0),(0,-4,0))
    t4 = Tri((0,0,0),(3,0,0),(0,-4,0))    
    outfile = Povray("test3.pov")
    outfile.header()
    outfile.body(t1)
    outfile.body(t2)
    outfile.body(t3)
    outfile.body(t4)    
    for i in file(Povray.filepath + "test3.pov"):
        print i,        
