Boost logo

Geometry :

Subject: [geometry] intersection() gives nonconsistent results
From: Ethan (zhangq.rhy_at_[hidden])
Date: 2018-11-19 07:23:37


Hi,

intersection() gives non-consistent results for the case where the line is
right across the vertex of the polygon. The ideal result should be one
single intersection point.

I'm listing a few test cases to show the problem. See the attached image.

<http://boost-geometry.203548.n3.nabble.com/file/t496085/intersection3.png>

Code:
//==========================================
#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;
}
//==========================================

Results:
//==========================================
*==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

So case #1 is giving a perfect result with one single intersection. Case #2
gives no intersecion which is understandable because only one single point
does not comprise a linestring. Case 3 and Case 4, instead of giving a
single point, give two exactly the same intersection point and the reason is
beyond my thoughts. Could any one can help on explaining this? Thanks in
advance.

Best regards,
Ethan

--
Sent from: http://boost-geometry.203548.n3.nabble.com/

Geometry list run by mateusz at loskot.net