.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/MeshModification/demo_contouring.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_examples_MeshModification_demo_contouring.py: .. currentmodule:: mymesh Contouring ========== Contouring can be used to create meshes from images, implicit functions, or other scalar fields. It can also be used to modify existing meshes, for example by evaluating implicit functions at the nodes of the mesh. See also: :ref:`theory_contour`, :mod:`mymesh.contour`, :ref:`Constructive Solid Geometry` .. GENERATED FROM PYTHON SOURCE LINES 14-16 .. code-block:: Python from mymesh import primitives, implicit, image, demo_image .. GENERATED FROM PYTHON SOURCE LINES 17-19 Starting with a cube, an implicit function of a sphere can be evaluated at all nodes .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: Python cube = primitives.Grid([-0.9,0.9,-0.9,0.9,-0.9,0.9], .05) func = implicit.sphere([0,0,0],1.1) cube.NodeData['sphere'] = func(cube.NodeCoords[:,0], cube.NodeCoords[:,1], cube.NodeCoords[:,2]) cube.plot(scalars='sphere', clim=(-1.25, 1.25)) .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_contouring_001.png :alt: demo contouring :srcset: /examples/MeshModification/images/sphx_glr_demo_contouring_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() Identifying mesh nodes...Done .. GENERATED FROM PYTHON SOURCE LINES 28-33 Choosing a threshold of 0, which corresponds to the surface of our implicit sphere, we can then use the :meth:`~mymesh.mesh.mesh.Contour` method to "cut out" the sphere from the cube. Alternatively, one of several functions in the :mod:`~mymesh.contour` module can be used directly. .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: Python threshold = 0 contoured = cube.Contour('sphere', threshold) contoured.plot() .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_contouring_002.png :alt: demo contouring :srcset: /examples/MeshModification/images/sphx_glr_demo_contouring_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() Identifying mesh nodes...Done .. GENERATED FROM PYTHON SOURCE LINES 38-41 The same approach can be used with more complicated shapes, for example we can take the `Stanford bunny `_. and pattern it with a triply periodic minimal surface. .. GENERATED FROM PYTHON SOURCE LINES 41-52 .. code-block:: Python bunny_img = demo_image('bunny') voxelsize = (0.337891, 0.337891, 0.5) # mm threshold = 100 bunny_tet = image.TetMesh(bunny_img, voxelsize, threshold, scalefactor=0.35, interpolation='linear', voxel_mode='elem') bunny_tet.plot(view='-x-z') fischer_koch_S = implicit.wrapfunc(implicit.thicken(implicit.tpms('S', 20), 1)) bunny_tet.NodeData['f'] = fischer_koch_S(*bunny_tet.NodeCoords.T) bunny_tet.plot(scalars='f', view='-x-z') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_contouring_003.png :alt: demo contouring :srcset: /examples/MeshModification/images/sphx_glr_demo_contouring_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_contouring_004.png :alt: demo contouring :srcset: /examples/MeshModification/images/sphx_glr_demo_contouring_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() Identifying mesh nodes...Done RFBOutputContext() .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python bunny_tetS = bunny_tet.Contour('f', 0, threshold_direction=-1) bunny_tetS.plot(view='-x-z') .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_contouring_005.png :alt: demo contouring :srcset: /examples/MeshModification/images/sphx_glr_demo_contouring_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() Identifying mesh nodes...Done .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 49.540 seconds) .. _sphx_glr_download_examples_MeshModification_demo_contouring.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_contouring.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_contouring.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_contouring.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_