I have the following two input polygons for which I want to calculate a subtracted polygon:
A:
* (0, 8)
/ \
/ \
/ \
(-3, 0) *-------* (3, 0)
B:
(-1, 2) *-----* (1, 2)
| |
(-1, 1) *-----* (1, 1)
Thus, I want to calculate A - B
, which should result in a triangle with a square cutout. Calculating this using Boost Polygon results in an incorrect partial triangle with a cutout. It is hard to draw; the missing part of the result triangle is represented by the triangle (3, 0) => (0, 8) => (1, 2)
. I am using the following code to calculate the subtraction:
#include <boost/polygon/polygon.hpp>
namespace bp = boost::polygon;
int main()
{
using Polygon = bp::polygon_data<int>;
using Point = bp::point_data<int>;
using PolygonSet = bp::polygon_set_data<int>;
using SimplePolygons = std::vector<bp::polygon_data<int>>;
using namespace boost::polygon::operators;
Polygon A;
{
std::vector<Point> points{{-3, 0}, {3, 0}, {0, 8}};
bp::set_points(A, points.begin(), points.end());
}
Polygon B;
{
std::vector<Point> points{{-1, 1}, {1, 1}, {1, 2}, {-1, 2}};
bp::set_points(B, points.begin(), points.end());
}
PolygonSet result{A - B};
SimplePolygons simplePolygons;
result.get<SimplePolygons>(simplePolygons);
for (const auto& polygon : simplePolygons)
{
for (const Point& p : polygon)
{
std::cout << '(' << std::to_string(p.x()) << ", " << std::to_string(p.y()) << ")\n";
}
}
return 0;
}
This prints the following subsequent points making up the cutout triangle:
(3, 0)
(1, 2)
(1, 1)
(-1, 1)
(-1, 2)
(1, 2)
(0, 8)
(-3, 0)
(3, 0)
So, the edges (1, 2) => (3, 0)
and (3, 0) => (0, 8)
are missing from the result. The upper right part of the input triangle is missing from the result.
Correct output might look as follows:
(3, 0)
(1, 2)
(1, 1)
(-1, 1)
(-1, 2)
(1, 2)
(3, 0)
(0, 8)
(-3, 0)
(3, 0)
Is this a bug in Boost Polygon, am I somehow using the library incorrectly, or is there something else I am missing?
Some additional information:
- I am using GCC 7.2.0, Boost 1.60.0. Boost Polygon has not been updated since, so upgrading Boost most likely will not help.
- Parameterizing the point type and all other geometric types using
double
instead ofint
does not fix the issue. - Calculating cutouts using axis-aligned rectangles for example works without problems.
- For my application I want to use Boost Polygon instead of Boost Geometry because it provides polygon keyhole fracturing support.