
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "tutorial/02_mesh/solutions/d_create-tri-surface.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_solutions_d_create-tri-surface.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_solutions_d_create-tri-surface.py:


.. _triangulated_surface:

Create Triangulated Surface
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create a surface from a set of points through a Delaunay triangulation.

.. note::
    We will use a filter from PyVista to perform our triangulation: `delaunay_2d <https://docs.pyvista.org/api/core/_autosummary/pyvista.PolyData.delaunay_2d.html>`_.

.. GENERATED FROM PYTHON SOURCE LINES 12-16

.. code-block:: Python


    import numpy as np
    import pyvista as pv








.. GENERATED FROM PYTHON SOURCE LINES 17-21

Simple Triangulations
+++++++++++++++++++++

First, create some points for the surface.

.. GENERATED FROM PYTHON SOURCE LINES 21-34

.. code-block:: Python


    # Define a simple Gaussian surface
    n = 20
    x = np.linspace(-200, 200, num=n) + np.random.uniform(-5, 5, size=n)
    y = np.linspace(-200, 200, num=n) + np.random.uniform(-5, 5, size=n)
    xx, yy = np.meshgrid(x, y)
    A, b = 100, 100
    zz = A * np.exp(-0.5 * ((xx / b) ** 2.0 + (yy / b) ** 2.0))

    # Get the points as a 2D NumPy array (N by 3)
    points = np.c_[xx.reshape(-1), yy.reshape(-1), zz.reshape(-1)]
    points[0:5, :]





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

 .. code-block:: none


    array([[-195.78402706, -197.57784091,    2.08913637],
           [-182.88719243, -197.57784091,    2.66694539],
           [-155.86894698, -197.57784091,    4.21463753],
           [-138.22976351, -197.57784091,    5.46271899],
           [-113.60910694, -197.57784091,    7.4481715 ]])



.. GENERATED FROM PYTHON SOURCE LINES 35-37

Now use those points to create a point cloud PyVista data object. This will
be encompassed in a :class:`pyvista.PolyData` object.

.. GENERATED FROM PYTHON SOURCE LINES 37-42

.. code-block:: Python


    # simply pass the numpy points to the PolyData constructor
    cloud = pv.PolyData(points)
    cloud.plot(point_size=15)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_001.png
        :alt: d create tri surface
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 43-46

Now that we have a PyVista data structure of the points, we can perform a
triangulation to turn those boring discrete points into a connected surface.
See :func:`pyvista.UnstructuredGridFilters.delaunay_2d`.

.. GENERATED FROM PYTHON SOURCE LINES 46-48

.. code-block:: Python

    help(cloud.delaunay_2d)





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

 .. code-block:: none

    Help on method delaunay_2d in module pyvista.core.filters.poly_data:

    delaunay_2d(tol=1e-05, alpha=0.0, offset=1.0, bound: 'bool' = False, inplace: 'bool' = False, edge_source=None, progress_bar: 'bool' = False) method of pyvista.core.pointset.PolyData instance
        Apply a 2D Delaunay filter along the best fitting plane.

        This filter can be used to generate a 2d surface from a set of
        points on a plane.  If you want to create a surface from a
        point cloud, see :func:`pyvista.PolyDataFilters.reconstruct_surface`.

        Parameters
        ----------
        tol : float, default: 1e-05
            Specify a tolerance to control discarding of closely
            spaced points. This tolerance is specified as a fraction
            of the diagonal length of the bounding box of the points.

        alpha : float, default: 0.0
            Specify alpha (or distance) value to control output of
            this filter. For a non-zero alpha value, only edges or
            triangles contained within a sphere centered at mesh
            vertices will be output. Otherwise, only triangles will be
            output.

        offset : float, default: 1.0
            Specify a multiplier to control the size of the initial,
            bounding Delaunay triangulation.

        bound : bool, default: False
            Boolean controls whether bounding triangulation points
            and associated triangles are included in the
            output. These are introduced as an initial triangulation
            to begin the triangulation process. This feature is nice
            for debugging output.

        inplace : bool, default: False
            If ``True``, overwrite this mesh with the triangulated
            mesh.

        edge_source : pyvista.PolyData, optional
            Specify the source object used to specify constrained
            edges and loops. If set, and lines/polygons are defined, a
            constrained triangulation is created. The lines/polygons
            are assumed to reference points in the input point set
            (i.e. point ids are identical in the input and
            source).

        progress_bar : bool, default: False
            Display a progress bar to indicate progress.

        Returns
        -------
        pyvista.PolyData
            Mesh from the 2D delaunay filter.

        Examples
        --------
        First, generate 30 points on circle and plot them.

        >>> import pyvista as pv
        >>> points = pv.Polygon(n_sides=30).points
        >>> circle = pv.PolyData(points)
        >>> circle.plot(show_edges=True, point_size=15)

        Use :func:`delaunay_2d` to fill the interior of the circle.

        >>> filled_circle = circle.delaunay_2d()
        >>> filled_circle.plot(show_edges=True, line_width=5)

        Use the ``edge_source`` parameter to create a constrained delaunay
        triangulation and plot it.

        >>> squar = pv.Polygon(n_sides=4, radius=8, fill=False)
        >>> squar = squar.rotate_z(45, inplace=False)
        >>> circ0 = pv.Polygon(center=(2, 3, 0), n_sides=30, radius=1)
        >>> circ1 = pv.Polygon(center=(-2, -3, 0), n_sides=30, radius=1)
        >>> comb = circ0.append_polydata(circ1, squar)
        >>> tess = comb.delaunay_2d(edge_source=comb)
        >>> tess.plot(cpos='xy', show_edges=True)

        See :ref:`create_tri_surface_example` for more examples using this filter.





.. GENERATED FROM PYTHON SOURCE LINES 49-50

Apply the ``delaunay_2d`` filter.

.. GENERATED FROM PYTHON SOURCE LINES 50-57

.. code-block:: Python


    surf = cloud.delaunay_2d()

    # And plot it with edges shown
    surf.plot(show_edges=True)









.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_002.png
        :alt: d create tri surface
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_002.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 58-60

Clean Edges & Triangulations
++++++++++++++++++++++++++++

.. GENERATED FROM PYTHON SOURCE LINES 60-73

.. code-block:: Python


    # Create the points to triangulate
    x = np.arange(10, dtype=float)
    xx, yy, zz = np.meshgrid(x, x, [0])
    points = np.column_stack((xx.ravel(order="F"), yy.ravel(order="F"), zz.ravel(order="F")))
    # Perturb the points
    points[:, 0] += np.random.rand(len(points)) * 0.3
    points[:, 1] += np.random.rand(len(points)) * 0.3

    # Create the point cloud mesh to triangulate from the coordinates
    cloud = pv.PolyData(points)
    cloud






.. 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>1.629e-02, 9.264e+00</td></tr>
    <tr><td>Y Bounds</td><td>2.147e-02, 9.287e+00</td></tr>
    <tr><td>Z Bounds</td><td>0.000e+00, 0.000e+00</td></tr>
    <tr><td>N Arrays</td><td>0</td></tr>
    </table>


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

.. GENERATED FROM PYTHON SOURCE LINES 74-76

.. code-block:: Python

    cloud.plot(cpos="xy")








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_003.png
        :alt: d create tri surface
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_003.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_003.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 77-78

Run the triangulation on these points

.. GENERATED FROM PYTHON SOURCE LINES 78-81

.. code-block:: Python

    surf = cloud.delaunay_2d()
    surf.plot(cpos="xy", show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_004.png
        :alt: d create tri surface
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_004.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_004.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 82-84

Note that some of the outer edges are unconstrained and the triangulation
added unwanted triangles. We can mitigate that with the ``alpha`` parameter.

.. GENERATED FROM PYTHON SOURCE LINES 84-87

.. code-block:: Python

    surf = cloud.delaunay_2d(alpha=1.0)
    surf.plot(cpos="xy", show_edges=True)








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_005.png
        :alt: d create tri surface
        :srcset: /tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_005.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-tutorial/pyvista-tutorial/doc/source/tutorial/02_mesh/solutions/images/sphx_glr_d_create-tri-surface_005.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 88-95

.. raw:: html

    <center>
      <a target="_blank" href="https://colab.research.google.com/github/pyvista/pyvista-tutorial/blob/gh-pages/notebooks/tutorial/02_mesh/solutions/d_create-tri-surface.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 1.029 seconds)


.. _sphx_glr_download_tutorial_02_mesh_solutions_d_create-tri-surface.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/solutions/d_create-tri-surface.ipynb
        :alt: Launch binder
        :width: 150 px

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

      :download:`Download Jupyter notebook: d_create-tri-surface.ipynb <d_create-tri-surface.ipynb>`

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

      :download:`Download Python source code: d_create-tri-surface.py <d_create-tri-surface.py>`

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

      :download:`Download zipped: d_create-tri-surface.zip <d_create-tri-surface.zip>`


.. only:: html

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

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