
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/02_mesh/a_lesson_mesh.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_tutorial_02_mesh_a_lesson_mesh.py>`
        to download the full example code. or to run this example in your browser via Binder

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_tutorial_02_mesh_a_lesson_mesh.py:


Lesson Overview
~~~~~~~~~~~~~~~

This exercise provides an overview of the example in the initial lesson for you
to try out!

.. GENERATED FROM PYTHON SOURCE LINES 9-14

.. code-block:: Python


    import numpy as np
    import pyvista as pv
    from pyvista import examples








.. GENERATED FROM PYTHON SOURCE LINES 16-21

What is a Point?
^^^^^^^^^^^^^^^^

Let's start with a point cloud - this is a mesh type that only has vertices.
You can create one by defining a 2D array of Cartesian coordinates like so:

.. GENERATED FROM PYTHON SOURCE LINES 21-25

.. code-block:: Python


    points = np.random.rand(100, 3)
    points[:5, :]  # output first 5 rows





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([[0.31269476, 0.6205251 , 0.77517889],
           [0.36461253, 0.18831158, 0.80777912],
           [0.16153448, 0.51554617, 0.15653644],
           [0.33719591, 0.18025688, 0.15188854],
           [0.62970302, 0.22250274, 0.22527579]])



.. GENERATED FROM PYTHON SOURCE LINES 26-27

Pass numpy array of points (n by 3) to PolyData

.. GENERATED FROM PYTHON SOURCE LINES 27-30

.. code-block:: Python

    mesh = pv.PolyData(points)
    mesh






.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">

    <table style='width: 100%;'>
    <tr><th>PolyData</th><th>Information</th></tr>
    <tr><td>N Cells</td><td>100</td></tr>
    <tr><td>N Points</td><td>100</td></tr>
    <tr><td>N Strips</td><td>0</td></tr>
    <tr><td>X Bounds</td><td>9.295e-03, 9.891e-01</td></tr>
    <tr><td>Y Bounds</td><td>4.455e-03, 9.966e-01</td></tr>
    <tr><td>Z Bounds</td><td>7.376e-03, 9.952e-01</td></tr>
    <tr><td>N Arrays</td><td>0</td></tr>
    </table>


    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 31-33

.. code-block:: Python

    mesh.plot(point_size=10, style="points")








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_001.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 34-36

But it's important to note that most meshes have some sort of connectivity
between points such as this gridded mesh:

.. GENERATED FROM PYTHON SOURCE LINES 36-46

.. code-block:: Python


    mesh = examples.load_hexbeam()
    cpos = [(6.20, 3.00, 7.50), (0.16, 0.13, 2.65), (-0.28, 0.94, -0.21)]

    pl = pv.Plotter()
    pl.add_mesh(mesh, show_edges=True, color="white")
    pl.add_points(mesh.points, color="red", point_size=20, render_points_as_spheres=True)
    pl.camera_position = cpos
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_002.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 47-55

.. code-block:: Python

    mesh = examples.download_bunny_coarse()

    pl = pv.Plotter()
    pl.add_mesh(mesh, show_edges=True, color="white")
    pl.add_points(mesh.points, color="red", point_size=10)
    pl.camera_position = [(0.02, 0.30, 0.73), (0.02, 0.03, -0.022), (-0.03, 0.94, -0.34)]
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_003.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 56-65

What is a Cell?
^^^^^^^^^^^^^^^

A cell is the geometry between points that defines the connectivity or
topology of a mesh. In the examples above, cells are defined by the lines
(edges colored in black) connecting points (colored in red). For example, a
cell in the beam example is a voxel defined by the region between eight
points in that mesh. Here we can extract one of the cells from the mesh,
show some information about it, and plot its location among the mesh.

.. GENERATED FROM PYTHON SOURCE LINES 65-71

.. code-block:: Python


    mesh = examples.load_hexbeam()

    single_cell = mesh.get_cell(mesh.n_cells - 1)
    single_cell





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Cell (0x7f4fe19ebb20)
      Type:        <CellType.HEXAHEDRON: 12>
      Linear:      True
      Dimension:   3
      N Points:    8
      N Faces:     6
      N Edges:     12
      X Bounds:    5.000e-01, 1.000e+00
      Y Bounds:    5.000e-01, 1.000e+00
      Z Bounds:    4.500e+00, 5.000e+00



.. GENERATED FROM PYTHON SOURCE LINES 72-86

.. code-block:: Python


    pl = pv.Plotter()
    pl.add_mesh(mesh, show_edges=True, color="white")
    pl.add_points(mesh.points, color="red", point_size=20)
    pl.add_mesh(
        single_cell.cast_to_unstructured_grid(),
        color="pink",
        edge_color="blue",
        line_width=5,
        show_edges=True,
    )
    pl.camera_position = [(6.20, 3.00, 7.50), (0.16, 0.13, 2.65), (-0.28, 0.94, -0.21)]
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_004.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 87-90

Cells aren't limited to voxels, they could be a triangle between three
points, a line between two points, or even a single point could be its own
cell (but that's a special case).

.. GENERATED FROM PYTHON SOURCE LINES 92-101

What are attributes?
^^^^^^^^^^^^^^^^^^^^

Attributes are data values that live on either the points or cells of a mesh.
In PyVista, we work with both point data and cell data and allow easy access
to data dictionaries to hold arrays for attributes that live either on all
points or on all cells of a mesh. These attributes can be accessed in a
dictionary-like attribute attached to any PyVista mesh accessible as one
of the following:

.. GENERATED FROM PYTHON SOURCE LINES 103-109

Point Data
~~~~~~~~~~
Point data refers to arrays of values (scalars, vectors, etc.) that live on
each point of the mesh. Each element in an attribute array corresponds to a
point in the mesh. Let's create some point data for the beam mesh. When
plotting, the values between points are interpolated across the cells.

.. GENERATED FROM PYTHON SOURCE LINES 109-113

.. code-block:: Python


    mesh.point_data["my point values"] = np.arange(mesh.n_points)
    mesh.plot(scalars="my point values", cpos=cpos, show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_005.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_005.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_005.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 114-119

Cell Data
~~~~~~~~~~
Cell data refers to arrays of values (scalars, vectors, etc.) that live
throughout each cell of the mesh. That is the entire cell (2D face or 3D
volume) is assigned the value of that attribute.

.. GENERATED FROM PYTHON SOURCE LINES 119-123

.. code-block:: Python


    mesh.cell_data["my cell values"] = np.arange(mesh.n_cells)
    mesh.plot(scalars="my cell values", cpos=cpos, show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_006.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_006.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_006.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 124-127

Here's a comparison of point data versus cell data and how point data is
interpolated across cells when mapping colors. This is unlike cell data
which has a single value across the cell's domain:

.. GENERATED FROM PYTHON SOURCE LINES 127-140

.. code-block:: Python


    import pyvista as pv
    from pyvista import examples

    uni = examples.load_uniform()

    pl = pv.Plotter(shape=(1, 2), border=False)
    pl.add_mesh(uni, scalars="Spatial Point Data", show_edges=True)
    pl.subplot(0, 1)
    pl.add_mesh(uni, scalars="Spatial Cell Data", show_edges=True)
    pl.link_views()
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_007.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_007.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_007.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 141-145

Field Data
~~~~~~~~~~
Field data is not directly associated with either the points or cells but
still should be attached to the mesh. This may be a string array storing notes.

.. GENERATED FROM PYTHON SOURCE LINES 145-149

.. code-block:: Python

    mesh = pv.Cube()
    mesh.field_data["metadata"] = ["Foo", "bar"]
    mesh.field_data





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    pyvista DataSetAttributes
    Association     : NONE
    Contains arrays :
        metadata                <U3        (2,)



.. GENERATED FROM PYTHON SOURCE LINES 150-158

Assigning Scalars to a Mesh
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's how we assign values to cell attributes and plot it. Here, we
generate cube containing 6 faces and assign each face an integer from
``range(6)`` and then have it plotted.

Note how this varies from assigning scalars to each point

.. GENERATED FROM PYTHON SOURCE LINES 158-170

.. code-block:: Python

    cube = pv.Cube()
    cube.cell_data["myscalars"] = range(6)

    other_cube = cube.copy()
    other_cube.point_data["myscalars"] = range(8)

    pl = pv.Plotter(shape=(1, 2), border_width=1)
    pl.add_mesh(cube, cmap="coolwarm")
    pl.subplot(0, 1)
    pl.add_mesh(other_cube, cmap="coolwarm")
    pl.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_008.png
        :alt: a lesson mesh
        :srcset: /tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_008.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/images/sphx_glr_a_lesson_mesh_008.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 171-178

.. raw:: html

    <center>
      <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/02_mesh/a_lesson_mesh.ipynb">
        <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/ width="150px">
      </a>
    </center>


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 3.381 seconds)


.. _sphx_glr_download_tutorial_02_mesh_a_lesson_mesh.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: binder-badge

      .. image:: images/binder_badge_logo.svg
        :target: https://mybinder.org/v2/gh/pyvista/pyvista-tutorial/gh-pages?urlpath=lab/tree/notebooks/tutorial/02_mesh/a_lesson_mesh.ipynb
        :alt: Launch binder
        :width: 150 px

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: a_lesson_mesh.ipynb <a_lesson_mesh.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: a_lesson_mesh.py <a_lesson_mesh.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: a_lesson_mesh.zip <a_lesson_mesh.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
