如何将 boost::geometry 多边形转换为 STL 对象?
我相信这一定很简单,因为文档中没有任何示例。然而,我已经花费了大约四个工作日来尝试完成这一微小的事情。虽然我是 R 的长期程序员,但是这些小数据转换问题让我发狂。
是的,有一个问题的标题与我的类似:从 Boost Geometry 多边形获取点坐标。
但是,代码非常复杂(并且发布者改变了它很多次),我无法理解,其他 C++ 初学者也可能无法理解。
这是一个简单的示例,应该适用于其他 boost::geometry 数据类型,因此希望任何人都能理解。
我相信这一定很简单,因为文档中没有任何示例。然而,我已经花费了大约四个工作日来尝试完成这一微小的事情。虽然我是 R 的长期程序员,但是这些小数据转换问题让我发狂。
是的,有一个问题的标题与我的类似:从 Boost Geometry 多边形获取点坐标。
但是,代码非常复杂(并且发布者改变了它很多次),我无法理解,其他 C++ 初学者也可能无法理解。
这是一个简单的示例,应该适用于其他 boost::geometry 数据类型,因此希望任何人都能理解。
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
//One thing I tried is a function to use with `for_each_point()` so I set that up first.
template <typename Point>
void get_coordinates(Point const& p)
{
using boost::geometry::get;
std::cout << get<0>(p) get<1>(p) << std::endl;
}
int main()
{
typedef boost::tuple<double, double> point;
typedef boost::geometry::model::polygon<point> polygon;
polygon poly;
boost::geometry::read_wkt("polygon((2.0 1.3, 2.4 1.7, 2.8 1.8, 3.4 1.2, 3.7 1.6, 3.4 2.0, 4.1 3.0, 5.3 2.6, 5.4 1.2, 4.9 0.8, 2.9 0.7, 2.0 1.3))", poly);
polygon hull;
boost::geometry::convex_hull(poly, hull);
// Now I know I can `dsv()` and print to the screen like this:
using boost::geometry::dsv;
std::cout
<< "hull: " << dsv(hull) << std::endl;
// And/Or I can use my function with for_each_point()
boost::geometry::for_each_point(hull, get_coordinates<point>);
return 0;
}
如何将这些坐标放入STL容器中?我希望有两个std::vector,一个是x轴坐标,一个是y轴坐标,但是任何容器都可以。
exterior_ring()
来访问std::vector的?double vectSize = boost::geometry::exterior_ring(hull).size(); //返回8
但尝试访问元素是不起作用的:double element1 = boost::geometry::exterior_ring(hull)[0];
错误:无法将 'boost::tuples::tuple<double,double,...>' 转换为 'double' 在初始化中 - politicalEconomist