增强::几何体如何将多边形添加到多重多边形中

Boost::geometry How do you add polygons to a multi_polygon

本文关键字:多边形 添加 几何体 增强      更新时间:2023-10-16

我想通过在循环中添加boost::geometry::model::polygons来创建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
    }

多多边形概念的默认模型只是多边形的向量

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

在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);
    }
}