Search code examples
c++computational-geometrycgaldelaunayvoronoi

How to print Edges of Delaunay Graph?


Following this: How to print the faces of a Voronoi diagram?, I now have:

#include <iostream>
#include <fstream>
#include <cassert>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Segment_Delaunay_graph_2.h>
#include <CGAL/Segment_Delaunay_graph_adaptation_policies_2.h>
#include <CGAL/Segment_Delaunay_graph_traits_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Segment_Delaunay_graph_traits_2<K> Gt;
typedef CGAL::Segment_Delaunay_graph_2<Gt> DT;

int main() {

    std::ifstream ifs("data.cin");
    assert( ifs );
    DT         vd;
    DT::Site_2 site;
    // read the sites from the stream and insert them in the diagram
    while ( ifs >> site ) { vd.insert( site ); }
    ifs.close();
    // validate the diagram
    assert( vd.is_valid(true, 1) );
    std::cout << std::endl << std::endl;
    // Iterate over edges
    DT::Finite_edges_iterator eit = vd.finite_edges_begin();
    for (int k = 1; eit != vd.finite_edges_end(); ++eit, ++k) {
        DT::Edge e = *eit;
        //std::cout << e << std::endl;
    }
}

However, an edge cannot be printed that simply (with cout). How to do it?


Here is the error from another attempt:

/home/gsamaras/CGAL-4.7/code/DelaunayTOvoronoi/delTovor.cpp:30:27: error: ‘CGAL::Triangulation_ds_edge_iterator_2<CGAL::Triangulation_data_structure_2<CGAL::Segment_Delaunay_graph_vertex_base_2<CGAL::Segment_Delaunay_graph_storage_traits_2<CGAL::Segment_Delaunay_graph_traits_2<CGAL::Epick> >, CGAL::Triangulation_ds_vertex_base_2<void> >, CGAL::Segment_Delaunay_graph_face_base_2<CGAL::Segment_Delaunay_graph_traits_2<CGAL::Epick>, CGAL::Triangulation_ds_face_base_2<void> > >, true>::Edge’ has no member named ‘site’
         std::cout << eit->site() << std::endl;
                           ^

Solution

  • You can print the sites forming the edge like this:

    for (int k = 1; eit != vd.finite_edges_end(); ++eit, ++k) {
        DT::Edge e = *eit;
        std::cout << "k = " << k << std::endl;
        if ( vd.is_infinite(e.first->vertex( vd.ccw(e.second) )) )
            std::cout << "infinite\n";
        else
            std::cout << e.first->vertex( vd.ccw(e.second) )->site() << std::endl;
        if ( vd.is_infinite(e.first->vertex( vd.cw(e.second) )) )
            std::cout << "infinite\n";
        else
            std::cout << e.first->vertex( vd.cw(e.second) )->site() << std::endl;
        if ( vd.is_infinite(e.first->vertex( e.second )) )
            std::cout << "infinite\n";
        else
            std::cout << e.first->vertex( e.second )->site() << std::endl;
        if ( vd.is_infinite(vd.tds().mirror_vertex(e.first, e.second) ) )
            std::cout << "infinite\n";
        else
            std::cout << vd.tds().mirror_vertex(e.first, e.second)->site() << std::endl;
    }