Perform boolean operations on meshes in RvtkStatismo
19 Feb 2016Due to the wrappers in RvtkStatismo for meshes from R to vtkPolyData, extending the functionality of this package is quite easy. Recently, I added the functionality to perform boolean operations on triangular meshes using the new function vtkBooleanOp
.
It creates Unions, Intersections and Differences from two triangular meshes stored in R as objects of class mesh3d
.
For creating intersection planes based on an object’s bounding box, the function BBoxSlices
was added to mesheR.
As usual, here is some example code, ready to be run in an R:
Example 1: create the union of two spheres (Figure 1)
require(Rvcg);require(Morpho);require(rgl)
require(RvtkStatismo)
## create two unit spheres
sphere1 <- vcgSphere()
sphere2 <- vcgSphere()
## translate sphere2 by half its radius
sphere2 <- translate3d(sphere2,0,0,0.5)
union <- vtkBooleanOp(sphere1,sphere2)
## create normals for smooth rendering
union <- vcgUpdateNormals(union)
shade3d(union,alpha=0.5,col=2)
wire3d(union)
Example 2: crop a mesh by a bounding box slice (Figure 2 and Figure 3)
require(Rvcg)
require(rgl)
require(mesheR)
sphere <- vcgSphere()
## get corners of the bounding box
bbox <- getMeshBox(sphere,pca=FALSE)
## get a triangular mesh consisting of 3 triangles cutting a slice from the y-z plane
slice <- BBoxSlices(bbox,axis = 1,percent = 0.7)
myplot <- plot3d(sphere,col="white")
shade3d(slice,col=2,alpha=0.5)
## now use the slice to get the difference
cuthead <- vtkBooleanOp(sphere,slice,type=2)
## create normals for smooth rendering
cuthead <- vcgUpdateNormals(cuthead)
shade3d(cuthead,col="white")