Reputation: 181
I am triying to add an index info to vertices in a basic 2D triangulation using CGAL to make a .obj file, i have been searching many examples of this, but most of the cases are for Delaunay_triagnulation but i don't want that, i would like to have an example to basic 2D triangulations (Triangulation_2.h). My code is this:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Triangulation_vertex_base_with_info_2.h>
#include <CGAL/Triangulation_2.h>
#include <iostream>
#include <fstream>
#include <vector>
typedef unsigned int TIndex;
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef CGAL::Triangulation_vertex_base_with_info_2<TIndex,Kernel> Vb;
typedef CGAL::Triangulation_data_structure_2<Vb> Tds;
typedef CGAL::Triangulation_2<Kernel,Tds> Triangulation;
typedef Triangulation::Point Point;
typedef Triangulation::Finite_vertices_iterator Finite_vertices_iterator;
typedef Triangulation::Finite_faces_iterator Finite_faces_iterator;
int main()
{
std::string input="../input.txt";
std::string output="../triangulation.obj";
std::vector<std::string> colors;
colors.push_back("red");
colors.push_back("green");
colors.push_back("blue");
colors.push_back("black");
colors.push_back("yellow");
colors.push_back("purple");
std::ifstream input_file;
input_file.open(input);
if(!input_file){
std::cout << "Cannot open file: " << input << std::endl;
return 0;
}
std::vector< std::pair<Point,TIndex>> points;
TIndex n;
input_file >> n;
//READING INPUT FILE
for( TIndex i = 0; i < n; ++i )
{
Point p;
input_file >> p;
points.push_back(std::make_pair(p,i));
}
input_file.close();
Triangulation T;
T.insert(points.begin(), points.end());
//OUTPUT FILE
std::ofstream os(output);
os << "mtllib material.mtl" << std::endl << std::endl;
for(Finite_vertices_iterator it = T.finite_vertices_begin();
it != T.finite_vertices_end();
++it){
os << "v " << it->point() << " 0.0" << std::endl;
}
os << std::endl;
int i=0;
for(Finite_faces_iterator it = T.finite_faces_begin();
it != T.finite_faces_end();
++it){
os << "usemtl " << colors[i%6] << std::endl;
os << "f " << it->vertex(0)->info() << " " << it->vertex(1)->info() << " " << it->vertex(2)->info() << std::endl;
os << std::endl;
i++;
}
std::cout << "File " << output << " generated" << std::endl;
return 0;
}
The input.txt file is only this:
4
0 0
0 1
1 1
2 4
This code throw me an error in this line:
T.insert(points.begin(), points.end());
the compilation error is something like this:
error: no matching function for call to ‘CGAL::Point_2<CGAL::Epick>::Point_2(std::pair<CGAL::Point_2<CGAL::Epick>, unsigned int>&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/local/include/CGAL/user_classes.h:30:0,
from /usr/local/include/CGAL/Kernel/global_functions_2.h:34,
from /usr/local/include/CGAL/Kernel/global_functions.h:32,
from /usr/local/include/CGAL/Cartesian/Cartesian_base.h:31,
from /usr/local/include/CGAL/Simple_cartesian.h:29,
from /usr/local/include/CGAL/Exact_predicates_inexact_constructions_kernel.h:29,
There is something that i am doing wrong with this basic triangulations, because if i use Delaunay triangulations instead of basic triangulations the code compiles with no problem. But as i said before i don't want to use this kind triangulation.
Upvotes: 0
Views: 355
Reputation: 726
If you use CGAL 5.0, your code works. Before, the basic Triangulation_2 does not have the overload of insert
that takes a pair.
Then you have to manually add the indices to the vertices.
You should be able to do something like
for( TIndex i = 0; i < n; ++i )
{
Vb vert = T.insert(p);
vert->info() = i;
}
Upvotes: 1
Reputation: 6263
This overload does not exist for the basic Triangulation_2
class. It is not there because it is equivalent to class consecutive for (const Point& p : points) t.insert(p.first)->info()=p.second
. For Delaunay that is a different story because you get better timing by shuffling your input points (which insert(begin, end)
is doing internally).
Upvotes: 2