def move_2_to_2(cmpx): """ Applies a 2->2 move to the input complex, cmpx. Input must be of type complex22. """ # Declare local constants triangle_size = 3 # The number of veritces in a triangle complex_size = 2 # The complex has 2 triangles number_of_shared_vertices = 2 # The complex has 2 shared vertices number_of_unshared_vertices = 2 # The complex has 2 unshared vertices total_number_of_vertices = 4 # There should be a total of 4 # vertices in the complex. # Extract the triangles. Don't need error checking because if the # complexd is not correct, we won't be able to extract the shared # and unshared vertices. triangles = [sd.triangle.parse_input(t) for t in cmpx.get_triangles()] # Extract the shared_vertices shared_vertices = cmpx.get_shared_vertices() # Extract the unshared vertices unshared_vertices = cmpx.get_unshared_vertices() # Tiny bit of error checking just in caes assert len(triangles) == complex_size assert len(shared_vertices) == number_of_shared_vertices assert len(unshared_vertices) == number_of_unshared_vertices assert len(set(shared_vertices+unshared_vertices)) \ == total_number_of_vertices # The edge shared by the two shared vertices. If it doesn't # exist, something went very wrong! if sd.edge.exists(shared_vertices): shared_edge = sd.edge.exists(shared_vertices)[0] else: raise ValueError("The pair of vertices you gave me are not " + "endpoints of an edge!") # Calculate the vertices for the new triangles new_triangles = [set(unshared_vertices+[s]) for s in shared_vertices \ if len(set(unshared_vertices+[s])) == triangle_size] # Tiny bit of error checking just in case. The number of new # triangles should be the same as the number of old triangles. assert len(new_triangles) == complex_size # Remove the old triangles for t in triangles: sm.remove_triangle(t) # Remove the old edge we no longer need. sm.remove_edge(shared_edge) # With the old geometric constructs out of the way, make the new one! new_triangles = [sm.build_triangle_and_edges(point_list) \ for point_list in new_triangles] # Connect triangles and connect the triangles to their respective edges changed_vertices = shared_vertices + unshared_vertices set_neighbors_and_triangles(changed_vertices,new_triangles) return True
def move_3_to_1(cmpx): "Applies a 3->1 move to the input complex, cmpx." # Extract the complex, which contains three triangles. if len(cmpx.get_triangles()) == 3: original_triangles = [sd.triangle.instances[i] \ for i in cmpx.get_triangles()] else: raise ValueError("There should be exactly 3 triangles for the " + "3->1 complex.") # Extract the boundary vertices original_vertices = [set(t.get_vertices()) for t in original_triangles] central_vertex = list(ut.set_intersection(original_vertices))[0] boundary_vertices = ut.set_union(original_vertices) \ - set([central_vertex]) boundary_vertex_ids = [v.get_id() for v in boundary_vertices] # Extract the edges to be removed (there are 3) # Lambda functions for clarity get_edges = lambda i: set(sd.triangle.instances[i].get_edges()) intersected_element = lambda L: ut.only_element(ut.set_intersection(L)) # Nested list comprehensions: take the intersection of the set of # edges associated with pairs of the triangles we care # about. These edges are flagged for deleteion. shared_edges = [intersected_element([get_edges(i) for i in c]) \ for c in ut.k_combinations(cmpx.get_triangles())] # There should only be 3 shared edges. If there are more, we messed up. assert len(shared_edges) == 3 # Make the new triangle new_triangle = sm.build_triangle_and_edges(boundary_vertex_ids) # Clean up for t in original_triangles: # Delete the old triangles: sm.remove_triangle(t) for e in shared_edges: # Delete the old edges. sm.remove_edge(e) sd.vertex.delete(central_vertex) # Delete the old vertex # Set triangles, neihgbors, and check topology set_neighbors_and_triangles(boundary_vertices,[new_triangle]) return True