使用 boost::geometry::追加时,自定义点类中的 ID 字段间歇性丢失



我的问题与在自定义点类中间歇性丢失的ID字段中提出的问题非常相似。

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

如果不可能,我当然可以使用 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。输出点可以复制(从输入)或从默认构造函数创建,具体取决于两个输入面的几何配置。

相关内容

最新更新