stla / MeshesOperations

Operations on 3D meshes with R
GNU General Public License v3.0
3 stars 0 forks source link

Interface to CGAL::PMP::centroid() #2

Closed dwoll closed 2 years ago

dwoll commented 2 years ago

MeshesOperations currently provides an interface to CGAL::PMP::volume() which is very convenient. It would be very nice to also have an interface to CGAL::PMP::centroid(). My use case is mesh similarity measures. The distance between the mesh centroids is one such measure, and could be derived from having the centroid. Many thanks in advance!

stla commented 2 years ago

Hi, The centroid is nothing but the average vertex, I think.

dwoll commented 2 years ago

The average vertex would be drawn towards regions with high mesh resolution (and thus higher number of vertices). Both CGAL as well as libigl do more complicated things:

https://github.com/CGAL/cgal/blob/40fdc6c6bc48eec140353fca3e930f578745f18b/Principal_component_analysis_LGPL/include/CGAL/centroid.h#L516

https://github.com/libigl/libigl/blob/574ab1a3a819b3798cf452fa44bcb47704a529c6/include/igl/centroid.cpp#L11

If you'd like, I can try to do a pull request with an interface to CGAL::PMP::centroid().

stla commented 2 years ago

Ah ok. The point embarassing me is that this package will never be on CRAN (because it is big and there are some warnings). As you have seen, I started to split it: PolygonSoup and Boov. I'm wondering where I could add the centroid feature. I'm ok for doing a new package but what could be its main purpose? Maybe a package MeshesUtilities? In which I could implement: volume, centroid, convex parts, clipping. What do you think? I also plan to do another package for the Minkowski addition.

dwoll commented 2 years ago

Splitting up for different sub-tasks seems like a good idea. Just how to carve the big library into pieces, I'm not sure, I don't know CGAL (or mesh processing for that matter) well enough to understand how coherent sets of functions could be defined. There certainly needs to be a "tools" or "utils" or "base" package for basic tasks that are re-used multiple times in more complex processing operations.

stla commented 2 years ago

MeshesTools sounds nice. I implemented the centroid now: MeshesTools.

dwoll commented 2 years ago

Many thanks!

stla commented 2 years ago

Hi @dwoll

I'm preparing a new package: cgalMeshes. It would contain everything included in the previous packages. Its advantage is that a R6 class is used to represent a mesh, that makes the package more comfortable for the user. Moreover, once you define a new mesh, it stays in memory, whereas with the other packages, the mesh was re-processed for each operation.

Example:

# read mesh from file
mesh <- cgalMesh$new("myfile.ply")
# triangulation
mesh$triangulate()
# volume
mesh$volume()
# centroid
mesh$centroid()
dwoll commented 2 years ago

Thanks for the heads up about your development plans! I've now written a first version of the Shiny front-end to your packages (and Rvcg) dedicated to calculate agreement measures for 3D meshes: https://github.com/dwoll/MeshAgreement (Shiny app in inst/MeshAgreement)

The package currently uses your packages SurfaceReconstruction, PolygonSoup, MeshesTools, and Boov. I'll follow along as you update these. One thing I haven't fully grasped is the difference between PolygonSoup and MeshesTools - both seem to have similar scope.

Many thanks for a brisk pace in bringing your packages to a stable state and pushing to CRAN! It's really nice to just have to write some front-end code for them to get a useful practical application.

stla commented 2 years ago

PolygonSoup only allows to get a consistent mesh from a polygon soup. And it contains the two functions toRGL and plotEdges, which are imported by the other packages. In MeshesTools I implemented the volume, the area, the distance from a point, the clipping, and the convex parts.

stla commented 2 years ago

Hi @dwoll

uses your packages SurfaceReconstruction, PolygonSoup, MeshesTools, and Boov.

Now you can abandon all these packages. I won't further develop these ones. Everything is contained in cgalMeshes. Except the surface reconstruction, but there's a new feature for the removal of self-intersections. It is experimental but it works fine with your two PLY files:

library(cgalMeshes)

mesh1 <- cgalMesh$new("inst/essais/mesh_1.ply")
mesh1$selfIntersects() # TRUE
mesh1$removeSelfIntersections()
mesh1$selfIntersects() # FALSE

mesh2 <- cgalMesh$new("inst/essais/mesh_2.ply")
mesh2$selfIntersects() # TRUE
mesh2$removeSelfIntersections()
mesh2$selfIntersects() # FALSE

# union
umesh <- mesh1$union(mesh2)
rglumesh <- umesh$getMesh(normals = FALSE)

rgl::shade3d(rglumesh, color = "orange")