我有一个二维点的向量。让我们假设它们是std::pair形式的。我想用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,使用用于boost 1.66的窗口二进制文件。接下来,我必须向项目属性添加以下两行
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS;_SCL_SECURE_NO_WARNINGSIDE将这些“警告”视为致命的。仅禁用所有警告是不够的。此外,一些“不推荐的警告”似乎是来自微软的指手画脚,而不是官方反对的C++事物。
发布于 2018-01-02 23:12:11
有一种方法可以做到这一点,而不必将数据从容器复制到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,有必要将它们添加到属性/C++/预处理器/预处理器定义中
_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS;_SCL_SECURE_NO_WARNINGS将这些“警告”视为致命的。仅禁用所有警告是不够的。此外,一些“弃用警告”似乎是特定于微软的,而不是官方所反对的C++内容。
发布于 2018-01-02 22:09:43
您可以将点向量转换为多点类型。
#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;
}发布于 2018-01-02 23:18:17
您可以在std::pair<int,int>宏中注册点类型BOOST_GEOMETRY_REGISTER_POINT_2D,然后使用它。你不需要多点。这里有一个例子。希望它有帮助:
#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;
}https://stackoverflow.com/questions/48068349
复制相似问题