From bf47181a9072bcd049949b77d8f6fd9996612a57 Mon Sep 17 00:00:00 2001 From: Sven Oesau Date: Mon, 1 Dec 2025 15:28:39 +0100 Subject: [PATCH] added refitting --- .../Convex_decomposition_3.txt | 222 ++-------- .../approximate_convex_decomposition.cpp | 1 + ...Approximate_convex_decomposition_dialog.ui | 123 +++--- ...pproximate_convex_decomposition_plugin.cpp | 34 +- .../Convex_decomposition/CMakeLists.txt | 6 + .../internal/parameters_interface.h | 1 + .../CGAL/approximate_convex_decomposition.h | 381 +++++++++++++++--- 7 files changed, 454 insertions(+), 314 deletions(-) diff --git a/Convex_decomposition_3/doc/Convex_decomposition_3/Convex_decomposition_3.txt b/Convex_decomposition_3/doc/Convex_decomposition_3/Convex_decomposition_3.txt index ea5b8e8d42b..16e859e049d 100644 --- a/Convex_decomposition_3/doc/Convex_decomposition_3/Convex_decomposition_3.txt +++ b/Convex_decomposition_3/doc/Convex_decomposition_3/Convex_decomposition_3.txt @@ -102,7 +102,7 @@ This example shows the usage of `CGAL::convex_decomposition_3(Nef_polyhedron_3&) \cgalFigureCaptionBegin{Acd_topfig} -Approximate convex decomposition of the elephant.off model.\n From left to right: input mesh, 5, 8, and 12 convex volumes +Approximate convex decomposition of the elephant.off model.\n From left to right: input mesh, 6, 9, and 12 convex volumes \cgalFigureCaptionEnd The H-VACD method \cgalCite{cgal:mamou2016volumetric}, "Hierarchical volumetrix approximate convex decomposition", computes a set of convex volumes that fit the polyhedron. Contrary to the decomposition of the polyhedron into convex parts, the convex volumes cover the polyhedron, but also include additional volume outside of it. @@ -120,7 +120,7 @@ The algorithm computes a set of convex volumes \f$ C=\{C_i\f$ with \f$ i \in[0.. d(A, B) = |A| - |B| \f} -Where \f$|A|\f$ is the volume of A, P is the input polyhedron and \f$C_i\f$ are convex volumes. The convex volumes \f$C_i\f$ are pairwise disjoint, i.e., \f$|C_i \cap C_j| = 0\f$ if \f$i \neq j\f$. And the union of convex volumes contain the input polyhedron \f$ P \subset \bigcup_{C_i \in C} \f$. +Where \f$|A|\f$ is the volume of A, P is the input polyhedron and \f$C_i\f$ are convex volumes. The convex volumes \f$C_i\f$ are may slightly overlap and their union contain the input polyhedron \f$ P \subset \bigcup_{C_i \in C} \f$. \cgalFigureAnchor{Acd_pipelinefig}
@@ -133,7 +133,9 @@ Approximate convex decomposition pipeline.\n From left to right: 1. input mesh 2 The method employs a top-down splitting phase followed by a bottom-up merging to achieve the target number of convex volumes. The splitting phase aims at decomposing the input mesh into smaller mostly convex parts. Each part of the input mesh is approximated with its convex hull. In a hierarchical manner, each part of the mesh is split into two parts when its convexity is low. The convexity is measured by the volume difference of the part and its convex hull. Splitting a part into two can be done by simply cutting the longest side of the bounding box in half. A better choice is often found by searching the longest side of the bounding box for a concave spot. However, it comes with a higher computational cost. The hierarchical splitting stops when either the convexity is sufficiently high or the maximum depth is reached. The volume calculation, convex hull computation and the concavity search is accelerated by a voxel grid. The grid is prepared before the splitting phase and voxel cells overlapping with triangles are labeled as surface. The remaining voxels are labeled as outside or inside by flood fill, in case the input mesh is closed, or by axis-aligned ray shooting, i.e., along x, y and z-axes in positive and negative directions. A voxel is only labeled as inside if at least 3 faces facing away from the voxel have been hit and no face facing towards the voxel. The convex hulls are calculated from voxel corners. Thus, a mesh with a high resolution is less penalized by its number of vertices. -The splitting phase typically results in a number of convex volumes larger than targeted. The second phase employs a bottom-up merging that reduces the number of convex volumes to the targeted number while aiming at maintaining a low volume difference between convex volumes and the input mesh. The greedy merging maintains a priority queue to incrementally merge the pair of convex volumes with the smallest increase of volume difference. +The splitting phase typically results in a number of convex volumes larger than targeted. + +To optionally improve the fit of the convex volumes, they can be refitted to the mesh before starting the second phase. The second phase employs a bottom-up merging that reduces the number of convex volumes to the targeted number while aiming at maintaining a low volume difference between convex volumes and the input mesh. The greedy merging maintains a priority queue to incrementally merge the pair of convex volumes with the smallest increase of volume difference. The splitting phase is not limited by the chosen `maximum_number_of_convex_volumes`, because a splitting into a larger number of more convex parts with a subsequent merging leads to better results. @@ -142,6 +144,7 @@ The splitting phase is not limited by the chosen `maximum_number_of_convex_volum Several parameters of the algorithm impact the quality of the result as well as the running time. - `maximum_number_of_convex_volumes`: The maximum number of convex volumes output by the method. The actual number may be lower for mostly convex input meshes, e.g., a sphere. The impact on the running time is rather low. The default is 16. - `maximum_depth`: The maximum depth for the hierarchical splitting phase. For complex meshes, a higher maximum depth is required to split small concavities into convex parts. The choice of `maximum_depth` has a larger impact on the running time. The default is 10. +- `refitting`: The convex hulls can be refitted after the splitting phase to more tightly enclose the input mesh. It increases the running time, but significantly reduces the overhead volume included by the computed convex volulmes. It is enabled by default. - `maximum_number_of_voxels`: This parameter controls the resolution of the voxel grid used for speed-up. Larger numbers result in a higher memory footprint and a higher running time. A small number also limits the `maximum_depth`. The voxel grid is isotropic and the longest axis of the bounding box will be split into a number of voxels equal to the cubic root of `maximum_number_of_voxels`. The default value is 1.000.000. - `volume_error`: The splitting of a convex volume into smaller parts is controlled by the `volume_error` which provides the tolerance for difference in volume. The difference is calculated by \f$ (|C_i| - |P_i|) / |P_i|\f$. The default value is 0.01. Thus, if a convex volume has 1 percent more volume that the part of the input mesh it approximates, it will be further divided. - `split_at_concavity`: The splitting can be either performed after searching a concavity on the longest side of the bounding box or simply by splitting the longest side of the bounding box in half. The default value is true, i.e., splitting at the concavity. @@ -151,209 +154,40 @@ Several parameters of the algorithm impact the quality of the result as well as Here will be images and more tables to show the impact of different parameters The method has been evaluated on several models: - - - - +| Data set | Faces | Volume | Convex hull volume | Overhead | +| :---- | ----: | ----: | -: | -: | +| Camel | 19.536 | 0.0468 | 0.15541 | 2.32388 | +| Elephant | 5.558 | 0.0462 | 0.12987 | 1.81087 | +| Triceratops | 5.660 | 136.732 | 336.925 | 1.46412 | - - - -

-
-Data set - -Faces - -Volume - -Convex hull volume - -Overhead -

-
-Camel - -19.536 - -0.0468 - -0.15541 - -2.32388 -
-Elephant - -5.558 - -0.0462 - -0.12987 - -1.81087 -
-Triceratops - -5.660 - -136.732 - -336.925 - -1.46412 -

-
If not mentioned otherwise, all tests used a volume error of 0.01, a maximum depth of 10, 1 million voxels and split at the concavity. -Impact of varying the number of generated convex volumes with splitting at the concavity on volume overhead: - - - +Impact of varying the number of generated convex volumes with splitting at the concavity and refitting to the convex hull of the input mesh on volume overhead: - - +| Data set | Split location | Refitting | 4 volumes | 6 volumes | 8 volumes | 10 volumes | 12 volumes | +| :---- | ----: | ----: | -: | -: | -: | -: | -: | +| Camel | Concavity | + | 0.6951 | 0.4316 | 0.3016 | 0.2173 | 0.1939 | +| Camel | Concavity | - | 0.9932 | 0.7482 | 0.6174 | 0.5507 | 0.5261 | +| Elephant | Concavity | + | 0.7897 | 0.6505 | 0.4973 | 0.3986 | 0.3299 | +| Elephant | Concavity | - | 1.2140 | 1.0028 | 0.8071 | 0.7290 | 0.6870 | +| Triceratops | Concavity | + | 0.5952 | 0.3978 | 0.3548 | 0.2385 | 0.2057 | +| Triceratops | Concavity | - | 1.0073 | 0.7490 | 0.7035 | 0.5966 | 0.5429 | - -

-
-Data set - -Split location - -5 volumes - -7 volumes - -9 volumes - -11 volumes - -12 volumes -

-
-Camel - -Concavity - -0.8006 - -0.6680 - -0.5871 - -0.5736 - -0.5463 - -
-Elephant - -Concavity - -1.1927 - -0.9731 - -0.8461 - -0.7506 - -0.6947 - -
-Triceratops - -Concavity - -0.9770 - -0.7676 - -0.6722 - -0.5971 - -0.5658 - -

-
And by using the mid split: - - - - - - - -

-
-Data set - -Split location - -5 volumes - -7 volumes - -9 volumes - -11 volumes - -12 volumes -

-
-Camel - -Mid - -1.1158 - -1.0468 - -0.8660 - -0.6764 - -0.6057 - -
-Elephant - -Mid - -1.2121 - -1.0087 - -0.8444 - -0.7465 - -0.6931 - -
-Triceratops - -Mid - -0.8358 - -0.6870 - -0.7071 - -0.7375 - -0.6586 - -

-
+| Data set | Split location | Refitting | 4 volumes | 6 volumes | 8 volumes | 10 volumes | 12 volumes | +| :---- | ----: | ----: | -: | -: | -: | -: | -: | +| Camel | Mid | + | 0.9970 | 0.5762 | 0.5332 | 0.4040 | 0.2390 | +| Camel | Mid | - | 1.0875 | 0.9280 | 0.8394 | 0.6801 | 0.5529 | +| Elephant | Mid | + | 0.7424 | 0.5672 | 0.4207 | 0.3619 | 0.3043 | +| Elephant | Mid | - | 1.2075 | 1.0410 | 0.8247 | 0.6950 | 0.6434 | +| Triceratops | Mid | + | 0.7671 | 0.5096 | 0.3965 | 0.3309 | 0.2494 | +| Triceratops | Mid | - | 1.0470 | 0.8990 | 0.6749 | 0.5803 | 0.5230 | The running time for all cases in the above tables were between 1.4 and 3 seconds while being slightly lower when splitting at the concavity. Although searching the voxel grid for the concavity takes additional computational time, it is more than compensated by fewer splits. diff --git a/Convex_decomposition_3/examples/Convex_decomposition_3/approximate_convex_decomposition.cpp b/Convex_decomposition_3/examples/Convex_decomposition_3/approximate_convex_decomposition.cpp index ea2518d1718..9cb86c54e3b 100644 --- a/Convex_decomposition_3/examples/Convex_decomposition_3/approximate_convex_decomposition.cpp +++ b/Convex_decomposition_3/examples/Convex_decomposition_3/approximate_convex_decomposition.cpp @@ -35,6 +35,7 @@ int main(int argc, char* argv[]) .volume_error(0.1) .maximum_number_of_convex_volumes(9) .split_at_concavity(true) + .refitting(true) .maximum_number_of_voxels(1000000) .concurrency_tag(CGAL::Parallel_if_available_tag())); diff --git a/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_dialog.ui b/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_dialog.ui index 86618c748c6..bb7668ebf5e 100644 --- a/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_dialog.ui +++ b/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_dialog.ui @@ -7,7 +7,7 @@ 0 0 448 - 215 + 225 @@ -26,30 +26,13 @@ - - - - 0.01 - - - Maximum number of components - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter @@ -59,7 +42,30 @@ Number of voxels - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter + + + + + + + Maximum decomposition depth + + + Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter + + + + + + + 1 + + + 10000 + + + 16 @@ -69,37 +75,44 @@ Volume error - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter - - + + - Qt::Vertical + Qt::Orientation::Horizontal - - - 20 - 40 - + + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok - + - - - - Maximum decomposition depth + + + + 2 - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + 50 + + + 10 + + + + + + + 0.01 - Qt::RightToLeft + Qt::LayoutDirection::RightToLeft Split at concavity @@ -125,29 +138,29 @@ - - - - 2 + + + + Qt::Orientation::Vertical - - 50 + + + 20 + 40 + - - 10 - - + - - - - 1 + + + + Qt::LayoutDirection::RightToLeft - - 10000 + + Refit to mesh - - 16 + + true diff --git a/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_plugin.cpp b/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_plugin.cpp index 6dff06deed5..8d31ef536dc 100644 --- a/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_plugin.cpp +++ b/Lab/demo/Lab/Plugins/Convex_decomposition/Approximate_convex_decomposition_plugin.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -103,40 +104,45 @@ approximate_convex_decomposition() if (i == QDialog::Rejected) return; - const unsigned int maximumDepth = static_cast(ui.maximumDepth->value()); - const unsigned int maximumConvexHulls = static_cast(ui.maximumConvexHulls->value()); - const unsigned int numVoxels = static_cast(ui.numVoxels->value()); - const double volumeError = ui.volumeError->value(); - const bool splitConcavity = ui.splitConcavity->isChecked(); + const unsigned int maximum_depth = static_cast(ui.maximumDepth->value()); + const unsigned int maximum_convex_hulls = static_cast(ui.maximumConvexHulls->value()); + const unsigned int num_voxels = static_cast(ui.numVoxels->value()); + const double volume_error = ui.volumeError->value(); + const bool split_concavity = ui.splitConcavity->isChecked(); + const bool refitting = ui.refitting->isChecked(); QApplication::setOverrideCursor(Qt::WaitCursor); std::vector convex_volumes; - convex_volumes.reserve(9); + convex_volumes.reserve(maximum_convex_hulls); CGAL::approximate_convex_decomposition(*(sm_item->face_graph()), std::back_inserter(convex_volumes), - CGAL::parameters::maximum_depth(maximumDepth) - .volume_error(volumeError) - .maximum_number_of_convex_volumes(maximumConvexHulls) - .split_at_concavity(splitConcavity) - .maximum_number_of_voxels(numVoxels) + CGAL::parameters::maximum_depth(maximum_depth) + .volume_error(volume_error) + .maximum_number_of_convex_volumes(maximum_convex_hulls) + .split_at_concavity(split_concavity) + .refitting(refitting) + .maximum_number_of_voxels(num_voxels) .concurrency_tag(CGAL::Parallel_if_available_tag())); - std::vector distinct_colors; // the first color is either the background or the unique domain compute_deterministic_color_map(QColor(80, 250, 80), convex_volumes.size(), std::back_inserter(distinct_colors)); + Scene_group_item* group = new Scene_group_item(tr("%1 %2 decomposition").arg(sm_item->name()).arg(maximum_convex_hulls)); + scene->addItem(group); + for (std::size_t i = 0; i < convex_volumes.size(); i++) { - const Convex_hull& ch = convex_volumes[i]; + Convex_hull& ch = convex_volumes[i]; SMesh sm; CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(ch.first, ch.second, sm); - Scene_surface_mesh_item* component_item = new Scene_surface_mesh_item(sm); + Scene_surface_mesh_item* component_item = new Scene_surface_mesh_item(std::move(sm)); component_item->setName(tr("%1 %2").arg(sm_item->name()).arg(i)); component_item->setColor(distinct_colors[i]); Three::scene()->addItem(component_item); + Three::scene()->changeGroup(component_item, group); } QApplication::restoreOverrideCursor(); diff --git a/Lab/demo/Lab/Plugins/Convex_decomposition/CMakeLists.txt b/Lab/demo/Lab/Plugins/Convex_decomposition/CMakeLists.txt index d4d7740ff8e..d5e2be10a60 100644 --- a/Lab/demo/Lab/Plugins/Convex_decomposition/CMakeLists.txt +++ b/Lab/demo/Lab/Plugins/Convex_decomposition/CMakeLists.txt @@ -9,3 +9,9 @@ cgal_lab_plugin(acd_plugin Approximate_convex_decomposition_plugin ${acdUI_FILES target_link_libraries(nef_plugin PRIVATE scene_nef_polyhedron_item scene_surface_mesh_item) target_link_libraries(acd_plugin PRIVATE scene_surface_mesh_item) + +find_package(TBB QUIET) +include(CGAL_TBB_support) +if(TARGET CGAL::TBB_support) + target_link_libraries(acd_plugin PRIVATE CGAL::TBB_support) +endif() diff --git a/STL_Extension/include/CGAL/STL_Extension/internal/parameters_interface.h b/STL_Extension/include/CGAL/STL_Extension/internal/parameters_interface.h index f02f1fd4676..4695d38325e 100644 --- a/STL_Extension/include/CGAL/STL_Extension/internal/parameters_interface.h +++ b/STL_Extension/include/CGAL/STL_Extension/internal/parameters_interface.h @@ -181,6 +181,7 @@ CGAL_add_named_parameter(apply_iterative_snap_rounding_t, apply_iterative_snap_r CGAL_add_named_parameter(snap_grid_size_t, snap_grid_size, snap_grid_size) CGAL_add_named_parameter(maximum_number_of_voxels_t, maximum_number_of_voxels, maximum_number_of_voxels) CGAL_add_named_parameter(maximum_depth_t, maximum_depth, maximum_depth) +CGAL_add_named_parameter(refitting_t, refitting, refitting) CGAL_add_named_parameter(volume_error_t, volume_error, volume_error) CGAL_add_named_parameter(maximum_number_of_convex_volumes_t, maximum_number_of_convex_volumes, maximum_number_of_convex_volumes) CGAL_add_named_parameter(split_at_concavity_t, split_at_concavity, split_at_concavity) diff --git a/Surface_mesh_decomposition/include/CGAL/approximate_convex_decomposition.h b/Surface_mesh_decomposition/include/CGAL/approximate_convex_decomposition.h index df810f94104..c63ca2f6acb 100644 --- a/Surface_mesh_decomposition/include/CGAL/approximate_convex_decomposition.h +++ b/Surface_mesh_decomposition/include/CGAL/approximate_convex_decomposition.h @@ -18,8 +18,6 @@ #include #include -#include - #include #include @@ -44,6 +42,8 @@ #include #endif +#include + namespace CGAL { namespace internal { @@ -325,7 +325,7 @@ Bbox_uint grid_bbox_face(const FaceGraph& mesh, const typename boost::graph_trai } template -Iso_cuboid_3 bbox_voxel(const Vec3_uint& voxel, const Bbox_3& bb, const typename GeomTraits::FT& voxel_size) { +typename GeomTraits::Iso_cuboid_3 bbox_voxel(const Vec3_uint& voxel, const Bbox_3& bb, const typename GeomTraits::FT& voxel_size) { double vs = to_double(voxel_size); return Bbox_3( bb.xmin() + voxel[0] * vs, @@ -337,6 +337,20 @@ Iso_cuboid_3 bbox_voxel(const Vec3_uint& voxel, const Bbox_3& bb, co ); } + +template +typename GeomTraits::Iso_cuboid_3 bbox_voxel_bbox(const Bbox_uint& voxelbb, const Bbox_3& bb, const typename GeomTraits::FT& voxel_size) { + double vs = to_double(voxel_size); + return Bbox_3( + bb.xmin() + voxelbb.lower[0] * vs, + bb.ymin() + voxelbb.lower[1] * vs, + bb.zmin() + voxelbb.lower[2] * vs, + bb.xmin() + (voxelbb.upper[0] + 1) * vs, + bb.ymin() + (voxelbb.upper[1] + 1) * vs, + bb.zmin() + (voxelbb.upper[2] + 1) * vs + ); +} + void scanline_floodfill(Grid_cell label, std::vector& grid, const Vec3_uint& grid_size, std::deque& todo) { const auto vox = [&grid, &grid_size](unsigned int x, unsigned int y, unsigned int z) -> int8_t& { return grid[z + (y * grid_size[2]) + (x * grid_size[1] * grid_size[2])]; @@ -728,7 +742,7 @@ template struct Convex_hull_candidate { using FT = typename GeomTraits::FT; using Point_3 = typename GeomTraits::Point_3; - Iso_cuboid_3 bbox; + typename GeomTraits::Iso_cuboid_3 bbox; FT voxel_volume; FT volume; FT volume_error; @@ -736,14 +750,14 @@ struct Convex_hull_candidate { std::vector> indices; Convex_hull_candidate() noexcept : voxel_volume(0), volume(0), volume_error(0) {} - Convex_hull_candidate(Convex_hull_candidate&& o) noexcept { - bbox = o.bbox; - voxel_volume = o.voxel_volume; - volume = o.volume; - volume_error = o.volume_error; - points = std::move(o.points); - indices = std::move(o.indices); - } + Convex_hull_candidate(Convex_hull_candidate&& o) noexcept : + bbox(o.bbox), + voxel_volume(o.voxel_volume), + volume(o.volume), + volume_error(o.volume_error), + points(std::move(o.points)), + indices(std::move(o.indices)) + {} Convex_hull_candidate& operator= (Convex_hull_candidate&& o) noexcept { bbox = o.bbox; @@ -892,7 +906,7 @@ void fill_grid(Candidate &c, std::vector &grid, const FaceGr for (unsigned int x = face_bb.lower[0]; x <= face_bb.upper[0]; x++) for (unsigned int y = face_bb.lower[1]; y <= face_bb.upper[1]; y++) for (unsigned int z = face_bb.lower[2]; z <= face_bb.upper[2]; z++) { - Iso_cuboid_3 box = bbox_voxel({ x, y, z }, bb, voxel_size); + typename GeomTraits::Iso_cuboid_3 box = bbox_voxel({ x, y, z }, bb, voxel_size); const typename GeomTraits::Point_3 &p = point(fd, mesh); if (do_intersect(Polygon_mesh_processing::triangle(fd, mesh), box) || box.has_on_bounded_side(p)) vox(x, y, z) = Grid_cell::SURFACE; @@ -1169,6 +1183,217 @@ bool finished(Candidate &c, const NamedParameters& np) { return false; } +template +void shrink_candidates(const FaceGraph& tmesh, std::vector>& candidates, const Bbox_3& bbox, const typename GeomTraits::FT& voxel_size, CGAL::Sequential_tag) { + using face_descriptor = typename boost::graph_traits::face_descriptor; + + using Point_3 = typename GeomTraits::Point_3; + using Segment_3 = typename GeomTraits::Segment_3; + using Triangle_3 = typename GeomTraits::Triangle_3; + using FT = typename GeomTraits::FT; + + using Primitive = CGAL::AABB_face_graph_triangle_primitive; + using Traits = CGAL::AABB_traits_3; + using Tree = CGAL::AABB_tree; + + Tree tree(faces(tmesh).first, faces(tmesh).second, tmesh); + + for (Candidate& c : candidates) { + std::vector pts; + pts.reserve(c.new_surface.size() * 8); + using Box = typename GeomTraits::Iso_cuboid_3; + using IP_id = typename Tree::template Intersection_and_primitive_id::Type; + std::vector intersections; + tree.all_intersections(c.ch.bbox, std::back_inserter(intersections)); + + std::vector corners(8); + std::vector taken(8, false); + + corners[0] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymin(), c.ch.bbox.zmin()); + corners[1] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymax(), c.ch.bbox.zmin()); + corners[2] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymin(), c.ch.bbox.zmax()); + corners[3] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymax(), c.ch.bbox.zmax()); + corners[4] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymin(), c.ch.bbox.zmin()); + corners[5] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymax(), c.ch.bbox.zmin()); + corners[6] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymin(), c.ch.bbox.zmax()); + corners[7] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymax(), c.ch.bbox.zmax()); + + for (auto& i : intersections) { + const Point_3* p; + const Segment_3* s; + const Triangle_3* t; + const std::vector* v; + if (p = std::get_if(&(i.first))) { + pts.push_back(*p); + } + else if (s = std::get_if(&(i.first))) { + pts.push_back(s->source()); + pts.push_back(s->target()); + } + else if (t = std::get_if(&(i.first))) { + pts.push_back((*t)[0]); + pts.push_back((*t)[1]); + pts.push_back((*t)[2]); + auto pl = t->supporting_plane(); + for (std::size_t c = 0; c < 8; c++) + if (!taken[c]) + if (pl.oriented_side(corners[c]) != CGAL::ON_POSITIVE_SIDE) + taken[c] = true; + } + else if (v = std::get_if>(&(i.first))) { + const Triangle_3 &t = CGAL::Polygon_mesh_processing::triangle(i.second, tmesh); + auto pl = t.supporting_plane(); + for (std::size_t c = 0; c < 8; c++) + if (!taken[c]) + if (pl.oriented_side(corners[c]) != CGAL::ON_POSITIVE_SIDE) + taken[c] = true; + std::copy(v->begin(), v->end(), std::back_inserter(pts)); + } + } + + for (std::size_t c = 0; c < 8; c++) + if (taken[c]) + pts.push_back(corners[c]); + + pts.reserve(pts.size() + c.new_surface.size() * 8); + + for (const Vec3_uint& v : c.new_surface) { + FT xmin = bbox.xmin() + FT(v[0]) * voxel_size; + FT ymin = bbox.ymin() + FT(v[1]) * voxel_size; + FT zmin = bbox.zmin() + FT(v[2]) * voxel_size; + FT xmax = bbox.xmin() + FT(v[0] + 1) * voxel_size; + FT ymax = bbox.ymin() + FT(v[1] + 1) * voxel_size; + FT zmax = bbox.zmin() + FT(v[2] + 1) * voxel_size; + pts.push_back(Point_3(xmin, ymin, zmin)); + pts.push_back(Point_3(xmin, ymax, zmin)); + pts.push_back(Point_3(xmin, ymin, zmax)); + pts.push_back(Point_3(xmin, ymax, zmax)); + pts.push_back(Point_3(xmax, ymin, zmin)); + pts.push_back(Point_3(xmax, ymax, zmin)); + pts.push_back(Point_3(xmax, ymin, zmax)); + pts.push_back(Point_3(xmax, ymax, zmax)); + } + + convex_hull_3(pts.begin(), pts.end(), c.ch.points, c.ch.indices); + c.ch.bbox = bbox_3(c.ch.points.begin(), c.ch.points.end()); + + if (c.ch.indices.size() <= 3 || (c.ch.volume = volume(c.ch.points, c.ch.indices)) == 0) + c.ch.volume_error = -1; + else + c.ch.volume_error = CGAL::abs(c.ch.volume - c.ch.voxel_volume) / c.ch.voxel_volume; + } +} + +template +void shrink_candidates(const FaceGraph& tmesh, std::vector>& candidates, const Bbox_3& bbox, const typename GeomTraits::FT& voxel_size, CGAL::Parallel_tag) { +#ifdef CGAL_LINKED_WITH_TBB + using face_descriptor = typename boost::graph_traits::face_descriptor; + + using Point_3 = typename GeomTraits::Point_3; + using Segment_3 = typename GeomTraits::Segment_3; + using Triangle_3 = typename GeomTraits::Triangle_3; + using FT = typename GeomTraits::FT; + + using Primitive = CGAL::AABB_face_graph_triangle_primitive; + using Traits = CGAL::AABB_traits_3; + using Tree = CGAL::AABB_tree; + + Tree tree(faces(tmesh).first, faces(tmesh).second, tmesh); + + const auto shrink = [&tree, voxel_size, &bbox, &tmesh](Candidate& c) { + std::vector pts; + pts.reserve(c.new_surface.size() * 8); + using Box = typename GeomTraits::Iso_cuboid_3; + using IP_id = typename Tree::template Intersection_and_primitive_id::Type; + std::vector intersections; + tree.all_intersections(c.ch.bbox, std::back_inserter(intersections)); + + std::vector corners(8); + std::vector taken(8, false); + + corners[0] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymin(), c.ch.bbox.zmin()); + corners[1] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymax(), c.ch.bbox.zmin()); + corners[2] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymin(), c.ch.bbox.zmax()); + corners[3] = Point_3(c.ch.bbox.xmin(), c.ch.bbox.ymax(), c.ch.bbox.zmax()); + corners[4] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymin(), c.ch.bbox.zmin()); + corners[5] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymax(), c.ch.bbox.zmin()); + corners[6] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymin(), c.ch.bbox.zmax()); + corners[7] = Point_3(c.ch.bbox.xmax(), c.ch.bbox.ymax(), c.ch.bbox.zmax()); + + for (auto& i : intersections) { + const Point_3* p; + const Segment_3* s; + const Triangle_3* t; + const std::vector* v; + if (p = std::get_if(&(i.first))) { + pts.push_back(*p); + } + else if (s = std::get_if(&(i.first))) { + pts.push_back(s->source()); + pts.push_back(s->target()); + } + else if (t = std::get_if(&(i.first))) { + pts.push_back((*t)[0]); + pts.push_back((*t)[1]); + pts.push_back((*t)[2]); + auto pl = t->supporting_plane(); + for (std::size_t c = 0; c < 8; c++) + if (!taken[c]) + if (pl.oriented_side(corners[c]) != CGAL::ON_POSITIVE_SIDE) + taken[c] = true; + } + else if (v = std::get_if>(&(i.first))) { + const Triangle_3& t = CGAL::Polygon_mesh_processing::triangle(i.second, tmesh); + auto pl = t.supporting_plane(); + for (std::size_t c = 0; c < 8; c++) + if (!taken[c]) + if (pl.oriented_side(corners[c]) != CGAL::ON_POSITIVE_SIDE) + taken[c] = true; + std::copy(v->begin(), v->end(), std::back_inserter(pts)); + } + } + + for (std::size_t c = 0;c<8;c++) + if (taken[c]) + pts.push_back(corners[c]); + + pts.reserve(pts.size() + c.new_surface.size() * 8); + + for (const Vec3_uint& v : c.new_surface) { + FT xmin = bbox.xmin() + FT(v[0]) * voxel_size; + FT ymin = bbox.ymin() + FT(v[1]) * voxel_size; + FT zmin = bbox.zmin() + FT(v[2]) * voxel_size; + FT xmax = bbox.xmin() + FT(v[0] + 1) * voxel_size; + FT ymax = bbox.ymin() + FT(v[1] + 1) * voxel_size; + FT zmax = bbox.zmin() + FT(v[2] + 1) * voxel_size; + pts.push_back(Point_3(xmin, ymin, zmin)); + pts.push_back(Point_3(xmin, ymax, zmin)); + pts.push_back(Point_3(xmin, ymin, zmax)); + pts.push_back(Point_3(xmin, ymax, zmax)); + pts.push_back(Point_3(xmax, ymin, zmin)); + pts.push_back(Point_3(xmax, ymax, zmin)); + pts.push_back(Point_3(xmax, ymin, zmax)); + pts.push_back(Point_3(xmax, ymax, zmax)); + } + + convex_hull_3(pts.begin(), pts.end(), c.ch.points, c.ch.indices); + c.ch.bbox = bbox_3(c.ch.points.begin(), c.ch.points.end()); + + if (c.ch.indices.size() <= 3 || (c.ch.volume = volume(c.ch.points, c.ch.indices)) == 0) + c.ch.volume_error = -1; + else + c.ch.volume_error = CGAL::abs(c.ch.volume - c.ch.voxel_volume) / c.ch.voxel_volume; + }; + + tbb::parallel_for_each(candidates, shrink); +#else + assert(false); + CGAL_USE(candidates); + CGAL_USE(bbox); + CGAL_USE(voxel_size); +#endif +} + template void recurse(std::vector>& candidates, std::vector& grid, const Vec3_uint& grid_size, const Bbox_3& bbox, const typename GeomTraits::FT& voxel_size, const NamedParameters& np, CGAL::Parallel_tag) { #ifdef CGAL_LINKED_WITH_TBB @@ -1264,11 +1489,14 @@ void merge(std::vector>& candidates, const typ FT volume_error; bool operator < (const Merged_candidate& other) const { + if (volume_error == other.volume_error) + return other.ch > ch; + else return (volume_error > other.volume_error); } - Merged_candidate() : ch_a(-1), ch_b(-1) {} - Merged_candidate(std::size_t ch_a, std::size_t ch_b) : ch_a(ch_a), ch_b(ch_b) {} + Merged_candidate() : ch_a(-1), ch_b(-1), ch(-1) {} + Merged_candidate(std::size_t ch_a, std::size_t ch_b) : ch_a(ch_a), ch_b(ch_b), ch(-1) {} }; tbb::concurrent_unordered_map> hulls; @@ -1285,12 +1513,12 @@ void merge(std::vector>& candidates, const typ candidates.reserve(max_convex_hulls); std::vector todo; - tbb::concurrent_priority_queue queue; + std::priority_queue queue; const auto do_merge = [hull_volume, &hulls, &num_hulls](Merged_candidate& m) { - Convex_hull_candidate& ci = hulls[m.ch_a]; - Convex_hull_candidate& cj = hulls[m.ch_b]; - m.ch = num_hulls.fetch_add(1); + const Convex_hull_candidate& ci = hulls[m.ch_a]; + const Convex_hull_candidate& cj = hulls[m.ch_b]; + Convex_hull_candidate& ch = hulls[m.ch]; ch.bbox = box_union(ci.bbox, cj.bbox); @@ -1299,9 +1527,18 @@ void merge(std::vector>& candidates, const typ std::copy(cj.points.begin(), cj.points.end(), std::back_inserter(pts)); convex_hull_3(pts.begin(), pts.end(), ch.points, ch.indices); - ch.volume = volume(ch.points, ch.indices); + if (ch.indices.size() <= 3) { + m.volume_error = ch.volume_error = -1; + return; + } - ch.volume_error = m.volume_error = CGAL::abs(ci.volume + cj.volume - ch.volume) / hull_volume; + ch.volume = volume(ch.points, ch.indices); + if (ci.volume_error == -1 || cj.volume_error == -1) { + m.volume_error = -1; + ch.volume_error = 0; + } + else + ch.volume_error = m.volume_error = CGAL::abs(ci.volume + cj.volume - ch.volume) / hull_volume; }; for (std::size_t i : keep) { @@ -1310,13 +1547,18 @@ void merge(std::vector>& candidates, const typ if (j <= i) continue; const Convex_hull_candidate& cj = hulls[j]; - if (CGAL::do_intersect(ci.bbox, cj.bbox)) + if (CGAL::do_intersect(ci.bbox, cj.bbox)) { todo.emplace_back(Merged_candidate(i, j)); + todo.back().ch = num_hulls++; + } else { Merged_candidate m(i, j); Bbox_3 bbox = box_union(ci.bbox, cj.bbox).bbox(); - m.ch = -1; - m.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; + + if (ci.volume_error == -1 || cj.volume_error == -1) + m.volume_error = -1; + else + m.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; queue.push(std::move(m)); } } @@ -1329,8 +1571,8 @@ void merge(std::vector>& candidates, const typ todo.clear(); while (!queue.empty() && keep.size() > max_convex_hulls) { - Merged_candidate m; - while (!queue.try_pop(m) && !queue.empty()); + Merged_candidate m = queue.top(); + queue.pop(); auto ch_a = hulls.find(m.ch_a); if (ch_a == hulls.end()) @@ -1340,8 +1582,10 @@ void merge(std::vector>& candidates, const typ if (ch_b == hulls.end()) continue; - if (m.ch == -1) + if (m.ch == -1) { + m.ch = num_hulls++; do_merge(m); + } keep.erase(m.ch_a); keep.erase(m.ch_b); @@ -1353,13 +1597,17 @@ void merge(std::vector>& candidates, const typ for (std::size_t id : keep) { const Convex_hull_candidate& ci = hulls[id]; - if (CGAL::do_intersect(ci.bbox, cj.bbox)) + if (CGAL::do_intersect(ci.bbox, cj.bbox)) { todo.emplace_back(Merged_candidate(id, m.ch)); + todo.back().ch = num_hulls++; + } else { Merged_candidate merged(id, m.ch); Bbox_3 bbox = box_union(ci.bbox, cj.bbox).bbox(); - merged.ch = -1; - merged.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; + if (ci.volume_error == -1 || cj.volume_error == -1) + merged.volume_error = -1; + else + merged.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; queue.push(std::move(merged)); } } @@ -1374,6 +1622,8 @@ void merge(std::vector>& candidates, const typ num_hulls = 0; + candidates.reserve(max_convex_hulls); + for (std::size_t i : keep) candidates.push_back(std::move(hulls[i])); #else @@ -1397,11 +1647,14 @@ void merge(std::vector>& candidates, const typ FT volume_error; bool operator < (const Merged_candidate& other) const { + if (volume_error == other.volume_error) + return other.ch > ch; + else return (volume_error > other.volume_error); } - Merged_candidate() : ch_a(-1), ch_b(-1) {} - Merged_candidate(std::size_t ch_a, std::size_t ch_b) : ch_a(ch_a), ch_b(ch_b) {} + Merged_candidate() : ch_a(-1), ch_b(-1), ch(-1) {} + Merged_candidate(std::size_t ch_a, std::size_t ch_b) : ch_a(ch_a), ch_b(ch_b), ch(-1) {} }; std::unordered_map> hulls; @@ -1423,7 +1676,6 @@ void merge(std::vector>& candidates, const typ Convex_hull_candidate& ci = hulls[m.ch_a]; Convex_hull_candidate& cj = hulls[m.ch_b]; - m.ch = num_hulls++; Convex_hull_candidate& ch = hulls[m.ch]; ch.bbox = box_union(ci.bbox, cj.bbox); @@ -1432,9 +1684,18 @@ void merge(std::vector>& candidates, const typ std::copy(cj.points.begin(), cj.points.end(), std::back_inserter(pts)); convex_hull_3(pts.begin(), pts.end(), ch.points, ch.indices); - ch.volume = volume(ch.points, ch.indices); + if (ch.indices.size() <= 3) { + m.volume_error = ch.volume_error = -1; + return; + } - ch.volume_error = m.volume_error = CGAL::abs(ci.volume + cj.volume - ch.volume) / hull_volume; + ch.volume = volume(ch.points, ch.indices); + if (ci.volume_error == -1 || cj.volume_error == -1) { + m.volume_error = -1; + ch.volume_error = 0; + } + else + ch.volume_error = m.volume_error = CGAL::abs(ci.volume + cj.volume - ch.volume) / hull_volume; }; for (std::size_t i : keep) { @@ -1462,8 +1723,11 @@ void merge(std::vector>& candidates, const typ else { Merged_candidate m(i, j); Bbox_3 bbox = box_union(ci.bbox, cj.bbox).bbox(); - m.ch = -1; - m.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; + + if (ci.volume_error == -1 || cj.volume_error == -1) + m.volume_error = -1; + else + m.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; queue.push(std::move(m)); } } @@ -1481,8 +1745,10 @@ void merge(std::vector>& candidates, const typ if (ch_b == hulls.end()) continue; - if (m.ch == -1) + if (m.ch == -1) { + m.ch = num_hulls++; do_merge(m); + } keep.erase(m.ch_a); keep.erase(m.ch_b); @@ -1513,8 +1779,11 @@ void merge(std::vector>& candidates, const typ else { Merged_candidate merged(id, m.ch); Bbox_3 bbox = box_union(ci.bbox, cj.bbox).bbox(); - merged.ch = -1; - merged.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; + + if (ci.volume_error == -1 || cj.volume_error == -1) + merged.volume_error = -1; + else + merged.volume_error = CGAL::abs(ci.volume + cj.volume - bbox.x_span() * bbox.y_span() * bbox.z_span()) / hull_volume; queue.push(std::move(merged)); } } @@ -1562,6 +1831,12 @@ void merge(std::vector>& candidates, const typ * \cgalParamDefault{10} * \cgalParamNEnd * + * \cgalParamNBegin{refitting} + * \cgalParamDescription{refitting of convex volumes after split phase} + * \cgalParamType{Boolean} + * \cgalParamDefault{true} + * \cgalParamNEnd + * * \cgalParamNBegin{maximum_number_of_convex_volumes} * \cgalParamDescription{maximum number of convex volumes produced by the method} * \cgalParamType{unsigned int} @@ -1616,6 +1891,7 @@ std::size_t approximate_convex_decomposition(const FaceGraph& tmesh, OutputItera using FT = typename Geom_traits::FT; const unsigned int num_voxels = parameters::choose_parameter(parameters::get_parameter(np, internal_np::maximum_number_of_voxels), 1000000); + const bool refitting = parameters::choose_parameter(parameters::get_parameter(np, internal_np::refitting), true); using Concurrency_tag = typename internal_np::Lookup_named_param_def::type; #ifndef CGAL_LINKED_WITH_TBB @@ -1625,10 +1901,10 @@ std::size_t approximate_convex_decomposition(const FaceGraph& tmesh, OutputItera } #endif - const unsigned int max_convex_hulls = parameters::choose_parameter(parameters::get_parameter(np, internal_np::maximum_number_of_convex_volumes), 16); - assert(max_convex_hulls > 0); + const unsigned int max_convex_volumes = parameters::choose_parameter(parameters::get_parameter(np, internal_np::maximum_number_of_convex_volumes), 16); + assert(max_convex_volumes > 0); - if (max_convex_hulls == 1) { + if (max_convex_volumes == 1) { internal::Convex_hull_candidate ch; using parameters::choose_parameter; @@ -1651,25 +1927,28 @@ std::size_t approximate_convex_decomposition(const FaceGraph& tmesh, OutputItera std::vector grid(grid_size[0] * grid_size[1] * grid_size[2], internal::Grid_cell::INSIDE); std::vector> candidates(1); - init(candidates[0], tmesh, grid, bb, grid_size, voxel_size, Concurrency_tag()); + internal::init(candidates[0], tmesh, grid, bb, grid_size, voxel_size, Concurrency_tag()); const FT hull_volume = candidates[0].ch.volume; - recurse(candidates, grid, grid_size, bb, voxel_size, np, Concurrency_tag()); + internal::recurse(candidates, grid, grid_size, bb, voxel_size, np, Concurrency_tag()); - std::vector> hulls; + if (refitting) + internal::shrink_candidates(tmesh, candidates, bb, voxel_size, Concurrency_tag()); + + std::vector> volumes; for (internal::Candidate &c : candidates) - hulls.emplace_back(std::move(c.ch)); + volumes.emplace_back(std::move(c.ch)); candidates.clear(); // merge until target number is reached - merge(hulls, hull_volume, max_convex_hulls, Concurrency_tag()); + internal::merge(volumes, hull_volume, max_convex_volumes, Concurrency_tag()); - for (std::size_t i = 0; i < hulls.size(); i++) - *out_volumes++ = std::make_pair(std::move(hulls[i].points), std::move(hulls[i].indices)); + for (std::size_t i = 0; i < volumes.size(); i++) + *out_volumes++ = std::make_pair(std::move(volumes[i].points), std::move(volumes[i].indices)); - return hulls.size(); + return volumes.size(); } } // namespace CGAL