feat: select level verticies

This commit is contained in:
gdamms 2022-09-28 19:51:07 +02:00
parent a5cd9161a2
commit a8a6733d7e

View file

@ -1,48 +1,57 @@
import obja.obja
from obja import 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):
"""_summary_
Args:
obja (_type_): _description_
"""
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 corresponding 1-ring
Args:
index (int): index of the 1-ring's main vertex
Returns:
list[int]: ordered list of the 1-ring vertices
"""
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 = (
# Found the face that correspond to the next vertex
current_index = ( # select the next vertex from the face
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
@ -53,31 +62,106 @@ class MAPS(obja.Model):
return ring
def compute_area_curvature(self, index: int) -> tuple[float, float]:
""" Compute area and curvature the corresponding 1-ring
Args:
index (int): index of the 1-ring's main vertex
Returns:
tuple[float, float]: area and curvature
"""
area_sum = 0
curvature_sum = 0
one_ring_vertices = self.one_ring(index)
p1 = self.vertices[index] # the center of the one-ring
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
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 = 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 = 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
return area_sum, curvature_sum, len(one_ring_vertices)
def compute_priority(self, lamb: float = 0.5, max_length: int = 12) -> list[float]:
""" Compute selection priority of vertices (0 -> hight priority 1 -> low priority)
Args:
lamb (float, optional): convex combination factor. Defaults to 0.5.
max_length (int, optional): 1-ring maximum length to be prioritary. Defaults to 12.
Returns:
list[float]: priority values
"""
# Compute area and curvature for each vertex
areas, curvatures, ring_lengths = [], [], []
for i in range(len(self.vertices)):
area, curvature, ring_length = self.compute_area_curvature(i)
areas.append(area)
curvatures.append(curvature)
ring_lengths.append(ring_length)
# Get maxes to normalize
max_area = max(areas)
max_curvature = max(curvatures)
# Compute priorities
priorities = []
for a, k, l in zip(areas, curvatures, ring_lengths):
if l <= max_length:
# Compute priority
priority = lamb * a / max_area + (1 - lamb) * k / max_curvature
else:
# Vertex with low priority
priority = 1.0
priorities.append(priority)
return priorities
def select_vertices(self) -> list[int]:
""" Select vertices for the current level reduction
Returns:
list[int]: selected vertices
"""
remaining_faces = self.faces.copy()
# Order vertices by priority
priorities = self.compute_priority()
vertices = [i[0]
for i in sorted(enumerate(priorities), key=lambda p: p[1])]
selected_vertices = []
while len(vertices) > 0:
# Select prefered vertex
vertex = vertices.pop(0) # remove it from remaining vertices
selected_vertices.append(vertex)
# Remove neighbors
for face in remaining_faces:
face_vertices = (face.a, face.b, face.c)
if vertex in face_vertices:
# Remove face and face's vertices form remainings
remaining_faces.remove(face)
for face_vertex in face_vertices:
if face_vertex in vertices:
vertices.remove(face_vertex)
return selected_vertices
# def contract(self, output):
# """
@ -114,10 +198,11 @@ class MAPS(obja.Model):
# if ty == "vertex":
# output_model.add_vertex(index, value)
# elif ty == "face":
# output_model.add_face(index, value)
# output_model.add_face(index, value)
# else:
# output_model.edit_vertex(index, value)
def main():
"""
Runs the program on the model given as parameter.
@ -130,5 +215,5 @@ def main():
if __name__ == '__main__':
np.seterr(invalid = 'raise')
np.seterr(invalid='raise')
main()