feat: select level verticies
This commit is contained in:
parent
a5cd9161a2
commit
a8a6733d7e
105
src/main.py
105
src/main.py
|
@ -1,7 +1,8 @@
|
||||||
import obja.obja
|
from obja import obja
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
|
|
||||||
def sliding_window(iterable, n=2):
|
def sliding_window(iterable, n=2):
|
||||||
iterables = itertools.tee(iterable, n)
|
iterables = itertools.tee(iterable, n)
|
||||||
|
|
||||||
|
@ -13,16 +14,24 @@ def sliding_window(iterable, n=2):
|
||||||
|
|
||||||
|
|
||||||
class MAPS(obja.Model):
|
class MAPS(obja.Model):
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obja (_type_): _description_
|
||||||
"""
|
"""
|
||||||
A simple class that decimates a 3D model stupidly.
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.deleted_faces = set()
|
self.deleted_faces = set()
|
||||||
|
|
||||||
def one_ring(self, index: int) -> list[int]:
|
def one_ring(self, index: int) -> list[int]:
|
||||||
"""
|
""" Return the corresponding 1-ring
|
||||||
Return the vertex indices of 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
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Find the 1-ring faces
|
# Find the 1-ring faces
|
||||||
|
@ -41,8 +50,8 @@ class MAPS(obja.Model):
|
||||||
prev_index = ring[-1]
|
prev_index = ring[-1]
|
||||||
for i, face in enumerate(ring_faces):
|
for i, face in enumerate(ring_faces):
|
||||||
if prev_index in (face.a, face.b, face.c):
|
if prev_index in (face.a, face.b, face.c):
|
||||||
# if the previous index is in the current face
|
# Found the face that correspond to the next vertex
|
||||||
current_index = (
|
current_index = ( # select the next vertex from the face
|
||||||
face.a if face.a != index and face.a != prev_index else
|
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.b if face.b != index and face.b != prev_index else
|
||||||
face.c
|
face.c
|
||||||
|
@ -53,8 +62,15 @@ class MAPS(obja.Model):
|
||||||
|
|
||||||
return ring
|
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
|
||||||
|
|
||||||
|
Args:
|
||||||
|
index (int): index of the 1-ring's main vertex
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple[float, float]: area and curvature
|
||||||
|
"""
|
||||||
area_sum = 0
|
area_sum = 0
|
||||||
curvature_sum = 0
|
curvature_sum = 0
|
||||||
one_ring_vertices = self.one_ring(index)
|
one_ring_vertices = self.one_ring(index)
|
||||||
|
@ -72,12 +88,80 @@ class MAPS(obja.Model):
|
||||||
area_sum += area
|
area_sum += area
|
||||||
|
|
||||||
curvature = 4 * area / ( # compute the curvature
|
curvature = 4 * area / ( # compute the curvature
|
||||||
np.linalg.norm(p1-p2) * np.linalg.norm(p2-p3) * np.linalg.norm(p3-p1)
|
np.linalg.norm(p1-p2) *
|
||||||
|
np.linalg.norm(p2-p3) *
|
||||||
|
np.linalg.norm(p3-p1)
|
||||||
)
|
)
|
||||||
curvature_sum += curvature
|
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):
|
# def contract(self, output):
|
||||||
# """
|
# """
|
||||||
|
@ -118,6 +202,7 @@ class MAPS(obja.Model):
|
||||||
# else:
|
# else:
|
||||||
# output_model.edit_vertex(index, value)
|
# output_model.edit_vertex(index, value)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""
|
"""
|
||||||
Runs the program on the model given as parameter.
|
Runs the program on the model given as parameter.
|
||||||
|
|
Loading…
Reference in a new issue