有没有办法在R中找到由一组顶点定义的3D对象(面(的维度(对象是顶点的凸包(。也就是说,定义函数getDim()
.
vertices<-matrix(c(1,1,1,1,1,1), ncol = 3, byrow = TRUE)
getDim(vertices) # should return 0
vertices<-matrix(c(0,0,0,1,1,1,2,2,2,3,3,3), ncol = 3, byrow = TRUE)
getDim(vertices) # should return 1
vertices<-matrix(c(0,0,0,0,1,1,0,2,2,0,0,2), ncol = 3, byrow = TRUE)
getDim(vertices) # should return 2
vertices<-matrix(c(0,0,0,0,1,1,0,2,2,0,0,2,1,1,1), ncol = 3, byrow = TRUE)
getDim(vertices) # should return 3
感谢 Stephane Laurent 的提示
getDim3D<-function(points) {
x <- unique(points)
if (dim(x)[1]==1) return(0)
x <- x[2:dim(x)[1],,drop=F] - matrix(rep(x[1,], times = dim(x)[1]-1), ncol = dim(x)[2], byrow = TRUE)
return(Matrix::rankMatrix(x)[1])
}