API Reference

Core Meshing & Types

The primary interface for generating meshes and handling the resulting data structures.

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, 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).
  • 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.

Note: The shape is (D x NumPoints) and (N x NumElements). This corresponds to Julia's column-major memory layout (columns are points). Modifying these arrays will modify the underlying DMesh.

source

Distance Functions: Basic Shapes

Pre-defined signed distance functions for common primitives in 2D and 3D.

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

Utilities for working with polygonal boundaries and line segments.

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)

Constructive Solid Geometry operations to combine multiple distance functions.

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

Helper functions for element sizing and specific test cases (like airfoils).

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, quality_func=element_quality)

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 from the mesh msh and re-index the connectivity.

This function identifies nodes that are coincident (or within a very small tolerance relative to the mesh size) and merges them. This is useful after mesh generation or modification operations that might create overlapping vertices.

Arguments

  • msh::DMesh: The input mesh containing nodes p and connectivity t.

Returns

A NamedTuple (msh, ix) where:

  • msh: The cleaned DMesh with duplicate nodes removed.
  • ix: An index vector mapping the new nodes to the old nodes (i.e., new_p = old_p[ix]).

Example

clean_msh, = cleanup_mesh(dirty_msh)  # Ignoring the index output (ix)
source