[英]Dilating a polygon boost::geometry
多邊形幾何對象有形態學膨脹運算嗎?
例如,假設我有一個邊長為1且以原點為中心的正方形(boost :: geometry :: model :: polygon,其點為(.5,.5),(-。5,.5),(-。 5,-.5),(.5,-.5))。 如果我以0.5的距離/半徑“擴張”它,我將得到一個邊長為2的正方形,該正方形仍以原點為中心。 也就是說,多邊形的所有邊緣都應沿其法線方向“推出”。
這個術語是“緩沖區”,緩沖多邊形只能通過以下策略使用buffer()的重載來實現: http : //www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry /reference/algorithms/buffer/buffer_7_with_strategies.html
這是將三角形擴大0.1個單位的示例。
#include <iostream>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
using coordinate_type = double;
using point_type = boost::geometry::model::d2::point_xy<coordinate_type>;
using polygon_type = boost::geometry::model::polygon<point_type>;
int main()
{
// Construct
polygon_type polygon;
// Counter clock-wise points don't seem to work (no error, but empty output)
// boost::geometry::append(polygon, point_type {0,0});
// boost::geometry::append(polygon, point_type {1,0});
// boost::geometry::append(polygon, point_type {0,1});
// boost::geometry::append(polygon, point_type {0,0});
// Points specified in clockwise order
boost::geometry::append(polygon, point_type {0,0});
boost::geometry::append(polygon, point_type {0,1});
boost::geometry::append(polygon, point_type {1,0});
boost::geometry::append(polygon, point_type {0,0});
//boost::geometry::buffer(poly, output, .5); // THIS_OPERATION_IS_NOT_OR_NOT_YET_IMPLEMENTED
const double buffer_distance = .1;
const int points_per_circle = 36;
boost::geometry::strategy::buffer::distance_symmetric<coordinate_type> distance_strategy(buffer_distance);
boost::geometry::strategy::buffer::join_round join_strategy(points_per_circle);
boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
boost::geometry::strategy::buffer::side_straight side_strategy;
boost::geometry::model::multi_polygon<polygon_type> input;
input.push_back(polygon);
boost::geometry::model::multi_polygon<polygon_type> outputMultiPolygon;
boost::geometry::buffer(polygon, outputMultiPolygon,
distance_strategy, side_strategy,
join_strategy, end_strategy, circle_strategy);
std::cout << outputMultiPolygon.size() << std::endl;
polygon_type outputPolygon = outputMultiPolygon[0];
// Print the points of the result (there are many more than in the input because the corners have been rounded)
for(unsigned int pointID = 0; pointID < outputPolygon.outer().size(); ++pointID) {
std::cout << boost::geometry::get<0>(outputPolygon.outer()[pointID]) << " " << boost::geometry::get<1>(outputPolygon.outer()[pointID]) << std::endl;
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.