Ejemplo n.º 1
0
 def __init__(self, operation):
     self.faces = []
     self.normals = []
     self.vertices = []
     self.colors = []
     self.vnormals = []
     self.list = -1
     
     c = CSG.cube()
     b = CSG.sphere()#slices=32,stacks=16)
     a = CSG.tetra()
     #b = CSG.cylinder(radius=0.5, start=[0., 0., 0.], end=[0., 2., 0.])#,slices=16)
     #c = CSG.cylinder(radius=0.5, start=[0., 0., 0.], end=[0., 2., 0.]).rotate([0,0,1],90)#,slices=16)
     for p in a.polygons:
         p.shared = [1.0, 0.0, 0.0, 1.0]
     for p in b.polygons:
         p.shared = [0.0, 1.0, 0.0, 1.0]
         
     recursionlimit = sys.getrecursionlimit()
     sys.setrecursionlimit(10000)
     try:
         if operation == 'subtract':
             polygons = a.subtract(b).toPolygons()
         elif operation == 'union':
             polygons =a.union(c).toPolygons()
         elif operation == 'intersect':
             polygons = a.intersect(b).toPolygons()
         else:
             raise Exception('Unknown operation: \'%s\'' % operation)
     except RuntimeError as e:
         raise RuntimeError(e)
     sys.setrecursionlimit(recursionlimit)
     
     for polygon in polygons:
         n = polygon.plane.normal
         indices = []
         for v in polygon.vertices:
             pos = [v.pos.x, v.pos.y, v.pos.z]
             if not pos in self.vertices:
                 self.vertices.append(pos)
                 self.vnormals.append([])
             index = self.vertices.index(pos)
             indices.append(index)
             self.vnormals[index].append(v.normal)
         self.faces.append(indices)
         self.normals.append([n.x, n.y, n.z])
         self.colors.append(polygon.shared)
     
     # setup vertex-normals
     ns = []
     for vns in self.vnormals:
         n = Vector(0.0, 0.0, 0.0)
         for vn in vns:
             n = n.plus(vn)
         n = n.dividedBy(len(vns))
         ns.append([a for a in n])
     self.vnormals = ns