def subdivideShapes(self, numDivision): newShapes = [] for shape in self.shapes: newShape = objct(shape.position, [], shape.matrix_stack) vertices = shape.vertices betweenlen_x = ((vertices[1].x - vertices[0].x)) / numDivision betweenlen_y = ((vertices[1].y - vertices[0].y)) / numDivision betweenlen_z = ((vertices[1].z - vertices[0].z)) / numDivision all_vertices = [] if (betweenlen_x == 0.0): betweenlen_x = ((vertices[3].x - vertices[0].x)) / numDivision if (betweenlen_y == 0.0): betweenlen_y = ((vertices[3].y - vertices[0].y)) / numDivision if (betweenlen_z == 0.0): betweenlen_z = ((vertices[3].z - vertices[0].z)) / numDivision all_vertices = self.divideXY(numDivision, betweenlen_x, betweenlen_y, vertices[0]) all_vertices = self.divideXZ(numDivision, betweenlen_x, betweenlen_z, vertices[0]) all_vertices = self.divideYZ(numDivision, betweenlen_y, betweenlen_z, vertices[0]) newShapes += self.verticesToQuads(all_vertices, shape) self.shapes = newShapes
def edges_to_shapes(self,edge_vertices,inital_shape): shapes = [] for i in range(len(edge_vertices)-1): for j in range(len(edge_vertices)-1): vertices = [edge_vertices[i][j],edge_vertices[i][j+1], edge_vertices[i+1][j+1],edge_vertices[i+1][j]] shape = objct(inital_shape.position, vertices,inital_shape.matrix_stack) shapes.append(shape) return shapes
def parse_file(self): tori_file = open(self.file_name, "r") tori_file_lines = tori_file.readlines() tori_face_vertex_indes = [] tori_vertecies = [] shapes = [] for line in tori_file_lines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[3] = line_array[3].rstrip("\n") line_array[0] = int(line_array[0]) line_array[1] = int(line_array[1]) line_array[2] = int(line_array[2]) line_array[3] = int(line_array[3]) tori_face_vertex_indes.append(line_array) xtori_face_vertex_indes = [] xtori_vertecies = [] xshapes = [] for line in tori_file_lines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[3] = line_array[3].rstrip("\n") line_array[0] = int(line_array[0]) - 1 line_array[1] = int(line_array[1]) - 1 line_array[2] = int(line_array[2]) - 1 line_array[3] = int(line_array[3]) - 1 xtori_face_vertex_indes.append(line_array) for line in tori_file_lines: if (line[0] == 'v'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = float(line_array[0]) line_array[1] = float(line_array[1]) line_array[2] = float(line_array[2]) tori_vertecies.append(line_array) tori_file.close() for face_indexes in tori_face_vertex_indes: shape = objct(vec3d(0.0, 0.0, 0.0, 1.0), [], []) for face_index in face_indexes: x = tori_vertecies[face_index - 1][0] y = tori_vertecies[face_index - 1][1] z = tori_vertecies[face_index - 1][2] vec = vec3d(x, y, z, 1) shape.vertices.append(vec) shapes.append(shape) return [shapes, tori_face_vertex_indes, tori_vertecies]
def sub_division_v2(self, numDivision): newShapes = [] for shape in self.shapes: newShape = objct(shape.position, [], shape.matrix_stack) vertices = shape.vertices betweenlen_x = ((vertices[1].x - vertices[0].x)) / numDivision betweenlen_y = ((vertices[1].y - vertices[0].y)) / numDivision betweenlen_z = ((vertices[1].z - vertices[0].z)) / numDivision vertices_edge_1 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[0], vertices[1], numDivision) betweenlen_x = ((vertices[2].x - vertices[1].x)) / numDivision betweenlen_y = ((vertices[2].y - vertices[1].y)) / numDivision betweenlen_z = ((vertices[2].z - vertices[1].z)) / numDivision vertices_edge_2 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[1], vertices[2], numDivision) betweenlen_x = ((vertices[3].x - vertices[2].x)) / numDivision betweenlen_y = ((vertices[3].y - vertices[2].y)) / numDivision betweenlen_z = ((vertices[3].z - vertices[2].z)) / numDivision vertices_edge_3 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[2], vertices[3], numDivision) betweenlen_x = ((vertices[0].x - vertices[3].x)) / numDivision betweenlen_y = ((vertices[0].y - vertices[3].y)) / numDivision betweenlen_z = ((vertices[0].z - vertices[3].z)) / numDivision vertices_edge_4 = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices[3], vertices[0], numDivision) vertices_between = [] for i in range(len(vertices_edge_2)): betweenlen_x = ((vertices_edge_2[i].x - vertices_edge_4[len(vertices_edge_2) - 1 - i].x)) / numDivision betweenlen_y = ((vertices_edge_2[i].y - vertices_edge_4[len(vertices_edge_2) - 1 - i].y)) / numDivision betweenlen_z = ((vertices_edge_2[i].z - vertices_edge_4[len(vertices_edge_2) - 1 - i].z)) / numDivision vertices_edge_middle = self.find_middle_vertices( vec3d(round(betweenlen_x, 3), round(betweenlen_y, 3), round(betweenlen_z, 3), 1), vertices_edge_4[i], vertices_edge_2[len(vertices_edge_2) - 1 - i], numDivision) vertices_between.append(vertices_edge_middle) newShapes += self.edges_to_shapes(vertices_between, shape) self.shapes = newShapes
def verticesToQuads(self,all_vertices,inital_shape): shapes = [] for i in range(len(all_vertices)-1): for j in range(len(all_vertices)-1): vertices = [all_vertices[i][j],all_vertices[i+1][j], all_vertices[i+1][j+1],all_vertices[i][j+1]] shape = objct(inital_shape.position, vertices,inital_shape.matrix_stack) shapes.append(shape) return shapes
def face_and_vertices_to_shapes(self,tori_face_vertex_indes, vertices): shapes = [] faces = [] for face_indexes in tori_face_vertex_indes: face = [face_indexes[0]+1,face_indexes[1]+1,face_indexes[2]+1,face_indexes[3]+1] faces.append(face) for face_indexes in faces: shape = objct(vec3d(0.0,0.0,0.0, 1.0),[],[]) for face_index in face_indexes: x = vertices[face_index-1][0] y = vertices[face_index-1][1] z = vertices[face_index-1][2] vec = vec3d(x,y,z,1) shape.vertices.append(vec) shapes.append(shape) return shapes
def findQuad(self, lat_angle, long_angle, lat, long, r): vertices = [] lat_step = math.pi / lat long_step = 2 * math.pi / long x = (r * math.cos(lat_angle)) * math.cos(long_angle) y = (r * math.cos(lat_angle)) * math.sin(long_angle) z = r * math.sin(lat_angle) vertx1 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle + lat_step)) * math.cos(long_angle) y = (r * math.cos(lat_angle + lat_step)) * math.sin(long_angle) z = r * math.sin(lat_angle + lat_step) vertx2 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle)) * math.cos(long_angle + long_step) y = (r * math.cos(lat_angle)) * math.sin(long_angle + long_step) z = r * math.sin(lat_angle) vertx3 = vec3d(x, y, z, 1) x = (r * math.cos(lat_angle + lat_step)) * math.cos(long_angle + long_step) y = (r * math.cos(lat_angle + lat_step)) * math.sin(long_angle + long_step) z = r * math.sin(lat_angle + lat_step) vertx4 = vec3d(x, y, z, 1) vertices.append(vertx4) vertices.append(vertx3) vertices.append(vertx1) vertices.append(vertx2) initial = mat3d([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) quad = objct(initial, vertices, []) return quad
def parse_file(self): tori_file = open(self.file_name, "r") tori_file_lines = tori_file.readlines() tori_face_vertex_indes = [] tori_vertecies = [] shapes = [] prims_shapes = [] primitivesLines = [] primitiveLines = [] primindex = 1 g_default_index = 0 for line in tori_file_lines: whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) if line_array[0] == "g" and line_array[1] == "default\n": g_default_index += 1 if primindex == g_default_index: primitiveLines.append(line) elif g_default_index > 0: primitivesLines.append(primitiveLines) primitiveLines = [] primindex += 1 primitivesLines.append(primitiveLines) for primitiveLines in primitivesLines: for line in primitiveLines: if (line[0] == 'f'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = int(line_array[0]) line_array[1] = int(line_array[1]) line_array[2] = int(line_array[2]) tori_face_vertex_indes.append(line_array) for line in tori_file_lines: if (line[0] == 'v'): whiteSpaceRegex = " " line_array = line.split(whiteSpaceRegex) line_array.pop(0) line_array[2] = line_array[2].rstrip("\n") line_array[0] = float(line_array[0]) / 2 line_array[1] = float(line_array[1]) / 2 line_array[2] = float(line_array[2]) / 2 tori_vertecies.append(line_array) tori_file.close() for face_indexes in tori_face_vertex_indes: shape = objct(vec3d(0.0, 0.0, 0.0, 1.0), [], []) for face_index in face_indexes: x = tori_vertecies[face_index - 1][0] y = tori_vertecies[face_index - 1][1] z = tori_vertecies[face_index - 1][2] vec = vec3d(x, y, z, 1) shape.vertices.append(vec) shapes.append(shape) prims_shapes.append(shapes) tori_face_vertex_indes = [] shapes = [] return prims_shapes
def create_primitive_box(self, quadlength): initial = mat3d([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) rotation_mat3d = mat3d.rotation_yz_matrix(initial, 0.01) square1 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(quadlength, quadlength, -1 * quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square2 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square3 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, quadlength, -quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square4 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, quadlength, quadlength, 1.0), vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square5 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, quadlength, 1.0), vec3d(-quadlength, -quadlength, quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square6 = objct( vec3d(0.0, 0.0, 0.0, 1.0), # Position [ # Vertices vec3d(-quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, quadlength, -quadlength, 1.0), vec3d(quadlength, -quadlength, -quadlength, 1.0), vec3d(-quadlength, -quadlength, -quadlength, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial, -1, 1.0, 0.0), rotation_mat3d, mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) prim = Primitive3D( [square1, square2, square3, square4, square5, square6]) return prim
face_list = [] for face in self.faces: index_list = [face[0]-1,face[1]-1,face[2]-1,face[3]-1] face_list.append(index_list) output_points, output_faces = self.vertices,face_list for i in range(numdivision-1): output_points, output_faces = cmc_subdiv(output_points, output_faces) self.shapes = self.face_and_vertices_to_shapes(output_faces,output_points) initial = mat3d([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]) square1 = objct(vec3d(1.1, 1.0, -8.0, 1.0), # Position [ # Vertices vec3d(-1.4, 1.0, -0.4, 1.0), vec3d(1.0, 1.0, -1.0, 1.0), vec3d(1.0, -1.0, -1.0, 1.0), vec3d(-1.4, -1.0, -0.4, 1.0), ], [ # matrix stack mat3d.translating_matrix(initial,-1, 1.0, 0.0), mat3d.rotation_zx_matrix(initial,0.01), mat3d.translating_matrix(initial,1.0, -1.0, 0.0) ])
from vec3d import vec3d from mat3d import mat3d from shape import objct import time import sys # Number of the glut window. window = 0 initial = mat3d([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) triangle = objct( vec3d(-2.7, 1, -8.0, 1.0), # Position [ # Vertices vec3d(0.0, 1.0, 0.0, 1.0), # Top vec3d(1.0, -1.0, 0.0, 1.0), # Bottom Right vec3d(-1.0, -1.0, 0.0, 1.0), # Bottom Left ], [ # matrix stack mat3d.translating_matrix(initial, -1.0, 1.0, 0.0), mat3d.rotation_xy_matrix(initial, 0.02), mat3d.translating_matrix(initial, 1.0, -1.0, 0.0) ]) square = objct( vec3d(1.1, 1.0, -8.0, 1.0), # Position [ # Vertices vec3d(-1.0, 1.0, 0.0, 1.0), # Top Left vec3d(1.0, 1.0, 0.0, 1.0), # Top Right vec3d(1.0, -1.0, 0.0, 1.0), # Bottom Right vec3d(-1.0, -1.0, 0.0, 1.0) # Bottom Left ], [ # matrix stack