
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/06_advanced/custom_tessellation.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_06_advanced_custom_tessellation.py>`
        to download the full example code.

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

.. _sphx_glr_examples_06_advanced_custom_tessellation.py:


Custom tessellation deflection
==============================

OCCT's ``BRepMesh_IncrementalMesh`` exposes a linear and angular
deflection knob. Coarser deflection yields fewer triangles; finer
deflection yields smoother curves. ``read_step``, ``read_brep``,
``read_fcstd``, and the build123d / cadquery bridges all forward
these knobs.

The part is a real M3 nyloc lock nut from the FreeCAD parts library
(CC-BY 3.0, FreeCAD-library contributors). Its helical thread and
chamfers are exactly the curved features the deflection knob
resolves, so the triangle count moves with the setting.

.. GENERATED FROM PYTHON SOURCE LINES 18-25

.. code-block:: Python


    import pandas as pd
    import pyvista as pv

    import pyvista_cad
    from pyvista_cad.examples import downloads








.. GENERATED FROM PYTHON SOURCE LINES 26-27

Tessellate the same real FCStd part at three deflection settings.

.. GENERATED FROM PYTHON SOURCE LINES 27-48

.. code-block:: Python


    fcstd = downloads.fcstd_nut_path()
    settings = [
        ('coarse', 0.5, 1.0),
        ('medium', 0.1, 0.5),
        ('fine', 0.02, 0.2),
    ]
    meshes = {
        name: pyvista_cad.read_fcstd(fcstd, linear_deflection=lin, angular_deflection=ang)
        .combine()
        .extract_surface()
        for name, lin, ang in settings
    }

    pd.DataFrame(
        [
            {'setting': name, 'linear': lin, 'angular': ang, 'triangles': meshes[name].n_cells}
            for name, lin, ang in settings
        ]
    )





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

 .. code-block:: none

    /home/runner/work/pyvista-cad/pyvista-cad/examples/06_advanced/custom_tessellation.py:37: PyVistaFutureWarning: The default value of `algorithm` for the filter
    `UnstructuredGrid.extract_surface` will change in the future. It currently defaults to
    `'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to
    silence this warning.
      .extract_surface()


.. raw:: html

    <div class="output_subarea output_html rendered_html output_result">
    <div>
    <style scoped>
        .dataframe tbody tr th:only-of-type {
            vertical-align: middle;
        }

        .dataframe tbody tr th {
            vertical-align: top;
        }

        .dataframe thead th {
            text-align: right;
        }
    </style>
    <table border="1" class="dataframe">
      <thead>
        <tr style="text-align: right;">
          <th></th>
          <th>setting</th>
          <th>linear</th>
          <th>angular</th>
          <th>triangles</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th>0</th>
          <td>coarse</td>
          <td>0.50</td>
          <td>1.0</td>
          <td>4110</td>
        </tr>
        <tr>
          <th>1</th>
          <td>medium</td>
          <td>0.10</td>
          <td>0.5</td>
          <td>8116</td>
        </tr>
        <tr>
          <th>2</th>
          <td>fine</td>
          <td>0.02</td>
          <td>0.2</td>
          <td>29600</td>
        </tr>
      </tbody>
    </table>
    </div>
    </div>
    <br />
    <br />

.. GENERATED FROM PYTHON SOURCE LINES 49-50

Side by side with the camera locked across views.

.. GENERATED FROM PYTHON SOURCE LINES 50-59

.. code-block:: Python


    pl = pv.Plotter(shape=(1, 3))
    for i, name in enumerate(['coarse', 'medium', 'fine']):
        pl.subplot(0, i)
        mesh = meshes[name]
        mesh['z'] = mesh.points[:, 2]
        pl.add_mesh(mesh, scalars='z', cmap='inferno', show_edges=True, edge_color='black')
    pl.link_views()
    pl.show()







.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /examples/06_advanced/images/sphx_glr_custom_tessellation_001.png
        :alt: custom tessellation
        :srcset: /examples/06_advanced/images/sphx_glr_custom_tessellation_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /home/runner/work/pyvista-cad/pyvista-cad/doc/examples/06_advanced/images/sphx_glr_custom_tessellation_001.vtksz







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

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


.. _sphx_glr_download_examples_06_advanced_custom_tessellation.py:

.. only:: html

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

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

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

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

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

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

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