From b67b389d63f686ba2932a7abdcd8eb8a3d0cf34b Mon Sep 17 00:00:00 2001 From: Daniel Funke Date: Mon, 16 Oct 2023 10:40:00 +0200 Subject: [PATCH] move to kNNG --- .../demo/CGAL_ipelets/lua/libCGAL_nng.lua | 4 ++-- CGAL_ipelets/demo/CGAL_ipelets/nng.cpp | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_nng.lua b/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_nng.lua index fc1f4679178..f9207b9b562 100644 --- a/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_nng.lua +++ b/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_nng.lua @@ -2,7 +2,7 @@ -- CGAL NNG ipelet description ---------------------------------------------------------------------- -label = "Nearest-neighbor graph" +label = "k-nearest-neighbor graph" about = [[ This ipelet is part of the CGAL_ipelet package. See www.cgal.org. @@ -17,7 +17,7 @@ function run(model, num) end methods = { - { label="NNG" }, + { label="kNNG" }, { label="Help" }, } diff --git a/CGAL_ipelets/demo/CGAL_ipelets/nng.cpp b/CGAL_ipelets/demo/CGAL_ipelets/nng.cpp index be43dc45fd7..896d74f138a 100644 --- a/CGAL_ipelets/demo/CGAL_ipelets/nng.cpp +++ b/CGAL_ipelets/demo/CGAL_ipelets/nng.cpp @@ -11,17 +11,19 @@ #include #include +#include + namespace CGAL_nng { typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; typedef CGAL::Delaunay_triangulation_2 Triangulation; const std::string Slab[] = { - "NNG", "Help" + "kNNG", "Help" }; 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 @@ -45,15 +47,28 @@ void nngIpelet::protected_run(int /*fn*/) return; } + int ret_val; + int kNeighbors=1; + + boost::tie(ret_val,kNeighbors)=request_value_from_user((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()); for(auto v = t.finite_vertices_begin(); v != t.finite_vertices_end(); ++v){ - auto nn = CGAL::nearest_neighbor(t, v); - draw_in_ipe(Kernel::Segment_2(v->point(), nn->point())); + std::vector kNN; + 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_(); } }