2017-01-16 11:53:11 +01:00
|
|
|
#include "CgalConversionResult.h"
|
|
|
|
|
|
2019-01-23 12:34:21 +01:00
|
|
|
#include "../../../ifcparse/IfcLogger.h"
|
|
|
|
|
#include "../../../ifcgeom/schema_agnostic/IfcGeomRepresentation.h"
|
|
|
|
|
|
2019-09-01 16:29:00 +02:00
|
|
|
void ifcopenshell::geometry::CgalShape::Triangulate(const settings& settings, const ifcopenshell::geometry::taxonomy::matrix4& place, Representation::Triangulation* t, int surface_style_id) const {
|
|
|
|
|
// Copy is made because triangulate_faces() does not accept a const argument
|
|
|
|
|
cgal_shape_t s = shape_;
|
2019-01-23 12:34:21 +01:00
|
|
|
|
2019-09-01 16:29:00 +02:00
|
|
|
if (!place.components.isIdentity()) {
|
|
|
|
|
const auto& m = place.components;
|
|
|
|
|
|
|
|
|
|
// @todo check
|
|
|
|
|
const cgal_placement_t trsf(
|
|
|
|
|
m(0, 0), m(0, 1), m(0, 2), m(0, 3),
|
|
|
|
|
m(1, 0), m(1, 1), m(1, 2), m(1, 3),
|
|
|
|
|
m(2, 0), m(2, 1), m(2, 2), m(2, 3));
|
|
|
|
|
|
|
|
|
|
// Apply transformation
|
|
|
|
|
for (auto &vertex : vertices(s)) {
|
|
|
|
|
vertex->point() = vertex->point().transform(trsf);
|
|
|
|
|
}
|
2017-02-13 15:26:06 -06:00
|
|
|
}
|
2017-02-08 16:56:59 -06:00
|
|
|
|
2017-04-21 15:43:19 +02:00
|
|
|
if (!s.is_valid()) {
|
2017-04-10 18:54:30 -05:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (before triangulation)");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-08 16:12:19 -06:00
|
|
|
// Triangulate the shape and compute the normals
|
2019-01-23 17:27:13 +01:00
|
|
|
// std::map<cgal_vertex_descriptor_t, Kernel_::Vector_3> vertex_normals;
|
|
|
|
|
// boost::associative_property_map<std::map<cgal_vertex_descriptor_t, Kernel_::Vector_3>> vertex_normals_map(vertex_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);
|
2019-01-23 12:34:21 +01:00
|
|
|
|
2017-04-21 15:43:19 +02:00
|
|
|
bool success = false;
|
|
|
|
|
try {
|
|
|
|
|
success = CGAL::Polygon_mesh_processing::triangulate_faces(s);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
Logger::Message(Logger::LOG_ERROR, "Triangulation crashed");
|
2017-02-08 16:12:19 -06:00
|
|
|
return;
|
|
|
|
|
}
|
2017-03-03 15:25:45 -06:00
|
|
|
|
2019-01-23 15:44:19 +01:00
|
|
|
if (!success) {
|
2019-01-23 17:27:13 +01:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Triangulation failed");
|
|
|
|
|
return;
|
2019-01-23 15:44:19 +01:00
|
|
|
}
|
|
|
|
|
// std::cout << "Triangulated model: " << s.size_of_facets() << " facets and " << s.size_of_vertices() << " vertices" << std::endl;
|
2017-04-10 18:54:30 -05:00
|
|
|
|
2017-04-21 15:43:19 +02:00
|
|
|
if (!s.is_valid()) {
|
2017-04-10 18:54:30 -05:00
|
|
|
Logger::Message(Logger::LOG_ERROR, "Invalid Polyhedron_3 in object (after triangulation)");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CGAL::Polygon_mesh_processing::compute_normals(s, vertex_normals_map, face_normals_map);
|
|
|
|
|
CGAL::Polygon_mesh_processing::compute_face_normals(s, face_normals_map);
|
2017-03-03 15:25:45 -06:00
|
|
|
|
2019-01-23 17:27:13 +01:00
|
|
|
int num_faces = 0, num_vertices = 0;
|
2017-02-08 16:12:19 -06:00
|
|
|
for (auto &face: faces(s)) {
|
2017-03-03 15:25:45 -06:00
|
|
|
if (!face->is_triangle()) {
|
|
|
|
|
std::cout << "Warning: non-triangular face!" << std::endl;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-01-23 17:27:13 +01:00
|
|
|
CGAL::Polyhedron_3<Kernel_>::Halfedge_around_facet_const_circulator current_halfedge = face->facet_begin();
|
2017-02-08 16:12:19 -06:00
|
|
|
do {
|
|
|
|
|
t->addVertex(surface_style_id,
|
|
|
|
|
CGAL::to_double(current_halfedge->vertex()->point().cartesian(0)),
|
|
|
|
|
CGAL::to_double(current_halfedge->vertex()->point().cartesian(1)),
|
|
|
|
|
CGAL::to_double(current_halfedge->vertex()->point().cartesian(2)));
|
2019-01-23 12:34:21 +01:00
|
|
|
|
|
|
|
|
const double nx = CGAL::to_double(face_normals_map[face].cartesian(0));
|
|
|
|
|
const double ny = CGAL::to_double(face_normals_map[face].cartesian(1));
|
|
|
|
|
const double nz = CGAL::to_double(face_normals_map[face].cartesian(2));
|
|
|
|
|
t->addNormal(nx, ny, nz);
|
|
|
|
|
|
2017-02-08 16:12:19 -06:00
|
|
|
++num_vertices;
|
|
|
|
|
++current_halfedge;
|
|
|
|
|
} while (current_halfedge != face->facet_begin());
|
2019-01-23 12:34:21 +01:00
|
|
|
|
|
|
|
|
t->addFace(surface_style_id, num_vertices-3, num_vertices-2, num_vertices-1);
|
|
|
|
|
|
2017-02-08 16:12:19 -06:00
|
|
|
++num_faces;
|
|
|
|
|
}
|
2019-01-23 12:34:21 +01:00
|
|
|
|
2017-01-16 11:53:11 +01:00
|
|
|
}
|