在STL容器中分散2-D点的Boost :: convex_hull

问题描述 投票:2回答:3

我有一个二维点矢量。让我们假设它们的形式为std :: pair <int,int>。我想用boost来计算凸包。这就是问题所在。我怎么做?

我发现的唯一文档就像是理论和琐事课程的研究生文本。

填写BLANKS:

#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 ++东西。

c++ boost std convex-hull boost-geometry
3个回答
2
投票

有一种方法可以在不将数据从容器复制到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,有必要将它们添加到属性/ C C ++ /预处理器/预处理器定义

 _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS;_SCL_SECURE_NO_WARNINGS

Visual Studio IDE将这些“警告”视为致命的。禁用所有警告是不够的。此外,一些“弃用警告”似乎是微软特有的,而不是正式弃用的C ++东西。


2
投票

您可以将点矢量转换为多点类型。

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

2
投票

您可以使用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

© www.soinside.com 2019 - 2024. All rights reserved.