【问题标题】:How to use vertice's info with basic triangulation using CGAL如何使用 CGAL 将顶点信息与基本三角剖分结合使用
【发布时间】:2020-07-12 15:51:54
【问题描述】:

我正在尝试使用 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 三角剖分而不是基本三角剖分,则代码编译没有问题。但正如我之前所说,我不想使用这种三角测量。

【问题讨论】:

    标签: c++ cgal triangulation


    【解决方案1】:

    基本Triangulation_2 类不存在此重载。它不存在,因为它相当于类连续for (const Point&amp; p : points) t.insert(p.first)-&gt;info()=p.second。对于 Delaunay 来说,情况就不同了,因为您可以通过改组输入点获得更好的时机(insert(begin, end) 正在内部进行)。

    【讨论】:

    • delaunay 的故事进展如何?
    • @lucidbrot 你是什么意思?插入函数确实存在,可见here
    • 我的意思是,如果它是一个 Delaunay 三角剖分,如果我想将一些 ìnfo 结构与每个插入点相关联(一个特定的,并非对所有人都相同),我该怎么办?似乎insert(begin, end) 没有提供选项来指定信息结构以及点。我是否必须将它们插入循环并自己洗牌?
    • 哦等等,我显然无法阅读。我现在第五次阅读该链接,这次我看到了有关信息的部分!谢谢!
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-04
      • 2012-03-29
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      相关资源
      最近更新 更多