Skip to content

Instantly share code, notes, and snippets.

@afabri
Created October 13, 2015 13:51
Show Gist options
  • Save afabri/b50fa1740f3341bafa7d to your computer and use it in GitHub Desktop.
Save afabri/b50fa1740f3341bafa7d to your computer and use it in GitHub Desktop.
Segment Delaunay Graph
#include <iostream>
#include <fstream>
#include <cassert>
// example that uses the filtered traits,
// the segment Delaunay graph and the spatial sorting
// choose the kernel
#include <CGAL/Simple_cartesian.h>
typedef CGAL::Simple_cartesian<double> K;
// typedefs for the traits and the algorithm
#include <CGAL/Segment_Delaunay_graph_2.h>
#include <CGAL/Segment_Delaunay_graph_filtered_traits_2.h>
typedef CGAL::Segment_Delaunay_graph_filtered_traits_2<K> Gt;
typedef CGAL::Segment_Delaunay_graph_2<Gt> SDG2;
int main(int argc, char* argv[])
{
std::ifstream ifs(argv[1]);
assert( ifs );
//polygon points
std::vector<Gt::Point_2> points;
//segments of the polygon as a pair of point indices
std::vector<std::pair<std::size_t,std::size_t> > indices;
SDG2::Site_2 site;
//read a close polygon given by its segments
// s x0 y0 x1 y1
// s x1 y1 x2 y2
// ...
// s xn yn x0 y0
Gt::Point_2 p,q;
ifs >> p;
points.push_back(p);
while(ifs >> q){
if(p == q){
std::cerr << "duplicate point: " << p << std::endl;
}else{
points.push_back(q);
p = q;
}
}
if(points.front() == points.back()){
points.pop_back();
}
for(int i=0; i < points.size()-1; i++){
indices.push_back( std::make_pair(i, i+1) );
}
ifs.close();
SDG2 sdg;
//insert the polygon segments all at once using spatial sorting to speed the insertion
sdg.insert_segments( points.begin(), points.end(), indices.begin(), indices.end() );
// validate the segment Delaunay graph
if(sdg.is_valid(true, 1) ){
std::cerr << "SDG is valid" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment