Learn R Programming

fmesher: Triangle Meshes and Other Geometry Tools

Generate planar and spherical triangle meshes, compute finite element calculations for 1- and 2-dimensional flat and curved manifolds with associated basis function spaces, methods for lines and polygons, and transparent handling of coordinate reference systems and coordinate transformation, including sf and sp geometries. The core fmesher library code was originally part of the INLA package, and also distributed in the EUSTACE Horizon 2020 project, and implements parts of “Triangulations and Applications” by Hjelle and Dæhlen (2006). The expanded crs/CRS support started as an add-on feature of inlabru.

Installation

You can install the current CRAN version version of fmesher:

install.packages("fmesher")

Installation using pak

You can install the latest bugfix release of fmesher from GitHub with:

# install.packages("pak")
pak::pkg_install("inlabru-org/fmesher@stable")

You can install the development version of inlabru from GitHub with

pak::pkg_install("inlabru-org/fmesher")

or track the development version builds via inlabru-org.r-universe.dev:

# Enable universe(s) by inlabru-org
pak::repo_add(inlabruorg = "https://inlabru-org.r-universe.dev")
pak::pkg_install("fmesher")

This will pick the r-universe version if it is more recent than the CRAN version.

To install and run fmesher in full debug mode (this is quite an experience!), use

# install.packages("pkgbuild")
source("https://raw.githubusercontent.com/inlabru-org/fmesher/devel/misc/build.R")
fmesher_install(repo = "inlabru-org/fmesher", debug = TRUE)

Installation using remotes

You can install the latest bugfix release of fmesher from GitHub with:

# install.packages("remotes")
remotes::install_github("inlabru-org/fmesher", ref = "stable")

You can install the development version of fmesher from GitHub with

remotes::install_github("inlabru-org/fmesher")

or track the development version builds via inlabru-org.r-universe.dev:

# Enable universe(s) by inlabru-org
options(repos = c(
  inlabruorg = "https://inlabru-org.r-universe.dev",
  getOption("repos")
))
install.packages("fmesher")

Online documentation

https://inlabru-org.github.io/fmesher/

Examples

2D triangular meshes

Refined constrained Delaunay triangulations can be constructed by fm_rcdt_2d() and fm_mesh_2d(). The _inla() versions of these will usually return the same meshes as the old INLA methods, INLA::inla.mesh.create() and INLA::inla.mesh.2d().

suppressPackageStartupMessages(library(fmesher))
suppressPackageStartupMessages(library(ggplot2))

bnd <- fm_extensions(cbind(0, 0), convex = c(1, 1.5))
(mesh <- fm_mesh_2d_inla(
  boundary = bnd,
  max.edge = c(0.2, 0.5)
))
#> fm_mesh_2d object:
#>   Manifold:  R2
#>   V / E / T: 269 / 772 / 504
#>   Euler char.:   1
#>   Constraints:   Boundary: 32 boundary edges (1 group: 1), Interior: 44 interior edges (1 group: 1)
#>   Bounding box: (-1.499887, 1.499887) x (-1.499887, 1.499887)
#>   Basis d.o.f.:  269
ggplot() +
  geom_fm(data = mesh) +
  theme_minimal()

Mostly regular triangulations can be constructed by supplying a regular set of input points. The (experimental, developed by Man Ho Suen) fm_hexagon_lattice() function generates points in a regular hexagonal lattice pattern, contained in a given sf polygon.

hex_points <- fm_hexagon_lattice(bnd = bnd[[1]], edge_len = 0.2)
(mesh_hex <- fm_mesh_2d_inla(
  loc = hex_points,
  boundary = bnd,
  max.edge = c(0.3, 0.5)
))
#> fm_mesh_2d object:
#>   Manifold:  R2
#>   V / E / T: 154 / 427 / 274
#>   Euler char.:   1
#>   Constraints:   Boundary: 32 boundary edges (1 group: 1), Interior: 32 interior edges (1 group: 1)
#>   Bounding box: (-1.499887, 1.499887) x (-1.499887, 1.499887)
#>   Basis d.o.f.:  154
ggplot() +
  geom_fm(data = mesh_hex) +
  theme_minimal()

1D B-spline function spaces

(mesh <- fm_mesh_1d(c(1, 2, 3, 4, 6),
  boundary = c("neumann", "free"),
  degree = 2
))
#> fm_mesh_1d object:
#>   Manifold:  R1
#>   #{knots}:  5
#>   Interval:  (1, 6)
#>   Boundary:  (neumann, free)
#>   B-spline degree:   2
#>   Basis d.o.f.:  5
ggplot() +
  geom_fm(data = mesh, xlim = c(0, 7))

Extended helper methods for CRS handling

The package provides methods fm_crs() and fm_CRS() for extracting CRS information from sf and sp objects and automatically converts to the desired output format. The fm_transform() wrapper similarly handles a variety of objects, as well as special handling for converting between spheres and globes of different radii, e.g. used to map between the Earth and a unit radius sphere uses as a model of the Earth.

# longlat for a spherical version of the Earth
print(fm_proj4string(fm_crs("longlat_globe")))
#> [1] "+proj=longlat +ellps=sphere +no_defs"

# longlat for a sphere of radius 1m
print(fm_proj4string(fm_crs("longlat_norm")))
#> [1] "+proj=longlat +R=1 +no_defs"

# A sphere of radius 1m
print(fm_proj4string(fm_crs("sphere")))
#> [1] "+proj=geocent +R=1 +units=m +no_defs"

Copy Link

Version

Install

install.packages('fmesher')

Monthly Downloads

5,816

Version

0.4.0

License

MPL-2.0

Issues

Pull Requests

Stars

Forks

Maintainer

Finn Lindgren

Last Published

June 12th, 2025

Functions in fmesher (0.4.0)

fm_CRS

Create a coordinate reference system object
fm_as_mesh_3d

Convert objects to fm_mesh_3d
fm_as_segm

Convert objects to fm_segm
fm_contains

Check which mesh triangles are inside a polygon
fm_crs<-

Assignment operators for crs information objects
fm_crs

Obtain coordinate reference system object
fm_CRS_as_list

Show expanded CRS arguments
fm_centroids

Extract triangle centroids from an fm_mesh_2d
fm_collect

Make a collection function space
fm_crs_is_null

Check if a crs is NULL or NA
fm_crs_is_identical

Check if two CRS objects are identical
fm_cprod

(Blockwise) cross product of integration points
as.triangles3d.fm_mesh_3d

Convert a 3D mesh to a 3D rgl triangulation
call-stack

Call stack utility functions
fm_as_sfc

Conversion methods from mesh related objects to sfc
fm_evaluate

Methods for projecting to/from mesh objects
fm_dof

Function spece degrees of freedom
fm_fem

Compute finite element matrices
fm_crs_plot

Plot CRS and fm_crs objects
fm_as_tensor

Convert objects to fm_tensor
fm_crs_wkt

Handling CRS/WKT
fm_detect_manifold

Detect manifold type
fm_generate_colors

Generate text RGB color specifications.
fm_int_multi_sampler

Multi-domain sampler integration
fm_is_within

Query if points are inside a mesh
fm_assess

Interactive mesh building and diagnostics
fm_bary

Compute barycentric coordinates
fm_bbox

Bounding box class
fm_nonconvex_hull

Compute an extension of a spatial object
fm_mesh_intersection

Construct the intersection mesh of a mesh and a polygon
fm_mesh_components

Compute connected mesh subsets
fm_block

Blockwise aggregation matrices
fm_mesh_1d

Make a 1D mesh object
fm_diameter

Diameter bound for a geometric object
fm_lattice_2d

Make a lattice object
fm_gmrf

SPDE, GMRF, and Matérn process methods
fm_int_mesh_2d

Subset integration on a mesh
fm_int_mesh_2d_core

Integration scheme for mesh triangle interiors
fm_mesh_2d

Make a 2D mesh object
fm_nonconvex_hull_inla

Non-convex hull computation
fm_segm_contour_helper

Contour segment
fm_segm_list

Methods for fm_segm lists
fm_subdivide

Split triangles of a mesh into subtriangles
fm_store_points

Store points in different formats
fm_hexagon_lattice_orig

Create hexagon lattice points
fmesher-print

Print objects
fm_lattice_Nd

Lattice grids for N dimensions
fm_row_kron

Row-wise Kronecker products
fm_refine

Refine a 2d mesh
fmesher-package

fmesher: Triangle Meshes and Related Geometry Tools
fm_int

Multi-domain integration
fm_hexagon_lattice

Create hexagon lattice points
fmesher_mesh3d

3D tetrahedralisation storage
plot_rgl

Low level triangulation mesh plotting
fmesher-deprecated

Deprecated functions in fmesher
fm_vertices

Extract vertex locations from an fm_mesh_2d
plot.fm_segm

Draw fm_segm objects.
fmesher_qinv

Compute sparse matrix inverse
fmesher_fem

Finite element matrix computation
fmesher_globe_points

Globe points
fm_list

Handle lists of fmesher objects
fm_pixels

Generate lattice points covering a mesh
fm_simplify

Recursive curve simplification.
fm_tensor

Make a tensor product function space
fm_manifold

Query the mesh manifold type
fm_simplify_helper

Recursive curve simplification.
fm_qinv

Sparse partial inverse
fm_segm

Make a spatial segment object
fm_mesh_3d

Construct a 3D tetrahedralisation
fm_sizes

fm_sizes
fm_safe_sp

Check for potential sp version compatibility issues
fm_mesh_2d_map

Special coordinate mappings for fm_mesh_2d projections.
fmexample_sp

Add sp data to fmexample
fm_raw_basis

Basis functions for mesh manifolds
fm_rcdt_2d

Refined Constrained Delaunay Triangulation
fm_unify_coords

Unify coordinates to 3-column matrix
fmesher_subdivide

Subdivide triangles
fm_vertex_projection

Project integration points to mesh vertices
fm_split_lines

Split lines at triangle edges
fmesher_rcdt

Refined Constrained Delaunay Triangulation
fm_as_dgCMatrix

Conversion between sparse matrix types
local_testthat

Unit test helpers
plot.fm_mesh_2d

Draw a triangulation mesh object
fm_wkt_as_wkt_tree

Internal WKT handling
fmexample

Example mesh data
print.fm_evaluator

Print method for fm_evaluator()
print.fm_basis

Print method for fm_basis
geom_fm

ggplot2 geomes for fmesher related objects
fmesher_split_lines

Split lines at triangle edges
fm_transform

Object coordinate transformation
fmesher_spherical_bsplines1

Rotationally invariant spherical B-splines
fmesher_bary

Barycentric coordinate computation
fmesher_bary3d

Barycentric coordinate computation
fm_as_mesh_1d

Convert objects to fm_segm
fm_as_lattice_Nd

Convert objects to fm_lattice_Nd
fm_bary_simplex

Extract Simplex information for Barycentric coordinates
fm_as_lattice_2d

Convert objects to fm_lattice_2d
fm_basis

Compute mapping matrix between mesh function space and points
fm_as_mesh_2d

Convert objects to fm_mesh_2d
fm_as_collect

Convert objects to fm_collect
fm_as_fm

Convert objects to fmesher objects
fm_basis_helpers

Internal helper functions for mesh field evaluation
fm_bary_loc

Extract Euclidean Sgeometry from Barycentric coordinates