Note
Go to the end to download the full example code.
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: Contour, mymesh.contour, Constructive Solid Geometry
from mymesh import primitives, implicit, image, demo_image
Starting with a cube, an implicit function of a sphere can be evaluated at all nodes
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))

RFBOutputContext()
Identifying mesh nodes...Done
Choosing a threshold of 0, which corresponds to the surface of our implicit
sphere, we can then use the Contour() method to
“cut out” the sphere from the cube.
Alternatively, one of several functions in the contour module
can be used directly.

RFBOutputContext()
Identifying mesh nodes...Done
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.
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')
RFBOutputContext()
Identifying mesh nodes...Done
RFBOutputContext()
bunny_tetS = bunny_tet.Contour('f', 0, threshold_direction=-1)
bunny_tetS.plot(view='-x-z')

RFBOutputContext()
Identifying mesh nodes...Done
Total running time of the script: (1 minutes 49.540 seconds)

