Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for area and volume calculations #2

Open
Dailer opened this issue Nov 12, 2023 · 4 comments
Open

Request for area and volume calculations #2

Dailer opened this issue Nov 12, 2023 · 4 comments

Comments

@Dailer
Copy link

Dailer commented Nov 12, 2023

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.

@stla
Copy link
Owner

stla commented Nov 13, 2023

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)
}

@Dailer
Copy link
Author

Dailer commented Nov 16, 2023

Thank you very much!

@stla
Copy link
Owner

stla commented Nov 16, 2023

I included it in the package now. Function cellVolume.

@Dailer
Copy link
Author

Dailer commented Nov 17, 2023

Great! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants