mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-15 18:14:08 +00:00
Catching some more CGAL errors, allow non-closed meshes, start of new code to triangulate faces
This commit is contained in:
@@ -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;
|
// std::cout << "Before: " << polyhedron.size_of_vertices() << " vertices and " << polyhedron.size_of_facets() << " facets" << std::endl;
|
||||||
CGAL::Polygon_mesh_processing::stitch_borders(polyhedron);
|
CGAL::Polygon_mesh_processing::stitch_borders(polyhedron);
|
||||||
if (!polyhedron.is_valid()) {
|
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;
|
// std::ofstream fresult;
|
||||||
// fresult.open("/Users/ken/Desktop/invalid.off");
|
// fresult.open("/Users/ken/Desktop/invalid.off");
|
||||||
// fresult << polyhedron << std::endl;
|
// 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);
|
nef_polyhedron.convert_to_polyhedron(polyhedron);
|
||||||
return polyhedron;
|
return polyhedron;
|
||||||
} catch (...) {
|
} 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>();
|
return CGAL::Polyhedron_3<Kernel>();
|
||||||
}
|
}
|
||||||
} else {
|
} 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>();
|
return CGAL::Polyhedron_3<Kernel>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CGAL::Nef_polyhedron_3<Kernel> IfcGeom::CgalKernel::create_nef_polyhedron(std::list<cgal_face_t> &face_list) {
|
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);
|
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) {
|
CGAL::Nef_polyhedron_3<Kernel> IfcGeom::CgalKernel::create_nef_polyhedron(CGAL::Polyhedron_3<Kernel> &polyhedron) {
|
||||||
if (polyhedron.is_valid()) {
|
if (polyhedron.is_valid()) {
|
||||||
CGAL::Polygon_mesh_processing::triangulate_faces(polyhedron);
|
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 {
|
} 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>();
|
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,7 +10,7 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
|
|||||||
vertex->point() = vertex->point().transform(trsf);
|
vertex->point() = vertex->point().transform(trsf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!s.is_valid() || !s.is_closed()) {
|
if (!s.is_valid()) {
|
||||||
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (before triangulation)");
|
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (before triangulation)");
|
||||||
std::ofstream ferror;
|
std::ofstream ferror;
|
||||||
ferror.open("/Users/ken/Desktop/error.off");
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
@@ -30,7 +30,17 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
|
|||||||
std::map<cgal_face_descriptor_t, Kernel::Vector_3> face_normals;
|
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);
|
boost::associative_property_map<std::map<cgal_face_descriptor_t, Kernel::Vector_3>> face_normals_map(face_normals);
|
||||||
cgal_shape_t s_copy(s);
|
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("/Users/ken/Desktop/error.off");
|
||||||
|
ferror << s << std::endl;
|
||||||
|
ferror.close();
|
||||||
|
return;
|
||||||
|
} if (!success) {
|
||||||
Logger::Message(Logger::LOG_ERROR, "Triangulation failed");
|
Logger::Message(Logger::LOG_ERROR, "Triangulation failed");
|
||||||
std::ofstream ferror;
|
std::ofstream ferror;
|
||||||
ferror.open("/Users/ken/Desktop/error.off");
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
@@ -45,7 +55,7 @@ void IfcGeom::CgalShape::Triangulate(const IfcGeom::IteratorSettings & settings,
|
|||||||
// fafter << s << std::endl;
|
// fafter << s << std::endl;
|
||||||
// fafter.close();
|
// 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)");
|
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (after triangulation)");
|
||||||
std::ofstream ferror;
|
std::ofstream ferror;
|
||||||
ferror.open("/Users/ken/Desktop/error.off");
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
|
|||||||
@@ -91,7 +91,12 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolid *l, cgal
|
|||||||
hole_top_face.outer.push_back(*vertex+height*dir);
|
hole_top_face.outer.push_back(*vertex+height*dir);
|
||||||
} face_list.push_back(hole_top_face);
|
} 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) {
|
if (has_position) {
|
||||||
@@ -104,7 +109,7 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolid *l, cgal
|
|||||||
nef_shape.convert_to_polyhedron(shape);
|
nef_shape.convert_to_polyhedron(shape);
|
||||||
return true;
|
return true;
|
||||||
} catch (...) {
|
} 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +243,12 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcExtrudedAreaSolidTapered*
|
|||||||
// f2 << inner_polyhedron << std::endl;
|
// f2 << inner_polyhedron << std::endl;
|
||||||
// f2.close();
|
// 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_face1;
|
||||||
++inner_face2;
|
++inner_face2;
|
||||||
@@ -453,14 +463,24 @@ bool IfcGeom::CgalKernel::convert(const IfcSchema::IfcBooleanResult* l, cgal_sha
|
|||||||
if (op == IfcSchema::IfcBooleanOperator::IfcBooleanOperator_DIFFERENCE) {
|
if (op == IfcSchema::IfcBooleanOperator::IfcBooleanOperator_DIFFERENCE) {
|
||||||
|
|
||||||
// std::cout << "Difference" << std::endl;
|
// std::cout << "Difference" << std::endl;
|
||||||
CGAL::Nef_polyhedron_3<Kernel> nef_result(s1);
|
CGAL::Nef_polyhedron_3<Kernel> nef_result;
|
||||||
if (is_halfspace) {
|
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);
|
if (is_plane) nef_result = nef_result.intersection(plane, CGAL::Nef_polyhedron_3<Kernel>::Intersection_mode::CLOSED_HALFSPACE);
|
||||||
} else {
|
} 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()) {
|
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;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// CGAL::Polyhedron_3<Kernel> result;
|
// CGAL::Polyhedron_3<Kernel> result;
|
||||||
|
|||||||
@@ -290,8 +290,26 @@ bool IfcGeom::CgalKernel::convert_openings(const IfcSchema::IfcProduct* entity,
|
|||||||
// fresult << polyhedron << std::endl;
|
// fresult << polyhedron << std::endl;
|
||||||
// fresult.close();
|
// fresult.close();
|
||||||
|
|
||||||
CGAL::Nef_polyhedron_3<Kernel> nef_opening(opening);
|
CGAL::Nef_polyhedron_3<Kernel> nef_opening;
|
||||||
nef_brep_cut_result -= nef_opening;
|
try {
|
||||||
|
nef_opening = CGAL::Nef_polyhedron_3<Kernel>(opening);
|
||||||
|
} catch (...) {
|
||||||
|
Logger::Message(Logger::LOG_WARNING, "Subtracting combined openings compound failed (Nef conversion):", entity->entity);
|
||||||
|
std::ofstream ferror;
|
||||||
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
|
ferror << entity_shape << std::endl;
|
||||||
|
ferror.close();
|
||||||
|
return false;
|
||||||
|
} try {
|
||||||
|
nef_brep_cut_result -= nef_opening;
|
||||||
|
} catch (...) {
|
||||||
|
Logger::Message(Logger::LOG_WARNING, "Subtracting combined openings compound failed (subtraction):", entity->entity);
|
||||||
|
std::ofstream ferror;
|
||||||
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
|
ferror << entity_shape << std::endl;
|
||||||
|
ferror.close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// brep_cut_result.convert_to_polyhedron(polyhedron);
|
// brep_cut_result.convert_to_polyhedron(polyhedron);
|
||||||
// fresult.open("/Users/ken/Desktop/after.off");
|
// fresult.open("/Users/ken/Desktop/after.off");
|
||||||
@@ -307,14 +325,22 @@ bool IfcGeom::CgalKernel::convert_openings(const IfcSchema::IfcProduct* entity,
|
|||||||
// Apparently processing the boolean operation failed or resulted in an invalid result
|
// Apparently processing the boolean operation failed or resulted in an invalid result
|
||||||
// in which case the original shape without the subtractions is returned instead
|
// in which case the original shape without the subtractions is returned instead
|
||||||
// we try convert the openings in the original way, one by one.
|
// 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_WARNING, "Subtracting combined openings compound failed (conversion):", entity->entity);
|
||||||
|
std::ofstream ferror;
|
||||||
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
|
ferror << entity_shape << std::endl;
|
||||||
|
ferror.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Apparently processing the boolean operation failed or resulted in an invalid result
|
// Apparently processing the boolean operation failed or resulted in an invalid result
|
||||||
// in which case the original shape without the subtractions is returned instead
|
// in which case the original shape without the subtractions is returned instead
|
||||||
// we try convert the openings in the original way, one by one.
|
// 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_WARNING, "Subtracting combined openings compound failed (invalid):", entity->entity);
|
||||||
|
std::ofstream ferror;
|
||||||
|
ferror.open("/Users/ken/Desktop/error.off");
|
||||||
|
ferror << entity_shape << std::endl;
|
||||||
|
ferror.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -140,6 +140,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);
|
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(std::list<cgal_face_t> &face_list);
|
||||||
CGAL::Polyhedron_3<Kernel> create_polyhedron(CGAL::Nef_polyhedron_3<Kernel> &nef_polyhedron);
|
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);
|
CGAL::Nef_polyhedron_3<Kernel> create_nef_polyhedron(std::list<cgal_face_t> &face_list);
|
||||||
|
|||||||
Reference in New Issue
Block a user