How to find center point of 3d convexl hull, 3d polygon or polyhedron (all by Delaunay triangulation) in R

69 views Asked by At

Here is the answer to solving the center point of 2d convex hull: Centroid of the minimum convex polygon

how to find center point of 3d convex hull(package:geometry) in R?

example for bunny data:

library("onion")
library("geometry")
library("rgl")

data(bunny)
bunnyConvexHull <- convhulln(bunny, output.options = TRUE)#convex hull
plot3d(bunny, col = "pink") #bunny poins
plot(bunnyConvexHull, col = "gray", alpha = 0.3, add = TRUE)
1

There are 1 answers

2
Stéphane Laurent On BEST ANSWER

Once you have a triangle rgl mesh, you can get its centroid with the packages cgalMeshes. Unfortunately, this package is not on CRAN currently. You can install it by running (this takes a while):

remotes::install_github("stla/cgalMeshes@github", dependencies = TRUE, build_vignettes = TRUE)

To get a rgl mesh of the convex hull, I didn't manage to use the to.mesh3d function of the geometry package. Here is how to get this mesh with the cxhull package (also based on the C(++) library qhull):

library(cxhull)
# take bunny data from the onion package
data(bunny, package = "onion")
# convex hull
hull <- cxhull(bunny, triangulate = TRUE)
# extract vertices and faces
hmesh <- hullMesh(hull)
vertices  <- hmesh$vertices
triangles <- hmesh$faces
# the triangles are given in terms of the vertex names
# let's transform them to get them in terms of the vertex indices
dict        <- 1L:nrow(vertices)
names(dict) <- rownames(vertices)
triangles <- apply(triangles, c(1L, 2L), function(x) dict[as.character(x)])
# make a rgl mesh
library(rgl)
rmesh <- tmesh3d(
  vertices = t(vertices),
  indices  = t(triangles)
)

Finally:

# now let's get the centroid
library(cgalMeshes)
mesh <- cgalMesh$new(rmesh)
mesh$centroid()
# [1] -0.02735468  0.09912979  0.00442025

EDIT

I've finally found how to get the rgl mesh with the geometry package; the point was to use the FA option:

library(geometry)
# take bunny data from the onion package
data(bunny, package = "onion")
# convex hull
hull <- convhulln(bunny, options = "Tv FA")
# make a rgl mesh
rmesh <- to.mesh3d(hull)