I have a list of points and a list of simplices. I would like to plot the simplices in 3D given their vertices. Essentially, I am looking for the equivalent of segment() in 3D.
Example
Pts<-matrix(c(0,0,0,1,0,0,0,1,0,0,0,1),ncol =3,byrow=TRUE)
Simplex<-c(1,2,3,4)
So, I am looking for a way to input Pts and Simplex and getting a plot of the tetrahedron.
I've tried searching but so far the only possibility seems to write out the functions for the linear spaces and plot those. Any tips will be highly appreciated.
With the 'rgl' package:
library(rgl)
vertices <- rbind(
c(0, 0, 0),
c(1, 0, 0),
c(0, 1, 0),
c(0, 0, 1)
)
faces <- combn(4,3)
for(f in 1:4){
triangles3d(rbind(
vertices[faces[1,f],],
vertices[faces[2,f],],
vertices[faces[3,f],]
), color="red", alpha=0.4)
}
You can add the edges and the vertices:
# add edges as thin cylinders
edges <- combn(4, 2)
for(e in 1:6){
shade3d(cylinder3d(rbind(vertices[edges[1,e],],vertices[edges[2,e],]),
radius = 0.02, sides = 30), col="yellow")
}
# add vertices as small spheres
spheres3d(vertices, radius= 0.03, color = "yellow")
Not as pretty or flexible, but here's a base R version using persp
and segments
for fun:
## empty perspective plot
tm <- persp(matrix(rep(0,4), nrow=2),
xlim=c(-1,1), ylim=c(-1,1), zlim=c(-1,1),
col="#00000000", border=NA, theta=30, phi=50, xlab="x")
## project points into 3d space
tpts <- data.frame(trans3d(pmat=tm, x=Pts[,1], y=Pts[,2], z=Pts[,3]))
## draw each segment
sgs <- combn(seq_len(nrow(tpts)), 2,
FUN=function(r) unlist(tpts[r,]), simplify=FALSE)
lapply(sgs, function(x) segments(x[1], x[3], x[2], x[4], col="red"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.