C++ CGAL多段线简化会导致自相交

C++ CGAL多段线简化会导致自相交,c++,cgal,C++,Cgal,我现在和CGAL有点麻烦 更具体地说,对于下面的示例,PS::simplify(ct,Cost(),Stop(0.2))生成自相交多段线。在下图中,蓝色多段线是输入到PS::simplify()的多段线,而绿色多段线是结果(输出)多段线。红色箭头指向生成的多段线中的自交点 下面,我从两个文件中复制并粘贴了我的代码simplify_test.cpp和CMakeLists.txt。安装了所需的库后,要运行此示例,您可以将它们放在同一目录中,cd到该目录,然后在终端中运行以下操作: $ cmake .

我现在和CGAL有点麻烦

更具体地说,对于下面的示例,
PS::simplify(ct,Cost(),Stop(0.2))
生成自相交多段线。在下图中,蓝色多段线是输入到
PS::simplify()
多段线,而绿色多段线是结果(输出)多段线。红色箭头指向生成的多段线中的自交点

下面,我从两个文件中复制并粘贴了我的代码
simplify_test.cpp
CMakeLists.txt
。安装了所需的库后,要运行此示例,您可以将它们放在同一目录中,
cd
到该目录,然后在终端中运行以下操作:

$ cmake .
$ make
$ ./simplify_test
这将输出结果坐标(下图中的绿色多段线),其中包含自相交

我想知道:
(1)为什么会导致自相交
(2)可以做些什么,使其不会导致自相交

谢谢你的帮助

下面是名为
simplify\u test.cpp
的文件中的简化代码:

#包括
#包括
#包括
#包括
#包括
typedef CGAL::精确谓词不精确结构内核K;
typedef CGAL::Point_2 Point;
typedef std::向量多段线;
名称空间PS=CGAL::Polyline\u简化\u 2;
typedef PS::顶点_基_2vb;
typedef CGAL::约束的_三角剖分_面_底_2 Fb;
typedef CGAL::三角测量数据结构TDS;
typedef CGAL::约束的Delaunay三角剖分CDT;
typedef CGAL::约束三角测量加上2 CT;
typedef PS::停止\低于\计数\比率\阈值停止;
typedef PS::平方距离成本;
无效打印坐标(多段线多段线){

STD::C++代码中的CUT< P>,我基本上用<代码> PulnGyth.OthelsEX2取代<代码> PultLo> <代码>。对于所有的值<代码>停止< /代码>,我现在得到了拓扑有效的简化多边形。我编辑了你的输出函数。你可以复制和粘贴新函数<代码> PrrtSyCordsSufGeGeBuffa()。
直接输入

这是
simplify\u test.cpp
的编辑版本。您可以使用原始帖子中包含的
CMakeLists.txt
编译它

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyline_simplification_2/simplify.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <iostream>
#include <vector>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_2<K> Point;
namespace PS = CGAL::Polyline_simplification_2;
typedef PS::Stop_below_count_ratio_threshold Stop;
typedef PS::Squared_distance_cost Cost;
typedef CGAL::Polygon_2<K> Polygon;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes;

void print_coords_for_geogebra(Polygon ext_ring)
{
  unsigned int i = 0;
  std::cout << "Polygon(";
  for (Point coord : ext_ring) {
    std::cout << "("
              << coord.x()
              << ", "
              << coord.y()
              << ")";
    if (i != ext_ring.size() - 1) std::cout << ", ";
    i++;
  }
  std::cout << ")" << std::endl << std::endl;
}

void simplify_test2(Polygon_with_holes polygon, double stop)
{
  Cost cost;
  polygon = PS::simplify(polygon, cost, Stop(stop));
  print_coords_for_geogebra(polygon.outer_boundary());
}

int main(int argc, char* argv[])
{
  std::vector<std::vector<int> > coords = {
    {64, 20}, {33, 27}, {27, 9}, {33, 18}, {44, 18}, {44, 8},
    {24, 0},  {0, 13},  {9, 49}, {84, 41}, {83, 29}, {64, 20},
  };

  // Insert coordinates into the external ring of a polygon with holes
  Polygon ext_ring;
  for (std::vector<int> coord : coords) {
    Point pt(coord[0], coord[1]);
    ext_ring.push_back(pt);
  }
  Polygon_with_holes polygon(ext_ring);
  print_coords_for_geogebra(polygon.outer_boundary());
  for (double stop = 1.0; stop > 0.0; stop -= 0.1) {
    simplify_test2(polygon, stop);
  }
  return EXIT_SUCCESS;
}

#包括
#包括
#包括
#包括
#包括
typedef CGAL::精确谓词不精确结构内核K;
typedef CGAL::Point_2 Point;
名称空间PS=CGAL::Polyline\u简化\u 2;
typedef PS::停止\低于\计数\比率\阈值停止;
typedef PS::平方距离成本;
typedef CGAL::Polygon_2 Polygon;
typedef CGAL::带孔的多边形\u 2带孔的多边形\u;
geogebra(多边形外环)的无效打印坐标
{
无符号整数i=0;

std::coutI进一步将其简化为多段线
std::vector coords={{64,20},{33,27},{27,9},{33,18},{44,18},{24,0};


您在约束迭代器中的点中发现了一个错误。作为一种解决方法,您应该使用,然后调用方法
point()
。我将在github上提交一个问题和一个修复程序。

它已在上的拉取请求中修复。希望这被接受为一个答案,即使对我来说,建议的解决方案是一个。嗨,Andreas,感谢您的回复。经过试验后,建议的解决方案确实适合我的目的。我想github上的这些修复程序将不可用e是否会很快出现,但只会包含在CGAL的下一版本中?
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyline_simplification_2/simplify.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <iostream>
#include <vector>

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_2<K> Point;
namespace PS = CGAL::Polyline_simplification_2;
typedef PS::Stop_below_count_ratio_threshold Stop;
typedef PS::Squared_distance_cost Cost;
typedef CGAL::Polygon_2<K> Polygon;
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes;

void print_coords_for_geogebra(Polygon ext_ring)
{
  unsigned int i = 0;
  std::cout << "Polygon(";
  for (Point coord : ext_ring) {
    std::cout << "("
              << coord.x()
              << ", "
              << coord.y()
              << ")";
    if (i != ext_ring.size() - 1) std::cout << ", ";
    i++;
  }
  std::cout << ")" << std::endl << std::endl;
}

void simplify_test2(Polygon_with_holes polygon, double stop)
{
  Cost cost;
  polygon = PS::simplify(polygon, cost, Stop(stop));
  print_coords_for_geogebra(polygon.outer_boundary());
}

int main(int argc, char* argv[])
{
  std::vector<std::vector<int> > coords = {
    {64, 20}, {33, 27}, {27, 9}, {33, 18}, {44, 18}, {44, 8},
    {24, 0},  {0, 13},  {9, 49}, {84, 41}, {83, 29}, {64, 20},
  };

  // Insert coordinates into the external ring of a polygon with holes
  Polygon ext_ring;
  for (std::vector<int> coord : coords) {
    Point pt(coord[0], coord[1]);
    ext_ring.push_back(pt);
  }
  Polygon_with_holes polygon(ext_ring);
  print_coords_for_geogebra(polygon.outer_boundary());
  for (double stop = 1.0; stop > 0.0; stop -= 0.1) {
    simplify_test2(polygon, stop);
  }
  return EXIT_SUCCESS;
}