tags:

views:

119

answers:

3
+1  Q: 

CGAL inheritance

How can I use an inherited class of a triangulation in the context of a triangulation in CGAL?

Basically I have the following code:

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;

typedef CGAL::Triangulation_vertex_base_with_info_2<int,K> Vb;
typedef CGAL::Triangulation_face_base_with_info_2<int,K>   Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb>        Tds;
typedef CGAL::Delaunay_triangulation_2<K,Tds>              Delaunay;
typedef CGAL::Triangulation_2<K,Tds>                       Triangulation;

typedef Triangulation::Point Point;

...

Triangulation *t = new Delaunay;

...

// x and y are properly defined and instantiated
t->insert(Point(x,y));

Well, of course, Delaunay_triangulation_2 inherits from Triangulation_2

So, when I execute this code, the linking is done against the Triangulation_2 class, in other words, it does not executes a delaunay triangulation, instead it executes a normal triangulation (executing the parent class methods instead of the child methods).

I think this is because the insert method of Triangulation_2 is not declared as virtual so redefinitions won't work.

Do you know a way around this? Maybe using Constrained_triangulation_2 and Constrained_delaunay_triangulation_2? (those classes define some virtual methods, but i've read the source code and I don't think they can be used without adding the explicit constraints)

Any ideas?

A: 

Are you sure these functions are virtual? Without them being defined virtual the compiler will not call the derived class functions.

From a cursory look at the CGAL headers it does not seem that these classes have any virtual functions at all.

That's kinda what I'm saying, the Constrained_* classes has some protected virtual methods (like the virtual_insert method) but, as they are protected, I can't use them.
Gastón
A: 

CGAL uses generic programming, not virtual functions. It resembles STL, just the domain is a little bit more difficult, and you need to rely more on algorithms than one usually does with STL.

I can't really tell, what is the answer to your problem, because you provided just a small snippet of the code, but try replacing

Triangle *t = new Delaunay;

with

Triangulation *t = new Delaunay;

first. If it does not help, please add more details from your type definitions.

wigy
Hi, I fixed the variable of t, that's what I actually had in my code and it doesn't work.You can see the complete code at http://github.com/gjorquera/triangle-mesh/blob/6eb7e304a486a1a3de47ac9264a322b5cf7bc4c1/main.cpp it's pretty simple.
Gastón
+3  A: 

I checked your program, you would need to reformat it a bit, so that it fits the generic programming model. Let me recall what your code does (the one available at github):

  1. reads the command line
  2. depending on the options, instantiates either a Triangulation or a Delaunay_triangulation on the heap
  3. uses this object for some processing, assuming that the methods are virtual (but they are not)

A solution for your problem would be to put step 3 in a separate method, with the triangulation type as template parameter. Something like (I use your types and names):

template < class Triangulation >
void compute_mesh(int n_vertices, int max_x, int max_y)
{
    Triangulation t;
    // DO WHATEVER YOU WANT WITH t
}

Then, in your main function, you would trigger the use of Delaunay or non-Delaunay triangulations in the following way:

if (triang_type == 'D') 
    compute_mesh<Delaunay>(n_vertices, max_x, max_y);
else 
    compute_mesh<Triangulation>(n_vertices, max_x, max_y);
Camille
I haven't connected to StackOverflow in a while. Thanks, that did the trick.
Gastón