diff --git a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/bbox.h b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/bbox.h index 96e61d7bb67..43e50077fc5 100644 --- a/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/bbox.h +++ b/Polygon_mesh_processing/include/CGAL/Polygon_mesh_processing/bbox.h @@ -15,14 +15,18 @@ #include - #include -#include +#include +#include +#include +#include #include #include +#include + namespace CGAL { namespace Polygon_mesh_processing { @@ -255,6 +259,83 @@ namespace CGAL { } return bb; } + + /*! + * adds a triangulated bounding box to a polygon mesh + * @todo add extended bbox factor + * @todo add triangulate or not as NP + */ + template + void add_bbox(PolygonMesh& pmesh, + const NamedParameters& np = parameters::default_values()) + { + using parameters::choose_parameter; + using parameters::get_parameter; + + typedef typename GetGeomTraits::type GT; + GT gt = choose_parameter(get_parameter(np, internal_np::geom_traits)); + typedef typename GT::Point_3 Point_3; + + const double factor = 1.1; //todo : add as NP + CGAL::Bbox_3 bb = bbox(pmesh); + if (factor != 1.0) + { + const double dx = bb.xmax() - bb.xmin(); + const double dy = bb.ymax() - bb.ymin(); + const double dz = bb.zmax() - bb.zmin(); + const Point_3 center( bb.xmin() + 0.5 * dx, + bb.ymin() + 0.5 * dy, + bb.zmin() + 0.5 * dz ); + bb = Bbox_3( center.x() - factor * 0.5 * dx, + center.y() - factor * 0.5 * dy, + center.z() - factor * 0.5 * dz, + center.x() + factor * 0.5 * dx, + center.y() + factor * 0.5 * dy, + center.z() + factor * 0.5 * dz ); + } + + const bool triangulate = true; //todo : add as NP + + const typename GT::Iso_cuboid_3 bbox(bb); + std::vector bb_points; + for (int i = 0; i < 8; ++i) + bb_points.push_back(bbox[i]); + + std::vector> faces; + if (!triangulate) + { + faces.push_back({0, 1, 2, 3});//bottom + faces.push_back({4, 5, 6, 7});//top + faces.push_back({0, 1, 6, 5});//front + faces.push_back({2, 3, 4, 7});//back + faces.push_back({1, 2, 7, 6});//right + faces.push_back({0, 3, 4, 5});//left + } + else + { + faces.push_back({0, 1, 2});//bottom + faces.push_back({0, 2, 3}); + faces.push_back({4, 5, 6});//top + faces.push_back({4, 6, 7}); + faces.push_back({0, 1, 5});//front + faces.push_back({1, 5, 6}); + faces.push_back({2, 3, 4});//back + faces.push_back({2, 4, 7}); + faces.push_back({1, 2, 6});//right + faces.push_back({2, 6, 7}); + faces.push_back({0, 3, 4});//left + faces.push_back({0, 4, 5}); + } + + PolygonMesh bbox_pmesh; + orient_polygon_soup(bb_points, faces); + polygon_soup_to_polygon_mesh(bb_points, faces, bbox_pmesh); + + CGAL::copy_face_graph(bbox_pmesh, pmesh, + parameters::default_values(), + np); + } } }