geometry icon indicating copy to clipboard operation
geometry copied to clipboard

boost::geometry::intersection wrong result

Open mjagodicblackshark opened this issue 2 years ago • 4 comments
trafficstars

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

mjagodicblackshark avatar Oct 25 '23 13:10 mjagodicblackshark

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.

jdmarsh avatar Oct 25 '23 15:10 jdmarsh

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

vissarion avatar Dec 05 '23 10:12 vissarion

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.

jdmarsh avatar Dec 05 '23 10:12 jdmarsh

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

vissarion avatar Dec 05 '23 13:12 vissarion