mymesh.improvement.Contract#

mymesh.improvement.Contract(M, h, FixedNodes={}, verbose=True, cleanup=True, labels=None, FeatureAngle=25, sizing=None, quadric=True, allow_inversion=False)[source]#

Edge contraction for triangular or tetrahedral mesh coarsening and quality improvement. Contraction of edges with edge length less than h will be attempted. Surfaces and features are preserved by prioritizing surface/feature nodes over interior nodes when deciding which node to remove in the edge collapse operation. Features (edges, corners), as determined by DetectFeatures(), are held fixed. An edge is only contracted if doing so doesn’t invert any elements or reduce the quality by creating a new element with a lower quality than was present in the local edge neighborhood before the contraction. Edges are processed in a heap sorted by edge length, with shorter edges being contracted first.

Parameters:
  • M (mymesh.mesh) – Tetrahedral or triangular mesh to be contracted

  • h (float) – Edge length below which wil be contracted. Using 4/5 of the target edge length is often recommended.

  • FixedNodes (set or array_like, optional) – Indices of nodes to be held fixed, by default {}

  • verbose (bool, optional) – If true, will display progress, by default True

  • cleanup (bool, optional) – If true, unused nodes will be removed from the mesh and nodes will be renumbered, by default True.

  • labels (str or array_like, optional) – Element labels used to identify separate regions (e.g. materials) within a mesh, by default None. If provided as a string, the string must refer to an entry in M.ElemData, otherwise must be an array_like with the number of entries equal to the number of elements (M.NElem). Providing labels will preserve the interface and interface features between regions of differening labels. The labels of the new mesh will be stored in the ElemData of the returned mesh, either in ElemData[‘labels’] (if labels were provided as an array), or the entry matching the original ElemData entry (if labels were provided as a string).

  • FeatureAngle (int, optional) – Angle (in degrees) used to identify features, by default 25. See DetectFeatures() for more information. To turn off feature preservation, use FeatureAngle = None

  • quadric (bool, optional) – Use quadric error minimization [GH97] to reposition surface nodes during contraction. This better preserves the shape of the original surface. By default, True.

Returns:

Mnew – Coarsened tetrahedral mesh

Return type:

mymesh.mesh

Examples

Coarsening preserves interfaces between labeled regions

# 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')

visualize.Subplot((S1, Sc, S1.Clip(), Sc.Clip()), (2,2), scalars='labels', show_edges=True, titles=['Original', 'Coarsened', '', ''], view='-yz')
../../_images/mymesh-improvement-Contract-1.png

Quadrics help to preserve shape even during extreme coarsening:

bunny = mymesh.demo_mesh('bunny')
coarse = improvement.Contract(bunny, 0.05, quadric=False, FeatureAngle=None)
quadric = improvement.Contract(bunny, 0.05, quadric=True, FeatureAngle=None)

visualize.Subplot((bunny, coarse, quadric), (1,3), view='xy', titles=['Original', 'quadric=False', 'quadric=True'])
../../_images/mymesh-improvement-Contract-2.png