move to kNNG

This commit is contained in:
Daniel Funke 2023-10-16 10:40:00 +02:00
parent 9d367d2862
commit b67b389d63
2 changed files with 21 additions and 6 deletions

View File

@ -2,7 +2,7 @@
-- CGAL NNG ipelet description -- CGAL NNG ipelet description
---------------------------------------------------------------------- ----------------------------------------------------------------------
label = "Nearest-neighbor graph" label = "k-nearest-neighbor graph"
about = [[ about = [[
This ipelet is part of the CGAL_ipelet package. See www.cgal.org. This ipelet is part of the CGAL_ipelet package. See www.cgal.org.
@ -17,7 +17,7 @@ function run(model, num)
end end
methods = { methods = {
{ label="NNG" }, { label="kNNG" },
{ label="Help" }, { label="Help" },
} }

View File

@ -11,17 +11,19 @@
#include <CGAL/nearest_neighbor_delaunay_2.h> #include <CGAL/nearest_neighbor_delaunay_2.h>
#include <CGAL/CGAL_Ipelet_base.h> #include <CGAL/CGAL_Ipelet_base.h>
#include <boost/format.hpp>
namespace CGAL_nng { namespace CGAL_nng {
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Delaunay_triangulation_2<Kernel> Triangulation; typedef CGAL::Delaunay_triangulation_2<Kernel> Triangulation;
const std::string Slab[] = { const std::string Slab[] = {
"NNG", "Help" "kNNG", "Help"
}; };
const std::string Hmsg[] = { const std::string Hmsg[] = {
"Draw the nearest-neighbor graph of a set of points" "Draw the k-nearest-neighbor graph of a set of points"
}; };
struct nngIpelet struct nngIpelet
@ -45,15 +47,28 @@ void nngIpelet::protected_run(int /*fn*/)
return; return;
} }
int ret_val;
int kNeighbors=1;
boost::tie(ret_val,kNeighbors)=request_value_from_user<int>((boost::format("Number of nearest neighbors (default : k=%1%)") % kNeighbors).str() );
if (ret_val == -1) return;
if (ret_val == 0) kNeighbors=1;
Triangulation t(pt_list.begin(), pt_list.end()); Triangulation t(pt_list.begin(), pt_list.end());
for(auto v = t.finite_vertices_begin(); for(auto v = t.finite_vertices_begin();
v != t.finite_vertices_end(); v != t.finite_vertices_end();
++v){ ++v){
auto nn = CGAL::nearest_neighbor(t, v); std::vector<Triangulation::Vertex_handle> kNN;
draw_in_ipe(Kernel::Segment_2(v->point(), nn->point())); CGAL::nearest_neighbors(t, v, kNeighbors+1, std::back_inserter(kNN)); // +1 as v itself counts as its nearest neigbhor for CGAL::nearest_neighbors
for(const auto & nn : kNN) {
draw_in_ipe(Kernel::Segment_2(v->point(), nn->point()));
}
} }
group_selected_objects_();
} }
} }