Note
Go to the end to download the full example code.
Flatten mesh using conformal mappingΒΆ
Map 3D mesh to a 2D (complex) plane with angle-preserving (conformal) mapping
Based on these course notes https://www.cs.cmu.edu/~kmcrane/Projects/DDG/ section 7.4.
import pyvista as pv
from bfieldtools._example_plots import vector_glyphs, point_cloud
pv.set_plot_theme("document")
import numpy as np
from bfieldtools.flatten_mesh import flatten_mesh
from bfieldtools.flatten_mesh import mesh2plane
from bfieldtools.flatten_mesh import plane2mesh
from bfieldtools.utils import load_example_mesh


lls = np.linspace(0.01,1.0, 100) mm = [] for ll in lls:
u, v, mesh2d = flatten_mesh(mesh, _lambda=ll) d = mesh2d.area_faces / mesh.area_faces mm.append(np.std(d)/np.mean(d)) print(np.std(d)/np.mean(d))
plt.plot(lls, mm)
plotter = plot_data_on_faces(mesh2d, mesh2d.area_faces / mesh.area_faces)

from bfieldtools.mesh_calculus import gradient
gx = gradient(u, mesh)
gy = gradient(v, mesh)
cos = np.sum(gx * gy, axis=0) / (
np.linalg.norm(gx, axis=0) * np.linalg.norm(gy, axis=0)
)
plotter = plot_data_on_faces(mesh, cos, vmin=-1, vmax=1)
plotter.add_mesh(
vector_glyphs(*mesh.triangles_center.T, *gx, mode="arrow"),
color=(1, 0, 0),
show_scalar_bar=False,
)
q = plotter.add_mesh(
vector_glyphs(*mesh.triangles_center.T, *gy, mode="arrow"),
color=(0, 0, 1),
show_scalar_bar=False,
)
plotter.view_isometric()

d = np.sqrt(3 / 4)
m = np.min((u.min(), v.min()))
mm = np.min((u.max(), v.max()))
xx = np.linspace(m * 1.05, mm * 1.05, 12)
yy = np.linspace(m * 1.05, mm * 1.05, 12) * d
p = np.array(np.meshgrid(xx, yy, 0, indexing="ij"))
p[0, :, ::2] += (xx[1] - xx[0]) * 0.5
p = p.reshape(3, -1).T
pp = plane2mesh(p, mesh, u, v)
plotter = plot_data_on_vertices(mesh, u, ncolors=15)
plotter.add_mesh(
point_cloud(*pp.T).glyph(scale=False, geom=pv.Sphere(radius=0.01 / 2)),
show_scalar_bar=False,
)
# Display the assembled scene (Sphinx-Gallery captures it during docs builds).
if "plotter" in globals() and not plotter._closed:
plotter.show()

/home/runner/work/bfieldtools.github.io/bfieldtools.github.io/examples/misc_examples/flatten_mesh.py:83: UserWarning: No vector-like data to use for orient. orient will be set to False.
point_cloud(*pp.T).glyph(scale=False, geom=pv.Sphere(radius=0.01 / 2)),
Total running time of the script: (0 minutes 6.069 seconds)