mirror of https://github.com/CGAL/cgal
Merge pull request #5173 from afabri/BGL-fix_test-GF
BGL: Fix test of split_graph_into_polylines()
This commit is contained in:
commit
24fc2dde33
|
|
@ -35,6 +35,8 @@ if(OpenMesh_FOUND)
|
|||
target_link_libraries(graph_concept_OpenMesh PRIVATE ${OPENMESH_LIBRARIES})
|
||||
endif()
|
||||
|
||||
create_single_source_cgal_program("test_split.cpp")
|
||||
|
||||
create_single_source_cgal_program("next.cpp")
|
||||
|
||||
create_single_source_cgal_program("test_circulator.cpp")
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
5
|
||||
2 1 1 1
|
||||
1 1 1 0
|
||||
1 0 2 0
|
||||
2 1 2 0
|
||||
3 1 2 1
|
||||
|
|
@ -1,113 +0,0 @@
|
|||
|
||||
#include <CGAL/Simple_cartesian.h>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
#include <CGAL/boost/graph/split_graph_into_polylines.h>
|
||||
|
||||
typedef CGAL::Simple_cartesian<double> K;
|
||||
typedef K::Point_2 Point_2;
|
||||
|
||||
typedef boost::adjacency_list < boost::listS,
|
||||
boost::vecS,
|
||||
boost::undirectedS,
|
||||
Point_2 > G;
|
||||
|
||||
typedef boost::graph_traits<G>::vertex_descriptor vertex_descriptor;
|
||||
|
||||
typedef std::vector<Point_2> Polyline_2;
|
||||
|
||||
struct Is_terminal
|
||||
{
|
||||
template <typename VertexDescriptor, typename Graph>
|
||||
bool operator ()(VertexDescriptor vd , const Graph& g )
|
||||
{
|
||||
return false; // degree(vd,g) != 2; is a bad test in case of parallel edges
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Graph>
|
||||
struct Polyline_visitor
|
||||
{
|
||||
std::list<Polyline_2>& polylines;
|
||||
const Graph& points_pmap;
|
||||
|
||||
Polyline_visitor(std::list<Polyline_2>& lines,
|
||||
const Graph& points_property_map)
|
||||
: polylines(lines),
|
||||
points_pmap(points_property_map)
|
||||
{}
|
||||
|
||||
void start_new_polyline()
|
||||
{
|
||||
Polyline_2 V;
|
||||
polylines.push_back(V);
|
||||
}
|
||||
|
||||
void add_node(typename boost::graph_traits<Graph>::vertex_descriptor vd)
|
||||
{
|
||||
Polyline_2& polyline = polylines.back();
|
||||
polyline.push_back(points_pmap[vd]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
G g;
|
||||
|
||||
std::list<Polyline_2> polylines;
|
||||
Polyline_visitor<G> polyline_visitor(polylines, g);
|
||||
std::map<Point_2, vertex_descriptor> p2vd;
|
||||
|
||||
int n;
|
||||
std::cin >> n; // number of segments
|
||||
|
||||
|
||||
Point_2 p, q;
|
||||
vertex_descriptor vdp, vdq;
|
||||
for(int i=0; i < n; i++){
|
||||
std::cin >> p >> q;
|
||||
|
||||
if(p2vd.find(p) == p2vd.end()){
|
||||
vdp = add_vertex(g);
|
||||
g[vdp] = p;
|
||||
p2vd[p] = vdp;
|
||||
} else {
|
||||
vdp = p2vd[p];
|
||||
}
|
||||
if(p2vd.find(q) == p2vd.end()){
|
||||
vdq = add_vertex(g);
|
||||
g[vdq] = q;
|
||||
p2vd[q] = vdq;
|
||||
} else {
|
||||
vdq = p2vd[q];
|
||||
}
|
||||
boost::add_edge(vdp, vdq, g);
|
||||
}
|
||||
|
||||
CGAL::split_graph_into_polylines( g,
|
||||
polyline_visitor,
|
||||
Is_terminal() );
|
||||
std::cout.precision(17);
|
||||
|
||||
|
||||
for(std::list<Polyline_2>::iterator it = polylines.begin(); it!= polylines.end(); ++it){
|
||||
Polyline_2& poly = *it;
|
||||
std::size_t n;
|
||||
if(poly.front() == poly.back()){
|
||||
std::cout << "POLYGON" << std::endl;
|
||||
n = poly.size() -1;
|
||||
}else{
|
||||
std::cout << "POLYLINE" << std::endl;
|
||||
n = poly.size();
|
||||
}
|
||||
for(std::size_t j=0; j < n; j++){
|
||||
std::cout << poly[j] << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
|
||||
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
||||
#include <CGAL/boost/graph/split_graph_into_polylines.h>
|
||||
|
||||
#include <boost/graph/adjacency_list.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
|
||||
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
||||
typedef K::Point_2 Point_2;
|
||||
|
||||
|
||||
typedef boost::adjacency_list<boost::vecS, boost::setS, boost::undirectedS, Point_2 > Graph;
|
||||
|
||||
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
||||
|
||||
typedef std::map<Point_2, vertex_descriptor> Point_vertex_map;
|
||||
|
||||
typedef std::vector<Point_2> Polyline_2;
|
||||
|
||||
|
||||
// inserts a polyline into a graph
|
||||
void insert(const std::vector<Point_2>& poly, Graph& graph, Point_vertex_map& pvmap)
|
||||
{
|
||||
vertex_descriptor u, v;
|
||||
for (std::size_t i = 0; i < poly.size(); i++) {
|
||||
// check if the point is not yet in the graph
|
||||
if (pvmap.find(poly[i]) == pvmap.end()) {
|
||||
v = add_vertex(graph);
|
||||
pvmap[poly[i]] = v;
|
||||
}
|
||||
else {
|
||||
v = pvmap[poly[i]];
|
||||
}
|
||||
graph[v] = poly[i]; // associate the point to the vertex
|
||||
if (i != 0) {
|
||||
add_edge(u, v, graph);
|
||||
}
|
||||
u = v;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Graph>
|
||||
struct Polyline_visitor
|
||||
{
|
||||
std::list<Polyline_2>& polylines;
|
||||
const Graph& points_pmap;
|
||||
|
||||
Polyline_visitor(std::list<Polyline_2>& lines,
|
||||
const Graph& points_property_map)
|
||||
: polylines(lines),
|
||||
points_pmap(points_property_map)
|
||||
{}
|
||||
|
||||
void start_new_polyline()
|
||||
{
|
||||
Polyline_2 V;
|
||||
polylines.push_back(V);
|
||||
}
|
||||
|
||||
void add_node(typename boost::graph_traits<Graph>::vertex_descriptor vd)
|
||||
{
|
||||
Polyline_2& polyline = polylines.back();
|
||||
polyline.push_back(points_pmap[vd]);
|
||||
}
|
||||
|
||||
void end_polyline()
|
||||
{}
|
||||
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
Polyline_2 polyA = { Point_2(0,0), Point_2(1,0), Point_2(2,0), Point_2(3,0), Point_2(4,0)};
|
||||
Polyline_2 polyB = { Point_2(1,-1), Point_2(1,0), Point_2(2,0), Point_2(2,1), Point_2(2,2) };
|
||||
|
||||
Graph graph;
|
||||
Point_vertex_map pvmap;
|
||||
|
||||
insert(polyA, graph, pvmap);
|
||||
insert(polyB, graph, pvmap);
|
||||
|
||||
std::list<Polyline_2> polylines;
|
||||
Polyline_visitor<Graph> polyline_visitor(polylines, graph);
|
||||
|
||||
CGAL::split_graph_into_polylines( graph,
|
||||
polyline_visitor);
|
||||
|
||||
|
||||
for(std::list<Polyline_2>::iterator it = polylines.begin(); it!= polylines.end(); ++it){
|
||||
Polyline_2& poly = *it;
|
||||
std::size_t n;
|
||||
if(poly.front() == poly.back()){
|
||||
std::cout << "POLYGON" << std::endl;
|
||||
n = poly.size() -1;
|
||||
}else{
|
||||
std::cout << "POLYLINE" << std::endl;
|
||||
n = poly.size();
|
||||
}
|
||||
for(std::size_t j=0; j < n; j++){
|
||||
std::cout << poly[j] << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue