c++ - Boost::geometry 如何将多边形添加到 multi_polygon

标签 c++ boost boost-geometry

我想通过在循环中向其添加 boost::geometry::model::polygon 来创建一个 boost::geometry::model::multi_polygon .我查看了 boost 的示例和文档,但不清楚如何操作。这是我的代码

typedef boost::geometry::model::d2::point_xy<double> point_xy;
typedef boost::geometry::model::polygon<point_xy> polygon_type;
typedef boost::geometry::model::multi_polygon<polygon_type> multi_polygon_type;

    // Calculate centroid
    multi_polygon_type polygons;

    Q_FOREACH( QGraphicsItem* graphicsItem, allItemsInScene )
    {
        // Make a polygon for each graphics item
        polygon_type poly;

        // Find bounding box surrounding item and create boost compatible points from it
        QRectF boundingBox = graphicsItem->boundingRect();

        std::vector< point_xy > pointList; // Store points in vector so we can assign them to a polygon

        point_xy topLeft( boundingBox.topLeft().x(), boundingBox.topLeft().y() );
        pointList.push_back( topLeft );

        point_xy topRight( boundingBox.topRight().x(), boundingBox.topRight().y() );
        pointList.push_back( topRight );

        point_xy bottomRight( boundingBox.bottomRight().x(), boundingBox.bottomRight().y() );
        pointList.push_back( bottomRight );

        point_xy bottomLeft( boundingBox.bottomLeft().x(), boundingBox.bottomLeft().y() );
        pointList.push_back( bottomLeft );

        // assign points to polygon
        boost::geometry::assign_points( poly, pointList );

        // Add polygon to multi-polygon
        boost::geometry::append( polygons, poly ); // DOESN'T WORK
    }

最佳答案

多边形概念的默认模型只是一个多边形 vector

因此,您可以使用 push_backemplace_backinsert 等。Assign 可能也很有趣。您甚至可以使用 transform(..,..,..,back_inserter(polygons))。有创意:)

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

typedef boost::geometry::model::d2::point_xy<double> point_xy;
typedef boost::geometry::model::polygon<point_xy> polygon_type;
typedef boost::geometry::model::multi_polygon<polygon_type> multi_polygon_type;

struct QPoint {
    double x() const { return 3; }
    double y() const { return 4; }
};

struct QRectF {
    QPoint bottomLeft()  const  { return { }; }
    QPoint topLeft()     const  { return { }; }
    QPoint topRight()    const  { return { }; }
    QPoint bottomRight() const  { return { }; }
};                                         
struct QGraphicsItem {                    
    QRectF boundingRect() const { return { }; };
};

int main() {

    std::vector<QGraphicsItem*> allItemsInScene;
    // Calculate centroid
    multi_polygon_type polygons;

    for(QGraphicsItem* graphicsItem : allItemsInScene)
    {
        // Make a polygon for each graphics item
        polygon_type poly;

        // Find bounding box surrounding item and create boost compatible points from it
        QRectF boundingBox = graphicsItem->boundingRect();

        std::vector< point_xy > pointList; // Store points in vector so we can assign them to a polygon

        point_xy topLeft( boundingBox.topLeft().x(), boundingBox.topLeft().y() );
        pointList.push_back( topLeft );

        point_xy topRight( boundingBox.topRight().x(), boundingBox.topRight().y() );
        pointList.push_back( topRight );

        point_xy bottomRight( boundingBox.bottomRight().x(), boundingBox.bottomRight().y() );
        pointList.push_back( bottomRight );

        point_xy bottomLeft( boundingBox.bottomLeft().x(), boundingBox.bottomLeft().y() );
        pointList.push_back( bottomLeft );

        // assign points to polygon
        boost::geometry::assign_points( poly, pointList );

        // Add polygon to multi-polygon
        polygons.push_back(poly);
    }

}

关于c++ - Boost::geometry 如何将多边形添加到 multi_polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33699974/

相关文章:

C++ - 链表堆栈运算符重载函数

c++ - 我应该选择什么同步方案来避免实时 C++/MFC 应用程序上的死锁?

c++ - 由于抽象模板 arg 的实例化,boost::lambda 表达式无法编译。任何解释和/或解决方法?

c++ - Boost geometry 将 Point 2d 注册为共享指针

c++ - C++中Boost.Geometry中的多边形转换:直线的平移,旋转,反射

c# - OpenCV - 如何检测和测量两帧之间的角度?

c++ - odeint 禁止负值

c++ - std::make_pair 与 c++ 11

c++ - 为 boost::program_options 设置精度

c++ - boost 几何/空间查询形状