繁体   English   中英

Boost :: convex_hull STL容器中分散的二维点

[英]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_pointpoint实体。

#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;
}

这里是示例: https : //wandbox.org/permlink/j2XkmLivqcy6EtdU

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM