删除重复项:如何为boost :: geometry :: box编写排序谓词?

时间:2018-05-24 02:47:10

标签: c++ boost boost-geometry

我在这样的课程中使用boost::geometry::model::box

struct Feature {
    typedef boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian> point;
    typedef boost::geometry::model::box<point> box;

    box bounds;
    //... other members ...
};

std::list<Feature> v;

我希望删除v中的重复项。

通常我会写一个排序谓词,以便我可以这样做:

v.sort(sort_pred);    //?
std::unique(v.begin(), v.end());

我可以使用boost::geometry::equals(如How do I compare Boost Geometries?中所述)来撰写operator==,因此std::unique有效。我可以使用类似的函数将谓词写入sort,还是需要从头开始写一个?

或者,boost::geometry中有更好的方法可以删除重复项吗?

1 个答案:

答案 0 :(得分:1)

我认为您无法重复使用boost::geometry::index中的任何内容,因为(遗憾的是)即使nearest()查询的结果也未被排序。

所以,在这里,你最简单地接受它:

<强> Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
// #include <boost/range/adaptors.hpp>
#include <tuple>
#include <iostream>
#include <iomanip>

namespace bg  = boost::geometry;
namespace bgm = bg::model;

typedef bgm::point<double, 2, bg::cs::cartesian> point;
typedef bgm::box<point> box;

namespace std {
    // less
    template <> struct less<point> {
        bool operator()(point const& a, point const& b) const { return std::tie(a.get<0>(), a.get<1>()) < std::tie(b.get<0>(), b.get<1>()); }
    };
    template <> struct less<box> {
        bool operator()(box const& a, box const& b) const { return 
                std::tie(a.min_corner().get<0>(), a.min_corner().get<1>(), a.max_corner().get<0>(), a.max_corner().get<1>())
              < std::tie(b.min_corner().get<0>(), b.min_corner().get<1>(), b.max_corner().get<0>(), b.max_corner().get<1>()); }
    };
    // equal_to
    template <> struct equal_to<point> {
        bool operator()(point const& a, point const& b) const { return std::tie(a.get<0>(), a.get<1>()) == std::tie(b.get<0>(), b.get<1>()); }
    };
    template <> struct equal_to<box> {
        bool operator()(box const& a, box const& b) const { return 
                std::tie(a.min_corner().get<0>(), a.min_corner().get<1>(), a.max_corner().get<0>(), a.max_corner().get<1>())
             == std::tie(b.min_corner().get<0>(), b.min_corner().get<1>(), b.max_corner().get<0>(), b.max_corner().get<1>()); }
    };
}

struct Feature {
    box bounds;
    //... other members ...
    std::string other;

    friend std::ostream& operator<<(std::ostream& os, Feature const& f) {
        return os << "{" << bg::wkt(f.bounds) << ", " << std::quoted(f.other) << "}";
    }

    struct less_by_box : std::less<box> {
        bool operator()(Feature const& a, Feature const& b) const { return std::less<box>::operator()(a.bounds, b.bounds); }
    };
    struct by_box : std::equal_to<box> {
        bool operator()(Feature const& a, Feature const& b) const { return std::equal_to<box>::operator()(a.bounds, b.bounds); }
    };
};

int main() {
    std::vector<Feature> v {
        { box { {2,3}, {4,5} }, "three" },
        { box { {1,2}, {3,4} }, "two" },
        { box { {2,3}, {4,5} }, "one" },
    };

    auto dump = [&] {
        std::cout << "Size: " << v.size() << "\n";
        for (Feature const& f : v) { std::cout << f << "\n"; }
    };

    dump();

    std::cout << " -- Sort, Unique, Erase:\n";
    std::stable_sort(v.begin(), v.end(), Feature::less_by_box{});
    v.erase(std::unique(v.begin(), v.end(), Feature::by_box{}), v.end());

    dump();
}

打印

Size: 3
{POLYGON((2 3,2 5,4 5,4 3,2 3)), "three"}
{POLYGON((1 2,1 4,3 4,3 2,1 2)), "two"}
{POLYGON((2 3,2 5,4 5,4 3,2 3)), "one"}
 -- Sort, Unique, Erase:
Size: 2
{POLYGON((1 2,1 4,3 4,3 2,1 2)), "two"}
{POLYGON((2 3,2 5,4 5,4 3,2 3)), "three"}