升压多边形序列化 [英] Boost Polygon Serialization

查看:274
本文介绍了升压多边形序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压几何在我的项目,我需要序列化的多边形。我一直在使用升压序列化,而不对许多提升的数据​​类型的问题,但提振几何看来,目前不支持序列化,因为我无法找到序列文件夹内的任何头。

有没有什么知名的方法来实现这一目标?

感谢。

编辑:二进制序列化实例:升压多边形连载:环


解决方案

我同意,这是怪异的Boost.Geometry不支持Boost.Serialization。也许这是很难支撑的模板参数所有可能的组合,或者作为高铁总站已经提供他们没有理会。

目前至少在默认的容器类型的情况下,它是微不足道的添加这样的功能。下面的code实现它,功能齐全。

下面我假设你使用自定义类(QPoint从QT)为您的点类。 code其他点类型将是几乎完全相同我的。

首先,你添加一个序列化Point类:

 的#include< QPoint>#包括LT&;升压/几何/ geometry.hpp>
#包括LT&;升压/几何/几何/注册/ point.hpp>BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPoint,INT,CS ::笛卡尔,X,Y,setX的,塞蒂);
typedef的QPoint MyPoint;
名字空间boost {
命名空间序列{模板<类归档和GT;
无效连载(归档和放大器; AR,MyPoint&安培; PT,const的无符号整型版){
    AR&安培;升压::序列:: make_nvp(×,pt.rx());
    AR&安培;升压::序列:: make_nvp(Y,pt.ry());
}
} //命名空间序列化
} //命名空间提升

接下来,您的戒指和多边形添加序列化。在这里,我分别使用点和戒指的事实,即环和多边形实质上的std ::向量(S)和性病系列化:: vector的是内置的Boost.Serialization。

 的#include<升压/系列化/ vector.hpp>
#包括LT&;升压/几何/几何/ polygon.hpp>
#包括LT&;升压/几何/几何/ ring.hpp>//改变模板标记为你感到高兴,但环和多边形必须是
//对应w.r.t.以关闭和顺时针政策
TYPEDEF提振::几何模型:: ::环LT; MyPoint,假的,真> MyRing;
TYPEDEF提振::几何模型:: ::多边形< MyPoint,假的,真> MyPolygon;名字空间boost {
命名空间序列{//默认环LT; MyPoint>简单地从性病::矢量&lt继承; MyPoint>中
//它的序列化是微不足道的,不需要实施。//默认多边形< MyPoint>给予外环直达和内圈的容器
模板<类归档和GT;
无效连载(归档和放大器; AR,MyPolygon&安培;聚,const的无符号整型版){
    AR&安培;提高::系列化:: make_nvp(外,poly.outer());
    AR&安培;升压::序列:: make_nvp(胆,poly.inners());
}
} //命名空间序列化
} //命名空间提升

这就是它,你都做了,现在你可以使用MyPolygon与Boost.Serialize任何其他类:

 的#include<升压/存档/ xml_iarchive.hpp>
#包括LT&;升压/存档/ xml_oarchive.hpp>无效foo_out(的std :: ostream的和放大器; OSS,常量MyPolygon&安培;聚)
{
    提高::档案:: xml_oarchive OA(OSS);
    OA&安培; BOOST_SERIALIZATION_NVP(聚);
}无效foo_in(的std :: istream的&放大器; ISS,MyPolygon&安培;聚)
{
    提高::档案:: xml_iarchive IA(ISS);
    1A和; BOOST_SERIALIZATION_NVP(聚);
}

享受!

I am using boost geometry in my project, and I need to serialize polygons. I have been using boost serialization without problems for many boost data types, but boost geometry seems that currently does not support serialization, as I cannot find any header inside serialization folder.

Is there any well known method to achieve this?

Thanks.

EDIT: Binary Serialization Example in: Boost Polygon Serialization: Ring

解决方案

I agree, it is weird that Boost.Geometry does not support Boost.Serialization. Probably it is hard to support all possible combinations of template parameters, or maybe they did not bother as WKT is already provided.

At least in the case of "default" container types it is trivial to add such functionality. The code below implements it and is fully functional.

Below I assume you use a custom class (QPoint from Qt) as your point class. Code for other point types will be almost identical to mine.

First, you add a serialization for the Point class:

#include <QPoint>

#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>

BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(QPoint, int, cs::cartesian, x, y, setX, setY);
typedef QPoint MyPoint;


namespace  boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, MyPoint& pt, const unsigned int version) {
    ar &  boost::serialization::make_nvp("x", pt.rx() );
    ar &  boost::serialization::make_nvp("y", pt.ry() );
}
} //namespace serialization
} //namespace boost

Next you add serialization for Ring and Polygon. Here I use the fact that Ring and Polygon are essentially std::vector(s) of points and rings respectively and the serialization for std::vector is built-in in Boost.Serialization.

#include <boost/serialization/vector.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>

//change template flags as you are pleased but ring and polygon has to be
//in correspondence w.r.t. to "closed" and "clockwise" policies
typedef boost::geometry::model::ring<MyPoint, false, true> MyRing;
typedef boost::geometry::model::polygon<MyPoint, false, true> MyPolygon; 

namespace  boost {
namespace serialization {

//Default ring<MyPoint> simply inherits from std::vector<MyPoint>, 
//its serialization is trivial and no implementation is needed. 

//Default polygon<MyPoint> gives direct access to outer ring and the container of inner rings
template<class Archive>
void serialize(Archive & ar, MyPolygon& poly, const unsigned int version) {
    ar &  boost::serialization::make_nvp("outer", poly.outer());
    ar &  boost::serialization::make_nvp("inners", poly.inners());
}


} //namespace serialization
} //namespace boost

That's it, you are done, now you can use MyPolygon with Boost.Serialize as any other class:

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

void foo_out(std::ostream & oss, const MyPolygon & poly)
{
    boost::archive::xml_oarchive oa(oss);
    oa & BOOST_SERIALIZATION_NVP(poly);
}

void foo_in(std::istream & iss, MyPolygon & poly)
{
    boost::archive::xml_iarchive ia(iss);
    ia & BOOST_SERIALIZATION_NVP(poly);
}

Enjoy!

这篇关于升压多边形序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆