boostorg / geometry

Boost.Geometry - Generic Geometry Library | Requires C++14 since Boost 1.75
http://boost.org/libs/geometry
Boost Software License 1.0
457 stars 216 forks source link

boost::geometry::intersection wrong result #1211

Open mjagodicblackshark opened 1 year ago

mjagodicblackshark commented 1 year ago

On version 1.82 without special defines. I have found a test case that gives unexpectedly wrong results.

Here is the test case to prove the problem. The correct result is close to the float precision limit, but the actual result is almost the same as one of the input polygons and therefore very wrong. Empty or even degenerate results would be fine for our use case. Possibly related to #1201

using Point = boost::geometry::model::d2::point_xy<float>;
using BoostPolygonType = boost::geometry::model::polygon<Point, false, false>;
using BoostMultiPolygonType = boost::geometry::model::multi_polygon<BoostPolygonType>;
BoostPolygonType polygon1;
boost::geometry::read_wkt("POLYGON((0.875 0.4375, 0.875 0.375, 0.9375 0.375))", polygon1);
BoostPolygonType polygon2;
boost::geometry::read_wkt("POLYGON((1. 0., 1. 0.581662834, 0.824054539 0.))", polygon2);

BoostMultiPolygonType result;
boost::geometry::intersection(polygon1, polygon2, result);

EXPECT_EQ(result.size(), 1);

EXPECT_LT(boost::geometry::area(result), 0.01);

for (auto& intersection : result)
{
    EXPECT_EQ(intersection.outer().size(), 3);
}

polygon1-orange, polygon2-red image the result of the intersection image

jdmarsh commented 1 year ago

I've had this issue crop up as well (1.82 and 1.83). It's not quite the same but I've added a repro of my problem here: https://godbolt.org/z/W1s7YEczP with the points plotted here: https://www.desmos.com/calculator/zrytxj5d8u The output line should be very small as it only intersects at a corner, but the output seems to be much much larger going outside of the bounds of the polygon.

vissarion commented 11 months ago

I can reproduce the issue. Probably a numerical issue. With double instead of float the results are as expected.

jdmarsh commented 11 months ago

I've had this issue using float and double. I only managed to solve it by moving to cpp_bin_float_quad in boost multiprecision but that's crazy slow and I can't guarantee that the issue isn't still going to crop up there as well, I just haven't seen it yet.

vissarion commented 11 months ago

@jdmarsh could you please open a new issue with your example and data?