from deepnote_settings import DeepNoteSettings
DeepNoteSettings = DeepNoteSettings()
DeepNoteSettings.install_req()
from pygel3d import hmesh, jupyter_display as jd
from numpy import zeros
from numpy.linalg import det
jd.set_export_mode(True)
def mesh_stats(m):
print("# faces : ", m.no_allocated_faces(), " # vertices : ", m.no_allocated_vertices())
DeepNoteSettings.set_plotting()
def smooth(m, max_iter=1):
"""
Smoothes each by averaging the positions of the neighbors
"""
pos = m.positions()
new_pos = zeros(pos.shape)
for iter in range(0,max_iter):
for vertex in m.vertices():
neighbors = list(m.circulate_vertex(vertex, "v"))
new_pos[vertex] = pos[neighbors].mean(axis=0)
pos[:] = new_pos
return m
def calculate_volume(m):
pos = m.positions()
vol = 0
for f in m.faces():
p = pos[m.circulate_face(f)].T # transpose to get one vertex per column
vol += 1/6 * det(p)
return abs(vol)
# check implementation
test = hmesh.load("test.obj") # load test file of box with volume 1
mesh_stats(test)
jd.display(test)
assert abs(calculate_volume(test) - 1) < 1e-6, "area of box != 1"
def dual(m):
"""
Create dual by creating a new vertex in the center of each face and connecting vertices based on which faces they shared a border with in the original mesh
"""
m2 = hmesh.Manifold()
# go through all vertices and create a face based on the center of the faces that are connected to it
for v in m.vertices():
m2.add_face([m.centre(f) for f in m.circulate_vertex(v, "f")])
return m2
m = hmesh.load("bunnygtest.obj")
mesh_stats(m)
print("Volume: ", calculate_volume(m))
jd.display(m)
m_s = hmesh.Manifold(m)
m_s = smooth(m_s,50)
print("Volume: ", calculate_volume(m_s))
jd.display(m_s)
print("Stats for original:")
mesh_stats(m)
m_d = dual(m)
print("Stats for dual:")
mesh_stats(m_d)
jd.display(m_d)