MeshesIntersection: Meshes intersection

View source: R/operations.R

MeshesIntersectionR Documentation

Meshes intersection

Description

Computes the intersection of the given meshes.

Usage

MeshesIntersection(meshes, clean = TRUE, normals = FALSE)

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 can be a numeric matrix or a matrix of bigq rational numbers (from the gmp package)

clean

Boolean, whether to clean the meshes (merging duplicated vertices, duplicated faces, removing isolated vertices); set to FALSE if you are sure your meshes are clean, to gain some speed

normals

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

Value

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

Examples

library(Boov)
library(rgl)

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

# mesh two: a cube
mesh2 <- translate3d( # (from the rgl package)
  cube3d(), 2, 0, 0
)

# 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(mesh2, 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(Boov)
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))
# 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
)

Boov documentation built on Oct. 31, 2022, 5:05 p.m.