Speed up insertation point into triangulation function by

Multi point insertation in CGAL(Constrained_Delaunay_triangulation_2::insert(begin, end))
and example on spatial sort index: https://doc.cgal.org/latest/Spatial_sorting/index.html#secsort_any_type_2
This commit is contained in:
Filip Sykala 2022-03-25 09:14:32 +01:00
parent bdb8c3729f
commit f68d03eb87

View File

@ -3,6 +3,7 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Constrained_Delaunay_triangulation_2.h> #include <CGAL/Constrained_Delaunay_triangulation_2.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h> #include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/spatial_sort.h>
using namespace Slic3r; using namespace Slic3r;
@ -29,27 +30,37 @@ inline void insert_edges(Triangulation::HalfEdges &edges, uint32_t &offset, cons
Triangulation::Indices Triangulation::triangulate(const Points &points, Triangulation::Indices Triangulation::triangulate(const Points &points,
const HalfEdges &constrained_half_edges) const HalfEdges &constrained_half_edges)
{ {
// IMPROVE use int point insted of float !!!
// use cgal triangulation // use cgal triangulation
using K = CGAL::Exact_predicates_inexact_constructions_kernel; using K = CGAL::Exact_predicates_inexact_constructions_kernel;
using Vb = CGAL::Triangulation_vertex_base_with_info_2<uint32_t, K>; using Vb = CGAL::Triangulation_vertex_base_with_info_2<uint32_t, K>;
using Fb = CGAL::Constrained_triangulation_face_base_2<K>; using Fb = CGAL::Constrained_triangulation_face_base_2<K>;
using Tds = CGAL::Triangulation_data_structure_2<Vb, Fb>; using Tds = CGAL::Triangulation_data_structure_2<Vb, Fb>;
using CDT = using CDT = CGAL::Constrained_Delaunay_triangulation_2<K, Tds, CGAL::Exact_predicates_tag>;
CGAL::Constrained_Delaunay_triangulation_2<K, Tds, CGAL::Exact_predicates_tag>;
// construct a constrained triangulation // construct a constrained triangulation
CDT cdt; CDT cdt;
{ {
std::vector<CDT::Vertex_handle> vertices_handle; // for constriants std::vector<CDT::Vertex_handle> vertices_handle(points.size()); // for constriants
vertices_handle.reserve(points.size()); using Point_with_ord = std::pair<CDT::Point, size_t>;
for (const auto &p : points) { using SearchTrait = CGAL::Spatial_sort_traits_adapter_2
uint32_t pi = &p - &points.front(); <K, CGAL::First_of_pair_property_map<Point_with_ord> >;
auto handle = cdt.insert({ p.x(), p.y() });
handle->info() = pi; std::vector<Point_with_ord> cdt_points;
vertices_handle.push_back(handle); cdt_points.reserve(points.size());
size_t ord = 0;
for (const auto &p : points)
cdt_points.emplace_back(std::make_pair(CDT::Point{p.x(), p.y()}, ord++));
SearchTrait st;
CGAL::spatial_sort(cdt_points.begin(), cdt_points.end(), st);
CDT::Face_handle f;
for (const auto& p : cdt_points) {
auto handle = cdt.insert(p.first, f);
handle->info() = p.second;
vertices_handle[p.second] = handle;
f = handle->face();
} }
// Constrain the triangulation. // Constrain the triangulation.
for (const HalfEdge &edge : constrained_half_edges) for (const HalfEdge &edge : constrained_half_edges)
cdt.insert_constraint(vertices_handle[edge.first], vertices_handle[edge.second]); cdt.insert_constraint(vertices_handle[edge.first], vertices_handle[edge.second]);