简体   繁体   English

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

[英]Boost::geometry::intersection gives wrong output. version BOOST_1_57_0

This might be a question asked multiple times.这可能是多次被问到的问题。 But I cross checked similar questions, to make sure mine is not repeated.但是我交叉检查了类似的问题,以确保不会重复我的问题。

I have a source code that uses boost::geometry::intersection in the code, to get the resultant of intersection between two polygons.我有一个源代码,它在代码中使用boost::geometry::intersection来获得两个多边形之间相交的结果。 I have boost::geometry::correct tested for the polygons used.我对所使用的多边形进行了boost::geometry::correct测试。 The sequence of points in the polygon is clockwise.多边形中点的顺序是顺时针的。 Everything seems right, but I get incorrect output from the boost::geometry::intersection call.一切似乎都正确,但我从boost::geometry::intersection调用中得到了错误的输出。

Please help me identify what the issue here is.请帮助我确定这里的问题是什么。

This is class Point definition:这是类 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;
};

code where boost libraries are used使用 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;

    }

I used basic iostream to check the output.我使用基本的 iostream 来检查输出。 (Note : The input values are taken from one of the run sequences of the program, but not fed in as a user input) (注意:输入值取自程序的运行序列之一,但不作为用户输入输入)

--------------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 )  ----  

The output should have 4 points and is clearly missing a point (0.07500 : - 22.8) .输出应该有 4 个点,并且显然缺少一个点(0.07500 : - 22.8)

(Note : The input points are rounded off during the display check. Not manually though. When the displayed points were used in the test case, the results were correct. But it is clearly because of the round off, the points used in the original program for calculation and in the test case are perturbed.) (注意:在显示检查时输入点被四舍五入。虽然不是手动。当显示点用于测试用例时,结果是正确的。但很明显因为四舍五入,原始使用的点用于计算和测试用例的程序受到干扰。)

Please help to identify the issue.请帮助确定问题。 Thanks in advance.提前致谢。

在此处输入图片说明

EDIT : This is my test case.编辑:这是我的测试用例。 The commented lines polygon_1 & polygon_2 are rounded off values.注释行polygon_1 和polygon_2 是四舍五入的值。 Using those values gives 4 corners of the new polygon.使用这些值给出新多边形的 4 个角。 The magnified values in use result in only 3 corners as got in the release mode.使用的放大值仅导致释放模式中的 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()

You're not intersecting polygons, you're passing rings .你不是在交叉多边形,你在传递ring Now, the problem is that you don't pass the right concept as an output collection.现在,问题是您没有将正确的概念作为输出集合传递。

Quite simply, two arbitrary polygons (evens rings) might have multiple disjunct intersection polygons.很简单,两个任意多边形(偶数环)可能有多个不相交的相交多边形。 You need to accomodate multiple intersections in the output type.您需要在输出类型中容纳多个交叉点。 @cv_and_he's further simplified and extended: @cv_and_he 进一步简化和扩展:

Live On Coliru 住在 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 }});
}

Output:输出:

----
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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM