diff --git a/CGAL_ipelets/demo/CGAL_ipelets/CMakeLists.txt b/CGAL_ipelets/demo/CGAL_ipelets/CMakeLists.txt index 26a9442a90e..cad2c3889dc 100644 --- a/CGAL_ipelets/demo/CGAL_ipelets/CMakeLists.txt +++ b/CGAL_ipelets/demo/CGAL_ipelets/CMakeLists.txt @@ -108,6 +108,7 @@ if ( CGAL_FOUND ) set(CGAL_IPELETS ${CGAL_IPELETS} generator) set(CGAL_IPELETS ${CGAL_IPELETS} mesh_2) set(CGAL_IPELETS ${CGAL_IPELETS} minkowski) + set(CGAL_IPELETS ${CGAL_IPELETS} mst) set(CGAL_IPELETS ${CGAL_IPELETS} multi_delaunay) set(CGAL_IPELETS ${CGAL_IPELETS} multi_regular) set(CGAL_IPELETS ${CGAL_IPELETS} partition) diff --git a/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_mst.lua b/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_mst.lua new file mode 100644 index 00000000000..6d9ed23a44a --- /dev/null +++ b/CGAL_ipelets/demo/CGAL_ipelets/lua/libCGAL_mst.lua @@ -0,0 +1,24 @@ +---------------------------------------------------------------------- +-- CGAL MST ipelet description +---------------------------------------------------------------------- + +label = "Minimum spanning tree" + +about = [[ +This ipelet is part of the CGAL_ipelet package. See www.cgal.org. +]] + +-- this variable will store the C++ ipelet when it has been loaded +ipelet = false + +function run(model, num) + if not ipelet then ipelet = assert(ipe.Ipelet(dllname)) end + model:runIpelet(methods[num].label, ipelet, num) +end + +methods = { + { label="MST" }, + { label="Help" }, +} + +---------------------------------------------------------------------- diff --git a/CGAL_ipelets/demo/CGAL_ipelets/mst.cpp b/CGAL_ipelets/demo/CGAL_ipelets/mst.cpp new file mode 100644 index 00000000000..dc2e938b171 --- /dev/null +++ b/CGAL_ipelets/demo/CGAL_ipelets/mst.cpp @@ -0,0 +1,119 @@ +// Copyright (c) 2018 Inria +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// SPDX-License-Identifier: LGPL-3.0+ +// +// Author(s) : Marc Glisse + +#include +#include +#include +#include +#include +#include + +namespace CGAL_mst { + +typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; +typedef CGAL::Delaunay_triangulation_2 Triangulation; + +const std::string Slab[] = { + "MST", "Help" +}; + +const std::string Hmsg[] = { + "Draw the minimum spanning tree of a set of points" +}; + +struct mstIpelet + : CGAL::Ipelet_base { + mstIpelet() : CGAL::Ipelet_base("Minimum spanning tree", Slab, Hmsg){} + void protected_run(int); +}; + +template +struct Is_finite { + const T* t_; + Is_finite() + : t_(NULL) + {} + Is_finite(const T& t) + : t_(&t) + { } + template + bool operator()(const VertexOrEdge& voe) const { + return ! t_->is_infinite(voe); + } +}; +typedef Is_finite Filter; +typedef boost::filtered_graph Finite_triangulation; +typedef boost::graph_traits::vertex_descriptor vertex_descriptor; +typedef boost::graph_traits::vertex_iterator vertex_iterator; +typedef boost::graph_traits::edge_descriptor edge_descriptor; +// The BGL makes use of indices associated to the vertices +// We use a std::map to store the index +typedef std::map VertexIndexMap; +VertexIndexMap vertex_id_map; +// A std::map is not a property map, because it is not lightweight +typedef boost::associative_property_map VertexIdPropertyMap; +VertexIdPropertyMap vertex_index_pmap(vertex_id_map); + +void mstIpelet::protected_run(int fn) +{ + std::list pt_list; + + read_active_objects( + CGAL::dispatch_or_drop_output( + std::back_inserter(pt_list) + ) + ); + + if (pt_list.empty()) { + print_error_message("No mark selected"); + return; + } + + Triangulation t(pt_list.begin(), pt_list.end()); + Filter is_finite(t); + Finite_triangulation ft(t, is_finite, is_finite); + + vertex_iterator vit, ve; + // Associate indices to the vertices + int index = 0; + // boost::tie assigns the first and second element of the std::pair + // returned by boost::vertices to the variables vit and ve + for(boost::tie(vit,ve)=boost::vertices(ft); vit!=ve; ++vit ){ + vertex_descriptor vd = *vit; + vertex_id_map[vd] = index++; + } + // We use the default edge weight which is the squared length of the edge + // This property map is defined in graph_traits_Triangulation_2.h + // In the function call you can see a named parameter: vertex_index_map + std::list mst; + boost::kruskal_minimum_spanning_tree(ft, + std::back_inserter(mst), + vertex_index_map(vertex_index_pmap)); + std::cout << "The edges of the Euclidean mimimum spanning tree:" << std::endl; + for(std::list::iterator it = mst.begin(); it != mst.end(); ++it){ + edge_descriptor ed = *it; + vertex_descriptor svd = source(ed,t); + vertex_descriptor tvd = target(ed,t); + Triangulation::Vertex_handle sv = svd; + Triangulation::Vertex_handle tv = tvd; + draw_in_ipe(Kernel::Segment_2(sv->point(), tv->point())); + } +} +} + +CGAL_IPELET(CGAL_mst::mstIpelet) diff --git a/CGAL_ipelets/doc/CGAL_ipelets/CGAL_ipelets.txt b/CGAL_ipelets/doc/CGAL_ipelets/CGAL_ipelets.txt index 690e3fb34cc..6d01a2c201e 100644 --- a/CGAL_ipelets/doc/CGAL_ipelets/CGAL_ipelets.txt +++ b/CGAL_ipelets/doc/CGAL_ipelets/CGAL_ipelets.txt @@ -244,6 +244,10 @@ The input selection must be a circle and simple polygons. The Minkowski sum of the polygons with the circle is computed and drawn. +\subsection mst_ipelet Minimum Spanning Tree +The input selection must be a set of points. The Euclidean minimum spanning +tree of the points is computed and drawn. + \subsection multi_delaunay_ipelet k Order Delaunay The input selection must be a set of points. The order k Voronoi diagram or its dual the order k Delaunay is drawn.