feat: oooooh ?

This commit is contained in:
gdamms 2022-10-19 14:51:01 +02:00
parent 45c8130749
commit 1b40190198

101
main.py
View file

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