[GUDHI] Basics – Mesh creation

发布于 2022-09-07  670 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

根据点和连接性创建网格实例

指定点坐标和连接性

import gudhi as gd
import numpy as np

# Coordinates of the points
points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1], [1, 1, 0], [0, 1, 1]])

# Build the simplicial complex with a tetrahedon, an edge and an isolated vertex
cplx = gd.SimplexTree()
cplx.insert([1, 2, 3, 5])   # tetrahedron
cplx.insert([4, 6])       # edge
cplx.insert([0])      # vertex

此时生成的SimplexTree对象cplx即为网格对象。

SimplexTree对象中获取三角形面和边

# List of triangles (point indices)
triangles = np.array([s[0] for s in cplx.get_skeleton(2) if len(s[0]) == 3])

# List of edges (point coordinates)
edges = []
for s in cplx.get_skeleton(1):
    e = s[0]
    if len(e) == 2:
        edges.append(points[[e[0], e[1]]])

网格可视化:

## With plotly
import plotly.graph_objects as go

# Plot triangles
f2 = go.Mesh3d(
    x=points[:, 0],
    y=points[:, 1],
    z=points[:, 2],
    i=triangles[:, 0],
    j=triangles[:, 1],
    k=triangles[:, 2],
)
# Plot points
f0 = go.Scatter3d(x=points[:, 0], y=points[:, 1], z=points[:, 2], mode="markers")
data = [f2, f0]
# Plot edges
for pts in edges:
    seg = go.Scatter3d(x=pts[:, 0], y=pts[:, 1], z=pts[:, 2], mode="lines", line=dict(color='green'))
    data.append(seg)
fig = go.Figure(data=data, layout=dict(showlegend=False))
# By default plotly would give each edge its own color and legend, that's too much
fig.show()

## With matplotlib
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')
# Plot triangles
ax.plot_trisurf(points[:, 0], points[:, 1], points[:, 2], triangles=triangles)
# Plot points
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
# Plot edges
ax.add_collection3d(Line3DCollection(segments=edges))
plt.show()

Edge collapse of 3D SURFACE mesh

print("#####################################################################")
print("RipsComplex (only the one-skeleton) creation from tore3D_300.off file")

off_file = './tore3D_300.off'
point_cloud = gudhi.read_points_from_off_file(off_file = off_file)
rips_complex = gudhi.RipsComplex(points=point_cloud, max_edge_length=12.0)
simplex_tree = rips_complex.create_simplex_tree(max_dimension=1)
print('1. Rips complex is of dimension ', simplex_tree.dimension(), ' - ',
      simplex_tree.num_simplices(), ' simplices - ',
      simplex_tree.num_vertices(), ' vertices.')

# Expansion of this one-skeleton would require a lot of memory. Let's collapse it
start = time.process_time()
simplex_tree.collapse_edges()
print('2. Rips complex is of dimension ', simplex_tree.dimension(), ' - ',
      simplex_tree.num_simplices(), ' simplices - ',
      simplex_tree.num_vertices(), ' vertices.')

simplex_tree.expansion(3)
diag = simplex_tree.persistence()
print("Collapse, expansion and persistence computation took ", time.process_time() - start, " sec.")

# Use subplots to display diagram and density side by side
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 5))
gudhi.plot_persistence_diagram(diag, axes=axes[0])
axes[0].set_title("Persistence after 1 collapse")

# Collapse can be performed several times. Let's collapse it 3 times
start = time.process_time()
simplex_tree.collapse_edges(nb_iterations = 3)
print('3. Rips complex is of dimension ', simplex_tree.dimension(), ' - ',
      simplex_tree.num_simplices(), ' simplices - ',
      simplex_tree.num_vertices(), ' vertices.')
simplex_tree.expansion(3)
diag = simplex_tree.persistence()
print("Collapse, expansion and persistence computation took ", time.process_time() - start, " sec.")

gudhi.plot_persistence_diagram(diag, axes=axes[1])
axes[1].set_title("Persistence after 3 more collapses")

# Plot the 2 persistence diagrams side to side to check the persistence is the same
plt.show()

The example dataset can be found here (右键另存为).

Edge collapse的另一个例子

#!/usr/bin/env python

from gudhi.datasets.generators import _points
from gudhi import AlphaComplex
import numpy as np
import gudhi, meshio, time
import matplotlib.pyplot as plt

print("AlphaComplex creation from generated points on sphere")

points = _points.sphere(n_samples=500, ambient_dim=3, radius=1, sample="random")

# Create an alpha complex
alpha_complex = AlphaComplex(points=points)
simplex_tree = alpha_complex.create_simplex_tree()

triangles = np.array([s[0] for s in simplex_tree.get_skeleton(2) if len(s[0]) == 3])

result_str = 'Alpha complex is of dimension ' + repr(simplex_tree.dimension()) + ' - ' + \
             repr(simplex_tree.num_simplices()) + 'simple - ' + \
             repr(simplex_tree.num_vertices()) + 'vertices.'
print(result_str)

# write to vtk
cells = [("triangle", triangles)]
mesh = meshio.Mesh(points, cells)
meshio.write("sphere.vtk", mesh)

# Expansion of this one-skeleton would require a lot of memory. Let's collapse it
start = time.process_time()
simplex_tree.collapse_edges(nb_iterations=50)
print('2. Rips complex is of dimension ', simplex_tree.dimension(), ' - ',
      simplex_tree.num_simplices(), ' simplices - ',
      simplex_tree.num_vertices(), ' vertices.')

simplex_tree.expansion(50)
diag = simplex_tree.persistence()
print("Collapse, expansion and persistence computation took ", time.process_time() - start, " sec.")

# Use subplots to display diagram and density side by side
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(8, 5))
gudhi.plot_persistence_diagram(diag, axes=axes)
axes.set_title("Persistence after 1 collapse")
plt.show()

points = points[np.array([v[0] for v in simplex_tree.get_skeleton(0) if len(v[0]) == 1], dtype=np.int32).flatten(), :]
triangles = np.array([s[0] for s in simplex_tree.get_skeleton(2) if len(s[0]) == 3], dtype=np.int32)

# write to vtk
cells = [("triangle", list(triangles)), ]
mesh = meshio.Mesh(points, cells)
meshio.write("sphere_collapse.vtk", mesh, binary=True)
Everything not saved will be lost.
最后更新于 2022-09-07