stla / MeshesOperations

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

R crashes for boolean operations on some meshes #1

Closed dwoll closed 2 years ago

dwoll commented 2 years ago

First of all, many thanks for starting to work on this package! I realize that it's in early development. If it becomes stable, it could be real gamechanger in radiation oncology / medical physics where boolean operations on meshes are carried out for quality assurance on delineated target structures.

So far, I've tinkered with libigl which works quite well. However, I need to wrap it in a C++ program which is difficult to deploy. Having an R package would be much more convenient, and would open up a lot of options with respect to building interactive Shiny apps. However, so far, MeshesOperations only crashes for me when trying boolean operations on some meshes I have. My C++ skills are weak, but I'd like to help if you have time working on user issues with this package. What kind of information do you need? Many thanks in advance!

stla commented 2 years ago

Hello,

Do you use numbersType = "lazyExact"? Using this option is better.

dwoll commented 2 years ago

Thanks for taking the time to respond! Unfortunately, the error is the same:

— Processing mesh n°1...
Polygon orientation failed.
The mesh is triangle.
The mesh is closed.

 *** caught segfault ***
address (nil), cause 'memory not mapped'

Traceback:
 1: Union_EK(meshes, clean, normals, triangulate)
 2: MeshesUnion(meshes, clean = FALSE, normals = TRUE, numbersType = "lazyExact")

For numbersType="double" the traceback instead starts with 1: Union_K(meshes, clean, normals, triangulate). For "gmp", the error instead is Error in checkMesh(mesh[["vertices"]], mesh[["faces"]], gmp, aslist = FALSE) : The 'vertices' argument must be a matrix with three columns.

This is all on Windows 10 WSL2, Ubuntu 20.04 LTS.

stla commented 2 years ago

Polygon orientation failed does not smell good. There should be a problem in the mesh. Can you share it? (e.g. in a CSV file)

dwoll commented 2 years ago

Here are the two PLY files, read in with readMeshFile(). mesh_1_PLY.txt mesh_2_PLY.txt

stla commented 2 years ago

I found an error in my R code and I fixed it. Now there's no crash anymore, but the union cannot be computed because CGAL detects self-intersections in the meshes.

stla commented 2 years ago

You can get rid of the self-intersections with a surface reconstruction.

library(MeshesOperations)
m1 <- readMeshFile("mesh_1.ply")
m2 <- readMeshFile("mesh_2.ply")

library(SurfaceReconstruction)
mm1 <- AFSreconstruction(m1$vertices)
mm2 <- AFSreconstruction(m2$vertices)

mm <- MeshesUnion(list(mm1, mm2), numbersType = "lazyExact")
dwoll commented 2 years ago

Many thanks, that's a big step ahead! However, all resulting meshes have issues. The results from AFSreconstruction() (saved with writeMeshFile() as a PLY file) cannot be opened in MeshLab which complains Error details: Bad vertex index in face. The same for the results from MeshesUnion() and MeshesIntersection(). Microsoft 10 3D viewer cannot open the meshes either. Any idea what's going wrong? Thanks in advance!

stla commented 2 years ago

Strange. If you have a rgl mesh (such as the output of AFSreconstruction) you can save it as a PLY file with the Rvcg package.

dwoll commented 2 years ago

Great, thanks again! Converting the output from MeshesIntersection() / MeshesUnion() from class cgalMesh to class mesh3d using toRGL(), and then saving using Rvcg::vcgPlyWrite() results in meshes that can be opened in MeshLab, etc. Combined with meshVolume() and Rvcg::vcgMetro(), a number of mesh similarity metrics can now be calculated.

stla commented 2 years ago

Nice. Now you can use Boov instead of MeshesOperations for the Boolean operations. Hopefully it will be on CRAN. For reading/writing a mesh file you can use PolygonSoup, which is already on CRAN.

dwoll commented 2 years ago

Thanks for the heads up regarding Boov!