feat: faster

This commit is contained in:
gdamms 2022-10-17 16:27:49 +02:00
parent 0c79065d11
commit dff7ae111e

52
main.py
View file

@ -89,48 +89,56 @@ class MAPS(obja.Model):
self.faces[i] = Face(face.a, face.b, face.c) self.faces[i] = Face(face.a, face.b, face.c)
def update(self): def update(self):
self.update_edges()
self.update_rings() self.update_rings()
self.update_edges()
self.update_normals() self.update_normals()
self.update_area_curvature() self.update_area_curvature()
def update_edges(self): def update_edges(self):
self.edges = {} self.edges = {}
remaining_faces = self.faces.copy()
while None in remaining_faces:
remaining_faces.remove(None)
for face in track(self.faces, description='Update edges'): for face in track(self.faces, description='Update edges'):
if face is None: if face is None:
continue continue
for a, b in sliding_window([face.a, face.b, face.c], n=2): for a, b in sliding_window([face.a, face.b, face.c], n=2):
new_edge = Edge(a, b) new_edge = Edge(a, b)
if self.edges.get(f"{new_edge.a}:{new_edge.b}") is None: if f"{new_edge.a}:{new_edge.b}" not in self.edges.keys():
new_edge.face1 = face new_edge.face1 = face
if face in remaining_faces: for face2_i in self.vertices[new_edge.a].face_ring:
remaining_faces.remove(face) face2 = self.faces[face2_i]
if face2 == face:
continue
for face2 in remaining_faces:
face2_vertices = (face2.a, face2.b, face2.c) face2_vertices = (face2.a, face2.b, face2.c)
if not (a in face2_vertices and b in face2_vertices): if not (a in face2_vertices and b in face2_vertices):
continue continue
new_edge.face2 = face2 new_edge.face2 = face2
break break
if new_edge.face2 is None:
print('ooooooooooooooooooooooo')
self.edges[f"{new_edge.a}:{new_edge.b}"] = new_edge self.edges[f"{new_edge.a}:{new_edge.b}"] = new_edge
def update_rings(self): def update_rings(self):
for i, vertex in enumerate(self.vertices): for vertex in self.vertices:
if vertex is None: if vertex is None:
continue continue
vertex.face_ring = []
vertex_ring, face_ring = self.one_ring(i) for face_i in track(range(len(self.faces)), description='Finding rings'):
vertex.vertex_ring = vertex_ring face = self.faces[face_i]
vertex.face_ring = face_ring if face is None:
continue
for vertex_i in (face.a, face.b, face.c):
self.vertices[vertex_i].face_ring.append(face_i)
for vertex_i in track(range(len(self.vertices)), description='Truc'):
vertex = self.vertices[vertex_i]
if vertex is None:
continue
ring = self.one_ring(vertex_i)
vertex.vertex_ring = ring
def update_area_curvature(self): def update_area_curvature(self):
for i, vertex in enumerate(self.vertices): for i, vertex in enumerate(self.vertices):
@ -175,7 +183,7 @@ class MAPS(obja.Model):
if norm != 0: if norm != 0:
vertex.normal /= norm vertex.normal /= norm
def one_ring(self, index: int) -> tuple[list[int], list[int]]: def one_ring(self, index: int) -> list[int]:
""" Return the corresponding 1-ring """ Return the corresponding 1-ring
Args: Args:
@ -187,15 +195,7 @@ class MAPS(obja.Model):
if self.vertices[index] is None: if self.vertices[index] is None:
return None, None return None, None
# Find the 1-ring faces ring_faces = [self.faces[i] for i in self.vertices[index].face_ring]
ring_faces, ring_face_indices = [], []
for face_index, face in enumerate(self.faces):
if face is None:
continue
if index in (face.a, face.b, face.c):
ring_faces.append(face)
ring_face_indices.append(face_index)
# Initialize the ring # Initialize the ring
start_index = (ring_faces[0].a if ring_faces[0].a != index and ring_faces[0].c != index else start_index = (ring_faces[0].a if ring_faces[0].a != index and ring_faces[0].c != index else
@ -225,7 +225,7 @@ class MAPS(obja.Model):
raise ValueError( raise ValueError(
f"Vertex {prev_index} is not in the remaining faces {ring_faces}. Origin {ring} on {index}") f"Vertex {prev_index} is not in the remaining faces {ring_faces}. Origin {ring} on {index}")
return ring, ring_face_indices return ring
def compute_area_curvature(self, index: int) -> tuple[float, float]: def compute_area_curvature(self, index: int) -> tuple[float, float]:
""" Compute area and curvature the corresponding 1-ring """ Compute area and curvature the corresponding 1-ring