def generate_icosahedron(side_length) : ''' Generate an icosahedron from the given side length and return an array of 20 triangles component from the icosahedron and his construction base quad set. ''' side_lt=side_length # Define the littler side of the rectangle. side_gt=side_length*((1+sqrt(5))/2.) # Compute the length of the greater side with the gold number ( (1+sqrt(5))/2. ) # Define the base quad so that his center is Vertex(0.0, 0.0, 0.0) : quad1=[Vertex(-side_lt/2.,-side_gt/2.,0.0),Vertex(side_lt/2.,-side_gt/2.,0.0),Vertex(side_lt/2.,side_gt/2.,0.0),Vertex(-side_lt/2.,side_gt/2.,0.0)] # Rotate the base quad on the y and z axes from 90°, to obtain the second crossing quad. quad2=[rotate_z(rotate_y(quad1[0],90),90), rotate_z(rotate_y(quad1[1],90),90), rotate_z(rotate_y(quad1[2],90),90), rotate_z(rotate_y(quad1[3],90),90)] # Rotate the base quad on the y and x axes from 90°, to obtain the third crossing quad. quad3=[rotate_x(rotate_y(quad1[0],90),90), rotate_x(rotate_y(quad1[1],90),90), rotate_x(rotate_y(quad1[2],90),90), rotate_x(rotate_y(quad1[3],90),90)] icosahedron_base_quads=[quad1,quad2,quad3] # We set the bases quads for our polyhedron: an icosahedron. # Define an array composed of triangles because an icosahedron is composed from 20 equilateral triangles: icosahedron_triangle_array=[(icosahedron_base_quads[0][0],icosahedron_base_quads[0][1],icosahedron_base_quads[2][1]), (icosahedron_base_quads[0][1],icosahedron_base_quads[1][0],icosahedron_base_quads[1][1]), (icosahedron_base_quads[0][0],icosahedron_base_quads[0][1],icosahedron_base_quads[2][2]), (icosahedron_base_quads[0][0],icosahedron_base_quads[1][2],icosahedron_base_quads[1][3]), (icosahedron_base_quads[0][0],icosahedron_base_quads[1][3],icosahedron_base_quads[2][1]), (icosahedron_base_quads[0][1],icosahedron_base_quads[2][1],icosahedron_base_quads[1][0]), (icosahedron_base_quads[0][1],icosahedron_base_quads[1][1],icosahedron_base_quads[2][2]), (icosahedron_base_quads[0][0],icosahedron_base_quads[2][2],icosahedron_base_quads[1][2]), (icosahedron_base_quads[0][2],icosahedron_base_quads[0][3],icosahedron_base_quads[2][3]), (icosahedron_base_quads[0][3],icosahedron_base_quads[1][2],icosahedron_base_quads[1][3]), (icosahedron_base_quads[0][2],icosahedron_base_quads[0][3],icosahedron_base_quads[2][0]), (icosahedron_base_quads[0][2],icosahedron_base_quads[1][0],icosahedron_base_quads[1][1]), (icosahedron_base_quads[0][2],icosahedron_base_quads[1][1],icosahedron_base_quads[2][3]), (icosahedron_base_quads[0][3],icosahedron_base_quads[2][3],icosahedron_base_quads[1][2]), (icosahedron_base_quads[0][3],icosahedron_base_quads[1][3],icosahedron_base_quads[2][0]), (icosahedron_base_quads[0][2],icosahedron_base_quads[2][0],icosahedron_base_quads[1][0]), (icosahedron_base_quads[2][0],icosahedron_base_quads[2][1],icosahedron_base_quads[1][3]), (icosahedron_base_quads[2][0],icosahedron_base_quads[2][1],icosahedron_base_quads[1][0]), (icosahedron_base_quads[2][2],icosahedron_base_quads[2][3],icosahedron_base_quads[1][1]), (icosahedron_base_quads[2][2],icosahedron_base_quads[2][3],icosahedron_base_quads[1][2])] # We return the base quad set and the triangle array. return [quad1,quad2,quad3],icosahedron_triangle_array
def generate_polygon_on_xz_side_length(edges,side_length,offset=0) : ''' Return an polygon on plan XZ: with edges edges from length side_length, with offset offset. z / _____/____x / / plan XZ. ''' angle=360.0/side_length polygon=[] # Polygon vertice container. scale=360.0/edges # Computing of the angle separating 2 points from the polygon. start_vertex1=Vertex(-side_length/2.,0.0,0) start_vertex2=Vertex(side_length/2.,0.0,0) point_to_rotate=start_vertex1 rotate_point=start_vertex2 polygon.append(point_to_rotate) polygon.append(rotate_point) i=2 while i < edges : vertex=rotate_on_xz(rotate_point,abs(180-scale),point_to_rotate) point_to_rotate=rotate_point rotate_point=vertex polygon.append(vertex) i += 1 center=get_center_from_polygon(polygon) # Compute polygon center. tmp=[] for v in polygon : # Translate polygon vertices so as his center is the display center. tmp.append(translate(v,-center.wx,-center.wy,-center.wz)) if offset : offset_set=[] for v in tmp : offset_set.append(rotate_y(v,offset)) tmp=offset_set polygon=tmp return polygon
def generate_toros(base,base_radius,toros_radius) : ''' Generate an toros in relationship to the given settings: base: the toros basis polygon. base_radius: the toros basis polygon radius. toros_radius: the toros radius (without the base polygon radius). and return an sequence of polygons base from the toros. ''' base_polygon=generate_polygon_on_yz_radius(base,base_radius,Vertex(0.,0.,0.)) i=0 toros=[] while i < 360.0 : polygon=[] for v in base_polygon : vertex=rotate_y(translate(v,0.0,0.0,toros_radius),i) polygon.append(vertex) i += 360.0/base toros.append(polygon) return toros
def generate_toros(base, base_radius, toros_radius): ''' Generate an toros in relationship to the given settings: base: the toros basis polygon. base_radius: the toros basis polygon radius. toros_radius: the toros radius (without the base polygon radius). and return an sequence of polygons base from the toros. ''' base_polygon = generate_polygon_on_yz_radius(base, base_radius, Vertex(0., 0., 0.)) i = 0 toros = [] while i < 360.0: polygon = [] for v in base_polygon: vertex = rotate_y(translate(v, 0.0, 0.0, toros_radius), i) polygon.append(vertex) i += 360.0 / base toros.append(polygon) return toros
def generate_icosahedron(side_length): ''' Generate an icosahedron from the given side length and return an array of 20 triangles component from the icosahedron and his construction base quad set. ''' side_lt = side_length # Define the littler side of the rectangle. side_gt = side_length * ( (1 + sqrt(5)) / 2. ) # Compute the length of the greater side with the gold number ( (1+sqrt(5))/2. ) # Define the base quad so that his center is Vertex(0.0, 0.0, 0.0) : quad1 = [ Vertex(-side_lt / 2., -side_gt / 2., 0.0), Vertex(side_lt / 2., -side_gt / 2., 0.0), Vertex(side_lt / 2., side_gt / 2., 0.0), Vertex(-side_lt / 2., side_gt / 2., 0.0) ] # Rotate the base quad on the y and z axes from 90°, to obtain the second crossing quad. quad2 = [ rotate_z(rotate_y(quad1[0], 90), 90), rotate_z(rotate_y(quad1[1], 90), 90), rotate_z(rotate_y(quad1[2], 90), 90), rotate_z(rotate_y(quad1[3], 90), 90) ] # Rotate the base quad on the y and x axes from 90°, to obtain the third crossing quad. quad3 = [ rotate_x(rotate_y(quad1[0], 90), 90), rotate_x(rotate_y(quad1[1], 90), 90), rotate_x(rotate_y(quad1[2], 90), 90), rotate_x(rotate_y(quad1[3], 90), 90) ] icosahedron_base_quads = [ quad1, quad2, quad3 ] # We set the bases quads for our polyhedron: an icosahedron. # Define an array composed of triangles because an icosahedron is composed from 20 equilateral triangles: icosahedron_triangle_array = [ (icosahedron_base_quads[0][0], icosahedron_base_quads[0][1], icosahedron_base_quads[2][1]), (icosahedron_base_quads[0][1], icosahedron_base_quads[1][0], icosahedron_base_quads[1][1]), (icosahedron_base_quads[0][0], icosahedron_base_quads[0][1], icosahedron_base_quads[2][2]), (icosahedron_base_quads[0][0], icosahedron_base_quads[1][2], icosahedron_base_quads[1][3]), (icosahedron_base_quads[0][0], icosahedron_base_quads[1][3], icosahedron_base_quads[2][1]), (icosahedron_base_quads[0][1], icosahedron_base_quads[2][1], icosahedron_base_quads[1][0]), (icosahedron_base_quads[0][1], icosahedron_base_quads[1][1], icosahedron_base_quads[2][2]), (icosahedron_base_quads[0][0], icosahedron_base_quads[2][2], icosahedron_base_quads[1][2]), (icosahedron_base_quads[0][2], icosahedron_base_quads[0][3], icosahedron_base_quads[2][3]), (icosahedron_base_quads[0][3], icosahedron_base_quads[1][2], icosahedron_base_quads[1][3]), (icosahedron_base_quads[0][2], icosahedron_base_quads[0][3], icosahedron_base_quads[2][0]), (icosahedron_base_quads[0][2], icosahedron_base_quads[1][0], icosahedron_base_quads[1][1]), (icosahedron_base_quads[0][2], icosahedron_base_quads[1][1], icosahedron_base_quads[2][3]), (icosahedron_base_quads[0][3], icosahedron_base_quads[2][3], icosahedron_base_quads[1][2]), (icosahedron_base_quads[0][3], icosahedron_base_quads[1][3], icosahedron_base_quads[2][0]), (icosahedron_base_quads[0][2], icosahedron_base_quads[2][0], icosahedron_base_quads[1][0]), (icosahedron_base_quads[2][0], icosahedron_base_quads[2][1], icosahedron_base_quads[1][3]), (icosahedron_base_quads[2][0], icosahedron_base_quads[2][1], icosahedron_base_quads[1][0]), (icosahedron_base_quads[2][2], icosahedron_base_quads[2][3], icosahedron_base_quads[1][1]), (icosahedron_base_quads[2][2], icosahedron_base_quads[2][3], icosahedron_base_quads[1][2]) ] # We return the base quad set and the triangle array. return [quad1, quad2, quad3], icosahedron_triangle_array
def generate_quad_sphere(basis,radius) : ''' Generate an quads sphere and return an tuple from 2 arrays: (lined displaying vertices sequence list, surface displaying vertices sequence list). In relationship with the base for the sphere generating: faces count = basis * basis and the given sphere radius. ''' if not isinstance(basis,int) : raise TypeError(int) if basis < 6 or basis % 2 : print "the basis for the sphere must be greater as 5 and basis % 2 == 0 " quit() if not isinstance(radius,int) and not isinstance(radius,float) : raise TypeError(int,float) # We generate the base polygon for the sphere generating with the given radius: polygon_1=generate_polygon_on_xy_radius(basis,radius,Vertex(0.0,0.0,0.0),0) polygons=[] # Container for the polygons. i=2 # The iterator variable is initialise with the value 2 # because this one is used for string formating and the polygon_1 variable exist. while i <= basis : # Generate from empty lists by execution, with the directive exec(), # from formattted strings. # Instead of defining variables because the number of polygons is relativ to the basis argument value. exec("polygon_{0}=[]".format(i)) exec("polygons.append(polygon_{0})".format(i)) i += 1 for v in polygon_1 : # Iteration over every vertice from our base polygon. i=0 angle=360.0/basis # Computing of the degress between 2 polygons on the XZ surface. while i < len(polygons) : # generating of all polygons containing the vertices from the sphere. polygons[i].append(rotate_y(v,angle)) i += 1 angle += 360./basis polygons_array=[] # temporary container variable definition. i=1 # The iterator variable is initialise with the value 1 # because this one is used for string formating. while i <= basis : # Filling from the temporary container variable # with the computed polygons containing the vertice from our sphere. # Which we gonna need to compute the quads from composing the sphere . exec("polygons_array.append(polygon_{0})".format(i)) i += 1 i=0 tmp_1=[] while i < len(polygons_array) : # Iteration over the temporary polygon container variable to compute the quads. ii=0 tmp_2=[] while ii < basis-1 : # We compute the quads: from the polygons list to an quads list. if not i == basis-1 : tmp_2.append(polygons_array[i][ii]) tmp_2.append(polygons_array[i+1][ii]) tmp_2.append(polygons_array[i+1][ii+1]) tmp_2.append(polygons_array[i][ii+1]) else : tmp_2.append(polygons_array[i][ii]) tmp_2.append(polygons_array[0][ii]) tmp_2.append(polygons_array[0][ii+1]) tmp_2.append(polygons_array[i][ii+1]) ii += 1 tmp_1.append(tmp_2) i += 1 polygons_line_array=polygons_array # Affectation of the variable to return for the case of lined sphere displaying. polygons_quad_array=tmp_1 # Affectation of the variable to return for the case of surfaces sphere displaying. return (polygons_line_array,polygons_quad_array)
def generate_trigon_sphere(basis,radius) : ''' Sphere generating function which has for faces triangles and return an tuple from 2 arrays: (lined displaying vertices sequence list, surface displaying vertices sequence list). In relationship with the base for the sphere generating: faces count = basis * basis and the given sphere radius. ''' if not isinstance(basis,int) : raise TypeError(int) if basis < 8 or basis % 4 : print "the basis for the sphere miust be greater as 7 and basis % 4 == 0 " quit() if not isinstance(radius,int) and not isinstance(radius,float) : raise TypeError(int,float) # We generate the base polygon for the sphere generating with the given radius: base_polygon=generate_polygon_on_xy_radius(basis,radius,Vertex(0.0,0.0,0.0),0) polygons=[] # Polygon for trigonized sphere generating container. i=0 angle=360./basis # Computing of the degress between 2 polygons on the XZ surface. while i < basis : # Loop generating an quads sphere polygons list. tmp=[] for v in base_polygon : # Iteration over every vertice from the base polygon # and compting from the next polygon vertices rotate from # the distance between the base polygon and the next polygon. tmp.append(rotate_y(v,angle)) polygons.append(tmp) i += 1 angle += 360./basis # Distance between the base polygon and the next polygon inctrementing. boolean=False tmp_1=[] i=0 while i < len(polygons) : # Iteration over the quads sphere polygons # to compute the polygons on the XZ surface with an alternativ offset. ii=0 tmp_2=[] while ii < len(polygons[0]) : if boolean : tmp_2.append(rotate_y(polygons[ii][i],360./basis/2.)) else : tmp_2.append(polygons[ii][i]) ii += 1 if boolean : boolean=False else : boolean=True i += 1 tmp_1.append(tmp_2) tmp_2=tmp_1 i=-1 polygons_trigons_array=[] boolean=False while i < len(tmp_2)-1 : # Loop to compute the triangles from our sphere. # Throught iterating over the polygons on the XZ surface. ii=-1 while ii < len(tmp_2[i])-1 : tmp=[] if boolean : tmp.append(tmp_2[i][ii]) tmp.append(tmp_2[i][ii+1]) tmp.append(tmp_2[i+1][ii]) else : tmp.append(tmp_2[i][ii]) tmp.append(tmp_2[i][ii+1]) tmp.append(tmp_2[i+1][ii+1]) polygons_trigons_array.append(tmp) ii += 1 if boolean : boolean=False else : boolean=True i += 1 polygon_vertice_array=tmp_1 # Return an polygon array building an sphere and an array of triangles composing our trigon sphere. return (polygon_vertice_array,polygons_trigons_array)