Skip to content

Instantly share code, notes, and snippets.

@Jihadist
Created June 20, 2022 11:09
Show Gist options
  • Save Jihadist/864acc5efefd26c79fbb262ace5c5604 to your computer and use it in GitHub Desktop.
Save Jihadist/864acc5efefd26c79fbb262ace5c5604 to your computer and use it in GitHub Desktop.
boost_buffer_issue
#include <QPainter>
#include <QtSvg/QSvgGenerator>
#include <boost/geometry/algorithms/buffer.hpp>
#include <boost/geometry/algorithms/correct.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/strategies/agnostic/buffer_distance_symmetric.hpp>
#include <boost/geometry/strategies/cartesian/buffer_end_flat.hpp>
#include <boost/geometry/strategies/cartesian/buffer_join_miter.hpp>
#include <boost/geometry/strategies/cartesian/buffer_point_circle.hpp>
#include <boost/geometry/strategies/cartesian/buffer_point_square.hpp>
#include <boost/geometry/strategies/cartesian/buffer_side_straight.hpp>
#include <vector>
namespace Geometry {
template <class T>
struct PointBase2 {
typedef T value_type;
T x, y;
PointBase2() = default;
PointBase2(T x_, T y_)
: x(x_)
, y(y_)
{
}
bool operator==(const PointBase2<T>& other) const
{
return x == other.x && y == other.y;
}
bool operator!=(const PointBase2<T>& other) const
{
return !(*this == other);
}
};
typedef PointBase2<int> PointI;
typedef PointBase2<float> PointF;
typedef PointBase2<double> PointD;
struct Limits {
PointD min;
PointD max;
void bound(PointD::value_type boundingDelta)
{
min.x -= boundingDelta;
min.y -= boundingDelta;
max.x += boundingDelta;
max.y += boundingDelta;
}
};
} // namespace Geometry
typedef double coordinate_type;
namespace bg = boost::geometry;
typedef double coordinate_type;
typedef bg::model::point<coordinate_type, 2, bg::cs::cartesian> PointModel;
typedef bg::model::polygon<PointModel> SitePolygon;
typedef bg::model::multi_polygon<SitePolygon> Sites;
const inline SitePolygon clippingPolygon { { { 577255, 928582 }, { 582964, 928631 }, { 583652, 929953 }, { 583504, 932546 }, { 583237, 932957 }, { 580929, 933297 }, { 580589, 933287 }, { 577619, 933122 }, { 577245, 932654 }, { 577228, 928786 }, { 577255, 928582 }, { 577255, 928582 } } };
constexpr double kBoundingDelta = 1000.0;
int main(int argc, char* argv[])
{
Geometry::Limits limits;
limits.max = { 582694, 932300 };
limits.min = { 578214, 929587 };
auto boundedLimits = limits;
boundedLimits.bound(kBoundingDelta);
QSvgGenerator generator;
generator.setFileName("buffered_polygons.svg");
generator.setSize(QSize(std::abs(boundedLimits.max.x - boundedLimits.min.x),
std::abs(boundedLimits.max.y - boundedLimits.min.y)));
generator.setTitle(("Buffered polygons"));
generator.setDescription(("Buffered polygons"));
QPainter painter;
painter.begin(&generator);
QPolygonF qpolygon;
std::transform(clippingPolygon.outer().cbegin(), clippingPolygon.outer().cend(),
std::back_inserter(qpolygon),
[](const PointModel& point) {
return QPointF(point.get<0>(), point.get<1>());
});
painter.drawPolygon(qpolygon);
auto testClippingPoly = clippingPolygon;
bg::correct(testClippingPoly);
// Declare the join_miter strategy
boost::geometry::strategy::buffer::join_miter join_strategy;
// Declare other strategies
boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(300);
boost::geometry::strategy::buffer::end_flat end_strategy;
boost::geometry::strategy::buffer::side_straight side_strategy;
boost::geometry::strategy::buffer::point_square point_strategy;
boost::geometry::model::multi_polygon<bg::model::polygon<PointModel>> mp;
bg::buffer(testClippingPoly, mp, distance_strategy, side_strategy, join_strategy, end_strategy,
point_strategy);
std::cout << "mp size: " << mp.size() << std::endl;
std::cout << "testClippingPoly size: " << testClippingPoly.outer().size() << std::endl;
std::cout << "mp internal size: " << mp.front().outer().size() << std::endl;
for (const auto& site : mp) {
QPolygonF qpolygon;
std::transform(site.outer().cbegin(), site.outer().cend(), std::back_inserter(qpolygon),
[](const PointModel& point) {
return QPointF(point.get<0>(), point.get<1>());
});
painter.drawPolygon(qpolygon);
}
painter.end();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment