gpt4 book ai didi

c++ - Boost::geometry::intersection 给出了错误的输出。版本 BOOST_1_57_0

转载 作者:搜寻专家 更新时间:2023-10-31 02:19:54 27 4
gpt4 key购买 nike

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

我有一个在代码中使用 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)

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

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

enter image description here

编辑:这是我的测试用例。注释行 polygon_1 和 polygon_2 是四舍五入的值。使用这些值给出新多边形的 4 个角。使用中的放大值导致在 Release模式下只有 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()

最佳答案

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

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

关于c++ - Boost::geometry::intersection 给出了错误的输出。版本 BOOST_1_57_0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33339359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com