简体   繁体   English

Boost几何相交无法正确输出

[英]Boost geometry intersection does not output correctly

From the following code I calculate the intersection of two polygons. 从以下代码中,我计算出两个多边形的交点。 I hope the output can be NULL if it is not polygon. 我希望如果不是多边形,则输出可以为NULL。 However the output is (((240, 52.9999), (240, 53), (240, 53), (240, 52.9999))). 但是输出为((((240,52.9999),(240,53),(240,53),(240,52.9999))))。 This is not a polygon. 这不是多边形。 Is there any way to check whether the output is really a polygon?? 有什么方法可以检查输出是否真的是多边形?

#include <iostream>
#include <deque>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/algorithms/append.hpp>

#include <algorithm> // for reverse, unique
#include <iostream>
#include <string>


int main()
{
   typedef boost::geometry::model::d2::point_xy<double,    boost::geometry::cs::cartesian> point_2d;
   typedef boost::geometry::model::polygon<point_2d> polygon_2d;

    polygon_2d green;
    boost::geometry::append(green, point_2d(286.188, 90.9575));
    boost::geometry::append(green, point_2d(274.902, 56.2215));
    boost::geometry::append(green, point_2d(274.238, 55.7393));
    boost::geometry::append(green, point_2d(246.908, 51.9765));
    boost::geometry::append(green, point_2d(194.477, 59.7441));
    boost::geometry::append(green, point_2d(159.213, 101.141));
    boost::geometry::append(green, point_2d(203.576, 149.537));
    boost::geometry::append(green, point_2d(286.188, 90.9575));

    polygon_2d blue;
    boost::geometry::append(blue, point_2d(240, 53));
    boost::geometry::append(blue, point_2d(240, -53));
    boost::geometry::append(blue, point_2d(-60, -53));
    boost::geometry::append(blue, point_2d(-60, 53));
    boost::geometry::append(blue, point_2d(240, 53));

    boost::geometry::correct(green);
    boost::geometry::correct(blue);

    std::vector<polygon_2d> output;
    boost::geometry::intersection(green, blue, output);

    std::cout << "green && blue:" << std::endl;
    if(output.size())
    {
       std::cout<<boost::geometry::dsv(output[0])<<std::endl;
       std::cout<<boost::geometry::area(output[0])<<std::endl;
    }

    return 0;
}

You can weed out the invalid geometries: 您可以清除无效的几何图形:

output.erase(
        std::remove_copy_if(output.begin(), output.end(), output.begin(), [](polygon_2d const&g) { return boost::geometry::is_valid(g); }),
        output.end());

Now, the result will be empty as expected. 现在,结果将为空。

See it Live On Coliru 在Coliru上实时观看


It's interesting to me how "invalid" geometries would result in the first place. 对我来说很有趣的是,“无效”的几何形状将如何产生第一位置。 As you were probably aware that your polygons are very well chosen: 您可能已经知道,多边形的选择非常好:

Let's zoom in a little: 让我们放大一点:

Now, you're likely looking at some floating point accuracy issues, as the same experiment with long long points or boost multiprecision, eg: 现在,您可能会遇到一些浮点精度问题,就像是使用long long或提高多精度的同一实验一样,例如:

typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<1024*128>> oopmh; // that's a lotta oomph
typedef boost::geometry::model::d2::point_xy<oopmh, boost::geometry::cs::cartesian> point_2d;

You will see that we get 您会看到我们得到

green && blue:
(((2.4e+06, 530000), (-600000, 530000), (-600000, -530000), (2.4e+06, -530000), (2.4e+06, 530000)))
nan

So it looks very much like we just get a singular point of area. 所以看起来非常像我们只是获得了一个奇异点。 If you don't want this, then maybe you should just define an "epsilon" value of sorts, a threshold at which you stop considering an overlap an overlap. 如果您不希望这样做,那么也许您应该只定义sorts的“ epsilon”值,即停止考虑重叠和重叠的阈值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM