Merge pull request #16 from kenohori/cgal

Validation code to catch more cases of invalid objects
This commit is contained in:
Ken Arroyo Ohori
2017-04-25 10:22:33 -05:00
committed by GitHub
5 changed files with 270 additions and 92 deletions
@@ -26,7 +26,7 @@ CGAL::Polyhedron_3<Kernel> IfcGeom::CgalKernel::create_polyhedron(std::list<cgal
// std::cout << "Before: " << polyhedron.size_of_vertices() << " vertices and " << polyhedron.size_of_facets() << " facets" << std::endl;
CGAL::Polygon_mesh_processing::stitch_borders(polyhedron);
if (!polyhedron.is_valid()) {
std::cout << "create_polyhedron: Polyhedron not valid!" << std::endl;
Logger::Message(Logger::LOG_ERROR, "create_polyhedron: Polyhedron not valid!");
// std::ofstream fresult;
// fresult.open("/Users/ken/Desktop/invalid.off");
// fresult << polyhedron << std::endl;
@@ -50,26 +50,66 @@ CGAL::Polyhedron_3<Kernel> IfcGeom::CgalKernel::create_polyhedron(CGAL::Nef_poly
nef_polyhedron.convert_to_polyhedron(polyhedron);
return polyhedron;
} catch (...) {
std::cout << "Conversion from Nef to polyhedron failed!" << std::endl;
Logger::Message(Logger::LOG_ERROR, "Conversion from Nef to polyhedron failed!");
return CGAL::Polyhedron_3<Kernel>();
}
} else {
std::cout << "Nef polyhedron not simple: cannot create polyhedron!" << std::endl;
Logger::Message(Logger::LOG_ERROR, "Nef polyhedron not simple: cannot create polyhedron!");
return CGAL::Polyhedron_3<Kernel>();
}
}
CGAL::Nef_polyhedron_3<Kernel> IfcGeom::CgalKernel::create_nef_polyhedron(std::list<cgal_face_t> &face_list) {
CGAL::Polyhedron_3<Kernel> polyhedron = create_polyhedron(face_list);
return CGAL::Nef_polyhedron_3<Kernel>(polyhedron);
CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron);
CGAL::Nef_polyhedron_3<Kernel> nef_polyhedron;
try {
nef_polyhedron = CGAL::Nef_polyhedron_3<Kernel>(polyhedron);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef polyhedron failed!");
return nef_polyhedron;
} return nef_polyhedron;
}
CGAL::Nef_polyhedron_3<Kernel> IfcGeom::CgalKernel::create_nef_polyhedron(CGAL::Polyhedron_3<Kernel> &polyhedron) {
if (polyhedron.is_valid()) {
CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron);
return CGAL::Nef_polyhedron_3<Kernel>(polyhedron);
CGAL::Nef_polyhedron_3<Kernel> nef_polyhedron;
try {
nef_polyhedron = CGAL::Nef_polyhedron_3<Kernel>(polyhedron);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef polyhedron failed!");
return nef_polyhedron;
} return nef_polyhedron;
} else {
std::cout << "Polyhedron not valid: cannot create Nef polyhedron!" << std::endl;
Logger::Message(Logger::LOG_ERROR, "Polyhedron not valid: cannot create Nef polyhedron!");
return CGAL::Nef_polyhedron_3<Kernel>();
}
}
//CGAL::Polyhedron_3<Kernel> IfcGeom::CgalKernel::triangulate_faces(CGAL::Polyhedron_3<Kernel> &polyhedron) {
// std::list<cgal_face_t> face_list;
//
// for (CGAL::Polyhedron_3<Kernel>::Facet_const_iterator current_facet = polyhedron.facets_begin();
// current_facet != polyhedron.facets_end();
// ++current_facet) {
//
// // Triangle
// if (current_facet->is_triangle()) {
// face_list.push_back(cgal_face_t());
// CGAL::Polyhedron_3<Kernel>::Halfedge_around_facet_const_circulator current_halfedge = current_facet->facet_begin();
// do {
// face_list.back().outer.push_back(current_halfedge->vertex()->point());
// ++current_halfedge;
// } while (current_halfedge != current_facet->facet_begin());
// }
//
// // Polygon
// else {
// std::list<Kernel::Point_3> points_in_polygon;
//
// }
// }
//
// return create_polyhedron(face_list);
//}
@@ -10,10 +10,19 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
vertex->point() = vertex->point().transform(trsf);
}
if (!s.is_valid() || !s.is_closed()) {
std::string error_file_path;
for (unsigned int error_number = 1; error_number < 1000; ++error_number) {
error_file_path = std::string("/Users/ken/Desktop/error/error");
error_file_path += std::to_string(error_number);
error_file_path += ".off";
std::ifstream file_path_test(error_file_path);
if (!file_path_test.good()) break;
}
if (!s.is_valid()) {
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (before triangulation)");
std::ofstream ferror;
ferror.open("/Users/ken/Desktop/error.off");
ferror.open(error_file_path);
ferror << s << std::endl;
ferror.close();
return;
@@ -30,10 +39,20 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
std::map<cgal_face_descriptor_t, Kernel::Vector_3> face_normals;
boost::associative_property_map<std::map<cgal_face_descriptor_t, Kernel::Vector_3>> face_normals_map(face_normals);
cgal_shape_t s_copy(s);
if (!CGAL::Polygon_mesh_processing::triangulate_faces(s) ) {
bool success = false;
try {
success = CGAL::Polygon_mesh_processing::triangulate_faces(s);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Triangulation crashed");
std::ofstream ferror;
ferror.open(error_file_path);
ferror << s << std::endl;
ferror.close();
return;
} if (!success) {
Logger::Message(Logger::LOG_ERROR, "Triangulation failed");
std::ofstream ferror;
ferror.open("/Users/ken/Desktop/error.off");
ferror.open(error_file_path);
ferror << s << std::endl;
ferror.close();
return;
@@ -45,10 +64,10 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
// fafter << s << std::endl;
// fafter.close();
if (!s.is_valid() || !s.is_closed()) {
if (!s.is_valid()) {
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (after triangulation)");
std::ofstream ferror;
ferror.open("/Users/ken/Desktop/error.off");
ferror.open(error_file_path);
ferror << s_copy << std::endl;
ferror.close();
return;
+27 -7
View File
@@ -91,7 +91,12 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolid *l, cgal
hole_top_face.outer.push_back(*vertex+height*dir);
} face_list.push_back(hole_top_face);
nef_shape -= create_nef_polyhedron(face_list);
try {
nef_shape -= create_nef_polyhedron(face_list);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "IfcExtrudedAreaSolid: cannot subtract opening for:", l->entity);
return false;
}
}
if (has_position) {
@@ -104,7 +109,7 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolid *l, cgal
nef_shape.convert_to_polyhedron(shape);
return true;
} catch (...) {
std::cout << "IfcExtrudedAreaSolid: cannot convert Nef to polyhedron!" << std::endl;
Logger::Message(Logger::LOG_ERROR, "IfcExtrudedAreaSolid: cannot convert Nef to polyhedron for:", l->entity);
return false;
}
@@ -238,7 +243,12 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolidTapered*
// f2 << inner_polyhedron << std::endl;
// f2.close();
nef_shape -= create_nef_polyhedron(face_list);
try {
nef_shape -= create_nef_polyhedron(face_list);
} catch (...) {
std::cout << "IfcExtrudedAreaSolidTapered: cannot subtract opening for:" << std::endl;
return false;
}
++inner_face1;
++inner_face2;
@@ -453,14 +463,24 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcBooleanResult* l, cgal_sha
if (op == IfcSchema::IfcBooleanOperator::IfcBooleanOperator_DIFFERENCE) {
// std::cout << "Difference" << std::endl;
CGAL::Nef_polyhedron_3<Kernel> nef_result(s1);
if (is_halfspace) {
CGAL::Nef_polyhedron_3<Kernel> nef_result;
try {
nef_result = CGAL::Nef_polyhedron_3<Kernel>(s1);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "s1: cannot convert to Nef?", operand1->entity);
return false;
} if (is_halfspace) {
if (is_plane) nef_result = nef_result.intersection(plane, CGAL::Nef_polyhedron_3<Kernel>::Intersection_mode::CLOSED_HALFSPACE);
} else {
nef_result -= CGAL::Nef_polyhedron_3<Kernel>(s2);
CGAL::Nef_polyhedron_3<Kernel> nef_s2;
try {
nef_s2 = CGAL::Nef_polyhedron_3<Kernel>(s2);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "s2: cannot convert to Nef?", operand2->entity);
} nef_result -= nef_s2;
}
if (!nef_result.is_simple()) {
std::cout << "Not simple: " << nef_result.number_of_volumes() << " volumes" << std::endl;
Logger::Message(Logger::LOG_ERROR, "s2: not simple?", operand2->entity);
return false;
} else {
// CGAL::Polyhedron_3<Kernel> result;
+170 -73
View File
@@ -96,13 +96,6 @@ IfcGeom::NativeElement<double>* IfcGeom::CgalKernel::create_brep_for_representat
try {
convert(product->ObjectPlacement(), trsf);
} catch (...) {}
// std::cout << "trsf" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
// Does the IfcElement have any IfcOpenings?
// Note that openings for IfcOpeningElements are not processed
@@ -210,30 +203,9 @@ bool IfcGeom::CgalKernel::convert_openings(const IfcSchema::IfcProduct* entity,
} catch (...) {}
}
// std::cout << "entity_trsf" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << entity_trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
//
// std::cout << "opening_trsf before" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << opening_trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
// Move the opening into the coordinate system of the IfcProduct
opening_trsf = entity_trsf.inverse() * opening_trsf;
// std::cout << "opening_trsf after" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << opening_trsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
IfcSchema::IfcProductRepresentation* prodrep = fes->Representation();
IfcSchema::IfcRepresentation::list::ptr reps = prodrep->Representations();
@@ -252,13 +224,6 @@ bool IfcGeom::CgalKernel::convert_openings(const IfcSchema::IfcProduct* entity,
cgal_shape_t opening_shape(((CgalShape*)opening_shapes[i].Shape())->shape());
for (auto &vertex: vertices(opening_shape)) vertex->point() = vertex->point().transform(gtrsf);
opening_shapelist.push_back(opening_shape);
// std::cout << "gtrsf" << std::endl;
// for (int i = 0; i < 3; ++i) {
// for (int j = 0; j < 4; ++j) {
// std::cout << gtrsf.cartesian(i, j) << " ";
// } std::cout << std::endl;
// }
}
}
@@ -273,51 +238,183 @@ bool IfcGeom::CgalKernel::convert_openings(const IfcSchema::IfcProduct* entity,
for (auto &vertex: vertices(entity_shape)) vertex->point() = vertex->point().transform(entity_shape_gtrsf);
}
cgal_shape_t brep_cut_result(entity_shape);
CGAL::Nef_polyhedron_3<Kernel> nef_brep_cut_result(brep_cut_result);
cgal_shape_t original_entity_shape(entity_shape);
if (!entity_shape.is_valid()) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef will fail. Invalid entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} if (!entity_shape.is_closed()) {
// TODO: There can be substractions to remove parts of non-volumetric objects. Maybe iterate over all faces of an entity and put them in a Nef_polyhedron_3 through Boolean union? Highly inefficient but maybe desirable...
Logger::Message(Logger::LOG_ERROR, "Subtraction of openings not supported for non-closed entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} bool success = false;
try {
success = CGAL::Polygon_mesh_processing::triangulate_faces(entity_shape);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Triangulation of entity crashed:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} if (!success) {
Logger::Message(Logger::LOG_ERROR, "Triangulation of entity failed:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} if (CGAL::Polygon_mesh_processing::does_self_intersect(entity_shape)) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef will fail. Self-intersecting entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} CGAL::Nef_polyhedron_3<Kernel> nef_brep_cut_result;
try {
nef_brep_cut_result = CGAL::Nef_polyhedron_3<Kernel>(entity_shape);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Could not convert entity to Nef:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} try {
cgal_shape_t brep_cut_result;
nef_brep_cut_result.convert_to_polyhedron(brep_cut_result);
} catch (...) {
Logger::Message(Logger::LOG_WARNING, "Final conversion will likely fail. Could not convert entity from Nef:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
// return false;
}
for (auto &opening: opening_shapelist) {
// CGAL::Polyhedron_3<Kernel> polyhedron;
// brep_cut_result.convert_to_polyhedron(polyhedron);
// std::ofstream fresult;
// fresult.open("/Users/ken/Desktop/before.off");
// fresult << polyhedron << std::endl;
// fresult.close();
//
// opening.convert_to_polyhedron(polyhedron);
// fresult.open("/Users/ken/Desktop/opening.off");
// fresult << polyhedron << std::endl;
// fresult.close();
CGAL::Nef_polyhedron_3<Kernel> nef_opening(opening);
nef_brep_cut_result -= nef_opening;
// brep_cut_result.convert_to_polyhedron(polyhedron);
// fresult.open("/Users/ken/Desktop/after.off");
// fresult << polyhedron << std::endl;
// fresult.close();
}
if (brep_cut_result.is_valid()) {
cgal_shape_t original_opening_shape(opening);
if (!opening.is_valid()) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef will fail. Invalid opening in entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} if (!opening.is_closed()) {
Logger::Message(Logger::LOG_ERROR, "Subtraction of opening makes no sense. Not closed opening in entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} success = false;
try {
nef_brep_cut_result.convert_to_polyhedron(brep_cut_result);
cut_shapes.push_back(IfcGeom::ConversionResult(new CgalShape(brep_cut_result), &it3->Style()));
success = CGAL::Polygon_mesh_processing::triangulate_faces(opening);
} catch (...) {
// Apparently processing the boolean operation failed or resulted in an invalid result
// in which case the original shape without the subtractions is returned instead
// we try convert the openings in the original way, one by one.
Logger::Message(Logger::LOG_WARNING, "Subtracting combined openings compound failed:", entity->entity);
Logger::Message(Logger::LOG_ERROR, "Triangulation of opening of entity crashed:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} if (!success) {
Logger::Message(Logger::LOG_ERROR, "Triangulation of opening of entity failed:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} if (CGAL::Polygon_mesh_processing::does_self_intersect(entity_shape)) {
Logger::Message(Logger::LOG_ERROR, "Conversion to Nef will fail. Self-intersecting opening of entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} CGAL::Nef_polyhedron_3<Kernel> nef_opening;
try {
nef_opening = CGAL::Nef_polyhedron_3<Kernel>(opening);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Could not convert opening of entity to Nef:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
} try {
cgal_shape_t opening_shape;
nef_opening.convert_to_polyhedron(opening_shape);
} catch (...) {
Logger::Message(Logger::LOG_WARNING, "Final conversion will likely fail. Could not convert opening of entity from Nef:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
// return false;
} try {
nef_brep_cut_result -= nef_opening;
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Could not subtract Nef opening of entity:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
std::ofstream fopening;
fopening.open("/Users/ken/Desktop/opening.off");
fopening << original_opening_shape << std::endl;
fopening.close();
return false;
}
} else {
// Apparently processing the boolean operation failed or resulted in an invalid result
// in which case the original shape without the subtractions is returned instead
// we try convert the openings in the original way, one by one.
Logger::Message(Logger::LOG_WARNING, "Subtracting combined openings compound failed:", entity->entity);
return false;
}
}
return true;
try {
nef_brep_cut_result.convert_to_polyhedron(entity_shape);
} catch (...) {
Logger::Message(Logger::LOG_ERROR, "Could not convert entity with openings from Nef:", entity->entity);
std::ofstream fentity;
fentity.open("/Users/ken/Desktop/entity.off");
fentity << original_entity_shape << std::endl;
fentity.close();
return false;
} cut_shapes.push_back(IfcGeom::ConversionResult(new CgalShape(entity_shape), &it3->Style()));
} return true;
}
+2
View File
@@ -47,6 +47,7 @@ if ( it != cache.T.end() ) { e = it->second; return true; }
#include <CGAL/Polygon_mesh_processing/orientation.h>
#include <CGAL/Polygon_mesh_processing/triangulate_faces.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.h>
#include <CGAL/Polygon_mesh_processing/self_intersections.h>
#include <CGAL/Nef_polyhedron_3.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
@@ -140,6 +141,7 @@ namespace IfcGeom {
bool convert_openings(const IfcSchema::IfcProduct* entity, const IfcSchema::IfcRelVoidsElement::list::ptr& openings, const ConversionResults& entity_shapes, const cgal_placement_t& entity_trsf, ConversionResults& cut_shapes);
// CGAL::Polyhedron_3<Kernel> triangulate_faces(CGAL::Polyhedron_3<Kernel> &polyhedron);
CGAL::Polyhedron_3<Kernel> create_polyhedron(std::list<cgal_face_t> &face_list);
CGAL::Polyhedron_3<Kernel> create_polyhedron(CGAL::Nef_polyhedron_3<Kernel> &nef_polyhedron);
CGAL::Nef_polyhedron_3<Kernel> create_nef_polyhedron(std::list<cgal_face_t> &face_list);