Relund
Relund

Reputation: 651

How to find the convex hull of a 3D facet in R

Is there a way to find the vertices of a facet in R given a set of points (the facet is the convex hull of the points and is of dimension 2). That is, how to define function convexHull:

vertices<-matrix(c(0,0,0,0,1,1,0,2,2,0,0,2), ncol = 3, byrow = TRUE)  # ex1
vertices<-matrix(c(1,0,0,1,1,1,1,2,2,3,1,1), ncol = 3, byrow = TRUE)  # ex2 (updated question)
vertices # one vertex in each row.
convexHull(vertices)  # should return indices 1,3,4 (vertex 1,3 and 4 since vertex 2 is a convex combination of 1 and 3)

Upvotes: 1

Views: 267

Answers (1)

Relund
Relund

Reputation: 651

Thanks to user2554330 for hints.

convexHull <- function(points) {
    points <- unique(points)
    l <- dim(points)[1]
    n <- dim(points)[2]
    comb <- t(combn(n,2))
    for (i in 1:dim(comb)[1]) {  # simple projection down on each axis
      p <- unique(points[,comb[i,]])
      if (l == dim(p)[1]) {
        return(chull(points[,comb[i,]]))
      }
    }
    stop("Cannot find the vertices!")
}

Upvotes: 1

Related Questions