def optimize_piece(piece): remap = {} new_indices = [] print 'optimizing:', piece.name for index in piece.indices: vertex = piece.vertices[index] if vertex not in remap: remap[vertex] = len(remap) new_indices.append(remap[vertex]) new_vertices = [(index, vertex) for vertex, index in remap.items()] new_vertices.sort() new_vertices = [vertex for index, vertex in new_vertices] if piece.primitive_type == "triangles" and len(new_indices) > 0: tris = list(chunks(new_indices, 3)) acmr = vertex_cache.average_transform_to_vertex_ratio(tris) tmp = vertex_cache.get_cache_optimized_triangles(tris) acmr_new = vertex_cache.average_transform_to_vertex_ratio(tmp) if acmr_new < acmr: new_indices = [] for tri in tmp: new_indices.extend(tri) vertex_map = [] remapped_indices = [] for index in new_indices: try: new_index = vertex_map.index(index) except ValueError: new_index = len(vertex_map) vertex_map.append(index) remapped_indices.append(new_index) new_vertices = [new_vertices[index] for index in vertex_map] new_indices = remapped_indices piece.indices = new_indices piece.vertices = new_vertices
def optimize_piece(piece): remap = {} new_indices = [] for index in piece.indices: vertex = piece.vertices[index] if vertex not in remap: remap[vertex] = len(remap) new_indices.append(remap[vertex]) new_vertices = [(index, vertex) for vertex, index in remap.items()] new_vertices.sort() new_vertices = [vertex for index, vertex in new_vertices] if piece.primitive_type == "triangles" and len(new_indices) > 0: tris = list(chunks(new_indices, 3)) acmr = vertex_cache.average_transform_to_vertex_ratio(tris) tmp = vertex_cache.get_cache_optimized_triangles(tris) acmr_new = vertex_cache.average_transform_to_vertex_ratio(tmp) if acmr_new < acmr: new_indices = [] for tri in tmp: new_indices.extend(tri) vertex_map = [] remapped_indices = [] for index in new_indices: try: new_index = vertex_map.index(index) except ValueError: new_index = len(vertex_map) vertex_map.append(index) remapped_indices.append(new_index) new_vertices = [new_vertices[index] for index in vertex_map] new_indices = remapped_indices piece.indices = new_indices piece.vertices = new_vertices