API Reference
Core Meshing & Types
DistMesh.distmesh2d — Function
distmesh2d(dfcn, hfcn, h0, bbox, pfix=[]; kwargs...) -> (p, t)Generate a 2D unstructured triangular mesh using a signed distance function.
This function implements the DistMesh algorithm (Persson/Strang), which treats the mesh generation as a physical equilibrium problem. A system of truss bars (edges) is relaxed until the force equilibrium is reached, constrained by the signed distance function dfcn to stay within the domain.
Arguments
dfcn::Function: Signed distance functiond(p). Returns negative values inside the region, positive outside. The inputpis a 2-element coordinate vector (e.g.,Vector,Tuple, orSVector).hfcn::Function: Element size functionh(p). Returns target edge length at pointp.h0::Real: Initial nominal edge length (scaling factor forhfcn).bbox: Bounding box tuple((xmin, ymin), (xmax, ymax))defining the initial grid generation area.pfix::Vector: (Optional) List of fixed node positions that must be part of the mesh (e.g., corners).
Keywords
plotting::Bool = false: Enable live visualization of the relaxation process (Plots or GLMakie).maxiter::Int = 10_000: Maximum number of relaxation iterations.- Several other parameters that are rarely modified
Returns
p::Vector{Point2d}: The node positions.t::Vector{Index3}: The triangle connectivity indices.
Examples
Uniform Mesh on a Unit Circle
using DistMesh
fd(p) = sqrt(sum(p.^2)) - 1 # or dcircle(p) - unit circle geometry
fh(p) = 1.0 # or huniform(p) - uniform size function
hmin = 0.2 # initial edge lengths
bbox = ((-1,-1), (1,1)) # bounding box for unit circle
msh = distmesh2d(fd, fh, hmin, bbox)
# Optionally, the mesh can be visualized using various plotting packages:
using GLMakie # or Plots, or CairoMakie
plot(msh)Rectangle with Circular Hole (Refined at Boundary)
using DistMesh
hmin = 0.05
fd(p) = ddiff(drectangle(p, -1, 1, -1, 1), dcircle(p, r=0.5))
fh(p) = hmin + 0.3*dcircle(p, r=0.5)
msh = distmesh2d(fd, fh, hmin, ((-1,-1), (1,1)), ((-1,-1), (-1,1), (1,-1), (1,1))) Polygon
using DistMesh
pv = [(-0.4, -0.5), (0.4, -0.2), (0.4, -0.7), (1.5, -0.4),
(0.9, 0.1), (1.6, 0.8), (0.5, 0.5), (0.2, 1.0),
(0.1, 0.4), (-0.7, 0.7), (-0.4, -0.5)]
fd(p) = dpoly(p, pv)
bbox = ((-1,-1), (2,1))
h0 = 0.15
msh = distmesh2d(fd, huniform, h0, bbox, pv)Ellipse
using DistMesh
fd(p) = (p[1]/2)^2 + (p[2]/1)^2 - 1
bbox = ((-2,-1), (2,1))
msh = distmesh2d(fd, huniform, 0.2, bbox)Square, with size function point and line sources
using DistMesh
fd(p) = drectangle(p, 0, 1, 0, 1)
fh(p) = min(min(0.01 + 0.3*abs(dcircle(p, r=0)),
0.025 + 0.3*abs(dpoly(p, [(0.3,0.7), (0.7,0.5)]))),
0.15)
msh = distmesh2d(fd, fh, 0.01, ((0,0), (1,1)), ((0,0), (1,0), (0,1), (1,1)))NACA0012 airfoil
See examples/002-naca_airfoil.jl
DistMesh.DMesh — Type
DMesh{D, T, E, N, I}A lightweight container for mesh data.
D: Spatial dimension (e.g., 2 for 2D coordinates).T: Floating point type for coordinates (e.g., Float64).E: Element topology type (e.g., Simplex{2} or Block{3}).N: Number of vertices per element (e.g., 3 for triangles).I: Integer type for indices (e.g., Int, Int32).
DistMesh.as_arrays — Function
p_view, t_view = as_arrays(m::DMesh)Return zero-copy views of the mesh nodes and elements.
The shape is (D x NumPoints) and (N x NumElements). Modifying these arrays will modify the underlying DMesh.
Distance Functions: Basic Shapes
DistMesh.dcircle — Function
dcircle(p; c=(0, 0), r=1.0)Signed distance function for a 2D circle. Wrapper around dhypersphere that enforces 2D inputs.
DistMesh.drectangle — Function
drectangle(p, x1, x2, y1, y2)Signed distance function for a 2D axis-aligned rectangle. Returns negative values inside the region [x1, x2] × [y1, y2].
DistMesh.dhypersphere — Function
dhypersphere(p, c, r)Signed distance function for a hypersphere of radius r centered at c. Works for any dimension, provided p and c have matching lengths.
DistMesh.dsphere — Function
dsphere(p; c=(0, 0, 0), r=1.0)Signed distance function for a 3D sphere. Wrapper around dhypersphere that enforces 3D inputs.
DistMesh.dblock — Function
dblock(p, x1, x2, y1, y2, z1, z2)Signed distance function for a 3D axis-aligned block (cuboid). Returns negative values inside the region [x1, x2] × [y1, y2] × [z1, z2].
Distance Functions: Polygons & Lines
DistMesh.dpoly — Function
dpoly(p, pv)Signed distance function for a polygon defined by vertices pv. Note: pv must be a closed loop (i.e., pv[end] == pv[1]). Returns negative values inside, positive outside.
DistMesh.dline — Function
dline(p, p1, p2)Distance from point p to the finite line segment defined by endpoints p1 and p2. Returns the Euclidean distance (always non-negative).
DistMesh.inpolygon — Function
inpolygon(p, pv)Point-in-polygon test using the Ray Casting algorithm. Returns true if p is inside the polygon defined by vertices pv.
Distance Functions: Boolean Operations (CSG)
DistMesh.ddiff — Function
ddiff(d1, d2)Difference of two regions (Region 1 minus Region 2). d1 and d2 are signed distances.
DistMesh.dunion — Function
dunion(d1, d2)Union of two regions. d1 and d2 are signed distances.
DistMesh.dintersect — Function
dintersect(d1, d2)Intersection of two regions. d1 and d2 are signed distances.
Distance Functions: Special Functions
DistMesh.dnaca — Function
dnaca(p)Implicit level-set function for a NACA 0012 airfoil. Zero contour defines the airfoil boundary.
Mesh Utilities: Size Functions
DistMesh.huniform — Function
huniform(p)Returns 1.0. Default sizing function for uniform meshes.
Mesh Utilities: General
DistMesh.element_volumes — Function
element_volumes(m::DMesh)Return a Vector of volumes (or areas) for every element in the mesh.
DistMesh.element_qualities — Function
element_qualities(m::DMesh; metric=default_quality_metric(E()))Return a Vector of quality metrics for every element in the mesh.
DistMesh.cleanup_mesh — Function
cleanup_mesh(msh::DMesh) -> (msh::DMesh, ix::Vector{Int})Remove duplicate nodes and re-index the connectivity.
Nodes that are coincident (within a small tolerance relative to the mesh size) are merged. Returns a named tuple (msh, ix) where ix maps new node indices to old ones (new_p = old_p[ix]).
DistMesh.is_manifold_mesh — Function
is_manifold_mesh(msh::DMesh) -> BoolReturn true if every face is shared by exactly two elements, false otherwise.
Element Utilities
DistMesh.element_face_neighbors — Function
element_face_neighbors(msh::DMesh) -> Matrix{Tuple{I, I}}Compute element-to-element connectivity across faces.
Returns a (num_faces_per_element, num_elements) matrix where each entry is a tuple (neighbor_element, neighbor_local_face). Boundary faces have entry (0, 0).
DistMesh.face_element_map — Function
face_element_map(msh::DMesh) -> Dict{SVector, Vector{Tuple{I, I}}}Map each face to the elements that contain it.
Returns a dictionary keyed by sorted face node indices; each value is a vector of (element_idx, local_face_idx) tuples. Interior faces have two entries; boundary faces have one.
DistMesh.find_boundary_elements — Function
find_boundary_elements(msh::DMesh) -> Vector{I}Return the indices of elements that have at least one boundary face.
DistMesh.find_nonmanifold_elements — Function
find_nonmanifold_elements(msh::DMesh) -> Vector{I}Return the indices of elements that share a face with more than one other element.
Face and Edge Utilities
DistMesh.all_faces — Function
all_faces(msh::DMesh) -> (Vector{SVector{L,I}}, Vector{Int})Return all unique faces in the mesh together with the indices of the boundary faces.
Each interior face is listed once; boundary faces are listed once and their positions in the output vector are recorded in boundary_idx. For 2D triangular meshes these are edges; for 3D tetrahedral meshes, triangles.
DistMesh.boundary_faces — Function
boundary_faces(msh::DMesh) -> Vector{SVector{L,I}}Return all faces on the boundary of the mesh (faces not shared by two elements).
For 2D triangular meshes these are boundary edges; for 3D tetrahedral meshes, boundary triangles.
DistMesh.all_edges — Function
all_edges(msh::DMesh) -> Vector{SVector{2,I}}Return all unique edges in the mesh, each oriented with the smaller node index first.
Node Utilities
DistMesh.boundary_nodes — Function
boundary_nodes(msh::DMesh) -> Vector{I}Return the indices of all nodes that lie on the boundary of the mesh.
DistMesh.node_degrees — Function
node_degrees(msh::DMesh) -> Vector{Int}Return the degree (number of incident edges) of each node.
DistMesh.node_adjacency — Function
node_adjacency(msh::DMesh) -> Vector{Vector{I}}Return the adjacency list of each node.
The i-th entry is a sorted vector of indices of all nodes connected to node i by an edge.
DistMesh.node_element_map — Function
node_element_map(msh::DMesh) -> Vector{Vector{I}}Return the list of elements containing each node.
The i-th entry is a sorted vector of indices of all elements that include node i.