Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate over faces in CGAL

I am trying to use CGAL to do some Delaunay triangulation. I used one of the CGAL samples to compute a triangulation which includes a height field attribute.

The problem I have having is that I have no idea how to get the resulting triangulation. I figured out how to get the face_iterator, but I don't know what to do from there. What I'm hoping to get is an index into the point array for each of the 3 points on each triangle.

I'm having trouble wading through all of the nested templates:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_euclidean_traits_xy_3.h>
#include <CGAL/Delaunay_triangulation_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_euclidean_traits_xy_3<K> Gt;
typedef CGAL::Delaunay_triangulation_2<Gt> Delaunay;
typedef K::Point_3 Point;

int main()
{
    //initialize the points with some trivial data
    std::vector<Point> pts;
    pts.push_back(Point(1., 2., 3.));
    pts.push_back(Point(2., 2., 3.));
    pts.push_back(Point(1., 3., 3.));
    pts.push_back(Point(4., 2., 3.));    

    //create a delaunay triangulation
    Delaunay dt;
    dt.insert(pts.begin(), pts.end());

    //iterate through the faces
    Delaunay::Finite_faces_iterator it;
    for (it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++)
    {
        //What do I do here??
    }

    return 0;
}
like image 483
Adam Tegen Avatar asked Jan 14 '10 21:01

Adam Tegen


2 Answers

You can use Delaunay::triangle to convert from a face (iterator) to the corresponding triangle. This is tested under CGAL 3.8:

// points.cin contains point pairs, e.g.,
// 3 5 
// 0 0
// 1 9
// ...
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <fstream>

typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Delaunay;
typedef K::Point_2   Point;

int main()
{
  std::ifstream in("points.cin");
  std::istream_iterator<Point> begin(in);
  std::istream_iterator<Point> end;

  Delaunay dt;
  dt.insert(begin, end);

  Delaunay::Finite_faces_iterator it;
  for (it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++)
  {
    std::cout << dt.triangle(it) << std::endl;
  }

  return 0;
}
like image 105
Ivan Xiao Avatar answered Sep 21 '22 08:09

Ivan Xiao


A vertex of a triangle can be accessed using dt.triangle(it)[idx], where it is a faces iterator and idx is vertex number (0,1 or 2). In the example below, a vertex is a Point_2 object, its cartesian coordinates can be accessed using x() and y() methods.

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_euclidean_traits_2.h>
#include <CGAL/Delaunay_triangulation_2.h>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Triangulation_euclidean_traits_2<K> Gt;
typedef CGAL::Delaunay_triangulation_2<Gt> Delaunay;

typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Points;

int main()
{
    Points points;
    points.push_back(Point_2(0,0));
    points.push_back(Point_2(0,7));
    points.push_back(Point_2(7,0));
    points.push_back(Point_2(7,7));

    Delaunay dt(points.begin(), points.end());

    // Print Cartesian coordinates of vertices of triangles in 2D Delaunay triangulation
    for (Delaunay::Finite_faces_iterator it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++)
    {
        std::cout << " " << dt.triangle(it)[0].x() << " " << dt.triangle(it)[0].y() << " ";
        std::cout << " " << dt.triangle(it)[1].x() << " " << dt.triangle(it)[1].y() << " ";
        std::cout << " " << dt.triangle(it)[2].x() << " " << dt.triangle(it)[2].y() << " ";
        std::cout << std::endl << "-------------------" << std::endl;
    }
    return 0;
}
like image 37
ciobrelis Avatar answered Sep 19 '22 08:09

ciobrelis