Пересечение Boost.Geometry () дает непоследовательные результаты - PullRequest
0 голосов
/ 19 ноября 2018

intersection () дает несогласованные результаты для случая, когда прямая проходит через вершину многоугольника.Идеальным результатом должна быть одна точка пересечения.

Я перечисляю несколько тестовых случаев, чтобы показать проблему.См. Прикрепленное изображение.

enter image description here

Код:

#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 вместо того, чтобы давать одну точку, дают две абсолютно одинаковые точки пересечения, и причина находится за пределами моих мыслей.Может ли кто-нибудь помочь объяснить это?Заранее спасибо.

С уважением, Итан

...