mri
mri

Reputation: 540

Cut 3D Mesh in R

I've tried to create a 3D mesh looking like a cylinder and then cut it in half as following:

library(rgl)
library(Morpho)

xpos = c(-500,500)
ypos = c(0,0)
zpos = c(-200,-200)
rad = 50

cylinder = cylinder3d(center = cbind(xpos,ypos,zpos), radius = rad, sides = 100)
plot3d(cylinder, axes = FALSE, xlab = "", ylab = "", zlab ="")
upperHalf = cutMeshPlane(cylinder,v1 = c(0,0,-200), normal = c(0,0,1),keep.upper = TRUE)
plot3d(t(upperHalf$vb), axes = FALSE, xlab = "", ylab = "", zlab ="")
plot3d(upperHalf) #this produces an error: index out of bounds 

The first plot shows the cylinder with the red line implying the plane along which I'd like to cut the cylinder.

cylinder

The second plot shows the corner coordinates after using cutMeshPlane to separate the cylinder along the z-axis. As desired, only the upper half remains.

points

However, I cannot plot a new 3d mesh representing half the cylinder. I believe that the vertex indices from cylinder$ib were not updated in upperHalf$ib and that's why. However, I cannot figure out how to solve this problem or create a new 3d mesh with the remaining coordinates. Help is much appreciated!

Upvotes: 0

Views: 331

Answers (2)

Stefan Schlager
Stefan Schlager

Reputation: 11

I just updated cutMeshPlane to also work with quad meshes. I tested with your example (see below)

you can install it using devtools::install_github("zarquon42b/Morpho")

enter image description here

Upvotes: 1

user2554330
user2554330

Reputation: 44788

The Morpho::cutMeshPlane function requires a mesh made of triangles, but yours is made up of quadrilaterals. The simplest fix is to use rgl::clipMesh3d instead:

upperHalfrgl <- clipMesh3d(cylinder, "z", -200)
plot3d(upperHalfrgl, axes = FALSE, xlab = "", ylab = "", zlab ="")

enter image description here

You could also convert your mesh to triangles using the internal rgl function rgl:::as.tmesh3d, but it's risky to use internal functions.

Upvotes: 1

Related Questions