Example #1
0
void triangulate( Rand &prng, Graph &graph )
{
	typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
	typedef CGAL::Delaunay_triangulation_2<K>        Triangulation;
	typedef Triangulation::Edge_iterator             Edge_iterator;
	typedef Triangulation::Vertex_handle             Vertex_handle;
	typedef Triangulation::Point                     Point;
	typedef std::map<Vertex_handle, int>             VertexIndexMap;

	const static int minEdgeWeight = 1;
	const static int maxEdgeWeight = 20;

	// create the delaunay triangulation
	Triangulation triangulation;
	VertexIndexMap vertexIndexMap;

	// dump all of the BGL vertices into CGAL
	Graph::vertices_size_type numVertices = boost::num_vertices( graph );
	for ( int idx = 0; idx < numVertices; ++idx ) 
	{
		const VertexInfo &vertexInfo = boost::get( VertexInfoTag(), graph, idx );
		Point position( vertexInfo.position.x, vertexInfo.position.y );
		Vertex_handle handle = triangulation.insert( position );
		vertexIndexMap[ handle ] = idx;
	}

	// edge weight property map
	auto edgeWeightMap = boost::get( boost::edge_capacity, graph );

	// read out the edges and add them to BGL
	EdgeReverseMap edgeReverseMap = boost::get( boost::edge_reverse, graph );
	Edge_iterator ei_end = triangulation.edges_end();
	for (Edge_iterator ei = triangulation.edges_begin(); 
		ei != ei_end; ++ei)
	{
		int idxSourceInFace = ( ei->second + 2 ) % 3;
		int idxTargetInFace = ( ei->second + 1 ) % 3;
		Vertex_handle sourceVertex = ei->first->vertex( idxSourceInFace );
		Vertex_handle targetVertex = ei->first->vertex( idxTargetInFace );
		int idxSource = vertexIndexMap[ sourceVertex ];
		int idxTarget = vertexIndexMap[ targetVertex ];
		EdgeHandle forwardEdge = boost::add_edge( idxSource, idxTarget, graph );
		EdgeHandle backwardEdge = boost::add_edge( idxTarget, idxSource, graph );
		edgeReverseMap[ forwardEdge.first ] = backwardEdge.first;
		edgeReverseMap[ backwardEdge.first ] = forwardEdge.first;
		int edgeWeight = prng.nextInt( 1, 20 );
		edgeWeightMap[ forwardEdge.first ] = edgeWeight;
		edgeWeightMap[ backwardEdge.first ] = edgeWeight;
	}
}
Example #2
0
int main( )
{
  std::ifstream in("data/voronoi.cin");
  std::istream_iterator<Point> begin(in);
  std::istream_iterator<Point> end;
  Triangulation T;
  T.insert(begin, end);

  int ns = 0;
  int nr = 0;
  Edge_iterator eit =T.edges_begin();
  for ( ; eit !=T.edges_end(); ++eit) {
    CGAL::Object o = T.dual(eit);
    if (CGAL::object_cast<K::Segment_2>(&o)) {++ns;}
    else if (CGAL::object_cast<K::Ray_2>(&o)) {++nr;}
  }
  std::cout << "The Voronoi diagram has " << ns << " finite edges "
	    << " and " << nr << " rays" << std::endl;
  return 0;
}