0

I would like to add N random vertex to a 2D Delaunay triangulation. My code is:

template <class Kernel, class TDS>
class DT : public CGAL::Delaunay_triangulation_2<Kernel,TDS>
{
public:
typedef typename Kernel::FT FT;
typedef typename Kernel::Point_2    Point;
typedef typename CGAL::Delaunay_triangulation_2<Kernel,TDS> Dt2;

// random number between zero and max
FT r(FT max = 1.0) { return max * (FT)rand() / (FT)RAND_MAX; }

void generators_random(unsigned int nb_generators)
{
   Dt2::clear();
   Point p(r(), r());
   for(unsigned int i = 0; i < nb_generators; i++)
        insert(p);
}

But when I call the generators_random method, the compiler gives this error:

/home/lucas/Algorithmes géometriques/TP2/src/dt.h:83:12: note:
cannot convert ‘p’ (type ‘DT<CGAL::Epick,CGAL::Triangulation_data_structure_2
<My_vertex_base<CGAL::Triangulation_vertex_base_2<CGAL::Epick> >, 
CGAL::Triangulation_face_base_2<CGAL::Epick> > >::Point {aka
CGAL::Point_2<CGAL::Epick>}’) to type ‘std::ostream& {aka 
std::basic_ostream<char>&}’ insert(p);

Instead of casting FT, I tried also double, float, but nothing works. What's wrong with it?

Thanks.

lucasn
  • 137
  • 2
  • 7
  • Not familiar with CGAL in any way, but is `insert(p)` the correct way to insert verts, or should they be streamed into it? Something like `insert() << p`? Just a shot from the hip. – pauluss86 Feb 09 '14 at 14:51
  • I guess it can't work as a stream. I tried to follow the documentation: http://doc.cgal.org/latest/Triangulation_2/classCGAL_1_1Delaunay__triangulation__2.html – lucasn Feb 09 '14 at 15:11

1 Answers1

0

I've solved the problem. Instead of using insert(), I used push_back() For the record, this is the working version:

// random number between zero and max
FT r(FT max = 1.0) { return max * (FT)rand() / (FT)RAND_MAX; }

// random (uniform)
void generators_random(unsigned int nb_generators)
{
    Dt2::clear();
    for(unsigned int i = 0; i < nb_generators; i++)
        this->push_back(Point(r(), r()));
}
lucasn
  • 137
  • 2
  • 7
  • 1
    Nice! It's implemented as a stream; much like trying to `insert()` a char with a `std::string` won't work; but `push_back()` will. I suspect you could also create a stream containing all your Points and `insert()` that instead. – pauluss86 Feb 09 '14 at 15:15
  • Note that it is more efficient to generate all the points first and then insert them all at once (there is a method taking iterators). – Marc Glisse Feb 09 '14 at 15:49