c++ - 使用 boost::geometry::append 时,自定义点类中的 ID 字段间歇性丢失

标签 c++ boost-geometry

我的问题与 ID field intermittently lost in custom point class 中的问题非常相似.

在我的例子中,我有两个多边形(每个点都有一个由 int 属性表示的类型),我想在这两个多边形上执行 union 操作,从而生成一个新的多边形。我希望有一种方法可以使来自现有多边形的 union 多边形中的点保持其类型。有什么办法可以实现吗?

如果不可能,我当然可以通过使用 id 来解决这个问题。

我已经尝试使用带有自定义点的多边形,但似乎连追加操作都不起作用。

这是我的代码:

#include <fstream>
#include <iostream>
#include <vector>

//#define BOOST_GEOMETRY_DEBUG_HAS_SELF_INTERSECTIONS
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

#include <boost/foreach.hpp>

namespace bg = boost::geometry;

class QPoint
{
public:
  double x;
  double y;

  int id;
  QPoint() { }
  QPoint(double x, double y) : x(x), y(y), id(0) { }
  QPoint(double x, double y, int id) : x(x), y(y), id(id) { }
  QPoint(const QPoint& p) : x(p.x), y(p.y), id(p.id) { }
};

namespace boost {
namespace geometry {
namespace traits {
// Adapt QPoint to Boost.Geometry

template <>
struct tag<QPoint>
{
  typedef point_tag type;
};

template <>
struct coordinate_type<QPoint>
{
  typedef double type;
};

template <>
struct coordinate_system<QPoint>
{
  typedef cs::cartesian type;
};

template <>
struct dimension<QPoint> : boost::mpl::int_<2>
{
};

template <>
struct access<QPoint, 0>
{
  static double get(QPoint const& p) { return p.x; }

  static void set(QPoint& p, double const& value) { p.x = value; }
};

template <>
struct access<QPoint, 1>
{
  static double get(QPoint const& p) { return p.y; }

  static void set(QPoint& p, double const& value) { p.y = value; }
};

template <>
struct access<QPoint, 2>
{
  static int get(QPoint const& p) { return p.id; }

  static void set(QPoint& p, int const& value) { p.id = value; }
};
} // namespace traits
} // namespace geometry
} // namespace boost

int main()
{
  //using point = bg::model::point<float, 2, bg::cs::cartesian>;
  using polygon = bg::model::polygon<QPoint, true, false>; // cw, open polygon

  polygon green;

  bg::append(green.outer(), QPoint(0.0, 0.0));
  bg::append(green.outer(), QPoint(10.0, 0.0));
  bg::append(green.outer(), QPoint(10.0, 10.0));
  bg::append(green.outer(), QPoint(0.0, 10.0));

  std::cout << "Points polygon green:" << std::endl;
  for (auto& p : green.outer())
  {
    std::cout << "x: " << p.x << ", y: " << p.y << ", id: " << p.id << 
  std::endl;
  }

  return 0;
}

我得到的输出是:

Points polygon green:
x: 0, y: 10, id: 4
x: 10, y: 10, id: 4
x: 10, y: 0, id: 4
x: 0, y: 0, id: 4

我想知道为什么我的 ID 突然变成了 4?

最佳答案

事实上,Boost.Geometry 目前不支持将自定义属性传输到输出几何体。

根据您提供的代码片段,我无法回答为什么 ID 为 4。根据两个输入多边形的几何配置,可以复制(从输入)或从默认构造函数创建输出点。

关于c++ - 使用 boost::geometry::append 时,自定义点类中的 ID 字段间歇性丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929528/

相关文章:

c++11 - boost 多边形上的几何矩阵变换

c++ - 编译时增加几何距离()错误

c++ - 调试时 has_new_operator.hpp 中的 Boost 错误

c++ - 错误输入 C vs C++ cin vs scanf

c++ - 通过开放空间检查两个物体的连通性

c++ - 内存博士 : are these lines really causing of memory leaks?

c++ - boost::geometry 中无效几何的数据集

c++ - 尝试在 MFC 应用程序中使用 QSettings 时出错

c++ - qtCreator 和 docker 最佳实践

c++ - 查找图中某点一定距离内的所有线段=边,如何结合boost-graph和boost-geometry?