c++ - 如何找到包含所有给定框的框?

标签 c++ boost boost-geometry

我有一系列 boost 几何框类型,希望找到包含所有这些对象的框的尺寸。

我注意到 boost geometry 提供了 encompass函数,它似乎可以满足我对一般几何概念的要求,但我不知道如何对一系列框执行此操作。

基本上我不得不自己动手,但我想知道是否可以简单地将一系列盒子变成“几何图形”,以便我可以简单地应用 envelope 函数.这是我目前写的:

// elsewhere in my code:
using Location = boost::geometry::model::d2::point_xy<double>;
using Box = boost::geometry::model::box<Location>;

// calculate the smallest box that fully encompasses all provided boxes            
template <template <typename...> class IterableContainer>                          
Box Envelope(const IterableContainer<Box>& sequence)                               
{                                                                                  
    Location minCorner(                                                            
        std::numeric_limits<double>::max(),                                        
        std::numeric_limits<double>::max());                                       

    Location maxCorner(                                                            
        std::numeric_limits<double>::lowest(),                                        
        std::numeric_limits<double>::lowest());                                       

    for (auto& box : sequence)                                                     
    {                                                                              
        if (box.min_corner().x() < minCorner.x())                                  
            minCorner.x() == box.min_corner().x();                                 

        if (box.min_corner().y() < minCorner.y())                                  
            minCorner.y() == box.min_corner().y();                                 

        if (box.max_corner().x() > maxCorner.x())                                  
            maxCorner.x() == box.max_corner().x();                                 

        if (box.max_corner().y() > maxCorner.y())                                  
            maxCorner.y() == box.max_corner().y();                                 
    }                                                                              

    return Box(minCorner, maxCorner);                                              
}                                                                                  

最佳答案

您要查找的函数称为 std::accumulate。您需要为其提供盒并集函数。

using Location = boost::geometry::model::d2::point_xy<double>;
using Box = boost::geometry::model::box<Location>;
double pinfi = std::numeric_limits<double>::max();
double ninfi = std::numeric_limits<double>::lowest();

Box u = std::accumulate(container.begin(), container.end(), 
                        Box(Location(pinfi,pinfi), Location(ninfi,ninfi)),
                        [](const Box& a, const Box& b) { 
                            return Box(
                                    Location(
                                     std::min(a.min_corner().x(),b.min_corner().x()),
                                     std::min(a.min_corner().y(),b.min_corner().y())),
                                    Location(
                                     std::max(a.max_corner().x(),b.max_corner().x()),
                                     std::min(a.max_corner().y(),b.max_corner().y())));

                        });

更新:这个函数的构建 block 已经存在于 boost::geometry 中。这是完整的测试代码:

template <typename T>
Box box_encompass (T beg, T end)
{
    return std::accumulate(beg, end, boost::geometry::make_inverse<Box>(),
                [](Box a, const Box& b) -> Box {
                    boost::geometry::expand(a,b);
                    return a;
                });
}

关于c++ - 如何找到包含所有给定框的框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27246311/

相关文章:

c++ - 如何使用循环清理此代码?

python - .build_release/lib/libcaffe.so : undefined reference to `boost::python::import(boost::python::str)'

c++ - 如何覆盖 boost::geometry::equals 算法中的浮点公差?

c++ - 在 CUDA 中添加时返回不正确的数字

c++ - Linux工具MPI调用导致瓶颈分析

c++ - 有没有办法将 boost::json::serializer 切换为美化输出?

c++ - 从 2D C 列表创建 boost.geometry.model.polygon

c++ - 盒子的 boost rtree 给出了与线段的错误交集

java - 计算几何 : find where the triangle is after rotation, 在镜子上的平移或反射

c++ - 使用 boost Spirit 解析为 STL vector