首页 > 解决方案 > 使用 boost::geometry::append 时,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?

标签: c++boost-geometry

解决方案


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

根据您提供的片段,我无法回答为什么 id 为 4。输出点要么复制(从输入),要么从默认构造函数创建,具体取决于两个输入多边形的几何配置。


推荐阅读