In my application I'm using boost geometry to perform mainly intersection and difference calculations. Unfortunately I noticed an inconsistency in the results of:
- bg::intersects and
- bg::intersection
What I do is:
- Calculate the intersection of polygon1 and polygon2 (= polygon3).
- Remove (bg::difference) this intersection from polygon1 (= poly4)
- The resulting polygon4 should not have any intersection with poly2.
#include <iostream>
#include <boost/geometry.hpp>
namespace bg = boost::geometry;
using point_t = bg::model::d2::point_xy<double>;
using polygon_t = bg::model::polygon<point_t>;
using mpolygon_t = bg::model::multi_polygon<polygon_t>;
int main()
{
polygon_t poly1, poly2;
mpolygon_t poly3, poly4, poly5;
bg::read_wkt("POLYGON(("
"12227.0 4967.0000000000009, 12238.0 4967.0000000000009, "
"12238.0 4813.0000000000009, 12227.0 4813.0000000000009, "
"12227.0 4967.0000000000009))", poly1);
bg::read_wkt("POLYGON(("
"12254.0 4947.0, 12219.0 4982.0, 12219.0 5020.0, 12254.0 5055.0, "
"12261.0 5055.0, 12263.0 5055.0, 12283.0 5055.0, 12283.0 4947.0, "
"12263.0 4947.0, 12261.0 4947.0, 12254.0 4947.0))", poly2);
bg::intersection(poly1, poly2, poly3);
bg::difference(poly1, poly3[0], poly4);
// b0 = true, b1 = false
bool b0 = bg::intersects(poly2, poly4[0]);
bool b1 = bg::intersection(poly2, poly4, poly5) && (poly5.size() != 0);
bool b2 = !bg::disjoint(poly2, poly4[0]) && !bg::touches(poly2, poly4[0]);
bool b3 = bg::overlaps(poly2, poly4[0]) || bg::within(poly4[0], poly2) || bg::within(poly2, poly4[0]);
std::cout << b0 << b1 << b2 << b3 << std::endl;
return 1;
}
The weird thing is that bg::intersects returns true while bg::intersection returns an empty intersection.
Does anyone have an idea why this happens? (Maybe accuracy problem?) and even more interesting: How can I avoid such problems?
I tried to avoid "intersects" by using other functions but the results were not helpful. Please see calculations for b2 and b3.
PS: Unfortunately the example seems cause a crash on coliru.
