From a5cd9161a26eb9ca98cdf06af3df7b1eb7648f52 Mon Sep 17 00:00:00 2001 From: gdamms Date: Wed, 28 Sep 2022 15:54:30 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20first=20s=C3=A9ance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.py | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 src/main.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..ea735f2 --- /dev/null +++ b/src/main.py @@ -0,0 +1,134 @@ +import obja.obja +import numpy as np +import itertools + +def sliding_window(iterable, n=2): + iterables = itertools.tee(iterable, n) + + for iterable, num_skipped in zip(iterables, itertools.count()): + for _ in range(num_skipped): + next(iterable, None) + + return zip(*iterables) + + +class MAPS(obja.Model): + """ + A simple class that decimates a 3D model stupidly. + """ + def __init__(self): + super().__init__() + self.deleted_faces = set() + + def one_ring(self, index: int) -> list[int]: + """ + Return the vertex indices of the corresponding 1-ring. + """ + + # Find the 1-ring faces + ring_faces = [] + for face in enumerate(self.faces): + if index in (face.a, face.b, face.c): + ring_faces.append(face) + + # Initialize the ring + start_index = ring_faces[0][0] if ring_faces[0][0] != index else ring_faces[0][1] + ring = [start_index] + ring_faces.pop(0) + + # Select the indexes of the ring in the right order + while len(ring_faces) > 0: + prev_index = ring[-1] + for i, face in enumerate(ring_faces): + if prev_index in (face.a, face.b, face.c): + # if the previous index is in the current face + current_index = ( + face.a if face.a != index and face.a != prev_index else + face.b if face.b != index and face.b != prev_index else + face.c + ) + ring.append(current_index) + ring_faces.pop(i) + break + + return ring + + + def compute_area_curvature(self, index: int) -> tuple[float, float]: + area_sum = 0 + curvature_sum = 0 + one_ring_vertices = self.one_ring(index) + + p1 = self.vertices[index] # the center of the one-ring + for index2, index3 in sliding_window(one_ring_vertices, 2): + p2 = self.vertices[index2] # the second vertice of the triangle + p3 = self.vertices[index3] # the third vertice of the triangle + M = np.array([ # build the matrix, used to compute the area + [p1[0], p2[0], p3[0]], + [p1[1], p2[1], p3[1]], + [p1[2], p2[2], p3[2]], + ]) + area = np.linalg.det(M) / 2 # compute the area + area_sum += area + + curvature = 4 * area / ( # compute the curvature + np.linalg.norm(p1-p2) * np.linalg.norm(p2-p3) * np.linalg.norm(p3-p1) + ) + curvature_sum += curvature + + return area_sum, curvature_sum + + + # def contract(self, output): + # """ + # Decimates the model stupidly, and write the resulting obja in output. + # """ + # operations = [] + + # for (vertex_index, vertex) in enumerate(self.vertices): + # operations.append(('ev', vertex_index, vertex + 0.25)) + + # # Iterate through the vertex + # for (vertex_index, vertex) in enumerate(self.vertices): + + # # Iterate through the faces + # for (face_index, face) in enumerate(self.faces): + + # # Delete any face related to this vertex + # if face_index not in self.deleted_faces: + # if vertex_index in [face.a,face.b,face.c]: + # self.deleted_faces.add(face_index) + # # Add the instruction to operations stack + # operations.append(('face', face_index, face)) + + # # Delete the vertex + # operations.append(('vertex', vertex_index, vertex)) + + # # To rebuild the model, run operations in reverse order + # operations.reverse() + + # # Write the result in output file + # output_model = obja.Output(output, random_color=True) + + # for (ty, index, value) in operations: + # if ty == "vertex": + # output_model.add_vertex(index, value) + # elif ty == "face": + # output_model.add_face(index, value) + # else: + # output_model.edit_vertex(index, value) + +def main(): + """ + Runs the program on the model given as parameter. + """ + model = MAPS() + model.parse_file('example/suzanne.obj') + + # with open("example/suzanne.obja", "w") as output: + # model.contract(output) + + +if __name__ == '__main__': + np.seterr(invalid = 'raise') + main()