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

Includes a port of inla mesh inla.mesh.create (as fm_rcdt_2d_inla()) and inla.mesh.2d interfaces.

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

(mesh <- fm_mesh_2d_inla(
  boundary = fm_extensions(cbind(0, 0), convex = c(1, 1.5)),
  max.edge = c(0.5, 1)
))
#> fm_mesh_2d object:
#>   Manifold:  R2
#>   V / E / T: 57 / 152 / 96
#>   Euler char.:   1
#>   Constraints:   Boundary: 16 boundary edges (1 group: 1), Interior: 16 interior edges (1 group: 1)
#>   Bounding box: (-1.499887, 1.499887) x (-1.499887, 1.499887)
#>   Basis d.o.f.:  57
ggplot() +
  geom_fm(data = mesh) +
  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_crs("longlat_globe"))
#> Coordinate Reference System:
#>   User input: +proj=longlat +ellps=sphere +no_defs 
#>   wkt:
#> GEOGCRS["unknown",
#>     DATUM["Unknown based on Normal Sphere (r=6370997) ellipsoid",
#>         ELLIPSOID["Normal Sphere (r=6370997)",6370997,0,
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]]],
#>     PRIMEM["Greenwich",0,
#>         ANGLEUNIT["degree",0.0174532925199433],
#>         ID["EPSG",8901]],
#>     CS[ellipsoidal,2],
#>         AXIS["longitude",east,
#>             ORDER[1],
#>             ANGLEUNIT["degree",0.0174532925199433,
#>                 ID["EPSG",9122]]],
#>         AXIS["latitude",north,
#>             ORDER[2],
#>             ANGLEUNIT["degree",0.0174532925199433,
#>                 ID["EPSG",9122]]]]

# longlat for a sphere of radius 1m
print(fm_crs("longlat_norm"))
#> Coordinate Reference System:
#>   User input: +proj=longlat +R=1 +no_defs 
#>   wkt:
#> GEOGCRS["unknown",
#>     DATUM["unknown",
#>         ELLIPSOID["unknown",1,0,
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]]],
#>     PRIMEM["Reference meridian",0,
#>         ANGLEUNIT["degree",0.0174532925199433,
#>             ID["EPSG",9122]]],
#>     CS[ellipsoidal,2],
#>         AXIS["longitude",east,
#>             ORDER[1],
#>             ANGLEUNIT["degree",0.0174532925199433,
#>                 ID["EPSG",9122]]],
#>         AXIS["latitude",north,
#>             ORDER[2],
#>             ANGLEUNIT["degree",0.0174532925199433,
#>                 ID["EPSG",9122]]]]

# A sphere of radius 1m
print(fm_crs("sphere"))
#> Coordinate Reference System:
#>   User input: +proj=geocent +R=1 +units=m +no_defs 
#>   wkt:
#> GEODCRS["unknown",
#>     DATUM["unknown",
#>         ELLIPSOID["unknown",1,0,
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]]],
#>     PRIMEM["Reference meridian",0,
#>         ANGLEUNIT["degree",0.0174532925199433,
#>             ID["EPSG",9122]]],
#>     CS[Cartesian,3],
#>         AXIS["(X)",geocentricX,
#>             ORDER[1],
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]],
#>         AXIS["(Y)",geocentricY,
#>             ORDER[2],
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]],
#>         AXIS["(Z)",geocentricZ,
#>             ORDER[3],
#>             LENGTHUNIT["metre",1,
#>                 ID["EPSG",9001]]]]

Copy Link

Version

Install

install.packages('fmesher')

Monthly Downloads

5,816

Version

0.3.0

License

MPL-2.0

Issues

Pull Requests

Stars

Forks

Maintainer

Finn Lindgren

Last Published

March 18th, 2025

Functions in fmesher (0.3.0)

fm_bary_simplex

Extract Simplex information for Barycentric coordinates
fm_bbox

Bounding box class
fm_bary_loc

Extract Euclidean Sgeometry from Barycentric coordinates
fm_as_segm

Convert objects to fm_segm
fm_as_tensor

Convert objects to fm_tensor
fm_assess

Interactive mesh building and diagnostics
fm_bary

Compute barycentric coordinates
fm_as_sfc

Conversion methods from mesh related objects to sfc
fm_crs_plot

Plot CRS and fm_crs objects
fm_contains

Check which mesh triangles are inside a polygon
fm_cprod

(Blockwise) cross product of integration points
fm_crs

Obtain coordinate reference system object
fm_centroids

Extract triangle centroids from an fm_mesh_2d
fm_crs<-

Assignment operators for crs information objects
fm_block

Blockwise aggregation matrices
fm_crs_wkt

Handling CRS/WKT
fm_dof

Function spece degrees of freedom
fm_fem

Compute finite element matrices
fm_evaluate

Methods for projecting to/from mesh objects
fm_generate_colors

Generate text RGB color specifications.
fm_gmrf

SPDE, GMRF, and Matérn process methods
fm_int

Multi-domain integration
fm_int_mesh_2d

Subset integration on a mesh
fm_int_multi_sampler

Multi-domain sampler integration
fm_is_within

Query if points are inside a mesh
fm_lattice_2d

Make a lattice object
fm_int_mesh_2d_core

Integration scheme for mesh triangle interiors
fm_lattice_Nd

Lattice grids for N dimensions
fm_mesh_3d

Construct a 3D tetrahedralisation
fm_mesh_2d_map

Special coordinate mappings for fm_mesh_2d projections.
fm_rcdt_2d

Refined Constrained Delaunay Triangulation
fm_nonconvex_hull_inla

Non-convex hull computation
fm_nonconvex_hull

Compute an extension of a spatial object
fm_mesh_2d

Make a 2D mesh object
fm_mesh_1d

Make a 1D mesh object
fm_pixels

Generate lattice points covering a mesh
fm_mesh_intersection

Construct the intersection mesh of a mesh and a polygon
fm_refine

Refine a 2d mesh
fm_row_kron

Row-wise Kronecker products
fm_simplify_helper

Recursive curve simplification.
fm_segm_list

Methods for fm_segm lists
fm_transform

Object coordinate transformation
fm_tensor

Make a tensor product function space
fm_split_lines

Split lines at triangle edges
fm_safe_sp

Check for potential sp version compatibility issues
fm_store_points

Store points in different formats
fmesher-print

Print objects
fmesher-package

fmesher: Triangle Meshes and Related Geometry Tools
fm_simplify

Recursive curve simplification.
fm_crs_is_null

Check if a crs is NULL or NA
fm_diameter

Diameter bound for a geometric object
fm_list

Handle lists of fmesher objects
fm_manifold

Query the mesh manifold type
fm_detect_manifold

Detect manifold type
fm_qinv

Sparse partial inverse
fm_crs_is_identical

Check if two CRS objects are identical
fmesher_mesh3d

3D tetrahedralisation storage
fmesher_qinv

Compute sparse matrix inverse
fmesher_rcdt

Refined Constrained Delaunay Triangulation
plot.fm_segm

Draw fm_segm objects.
fmesher_globe_points

Globe points
plot_rgl

Low level triangulation mesh plotting
print.fm_basis

Print method for fm_basis
fmexample

Example mesh data
fmesher_fem

Finite element matrix computation
print.fm_evaluator

Print method for fm_evaluator()
fm_subdivide

Split triangles of a mesh into subtriangles
fmesher_subdivide

Subdivide triangles
fmesher-deprecated

Deprecated functions in fmesher
fm_vertices

Extract vertex locations from an fm_mesh_2d
fmesher_split_lines

Split lines at triangle edges
fmesher_spherical_bsplines1

Rotationally invariant spherical B-splines
plot.fm_mesh_2d

Draw a triangulation mesh object
local_testthat

Unit test helpers
fm_vertex_projection

Project integration points to mesh vertices
fm_unify_coords

Unify coordinates to 3-column matrix
fmexample_sp

Add sp data to fmexample
fm_segm

Make a spatial segment object
fm_raw_basis

Basis functions for mesh manifolds
fmesher_bary

Barycentric coordinate computation
fmesher_bary3d

Barycentric coordinate computation
geom_fm

ggplot2 geomes for fmesher related objects
fm_segm_contour_helper

Contour segment
fm_wkt_as_wkt_tree

Internal WKT handling
fm_as_dgCMatrix

Conversion between sparse matrix types
as.triangles3d.fm_mesh_3d

Convert a 3D mesh to a 3D rgl triangulation
fm_as_mesh_2d

Convert objects to fm_mesh_2d
fm_as_lattice_Nd

Convert objects to fm_lattice_Nd
fm_as_mesh_3d

Convert objects to fm_mesh_3d
fm_CRS

Create a coordinate reference system object
fm_CRS_as_list

Show expanded CRS arguments
fm_as_mesh_1d

Convert objects to fm_segm
call-stack

Call stack utility functions
fm_as_fm

Convert objects to fmesher objects
fm_as_lattice_2d

Convert objects to fm_lattice_2d
fm_basis_helpers

Internal helper functions for mesh field evaluation
fm_basis

Compute mapping matrix between mesh function space and points