[英]Boost::convex_hull of scattered 2-D points in STL container
我有一個二維點的向量。 讓我們假設它們的格式為std :: pair <int,int>。 我想使用boost計算凸包。 這就是問題所在。 我怎么做?
我發現的唯一文檔就像是基礎知識和瑣事課程的研究生課文。
填寫空白:
#include <iostream>
#include <vector>
#include <utility>
// BLANK boost include-files
// #include <boost/geometry.hpp>
// #include <boost/geometry/geometries/polygon.hpp> // Noop.
// #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
// BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
int main() {
// Serving suggestion
std::vector<std::pair<int, int>> A{ { 0,3 },{ 1,4 },{ 2,2 },{ 1,0 }, \
{0,0},{ 2,0 },{ 0,1 },{ 0,2 },{ 3,1 },{ 3,3 },{ 4,4 },{ 4,3 },{ 4,2 } };
std::vector<std::pair<int, int>> the_hull; // Fill this, please.
// BLANK - Boost magic goes here.
// Print convex hull of A
for (auto h: the_hull) {
std::cout << h.first << "," << h.second << "\n";
}
std::cout<< std::endl;
return 0;
}
VC ++用戶注意事項。 使用VC ++ 2017,在獲取下面的答案進行編譯時,我遇到了數噸的麻煩。我終於使它工作了。 我重新安裝了boost,使用Windows二進制文件來增強1.66。 接下來,我必須將以下兩行添加到項目屬性中
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS;_SCL_SECURE_NO_WARNINGS
IDE將這些“警告”視為致命的。 禁用所有警告是不夠的。 此外,某些“棄用警告”似乎是微軟的搖動,而不是正式棄用的C ++。
您可以將點向量轉換為多點類型。
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
int main(){
typedef boost::geometry::model::d2::point_xy<double> point_type;
std::vector<std::pair<int, int>> A{ { 0,3 },{ 1,4 },{ 2,2 },{ 1,0 },
{0,0},{ 2,0 },{ 0,1 },{ 0,2 },{ 3,1 },{ 3,3 },{ 4,4 },{ 4,3 },{ 4,2 } };
boost::geometry::model::multi_point<point_type> pts;
for (const auto& pt : A){
pts.emplace_back(pt.first,pt.second);
}
boost::geometry::model::polygon<point_type> poly;
boost::geometry::convex_hull (pts, poly);
std::cout << boost::geometry::wkt(poly) << std::endl;
std::vector<std::pair<int, int>> the_hull; // Fill this, please.
for (auto it = poly.outer().begin(); it != poly.outer().end(); ++it)
the_hull.emplace_back(it->x(), it->y());
// Print convex hull of A
for (auto h: the_hull) {
std::cout << h.first << "," << h.second << "\n";
}
return 0;
}
有一種方法可以做到,而無需將數據從容器復制到multi_point
,反之亦然。 您必須將向量容器及其對注冊為multi_point
和point
實體。
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/multi/geometries/register/multi_point.hpp>
#include<iostream>
BOOST_GEOMETRY_REGISTER_POINT_2D(decltype(std::pair<int, int>{}), int, cs::cartesian, first, second)
BOOST_GEOMETRY_REGISTER_MULTI_POINT(decltype(std::vector<std::pair<int, int>>{}))
int main(){
std::vector<std::pair<int, int>> A{
{ 0,3 },{ 1,4 },{ 2,2 },{ 1,0 }, { 0,0 }, { 2,0 }, { 0,1 }, { 0,2 },
{ 3,1 },{ 3,3 },{ 4,4 },{ 4,3 }, { 4,2 }
};
std::cout << "A: " << boost::geometry::wkt(A) << std::endl;
std::vector<std::pair<int, int>> B;
boost::geometry::convex_hull(A, B);
std::cout << "B: " << boost::geometry::wkt(B) << std::endl;
}
輸出:
A: MULTIPOINT((0 3),(1 4),(2 2),(1 0),(0 0),(2 0),(0 1),(0 2),(3 1),(3 3),(4 4),(4 3),(4 2))
B: MULTIPOINT((0 0),(0 3),(1 4),(4 4),(4 2),(2 0),(0 0)
這適用於Fedora 27,gcc 7.2.1,clang ++ 4.0.1,Boost 1.64
對於Visual C ++ 2017 Boost 1.66,有必要將它們添加到properties / C C ++ / Preprocessor / Preprocessor Definitions
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS;_SCL_SECURE_NO_WARNINGS
Visual Studio IDE將這些“警告”視為致命的。 禁用所有警告是不夠的。 此外,某些“棄用警告”似乎是Microsoft特定的,不是正式棄用的C ++。
您可以使用BOOST_GEOMETRY_REGISTER_POINT_2D宏注冊點類型std::pair<int,int>
,然后使用它。 而且您不需要多點。 這里舉一個例子。 希望能幫助到你:
#include <iostream>
#include <utility>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/polygon.hpp>
using Point = std::pair<int,int>;
BOOST_GEOMETRY_REGISTER_POINT_2D(Point, int, boost::geometry::cs::cartesian, first, second)
int main()
{
std::vector<Point> v{ { 0,3 },{ 1,4 },{ 2,2 },{ 1,0 },{ 0,0 },{ 2,0 },{ 0,1 },{ 0,2 },{ 3,1 },{ 3,3 },{ 4,4 },{ 4,3 },{ 4,2 } };
using Polygon = boost::geometry::model::polygon<Point>;
Polygon poly, hull;
poly.outer().assign(v.begin(), v.end());
boost::geometry::convex_hull (poly, hull);
using boost::geometry::dsv;
std::cout << "polygon" << dsv(poly) << std::endl
<< "hull: " << dsv(hull) << std::endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.