.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_01_load_mesh.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_01_load_mesh.py: Loading and visualizing a mesh ============================== The shortest possible pyFM session: read a triangle mesh from disk, inspect its properties. Every 3D plot below is interactive. .. GENERATED FROM PYTHON SOURCE LINES 11-15 Locating data ------------- The example meshes are stored in ``examples/data`` and are not part of the package. .. GENERATED FROM PYTHON SOURCE LINES 15-35 .. code-block:: Python from pathlib import Path from pyFM.mesh import TriMesh from pyFM.viz import plot_mesh def example_data(name): """Return the path to a mesh in the ``examples/data`` folder.""" for folder in [Path.cwd(), *Path.cwd().parents]: candidate = folder / "examples" / "data" / name if candidate.exists(): return candidate raise FileNotFoundError( f"{name!r} not found. These examples read data from the pyFM repository; " "clone it and run them there." ) .. GENERATED FROM PYTHON SOURCE LINES 37-44 Reading a mesh -------------- :meth:`~pyFM.mesh.trimesh.TriMesh.load` reads a ``.off``, ``.obj`` or ``.ply`` file. ``area_normalize`` rescales the mesh to unit area and ``center`` moves its center of mass to the origin. This is usually desirable. A shortcut with the same effect is the ``normalize`` argument. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python mesh = TriMesh.load(example_data("cat.obj"), area_normalize=True, center=True) print(f"{mesh.n_vertices} vertices, {mesh.n_faces} faces") print(f"total area: {mesh.area:.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none 7207 vertices, 14410 faces total area: 1.000000 .. GENERATED FROM PYTHON SOURCE LINES 51-54 Vertices and faces are plain numpy arrays, available as ``vertices`` and ``faces``. Other geometric quantities such as normals, face areas, edges, the Laplacian, ... are computed the first time you ask for them, and stored afterwards. .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: Python print("vertices:", mesh.vertices.shape, mesh.vertices.dtype) print("faces :", mesh.faces.shape, mesh.faces.dtype) print("face areas:", mesh.face_areas.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none vertices: (7207, 3) float64 faces : (14410, 3) int64 face areas: (14410,) .. GENERATED FROM PYTHON SOURCE LINES 60-65 Visualizing ----------- ``pyFM.viz.plot_mesh`` renders a mesh. It is part of the optional ``viz`` extra (``pip install 'pyfmaps[viz]'``), which is built on PyVista. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python plot_mesh(mesh) .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /auto_examples/images/sphx_glr_plot_01_load_mesh_001.png :alt: plot 01 load mesh :srcset: /auto_examples/images/sphx_glr_plot_01_load_mesh_001.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyFM/pyFM/docs/source/auto_examples/images/sphx_glr_plot_01_load_mesh_001.vtksz .. GENERATED FROM PYTHON SOURCE LINES 69-76 Coloring by a scalar field -------------------------- Any per-vertex array can be passed as ``scalars``, and ``cmap`` names the matplotlib colormap it is read through. Here we simply use the height of each vertex. Next examples use eigenfunctions of the Laplacian, descriptors or geodesic distances. Per-face arrays work too. .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: Python height = mesh.vertices[:, 1] plot_mesh(mesh, scalars=height, cmap="viridis") .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /auto_examples/images/sphx_glr_plot_01_load_mesh_002.png :alt: plot 01 load mesh :srcset: /auto_examples/images/sphx_glr_plot_01_load_mesh_002.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyFM/pyFM/docs/source/auto_examples/images/sphx_glr_plot_01_load_mesh_002.vtksz .. GENERATED FROM PYTHON SOURCE LINES 82-89 Coloring by position -------------------- Mapping the ``(x, y, z)`` coordinates onto ``(r, g, b)`` gives each vertex a color that varies smoothly across the surface. This is a standard way to visualize correspondence. :func:`~pyFM.viz.plot.vertices_to_rgb` does this with a better transformation than the plain min/max rescaling below, and is what the later examples use. .. GENERATED FROM PYTHON SOURCE LINES 89-96 .. code-block:: Python lo = mesh.vertices.min(axis=0, keepdims=True) hi = mesh.vertices.max(axis=0, keepdims=True) rgb = (mesh.vertices - lo) / (hi - lo) plot_mesh(mesh, scalars=rgb) .. tab-set:: .. tab-item:: Static Scene .. image-sg:: /auto_examples/images/sphx_glr_plot_01_load_mesh_003.png :alt: plot 01 load mesh :srcset: /auto_examples/images/sphx_glr_plot_01_load_mesh_003.png :class: sphx-glr-single-img .. tab-item:: Interactive Scene .. offlineviewer:: /home/runner/work/pyFM/pyFM/docs/source/auto_examples/images/sphx_glr_plot_01_load_mesh_003.vtksz .. GENERATED FROM PYTHON SOURCE LINES 97-102 .. note:: ``rgb`` here is an ``(n, 3)`` float array in ``[0, 1]``, which is rendered directly as colors. A one-dimensional ``(n,)`` array is interpreted as a scalar field and mapped to colors using the ``cmap`` colormap. .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python print("rgb array:", rgb.shape, f"range [{rgb.min():.2f}, {rgb.max():.2f}]") .. rst-class:: sphx-glr-script-out .. code-block:: none rgb array: (7207, 3) range [0.00, 1.00] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.580 seconds) .. _sphx_glr_download_auto_examples_plot_01_load_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_load_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_load_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_load_mesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_