API Reference

Core Meshing & Types

DistMesh.distmesh2dFunction
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 function d(p). Returns negative values inside the region, positive outside. The input p is a 2-element coordinate vector (e.g., Vector, Tuple, or SVector).
  • hfcn::Function: Element size function h(p). Returns target edge length at point p.
  • h0::Real: Initial nominal edge length (scaling factor for hfcn).
  • 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

source
DistMesh.DMeshType
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).
source
DistMesh.as_arraysFunction
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.

source

Distance Functions: Basic Shapes

DistMesh.dcircleFunction
dcircle(p; c=(0, 0), r=1.0)

Signed distance function for a 2D circle. Wrapper around dhypersphere that enforces 2D inputs.

source
DistMesh.drectangleFunction
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].

source
DistMesh.dhypersphereFunction
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.

source
DistMesh.dsphereFunction
dsphere(p; c=(0, 0, 0), r=1.0)

Signed distance function for a 3D sphere. Wrapper around dhypersphere that enforces 3D inputs.

source
DistMesh.dblockFunction
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].

source

Distance Functions: Polygons & Lines

DistMesh.dpolyFunction
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.

source
DistMesh.dlineFunction
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).

source
DistMesh.inpolygonFunction
inpolygon(p, pv)

Point-in-polygon test using the Ray Casting algorithm. Returns true if p is inside the polygon defined by vertices pv.

source

Distance Functions: Boolean Operations (CSG)

DistMesh.ddiffFunction
ddiff(d1, d2)

Difference of two regions (Region 1 minus Region 2). d1 and d2 are signed distances.

source

Distance Functions: Special Functions

DistMesh.dnacaFunction
dnaca(p)

Implicit level-set function for a NACA 0012 airfoil. Zero contour defines the airfoil boundary.

source

Mesh Utilities: Size Functions

Mesh Utilities: General

DistMesh.element_qualitiesFunction
element_qualities(m::DMesh; metric=default_quality_metric(E()))

Return a Vector of quality metrics for every element in the mesh.

source
DistMesh.cleanup_meshFunction
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]).

source
DistMesh.is_manifold_meshFunction
is_manifold_mesh(msh::DMesh) -> Bool

Return true if every face is shared by exactly two elements, false otherwise.

source

Element Utilities

DistMesh.element_face_neighborsFunction
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).

source
DistMesh.face_element_mapFunction
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.

source

Face and Edge Utilities

DistMesh.all_facesFunction
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.

source
DistMesh.boundary_facesFunction
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.

source
DistMesh.all_edgesFunction
all_edges(msh::DMesh) -> Vector{SVector{2,I}}

Return all unique edges in the mesh, each oriented with the smaller node index first.

source

Node Utilities

DistMesh.boundary_nodesFunction
boundary_nodes(msh::DMesh) -> Vector{I}

Return the indices of all nodes that lie on the boundary of the mesh.

source
DistMesh.node_degreesFunction
node_degrees(msh::DMesh) -> Vector{Int}

Return the degree (number of incident edges) of each node.

source
DistMesh.node_adjacencyFunction
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.

source
DistMesh.node_element_mapFunction
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.

source