tessellation icon indicating copy to clipboard operation
tessellation copied to clipboard

Request for area and volume calculations

Open Dailer opened this issue 1 year ago • 4 comments

It is possible to include an output for the surface area and volume of the convex 3D hull of a Voronoi cell? That could be very useful, for example, in astronomy, where the inverse of the Voronoi cell volume is a proxy for the local density.

Thanks.

Dailer avatar Nov 12 '23 11:11 Dailer

Hello @Dailer

I never saw a non-convex Voronoï cell. Does that exist? If I'm right, the convex hull of the cell is nothing but the cell.

I would use the cxhull package to get the volume and the area.

library(tessellation)
# take the vertices of a bounded Voronoï cell
d <- delaunay(centricCuboctahedron())
v <- voronoi(d)
vcell <- Filter(isBoundedCell, v)[[1]]
vertices <- cellVertices(vcell)

# compute its triangulated convex hull
library(cxhull)
hull <- cxhull(vertices, triangulate = TRUE)
# then you have the volume:
hull$volume

# get the vertices of all triangles
trgls <- TrianglesXYZ(hull)

# compute the area of these triangles
crossProduct <- function(v, w) {
  c(
    v[2L] * w[3L] - v[3L] * w[2L],
    v[3L] * w[1L] - v[1L] * w[3L],
    v[1L] * w[2L] - v[2L] * w[1L]
  )
}
triangleArea <- function(A, B, C) {
  v <- B - A
  w <- C - A
  sqrt(c(crossprod(crossProduct(v, w)))) / 2
}

ntrgls <- nrow(trgls) / 3
area <- 0
for(i in 1:ntrgls) {
  A <- trgls[3*(i-1)+1, ]
  B <- trgls[3*(i-1)+2, ]
  C <- trgls[3*(i-1)+3, ]
  area <- area + triangleArea(A, B, C)
}

stla avatar Nov 13 '23 08:11 stla

Thank you very much!

Dailer avatar Nov 16 '23 19:11 Dailer

I included it in the package now. Function cellVolume.

stla avatar Nov 16 '23 21:11 stla

Great! Thanks!

Dailer avatar Nov 17 '23 01:11 Dailer