【问题标题】:Boost::geometry::intersection gives wrong output. version BOOST_1_57_0Boost::geometry::intersection 给出错误的输出。版本 BOOST_1_57_0
【发布时间】:2015-10-26 06:02:37
【问题描述】:

这可能是一个被多次询问的问题。但我交叉检查了类似的问题,以确保我的问题不会重复。

我有一个在代码中使用boost::geometry::intersection 的源代码,以获取两个多边形之间相交的结果。我对使用的多边形进行了boost::geometry::correct 测试。多边形中点的顺序是顺时针的。一切似乎都正确,但我从boost::geometry::intersection 调用中得到了不正确的输出。

请帮我找出问题所在。

这是类点定义:

class Point {
public:
    double _x, _y;
    Point();
    Point(double, double);
    Point(const Point& orig);
    void SetX(double x);
    void SetY(double y);
    double GetX() const;
    double GetY() const;
};

使用 boost 库的代码

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/geometry/geometries/register/point.hpp>

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, _x, _y)
BOOST_GEOMETRY_REGISTER_RING(std::vector<Point>)

namespace bg = boost::geometry;
namespace bd2 = boost::geometry::model::d2;

std::vector<Point> ORCAModel :: BoostPolyIntersect( std::vector<Point>& poly_1,
                                                    std::vector<Point>& poly_2,
                                                    bool &intersect_FLAG) const{

    std::vector<Point> poly_result;

    bg::correct(poly_1);
    bg::correct(poly_2);

    bg::intersection(poly_1, poly_2, poly_result);
    intersect_FLAG = (int(poly_result.size()) > 2)
    return poly_result;

    }

我使用基本 iostream 来检查输出。 (注意:输入值取自程序的运行序列之一,但不作为用户输入输入)

--------------poly_1------------
( 0.075 : 27.2 )  ----  ( 27 : 27.2 )  ----  ( 27 : -22.8 )  ----  ( 0.075 : -22.8 )  ----  ( 0.075 : 27.2 )  ----  
--------------poly_2------------
( -23 : -22.8 )  ----  ( -23 : 3.925 )  ----  ( 27 : 3.925 )  ----  ( 27 : -22.8 )  ----  ( -23 : -22.8 )  ----  
result in bstpolyint size : 3
-----------------RESULT  POLY ------------------------
( 27 : 3.925 )  ----  ( 27 : -22.8 )  ----  ( 0.0750023 : 3.925 )  ----  

输出应该有 4 个点并且显然缺少一个点 (0.07500 : - 22.8)

(注意:输入点在显示检查期间被四舍五入。但不是手动的。 当在测试用例中使用显示的点时,结果是 正确的。但显然是因为四舍五入,使用的点 在计算的原始程序和测试用例中是 不安。)

请帮助确定问题。提前致谢。

编辑:这是我的测试用例。注释线 polygon_1 和 polygon_2 是四舍五入的值。使用这些值会给出新多边形的 4 个角。使用中的放大值导致释放模式下只有3个角。

#define BOOST_TEST_MODULE convexHull
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "../../geometry/Point.h"

#include <vector> 
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <string>

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, _x, _y)
BOOST_GEOMETRY_REGISTER_RING(std::vector<Point>)

namespace bg = boost::geometry;
namespace bd2 = boost::geometry::model::d2;

BOOST_AUTO_TEST_SUITE(convexHull)

BOOST_AUTO_TEST_CASE(poly_Intersect_Test) {

//   std::vector<Point> polygon_1 = {Point(0.075, 27.2), Point( 27,  27.2), Point( 27, -22.8 ), Point( 0.075, -22.8 ), Point( 0.075,  27.2 )};
//   std::vector<Point> polygon_2 = {Point( -23, -22.8 ), Point( -23, 3.925 ), Point( 27, 3.925 ), Point( 27, -22.8 ), Point ( -23, -22.8 )};


  std::vector<Point> polygon_1 = {Point(749, 271999), Point(270000, 272000), Point(270000, -228000), Point(750, -227999), Point(749, 271999)};
  std::vector<Point> polygon_2 = {Point(-230000, -228000), Point (-230000, 39250) , Point(270000, 39250), Point (270000, -228000), Point (-230000, -228000)};
  std::vector<Point> polygon_result;

  bg::intersection(polygon_1, polygon_2, polygon_result);
  std::string points = "";
  for (auto it : polygon_result)
    points = points + "---" + it.toString();
  BOOST_CHECK_MESSAGE(false, points);
}

BOOST_AUTO_TEST_SUITE_END()

【问题讨论】:

  • 你能发一张图纸吗?从坐标中可视化几何并不容易。
  • 如果您标记您的问题boost-geometry,它将有更好的机会被图书馆的专家找到。一个更完整(最好是简化)的例子也会有所帮助。
  • 不清楚问题出在哪里。你能以最高精度打印 bg::intersection 的实际输入吗?
  • 我也不明白为什么会这样。实际输入来自半平面相交计算。您认为这可能是使用的 stl 的问题吗?
  • 我从来没有认真使用过这个库,但如果我正确理解the documentation,您需要在对intersection 的调用中将多边形/环向量作为“GeometryOut”传递(上面写着“集合”的几何图形,您正在传递一个点集合,并且其行为明显不同)。 This 似乎有效。希望有人能够在没有“似乎”和“显然”的情况下给出答案。

标签: c++ boost polygon boost-geometry


【解决方案1】:

您不是在与多边形相交,而是在传递。现在,问题是您没有将正确的概念作为输出集合传递。

很简单,两个任意多边形(偶数环)可能有多个不相交的多边形。您需要在输出类型中容纳多个交叉点。 @cv_and_he 进一步简化和扩展:

Live On Coliru

#include <vector> 
#include <boost/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <string>

class Point {
public:
    double _x, _y;
    Point():_x(),_y(){}
    Point(double x, double y):_x(x),_y(y){}
};

BOOST_GEOMETRY_REGISTER_POINT_2D(Point, double, cs::cartesian, _x, _y)
BOOST_GEOMETRY_REGISTER_RING(std::vector<Point>)

namespace bg = boost::geometry;

template <typename G>
void test(G const& g1, G const& g2) {
    std::cout << "----\nIntersecting\n\t" << bg::wkt(g1) << "\n\t" << bg::wkt(g2) << "\nresult: ";

    std::vector<G> polygon_results;
    bg::intersection<G, G>(g1, g2, polygon_results);

    for (auto polygon : polygon_results)
        std::cout << bg::wkt(polygon) << "\n";
}

int main() {
    using Ring = std::vector<Point>;

    test<Ring>(
            {{749,  271999},  {270000, 272000}, {270000, -228000},    {750, -227999},     {749,  271999}},
            {{-230000, -228000}, {-230000,  39250}, {270000,   39250}, {270000, -228000}, {-230000, -228000}});
    test<Ring>(
            {{0.075,   27.2},  { 27,   27.2}, { 27, -22.8 }, { 0.075, -22.8 }, { 0.075,  27.2 }},
            {{ -23, -22.8 }, { -23, 3.925 }, { 27, 3.925 },    { 27, -22.8 },   { -23, -22.8 }});
}

输出:

----
Intersecting
    POLYGON((749 271999,270000 272000,270000 -228000,750 -227999,749 271999))
    POLYGON((-230000 -228000,-230000 39250,270000 39250,270000 -228000,-230000 -228000))
result: POLYGON((270000 39250,270000 -228000,750 -227999,749.465 39250,270000 39250))
----
Intersecting
    POLYGON((0.075 27.2,27 27.2,27 -22.8,0.075 -22.8,0.075 27.2))
    POLYGON((-23 -22.8,-23 3.925,27 3.925,27 -22.8,-23 -22.8))
result: POLYGON((27 3.925,27 -22.8,0.075 -22.8,0.075 3.925,27 3.925))

【讨论】:

  • 非常感谢@cv_and_he 和sehe .. 现在它工作正常。没有意识到我必须提供一个多环作为输出收集器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多