diff --git a/src/ifcgeom/kernels/cgal/CgalConversionFunctions.cpp b/src/ifcgeom/kernels/cgal/CgalConversionFunctions.cpp index c01c6e8576..026b491b88 100644 --- a/src/ifcgeom/kernels/cgal/CgalConversionFunctions.cpp +++ b/src/ifcgeom/kernels/cgal/CgalConversionFunctions.cpp @@ -26,7 +26,7 @@ CGAL::Polyhedron_3 IfcGeom::CgalKernel::create_polyhedron(std::list 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(); } } 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(); } } CGAL::Nef_polyhedron_3 IfcGeom::CgalKernel::create_nef_polyhedron(std::list &face_list) { CGAL::Polyhedron_3 polyhedron = create_polyhedron(face_list); - return CGAL::Nef_polyhedron_3(polyhedron); + CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron); + CGAL::Nef_polyhedron_3 nef_polyhedron; + try { + nef_polyhedron = CGAL::Nef_polyhedron_3(polyhedron); + } catch (...) { + Logger::Message(Logger::LOG_ERROR, "Conversion to Nef polyhedron failed!"); + return nef_polyhedron; + } return nef_polyhedron; } CGAL::Nef_polyhedron_3 IfcGeom::CgalKernel::create_nef_polyhedron(CGAL::Polyhedron_3 &polyhedron) { if (polyhedron.is_valid()) { CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron); - return CGAL::Nef_polyhedron_3(polyhedron); + CGAL::Nef_polyhedron_3 nef_polyhedron; + try { + nef_polyhedron = CGAL::Nef_polyhedron_3(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(); } } + +//CGAL::Polyhedron_3 IfcGeom::CgalKernel::triangulate_faces(CGAL::Polyhedron_3 &polyhedron) { +// std::list face_list; +// +// for (CGAL::Polyhedron_3::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::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 points_in_polygon; +// +// } +// } +// +// return create_polyhedron(face_list); +//} diff --git a/src/ifcgeom/kernels/cgal/CgalConversionResult.cpp b/src/ifcgeom/kernels/cgal/CgalConversionResult.cpp index 8aa38cbee1..f2ecb1a66b 100644 --- a/src/ifcgeom/kernels/cgal/CgalConversionResult.cpp +++ b/src/ifcgeom/kernels/cgal/CgalConversionResult.cpp @@ -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 face_normals; boost::associative_property_map> 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; diff --git a/src/ifcgeom/kernels/cgal/CgalIfcGeomShapes.cpp b/src/ifcgeom/kernels/cgal/CgalIfcGeomShapes.cpp index 7b64a21321..f6af7fc884 100644 --- a/src/ifcgeom/kernels/cgal/CgalIfcGeomShapes.cpp +++ b/src/ifcgeom/kernels/cgal/CgalIfcGeomShapes.cpp @@ -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 nef_result(s1); - if (is_halfspace) { + CGAL::Nef_polyhedron_3 nef_result; + try { + nef_result = CGAL::Nef_polyhedron_3(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::Intersection_mode::CLOSED_HALFSPACE); } else { - nef_result -= CGAL::Nef_polyhedron_3(s2); + CGAL::Nef_polyhedron_3 nef_s2; + try { + nef_s2 = CGAL::Nef_polyhedron_3(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 result; diff --git a/src/ifcgeom/kernels/cgal/CgalKernel.cpp b/src/ifcgeom/kernels/cgal/CgalKernel.cpp index d13dab80ec..5a9ebd4f0f 100644 --- a/src/ifcgeom/kernels/cgal/CgalKernel.cpp +++ b/src/ifcgeom/kernels/cgal/CgalKernel.cpp @@ -96,13 +96,6 @@ IfcGeom::NativeElement* 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 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 nef_brep_cut_result; + try { + nef_brep_cut_result = CGAL::Nef_polyhedron_3(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 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 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 nef_opening; + try { + nef_opening = CGAL::Nef_polyhedron_3(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; } diff --git a/src/ifcgeom/kernels/cgal/CgalKernel.h b/src/ifcgeom/kernels/cgal/CgalKernel.h index 266b7a8294..c9c2c46495 100644 --- a/src/ifcgeom/kernels/cgal/CgalKernel.h +++ b/src/ifcgeom/kernels/cgal/CgalKernel.h @@ -47,6 +47,7 @@ if ( it != cache.T.end() ) { e = it->second; return true; } #include #include #include +#include #include 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 triangulate_faces(CGAL::Polyhedron_3 &polyhedron); CGAL::Polyhedron_3 create_polyhedron(std::list &face_list); CGAL::Polyhedron_3 create_polyhedron(CGAL::Nef_polyhedron_3 &nef_polyhedron); CGAL::Nef_polyhedron_3 create_nef_polyhedron(std::list &face_list);