I want to use the CGAL library to calculate the Boolean difference between two polyhedrons. Both polyhedrons are in the form of .off files. My idea is to first read these two files into two Polyhedron type variables p1 and p2, then initialize Nef_polyhedron type variables nef1 and nef2 with p1 and p2 respectively, and finally find the Boolean difference between nef1 and nef2. My code is as follows:
#include<fstream>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Nef_polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <CGAL/draw_nef_3.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron;
int main() {
Polyhedron p1, p2, res;
std::ifstream fin1("blank.off");
std::ifstream fin2("svreal.off");
fin1 >> p1;
fin2 >> p2;
fin1.close();
fin2.close();
Nef_polyhedron nef1(p1);
Nef_polyhedron nef2(p2);
Nef_polyhedron nef = nef1 - nef2;
nef.convert_to_polyhedron(res);
std::ofstream fout("res2.off");
fout << res;
return 0;
}
However, when the code executes to "Nef_polyhedron nef2(p2);
", the following exception is thrown: boost::wrapexcept<boost::bad_any_cast>
. This may mean an error in constructing Nef_polyhedron nef2 with Polyhedron object p2. But I really cant't figure out why such a mistake happened. The Polyhedron p1 and p2 are both valid. And it looks nice when I open file svreal.off which is the .off file form of Polyhedron object p2.
I would appreciate it if you could help me solve this problem.
Your input "svreal.off" contains some degenerate faces. You can remove them using CGAL::Polygon_mesh_processing::remove_degenerate_faces()
from CGAL/Polygon_mesh_processing/repair_degeneracies.h
or use the function OFF_to_nef_3()
directly from the istream.