Package: rmarchingcubes 0.1.3

S. H. Wilks

rmarchingcubes: Calculate 3D Contour Meshes Using the Marching Cubes Algorithm

A port of the C++ routine for applying the marching cubes algorithm written by Thomas Lewiner et al. (2012) <doi:10.1080/10867651.2003.10487582> into an R package. The package supplies the contour3d() function, which takes a 3-dimensional array of voxel data and calculates the vertices, vertex normals, and faces for a 3d mesh representing the contour(s) at a given level.

Authors:S. H. Wilks <sw463@cam.ac.uk> [aut, cre], Thomas Lewiner <lewiner@gmail.com> [aut]

rmarchingcubes_0.1.3.tar.gz
rmarchingcubes_0.1.3.tar.gz(r-4.5-noble)rmarchingcubes_0.1.3.tar.gz(r-4.4-noble)
rmarchingcubes_0.1.3.tgz(r-4.4-emscripten)rmarchingcubes_0.1.3.tgz(r-4.3-emscripten)
rmarchingcubes.pdf |rmarchingcubes.html
rmarchingcubes/json (API)
NEWS

# Install 'rmarchingcubes' in R:
install.packages('rmarchingcubes', repos = 'https://cloud.r-project.org')

Bug tracker:https://github.com/shwilks/rmarchingcubes/issues

Uses libs:
  • openblas– Optimized BLAS
  • c++– GNU Standard C++ Library v3

On CRAN:

Conda-Forge:

openblascpp

3.83 score 1 stars 1 packages 45 scripts 298 downloads 1 exports 2 dependencies

Last updated 4 years agofrom:e78fd30207. Checks:1 OK, 1 NOTE. Indexed: no.

TargetResultLatest binary
Doc / VignettesOKFeb 20 2025
R-4.5-linux-x86_64NOTEFeb 20 2025

Exports:contour3d

Dependencies:RcppRcppArmadillo

Calculating 3d contours

Rendered fromcalculating_3d_contours.Rmdusingknitr::rmarkdownon Feb 20 2025.

Last update: 2021-06-16
Started: 2021-06-16

Citation

To cite package ‘rmarchingcubes’ in publications use:

Wilks SH, Lewiner T (2021). rmarchingcubes: Calculate 3D Contour Meshes Using the Marching Cubes Algorithm. R package version 0.1.3, https://CRAN.R-project.org/package=rmarchingcubes.

ATTENTION: This citation information has been auto-generated from the package DESCRIPTION file and may need manual editing, see ‘help("citation")’.

Corresponding BibTeX entry:

  @Manual{,
    title = {rmarchingcubes: Calculate 3D Contour Meshes Using the
      Marching Cubes Algorithm},
    author = {S. H. Wilks and Thomas Lewiner},
    year = {2021},
    note = {R package version 0.1.3},
    url = {https://CRAN.R-project.org/package=rmarchingcubes},
  }

Readme and manuals

rmarchingcubes

An R package implementing the efficient marching cubes algorithm written by Thomas Lewiner. Minor changes have been made to the code in order to work with the armadillo C++ library.

Installation

devtools::install_github("shwilks/rmarchingcubes")

Example usage

The key and only function exported in this package is contour3d(), taking a 3-dimensional array of values and returning the calculated 3d mesh object fit to this data. A similar function with more flexibility for different inputs and outputs is provided in the misc3d package. The implementation here has two key advantages, firstly since the implementation is based on compiled C++ code the result should be considerably quicker, perhaps by orders of magnitude, secondly normals are additionally calculated and returned for each vertex making up the 3d contour.

# Function to generate values decreasing in a sphere-like way
f <- function(coords) coords[1]^2 + coords[2]^2 + coords[3]^2

# Set grid coordinates at which to calculate values
x <- seq(-2,2,len = 20)
y <- seq(-2,2,len = 20)
z <- seq(-2,2,len = 20)

# Calculate values across grid coordinates
grid_coords <- expand.grid(x, y, z)
grid_values <- apply(grid_coords, 1, f)

# Convert to a 3d array
grid_array <- array(grid_values, dim = c(length(x), length(y), length(z)))

# Calculate 3d contour from the grid data at a contour level of value 4
contour_shape <- contour3d(
  griddata = grid_array, 
  level = 4,
  x = x,
  y = y,
  z = z
)

# Optionally view the output using the r3js package
# devtools::install_github("shwilks/r3js")

# Setup plot object
data3js <- r3js::plot3js(
  x = x,
  y = y,
  z = z,
  type = "n"
)

# Add shape according to the calculated contours
data3js <- r3js::shape3js(
  data3js,
  vertices = contour_shape$vertices,
  faces = contour_shape$triangles,
  normals = contour_shape$normals,
  col = "red"
)

# View the plot
r3js::r3js(data3js)