通过自动分配增强几何图形并集
Boost geometry union with auto-allocation
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace boost::geometry;
class CustomPoint{
public:
double X;
double Y;
};
using cpPtr = boost::shared_ptr<CustomPoint>;
namespace boost { namespace geometry { namespace traits {
BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)
template<> struct access<cpPtr, 0> {
static inline double get(cpPtr const& p) { return p->X; }
static inline void set(cpPtr& p, double const& value) { p->X = value; }
};
template<> struct access<cpPtr, 1> {
static inline double get(cpPtr const& p) { return p->Y; }
static inline void set(cpPtr& p, double const& value) { p->Y = value; }
};
}}}
int main()
{
std::vector<cpPtr> one,Two;
//init polys
std::vector< std::vector<cpPtr> > output;
boost::geometry::union_(one,two,output)
}
您好,我尝试了提升::shared_ptr作为多边形。问题是当我进行联合裁剪时,算法没有分配内存。有人知道解决方案吗?
首先让我花时间说我对这种"尴尬"的点类型选择的动机感到困惑。
在我看来,
- 如果你的积分很少,那么分享似乎不是必需的
- 如果这样做,那么shared_ptr的开销(2 倍指针开销和原子引用计数锁定(似乎会阻止缩放。
为了两全其美(可以同时位于多个集合中的点(,您是否考虑过直接指针,甚至是 Boost 侵入性容器(不会拥有所包含元素的所有权(?
撇开所有问题不谈,这里有一种方法可以做到这一点:
您可以围绕shared_ptr做一个简单的包装器,允许您以这种方式使用它:
template<typename T>
struct shared_instancing : boost::shared_ptr<T> {
using boost::shared_ptr<T>::shared_ptr;
shared_instancing(boost::shared_ptr<T> sp = boost::make_shared<T>())
: boost::shared_ptr<T>(sp)
{ }
};
如您所见,它将默认构造为新实例;在科里鲁现场观看
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost::geometry;
struct CustomPoint{
double X;
double Y;
};
namespace {
template<typename T>
struct shared_instancing : boost::shared_ptr<T> {
using boost::shared_ptr<T>::shared_ptr;
shared_instancing(boost::shared_ptr<T> sp = boost::make_shared<T>())
: boost::shared_ptr<T>(sp)
{ }
};
}
using cpPtr = shared_instancing<CustomPoint>; // boost::shared_ptr<CustomPoint>;
namespace boost { namespace geometry { namespace traits {
BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)
template<> struct access<cpPtr, 0> {
static inline double get(cpPtr const& p) { return p->X; }
static inline void set(cpPtr& p, double const& value) { p->X = value; }
};
template<> struct access<cpPtr, 1> {
static inline double get(cpPtr const& p) { return p->Y; }
static inline void set(cpPtr& p, double const& value) { p->Y = value; }
};
}}}
int main()
{
typedef boost::geometry::model::polygon<cpPtr > polygon;
polygon green, blue;
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", green);
boost::geometry::read_wkt(
"POLYGON((4.0 -0.5 , 3.5 1.0 , 2.0 1.5 , 3.5 2.0 , 4.0 3.5 , 4.5 2.0 , 6.0 1.5 , 4.5 1.0 , 4.0 -0.5))", blue);
std::vector<polygon> output;
boost::geometry::union_(green, blue, output);
int i = 0;
std::cout << "green || blue:" << std::endl;
for(polygon const& p: output)
{
std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
}
}
相关文章:
- 无法将结构注册为增强几何体3D点
- 增强基于 XML class_id的反序列化
- 增强精神解析器规则以检测语句中的特殊结尾
- C++ 使用增强正则表达式库时断言崩溃
- 增强精神 X3:错误:在"..."中没有名为"大小"的类型
- 如何包装(撰写)增强 hana 地图并访问括号运算符(运算符 [])?
- 有没有办法在C ++中检测汉字?(使用增强)
- 如何将增强MSM与增强信号结合使用?
- 如何使用增强::几何计算多边形的旋转固体体积?
- 复制部分文件的C++/增强方式
- 增强 ASIO 和串行端口异步读取
- 增强 Hana index_if和类型
- 增强超几何测试
- 如何修改增强 hana 结构的成员
- 增强纤维work_stealing屏障会导致段错误
- 增强循环缓冲区push_back在前面插入数据
- 彩色增强::日志显示偏移的颜色
- 设置窗口几何图形特性的正确方法
- 增强序列化,按基类型加载存档类会产生错误的数据
- 通过自动分配增强几何图形并集