其他分享
首页 > 其他分享> > c – 来自Voronoi的Delaunay with boost:缺少具有非整数点坐标的三角形

c – 来自Voronoi的Delaunay with boost:缺少具有非整数点坐标的三角形

作者:互联网

遵循这两个资源:

> Boost basic tutorial
> SO Question

我用提升写了一个Delaunay三角剖分.如果点坐标是积分的,它可以正常工作(我生成了几个随机测试,但没有观察到错误).但是,如果这些点是非整数的,我会发现许多不正确的三角剖分,边缘缺失或边缘错误.

例如,此图像已使用舍入值构建并且是正确的(请参阅下面的代码)

enter image description here

但是这个图像是用原始值构建的并且不正确(参见下面的代码)

enter image description here

此代码重现了这两个示例(没有显示).

#include <boost/polygon/voronoi.hpp>
using boost::polygon::voronoi_builder;
using boost::polygon::voronoi_diagram;

struct Point
{
  double a;
  double b;
  Point(double x, double y) : a(x), b(y) {}
};

namespace boost
{
  namespace polygon
  {
    template <>
    struct geometry_concept<Point>
    {
      typedef point_concept type;
    };

    template <>
    struct point_traits<Point>
    {
      typedef double coordinate_type;

      static inline coordinate_type get(const Point& point, orientation_2d orient)
      {
        return (orient == HORIZONTAL) ? point.a : point.b;
      }
    };
  }  // polygon
}  // boost

int main()
{ 

 std::vector<double> xx = {8.45619987, 9.96573889, 0.26574428, 7.63918524, 8.15187618, 0.09100718};
 std::vector<double> yy = {9.2452883, 7.0843455, 0.4811701, 3.1193826, 5.1336435, 5.5368374};

 // std::vector<double> xx = {8, 10, 0, 8, 8, 0};
 // std::vector<double> yy = {9, 7, 0, 3, 5, 6};

  std::vector<Point> points;

  for (int i = 0 ; i < xx.size() ; i++)
  {
    points.push_back(Point(xx[i], yy[i]));
  }

  // Construction of the Voronoi Diagram.
  voronoi_diagram<double> vd;
  construct_voronoi(points.begin(), points.end(), &vd);

  for (const auto& vertex: vd.vertices())
  {
    std::vector<Point> triangle;
    auto edge = vertex.incident_edge();
    do
    {
      auto cell = edge->cell();
      assert(cell->contains_point());

      triangle.push_back(points[cell->source_index()]);
      if (triangle.size() == 3)
      {   
        // process output triangles
        std::cout << "Got triangle: (" << triangle[0].a << " " << triangle[0].b << ") (" << triangle[1].a << " " << triangle[1].b << ") (" << triangle[2].a << " " << triangle[2].b << ")" << std::endl;
        triangle.erase(triangle.begin() + 1);
      }

      edge = edge->rot_next();
    } while (edge != vertex.incident_edge());
  }

  return 0;
}

解决方法:

It works fine if the points coordinates are not decimal

在玩了你的样本后,我突然意识到你并不是说“当坐标不是十进制”时.你的意思是“积分”.很大的区别.

Documentation: Point Concept

The coordinate type is expected to be integral

Floating point coordinate types are not supported by all the algorithms and generally not suitable for use with the library at present.

标签:c,boost,delaunay,voronoi
来源: https://codeday.me/bug/20190910/1798499.html