diff --git a/main.py b/main.py index 856bc03..6445def 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,10 @@ +import enum import io -from math import floor import obja.obja as obja import numpy as np import argparse -from rich.progress import track, Progress +from rich.progress import Progress def cot(x: float): @@ -52,7 +52,7 @@ class Face: def __eq__(self, __o: object) -> bool: if isinstance(__o, Face): - return self.a == __o.a and self.b == __o.b and self.c == __o.c + return set((__o.a, __o.b, __o.c)) == set((self.a, self.b, self.c)) return False @@ -98,6 +98,19 @@ class MAPS(obja.Model): self.update_normals() self.update_area_curvature() + def fix(self): + fixed = True + for i, vertex in enumerate(self.vertices): + if vertex is None: + continue + if len(vertex.face_ring) < 3: + for face in vertex.face_ring: + self.faces[face] = None + self.vertices[i] = None + fixed = False + return fixed + + def update_edges(self): self.edges = {} @@ -125,23 +138,56 @@ class MAPS(obja.Model): self.edges[f"{new_edge.a}:{new_edge.b}"] = new_edge def update_rings(self): - for vertex in self.vertices: - if vertex is None: - continue - vertex.face_ring = [] + try: + fixed = False + while not fixed: + for vertex in self.vertices: + if vertex is None: + continue + vertex.face_ring = [] - for i, face in enumerate(self.faces): - if face is None: - continue - for vertex_i in (face.a, face.b, face.c): - self.vertices[vertex_i].face_ring.append(i) + for i, face in enumerate(self.faces): + if face is None: + continue + for vertex_i in (face.a, face.b, face.c): + self.vertices[vertex_i].face_ring.append(i) + + fixed = self.fix() - for i, vertex in enumerate(self.vertices): - vertex = self.vertices[i] - if vertex is None: - continue - ring = self.one_ring(i) - vertex.vertex_ring = ring + for i, vertex in enumerate(self.vertices): + vertex = self.vertices[i] + if vertex is None: + continue + if len(vertex.face_ring) == 0: + self.vertices[i] = None + continue + ring = self.one_ring(i) + vertex.vertex_ring = ring + except ValueError: + self.update_rings() + + + def fail(self, index): + print('fail') + output_file = open('obja/example/fail.obja', 'w') + output = obja.Output(output_file) + used = [] + for i, x in enumerate(self.vertices[index].face_ring): + face = self.faces[x] + for y in (face.a, face.b, face.c): + if y in used: + continue + output.add_vertex(y, self.vertices[y].to_obja()) + used.append(y) + output.add_face(x, face.to_obja()) + print('fc {} {} {} {}'.format( + i + 1, + np.random.rand(), + np.random.rand(), + np.random.rand()), + file=output_file + ) + print(x, (face.a, face.b, face.c)) def update_area_curvature(self): for i, vertex in enumerate(self.vertices): @@ -154,6 +200,8 @@ class MAPS(obja.Model): self.feature_edges = [] for edge in self.edges.values(): + if edge.face2 is None: + self.fail(edge.b) edge.fold = np.dot(edge.face1.normal, edge.face2.normal) if edge.fold < 0.5: @@ -223,24 +271,32 @@ class MAPS(obja.Model): break if not broke: - output_file = open('obja/example/fail.obja', 'w') - output = obja.Output(output_file) - used = [] - for i, x in enumerate(self.vertices[index].face_ring): - face = self.faces[x] - for y in (face.a, face.b, face.c): - if y in used: - continue - output.add_vertex(y, self.vertices[y].to_obja()) - used.append(y) - output.add_face(x, face.to_obja()) - print('fc {} {} {} {}'.format( - i + 1, - np.random.rand(), - np.random.rand(), - np.random.rand()), - file=output_file - ) + self.fail(index) + + for i, face_i in enumerate(self.vertices[index].face_ring): + for face_j in self.vertices[index].face_ring[i+1:]: + face1 = self.faces[face_i] + face2 = self.faces[face_j] + if face1 == face2: + self.faces[face_i] = None + self.faces[face_j] = None + verts = (face1.a, face1.b, face1.c) + for vert in verts: + if vert == index: + continue + to_remove = True + for face_k in self.vertices[vert].face_ring: + face = self.faces[face_k] + if face is None: + continue + if vert in (face.a, face.b, face.c): + to_remove = False + break + if to_remove: + self.vertices[vert] = None + break + break + raise ValueError( f"Vertex {prev_index} is not in the remaining faces {ring_faces}. Origin {ring} on {index}") @@ -288,7 +344,7 @@ class MAPS(obja.Model): return area_sum, curvature - def compute_priority(self, lamb: float = 0.0, max_length: int = 12) -> list[float]: + def compute_priority(self, lamb: float = 0.5, max_length: int = 12) -> list[float]: """ Compute selection priority of vertices (0.0 -> hight priority ; 1.0 -> low priority) Args: @@ -698,7 +754,10 @@ class MAPS(obja.Model): if op == 'av': output_model.add_vertex(index, value) elif op == 'af': - output_model.add_face(index, value) + try: + output_model.add_face(index, value) + except: + print(self.vertices[value.b]) elif op == 'ev': output_model.edit_vertex(index, value) elif op == 'ef':