MeshesIntersection: Meshes intersection

View source: R/operations.R

MeshesIntersectionR Documentation

Meshes intersection

Description

Computes the intersection of the given meshes.

Usage

MeshesIntersection(
  meshes,
  clean = FALSE,
  normals = FALSE,
  numbersType = "double"
)

Arguments

meshes

a list of meshes, each being either a rgl mesh, or as a list with (at least) two fields: vertices and faces; the vertices matrix must have the bigq class if numbersType="gmp", otherwise it must be numeric

clean

Boolean, whether to clean the input meshes (merging duplicated vertices, duplicated faces, removing isolated vertices) as well as the output mesh

normals

Boolean, whether to return the per-vertex normals of the output mesh

numbersType

the type of the numbers used in C++ for the computations; must be one of "double", "lazyExact" (a type provided by CGAL for exact computations), or "gmp" (exact computations with rational numbers); of course using exact computations is slower but more accurate

Value

A triangle mesh given as a list with fields vertices, faces, edges, exteriorEdges, gmpvertices if numbersType="gmp", and normals if normals=TRUE.

Examples

library(MeshesOperations)
library(rgl)

# mesh one: truncated icosahedron; we triangulate it for plotting
mesh1 <- Mesh(
  mesh = truncatedIcosahedron,
  triangulate = TRUE, normals = FALSE,
  numbersType = "lazyExact"
)

# mesh two: a cube
cube <- translate3d( # (from the rgl package)
  cube3d(), 2, 0, 0
)
mesh2 <-
  list(vertices = t(cube[["vb"]][-4L, ]), faces = t(cube[["ib"]]))

# compute the intersection
inter <- MeshesIntersection(list(mesh1, mesh2))

# plot
rglmesh1 <- toRGL(mesh1)
rglinter <- toRGL(inter)
open3d(windowRect = c(50, 50, 562, 562))
shade3d(rglmesh1, color = "yellow", alpha = 0.2)
shade3d(cube, color = "cyan", alpha = 0.2)
shade3d(rglinter, color = "red")
plotEdges(
  vertices = inter[["vertices"]], edges = inter[["exteriorEdges"]],
  edgesAsTubes = FALSE, lwd = 3, verticesAsSpheres = FALSE
)

# other example, with 'gmp' rational numbers ####
library(MeshesOperations)
library(gmp)
library(rgl)

cube <- cube3d()

rglmesh1 <- cube
mesh1 <-
  list(vertices = t(cube[["vb"]][-4L, ]), faces = t(cube[["ib"]]))
mesh1[["vertices"]] <- as.bigq(mesh1[["vertices"]])

rotMatrix <- t(cbind( # pi/3 around a great diagonal
  as.bigq(c(2, -1, 2), c(3, 3, 3)),
  as.bigq(c(2, 2, -1), c(3, 3, 3)),
  as.bigq(c(-1, 2, 2), c(3, 3, 3))
))
mesh2 <-
  list(vertices = t(cube[["vb"]][-4L, ]), faces = t(cube[["ib"]]))
mesh2[["vertices"]] <- as.bigq(mesh2[["vertices"]]) %*% rotMatrix
rglmesh2 <- rotate3d(cube, pi/3, 1, 1, 1)

inter <- MeshesIntersection(list(mesh1, mesh2), numbersType = "gmp")
# perfect vertices:
inter[["gmpVertices"]]
rglinter <- toRGL(inter)

open3d(windowRect = c(50, 50, 562, 562), zoom = 0.9)
bg3d("#363940")
shade3d(rglmesh1, color = "yellow", alpha = 0.2)
shade3d(rglmesh2, color = "orange", alpha = 0.2)
shade3d(rglinter, color = "hotpink")
plotEdges(
  inter[["vertices"]], inter[["exteriorEdges"]],
  only = inter[["exteriorVertices"]],
  color = "firebrick",
  tubesRadius = 0.05, spheresRadius = 0.07
)

stla/MeshesOperations documentation built on Oct. 23, 2022, 8:23 a.m.