intersection()会给出不一致的结果。理想结果应该是一个单一的交点。
我列出了一些测试用例来说明问题。请参阅所附图片。
代码:
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/geometries.hpp>
namespace bg = boost::geometry;
typedef bg::model::d2::point_xy<double> bg2DPointXY;
typedef bg::model::multi_point<bg2DPointXY> bgMulti2DPointXY;
typedef bg::model::linestring<bg2DPointXY> bgLinestring;
typedef bg::model::multi_linestring<bgLinestring> bgMultiLinestring;
typedef bg::model::polygon<bg2DPointXY> bgPolygon;
int main()
{
// Declare/fill a polygon
bgPolygon poly1{{{1.0, 3.0},
{5.0, 6.0},
{8.0, 6.0},
{10.0, 2.0},
{7.0, 1.0},
{1.0, 3.0}}};
bgPolygon poly2{{{7, 1.52705},
{2.06343, 3.17257},
{5.16667, 5.5},
{7.69098, 5.5},
{9.29497, 2.29204},
{7, 1.52705}}};
bg::correct(poly1);
bg::correct(poly2);
// Declare/fill a linestring
bgLinestring line1{{1.0, 0.0}, {1, 4.0}};
bgLinestring line2{{10, 3.43403}, {8.4973, 1.0}};
// Declare output for different output container
bgMulti2DPointXY result1, result2;
bgMultiLinestring result3, result4;
// Calculate intersections according to different output geometry
bg::intersection(poly1, line1, result1);
bg::intersection(poly2, line2, result2);
bg::intersection(poly1, line1, result3);
bg::intersection(poly2, line2, result4);
std::cout << "==POLY1==LINE1==MultiPoint==" << std::endl;
std::cout << "Poly1: " << bg::dsv(poly1) << std::endl;
std::cout << "Line1: " << bg::dsv(line1) << std::endl;
std::cout << "Intersections: " << bg::dsv(result1) << std::endl;
std::cout << "Num of intersection points: " << bg::num_points(result1) << std::endl;
std::cout << "==POLY1==LINE1==MultiLinestring==" << std::endl;
std::cout << "Poly1: " << bg::dsv(poly1) << std::endl;
std::cout << "Line1: " << bg::dsv(line1) << std::endl;
std::cout << "Intersections: " << bg::dsv(result3) << std::endl;
std::cout << "Num of intersection points: " << bg::num_points(result3) << std::endl;
std::cout << "==POLY2==LINE2==MultiPoint==" << std::endl;
std::cout << "Poly2: " << bg::dsv(poly2) << std::endl;
std::cout << "Line2: " << bg::dsv(line2) << std::endl;
std::cout << "Intersections: " << bg::dsv(result2) << std::endl;
std::cout << "Num of intersection points: " << bg::num_points(result2) << std::endl;
std::cout << "==POLY2==LINE2==MultiLinestring==" << std::endl;
std::cout << "Poly2: " << bg::dsv(poly2) << std::endl;
std::cout << "Line2: " << bg::dsv(line2) << std::endl;
std::cout << "Intersections: " << bg::dsv(result4) << std::endl;
std::cout << "Num of intersection points: " << bg::num_points(result4) << std::endl;
return 0;
}
结果:
==POLY1==LINE1==MultiPoint==
Poly1: (((1, 3), (5, 6), (8, 6), (10, 2), (7, 1), (1, 3)))
Line1: ((1, 0), (1, 4))
Intersections: ((1, 3))
Num of intersection points: 1
==POLY1==LINE1==MultiLinestring==
Poly1: (((1, 3), (5, 6), (8, 6), (10, 2), (7, 1), (1, 3)))
Line1: ((1, 0), (1, 4))
Intersections: ()
Num of intersection points: 0
==POLY2==LINE2==MultiPoint==
Poly2: (((7, 1.52705), (2.06343, 3.17257), (5.16667, 5.5), (7.69098, 5.5), (9.29497, 2.29204), (7, 1.52705)))
Line2: ((10, 3.43403), (8.4973, 1))
Intersections: ((9.29497, 2.29204), (9.29497, 2.29204))
Num of intersection points: 2
==POLY2==LINE2==MultiLinestring==
Poly2: (((7, 1.52705), (2.06343, 3.17257), (5.16667, 5.5), (7.69098, 5.5), (9.29497, 2.29204), (7, 1.52705)))
Line2: ((10, 3.43403), (8.4973, 1))
Intersections: (((9.29497, 2.29204), (9.29497, 2.29204)))
Num of intersection points: 2
因此,案例#1通过一个交叉点给出了理想的结果。情况#2没有交集,这是可以理解的,因为只有一个点不包含线串。情况3和情况4,不是给出单个点,而是给出两个完全相同的交点,其原因超出了我的想象。有人可以帮忙解释一下吗?预先感谢。
最好的问候, 伊桑