如何使用 CGAL 将顶点信息与基本三角剖分结合使用

How to use vertice's info with basic triangulation using CGAL

我正在尝试使用 CGAL 将索引信息添加到基本 2D 三角剖分中的顶点以制作 .obj 文件,我一直在搜索许多这样的示例,但大多数情况都是针对 Delaunay_triagnulation但我不想这样,我想举一个基本二维三角测量的例子 (Triangulation_2.h)。 我的代码是这样的:

#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;
}

input.txt文件只有这个:

4
0 0
0 1
1 1
2 4

此代码在这一行中抛出一个错误:

T.insert(points.begin(), points.end());

编译错误是这样的:

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,

我在这个基本三角剖分上做错了,因为如果我使用 Delaunay 三角剖分而不是基本三角剖分,代码编译没有问题。但正如我之前所说,我不想使用这种三角测量。

基本 Triangulation_2 class 不存在此重载。它不存在,因为它相当于 class 连续 for (const Point& p : points) t.insert(p.first)->info()=p.second。对于 Delaunay,这是一个不同的故事,因为您可以通过改组输入点来获得更好的时机(insert(begin, end) 在内部进行)。

如果您使用 CGAL 5.0,您的代码就可以工作。之前,基本的 Triangulation_2 没有带一对的 insert 的重载。 然后您必须手动将索引添加到顶点。 你应该可以做类似

for( TIndex i = 0; i < n; ++i )
{
  Vb vert = T.insert(p);
  vert->info() = i;
}