Boost :: geometry :: intersection输出错误。版本BOOST_1_57_0

时间:2015-10-26 06:02:38

标签: c++ boost polygon boost-geometry

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

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

请帮我确定这里的问题。

这是类Point定义:

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)

  

(注意:在显示检查期间,输入点会四舍五入。尽管不是手动的。   当在测试用例中使用显示的点时,结果是   正确。但显然是因为四舍五入所使用的分数   在原计划中进行计算并在测试用例中   扰动。)

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

enter image description here

编辑:这是我的测试用例。注释行polygon_1&amp; 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()

1 个答案:

答案 0 :(得分:2)

你没有交叉多边形,你正在传递 ring 。现在,问题在于您没有将正确的概念作为输出集合传递。

很简单,两个任意多边形(均匀环)可能有多个分离交叉多边形。您需要在输出类型中容纳多个交叉点。 @ 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))