MeshesIntersection: Meshes intersection

View source: R/meshes.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 triangular meshes, each given as a list with (at least) two fields: vertices and faces; the 'vertices' matrix must have the bigq class if numberTypes="gmp", otherwise it must be numeric

clean

Boolean, whether to clean the input meshes (merging duplicated vertices, duplicated faces, removed 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 triangular mesh given as a list with fields vertices, faces, edges, exteriorEdges, gmpvertices if numberTypes="gmp", and normals if normals=TRUE.

Examples

library(RCGAL)
library(rgl)

# mesh one: truncated icosahedron; one has to triangulate it
mesh1 <- Mesh(
  truncatedIcosahedron[["vertices"]],
  truncatedIcosahedron[["faces"]],
  triangulate = TRUE, normals = FALSE
)

# mesh two: a cube; one also has to triangulate it
cube <- translate3d( # (from the rgl package)
  cube3d(), 2, 0, 0
)
vertices <- t(cube$vb[-4L, ])
faces <- t(cube$ib)
mesh2 <- Mesh(vertices, faces, triangulate = TRUE, normals = FALSE)

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

# plot
rglmesh1 <- tmesh3d(
  vertices = t(mesh1[["vertices"]]),
  indices = t(mesh1[["faces"]]),
  homogeneous = FALSE
)
rglinter <- tmesh3d(
  vertices = t(inter[["vertices"]]),
  indices = t(inter[["faces"]]),
  homogeneous = FALSE
)
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(RCGAL)
library(gmp)
library(rgl)

cube <- cube3d()
vertices <- t(cube$vb[-4L, ])
faces <- t(cube$ib)

rglmesh1 <- cube
mesh1 <- Mesh(vertices, faces, triangulate = TRUE, normals = FALSE)
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 <- Mesh(vertices, faces, triangulate = TRUE, normals = FALSE)
mesh2$vertices <- as.bigq(vertices) %*% rotMatrix
rglmesh2 <- rotate3d(cube, pi/3, 1, 1, 1)

inter <- MeshesIntersection(list(mesh1, mesh2), numbersType = "gmp")
# perfect vertices:
inter[["vertices"]]
rglinter <- tmesh3d(
  vertices = t(inter[["vertices"]]),
  indices = t(inter[["faces"]]),
  homogeneous = FALSE
)

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/RCGAL documentation built on June 15, 2022, 6:45 a.m.