Learn R Programming

⚠️There's a newer version (0.5.0) of this package.Take me there.

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 of fmesher:

install.packages("fmesher")

You can install the latest stable release of fmesher from GitHub:

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

And the development version with:

# install.packages("remotes")
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",
  INLA = "https://inla.r-inla-download.org/R/testing",
  CRAN = "https://cloud.r-project.org"
))

install.packages("fmesher")

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)

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:   16 boundary edges (1 group: 1), 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

6,984

Version

0.1.7

License

MPL-2.0

Issues

Pull Requests

Stars

Forks

Maintainer

Finn Lindgren

Last Published

July 1st, 2024

Functions in fmesher (0.1.7)

fm_bbox

Bounding box class
fm_bary

Compute barycentric coordinates
fm_crs

Obtain coordinate reference system object
fm_centroids

Extract triangle centroids from an fm_mesh_2d
fm_contains

Check which mesh triangles are inside a polygon
fm_block

Blockwise aggregation matrices
fm_cprod

(Blockwise) cross product of integration points
fm_crs_is_identical

Check if two CRS objects are identical
fm_basis

Compute mapping matrix between mesh function space and points
fm_crs<-

Assignment operators for crs information objects
fm_evaluator_helpers

Internal helper functions for mesh field evaluation
fm_detect_manifold

Detect manifold type
fm_gmrf

SPDE, GMRF, and Matérn process methods
fm_crs_wkt

Handling CRS/WKT
fm_dof

Function spece degrees of freedom
fm_crs_is_null

Check if a crs is NULL or NA
fm_fem

Compute finite element matrices
fm_generate_colors

Generate text RGB color specifications.
fm_diameter

Diameter bound for a geometric object
fm_evaluate

Methods for projecting to/from mesh objects
fm_manifold

Query the mesh manifold type
fm_list

Handle lists of fmesher objects
fm_int_mesh_2d

Subset integration on a mesh
fm_int

Multi-domain integration
fm_int_multi_sampler

Multi-domain sampler integration
fm_mesh_2d

Make a 2D mesh object
fm_mesh_1d

Make a 1D mesh object
fm_int_mesh_2d_core

Integration scheme for mesh triangle interiors
fm_rcdt_2d

Refined Constrained Delaunay Triangulation
fm_is_within

Query if points are inside a mesh
fm_safe_sp

Check for potential sp version compatibility issues
fm_pixels

Generate lattice points covering a mesh
fm_raw_basis

Basis functions for mesh manifolds
fm_mesh_2d_map

Special coordinate mappings for fm_mesh_2d projections.
fm_nonconvex_hull

Compute an extension of a spatial object
fm_lattice_2d

Make a lattice object
fm_nonconvex_hull_inla

Non-convex hull computation
fm_row_kron

Row-wise Kronecker products
fm_segm

Make a spatial segment object
fm_refine

Refine a 2d mesh
fm_mesh_intersection

Construct the intersection mesh of a mesh and a polygon
fm_subdivide

Split triangles of a mesh into subtriangles
fm_split_lines

Split lines at triangle edges
fm_simplify_helper

Recursive curve simplification.
fm_segm_contour_helper

Contour segment
fm_store_points

Store points in different formats
fm_vertices

Extract vertex locations from an fm_mesh_2d
fmesher-print

Print objects
fm_segm_list

Methods for fm_segm lists
fm_simplify

Recursive curve simplification.
fmesher-deprecated

Deprecated functions in fmesher
fmesher-package

fmesher: Triangle Meshes and Related Geometry Tools
fm_tensor

Make a tensor product function space
fmesher_globe_points

Globe points
plot.fm_mesh_2d

Draw a triangulation mesh object
fmesher_rcdt

Refined Constrained Delaunay Triangulation
plot.fm_segm

Draw fm_segm objects.
fmesher_spherical_bsplines1

Rotationally invariant spherical B-splines
fmesher_bary

Barycentric coordinate computation
fm_as_dgCMatrix

Conversion between sparse matrix types
fmesher_fem

Finite element matrix computation
globeproj

Old globe projection methods
fmexample

Example mesh data
geom_fm

ggplot2 geomes for fmesher related objects
fmesher_split_lines

Split lines at triangle edges
plot_PolySet

Plot a projected PolySet
fmesher_subdivide

Subdivide triangles
plot_rgl

Low level triangulation mesh plotting
plot_globeproj

Plot a globeproj object
fm_wkt_as_wkt_tree

Internal WKT handling
local_testthat

Unit test helpers
fm_vertex_projection

Project integration points to mesh vertices
fm_transform

Object coordinate transformation
fm_unify_coords

Unify coordinates to 3-column matrix
fm_as_sfc

Conversion methods from mesh related objects to sfc
fm_CRS_as_list

Show expanded CRS arguments
fm_as_mesh_1d

Convert objects to fm_segm
fm_as_tensor

Convert objects to fm_tensor
fm_as_segm

Convert objects to fm_segm
fm_as_fm

Convert objects to fmesher objects
fm_as_mesh_2d

Convert objects to fm_mesh_2d
fm_CRS

Create a coordinate reference system object
fm_as_lattice_2d

Convert objects to fm_lattice_2d
call-stack

Call stack utility functions