.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/MeshModification/demo_coarsening.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_coarsening.py: .. currentmodule:: mymesh Coarsening ========== Mesh coarsening can be performed as part of a mesh quality improvement process, or to reduce the resolution/complexity of a mesh for various other purposes. The :func:`~mymesh.improvement.Contract` function supports coarsening of either tetrahedral or triangular meshes by contracting edges to achieve the desired edge length. For non-triangular or non-tetrahedral meshes, they can be first converted using :func:`~mymesh.converter.solid2tets` or :func:`~mymesh.converter.surf2tris` (see also :ref:`Conversion`). See also: :ref:`Edge Contraction`, :func:`~mymesh.improvement.Contract`. .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python import mymesh from mymesh import * import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 23-25 Surface Mesh Coarsening ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 25-39 .. code-block:: Python threshold = 100 bunny_img = mymesh.demo_image('bunny') voxelsize = (0.337891, 0.337891, 0.5) bunny_surf = image.SurfaceMesh(bunny_img, voxelsize, threshold, scalefactor=0.5, method='mc33') # The feature angle option can be used to preserve sharp edges/corners by # limiting coarsening in those areas, here it's disabled. bunny_coarse = improvement.Contract(bunny_surf, 8, FeatureAngle=None, verbose=True) bunny_surf.plot(view='-x-z') bunny_coarse.plot(view='-x-z') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_coarsening_001.png :alt: demo coarsening :srcset: /examples/MeshModification/images/sphx_glr_demo_coarsening_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_coarsening_002.png :alt: demo coarsening :srcset: /examples/MeshModification/images/sphx_glr_demo_coarsening_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Identifying element types...Done Identifying element edges...Done Identifying boundary nodes... Identifying boundary...Done Done Edge Contraction:RFBOutputContext() Identifying mesh nodes...Done RFBOutputContext() Identifying mesh nodes...Done .. GENERATED FROM PYTHON SOURCE LINES 40-45 Label-preserving coarsening --------------------------- Labels can be assigned to different regions of the mesh to preserve the interfaces between the regions during coarsening. This can be useful for preserving interfaces in multi-material meshes for finite element simulations .. GENERATED FROM PYTHON SOURCE LINES 45-64 .. code-block:: Python # Create a spherical mesh S = implicit.TetMesh(implicit.sphere([0,0,0], 1), [-1,1,-1,1,-1,1], .1) # Embed a torus in the mesh S.NodeData['torus'] = implicit.torus([0,0,0],1,.5)(*S.NodeCoords.T) S1 = S.Contour('torus', 0, threshold_direction=1, mixed_elements=False) S1.ElemData['labels'] = np.zeros(S1.NElem) S2 = S.Contour('torus', 0, threshold_direction=-1, mixed_elements=False) S2.ElemData['labels'] = np.ones(S2.NElem) S1.merge(S2) # Coarsen Sc = improvement.Contract(S1, 0.2, labels='labels', verbose=True) visualize.Subplot((S1, Sc, S1.Clip(), Sc.Clip()), (2,2), scalars='labels', show_edges=True, titles=['Original', 'Coarsened', '', ''], view='-yz') .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_coarsening_003.png :alt: Original, Coarsened :srcset: /examples/MeshModification/images/sphx_glr_demo_coarsening_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Identifying element types...Done Identifying element edges...Done Identifying boundary nodes... Identifying boundary... Identifying surface...Done Done Done Edge Contraction: Calculating element centroids...Done Calculating element centroids...Done RFBOutputContext() Identifying element edges...Done Identifying mesh nodes...Done RFBOutputContext() Identifying surface...Done Identifying element edges...Done Identifying mesh nodes...Done RFBOutputContext() Identifying surface...Done Identifying element edges...Done Identifying mesh nodes...Done RFBOutputContext() Identifying surface...Done Identifying element edges...Done Identifying mesh nodes...Done .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 29.100 seconds) .. _sphx_glr_download_examples_MeshModification_demo_coarsening.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_coarsening.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_coarsening.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_coarsening.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_