Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Last active June 3, 2024 14:24
Show Gist options
  • Save pgtwitter/f17da551eb6dfe749d058cd1559b36b9 to your computer and use it in GitHub Desktop.
Save pgtwitter/f17da551eb6dfe749d058cd1559b36b9 to your computer and use it in GitHub Desktop.
フィードラーベクトルの応用 ( bun_zipper.ply は https://graphics.stanford.edu/data/3Dscanrep/ から)
# %%
import open3d as o3d
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# %%
mesh = o3d.io.read_triangle_mesh("bun_zipper.ply")
coordinates = mesh.vertices
edges = []
for tri in mesh.triangles:
edges.append((tri[0], tri[1]))
edges.append((tri[1], tri[2]))
edges.append((tri[2], tri[0]))
G = nx.Graph(edges)
S = [G.subgraph(c).copy() for c in nx.connected_components(G)]
G = S[0]
coordinates = [coordinates[i] for i in G.nodes]
xyz = np.array(coordinates).T
fiedler_vector = nx.fiedler_vector(G)
minV = min(fiedler_vector)
maxV = max(fiedler_vector)
w = maxV - minV
gscale = [(v - minV)/w for v in fiedler_vector]
def f2c(vs, t, dt):
return ['lightgreen' if (t <= v and v <= t+dt) else 'blue' for v in vs]
def f2s(vs, t, dt):
return np.array([1 if (t <= v and v <= t+dt) else 0.005 for v in vs])
# %%
dt = 1/45
plt.style.use('dark_background')
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_proj_type('persp', focal_length=0.2)
ax.set_axis_off()
ax.view_init(elev=0, azim=0, roll=90)
scat = ax.scatter(xyz[0], xyz[1], xyz[2],
s=f2s(gscale, 0, dt),
c=f2c(gscale, 0, dt))
def update(frame):
ax.view_init(elev=frame*4, azim=0, roll=90)
x = frame/90
t = 2 * (x if frame < 45 else 1-x)
scat.set_sizes(f2s(gscale, t, dt))
scat.set_color(f2c(gscale, t, dt))
anim = animation.FuncAnimation(fig=fig, func=update, frames=90)
anim.save('output.mp4', writer=animation.FFMpegWriter(fps=30))
plt.show()
@pgtwitter
Copy link
Author

mymovie.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment