簡體   English   中英

Boost::geometry::intersection 給出錯誤的輸出。 版本 BOOST_1_57_0

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

這可能是多次被問到的問題。 但是我交叉檢查了類似的問題,以確保不會重復我的問題。

我有一個源代碼,它在代碼中使用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)

(注意:在顯示檢查時輸入點被四舍五入。雖然不是手動。當顯示點用於測試用例時,結果是正確的。但很明顯因為四舍五入,原始使用的點用於計算和測試用例的程序受到干擾。)

請幫助確定問題。 提前致謝。

在此處輸入圖片說明

編輯:這是我的測試用例。 注釋行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()

你不是在交叉多邊形,你在傳遞ring 現在,問題是您沒有將正確的概念作為輸出集合傳遞。

很簡單,兩個任意多邊形(偶數環)可能有多個不相交的相交多邊形。 您需要在輸出類型中容納多個交叉點。 @cv_and_he 進一步簡化和擴展:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM