增强::几何体如何将多边形添加到多重多边形中
Boost::geometry How do you add polygons to a multi_polygon
我想通过在循环中添加boost::geometry::model::polygon
s来创建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_back
、emplace_back
、insert
等。分配可能也很有趣。你甚至可以使用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);
}
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- 计算缩放多边形的比例,得到给定的多边形面积
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 添加带有提升的多边形::p奥利贡不编译?
- 向二维多边形添加点
- 增强::几何体如何将多边形添加到多重多边形中