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

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

问题描述

根据此相关的问题(升压多边形系列化)。我试图用升压序列化的多边形。我现在的问题是,我试图编译使用自定义的X,Y,点的多边形的例子,但是编译器会引发在编译时这个错误:

 错误:'类boost ::几何模型:: ::环LT;提高::几何模型:: :: D2 :: point_xy<双> >'没有成员名为连载

好像没有定义任何功能,序列化环。由于环自std ::载体延伸,并且如在相关的问题指出,这是没有必要定义为其序列的方法。但是,编译器会抱怨。

在这里,我对多边形定义一个完整的例子,它的序列化:

 的#include<&的fstream GT;#包括LT&;升压/系列化/ vector.hpp>
#包括LT&;升压/系列化/ nvp.hpp>
#包括LT&;升压/系列化/ version.hpp>
#包括LT&;升压/系列化/ tracking.hpp>#包括LT&;升压/几何/ geometry.hpp>
#包括LT&;升压/几何/几何/ polygon.hpp>
#包括LT&;升压/几何/几何/ point_xy.hpp>
#包括LT&;升压/几何/ IO /唤醒/ wkt.hpp>
#包括LT&;升压/几何/多/几何/ multi_polygon.hpp>
#包括LT&;升压/几何/几何/ ring.hpp>#包括LT&;升压/存档/ xml_iarchive.hpp>
#包括LT&;升压/存档/ xml_oarchive.hpp>TYPEDEF提振::几何模型:: :: D2 :: point_xy<双>点;
TYPEDEF提振::几何模型:: ::环LT;点>环;
TYPEDEF提振::几何模型:: ::多边形<点>多边形;名字空间boost {
        命名空间序列{                模板<类归档和GT;
                内嵌无效连载(归档和放大器; AR,点和放大器;点,const的无符号整型FILE_VERSION)
                {
                        性病::法院LT&;< 点:序列化点<<的std :: ENDL;
                        AR&安培;升压::序列:: make_nvp(×,point.x());
                        AR&安培;升压::序列:: make_nvp(Y,point.y());
                }                模板<类归档和GT;
                内嵌无效连载(归档和放大器; AR,多边形& T公司,const的无符号整型FILE_VERSION)
                {
                        性病::法院LT&;< 多边形:序列化外环<<的std :: ENDL;
                        AR&安培;提高::系列化:: make_nvp(外,t.outer());                        性病::法院LT&;< 多边形:序列化内圈<<的std :: ENDL;
                        AR&安培;升压::序列:: make_nvp(胆,t.inners());
                }
        }
}使用空间boost ::几何形状;
使用空间boost ::存档;
使用命名空间std;诠释的main()
{
        多边形聚;
        追加(聚,使<点和GT(0.0,0.0));
        追加(聚,使<&点GT;(5.0,5.0));
        追加(聚,使<点和GT(5.0,0.0));
        正确的(多);        的ofstream OFS(polygon.xml);
        xml_oarchive OA(OFS);
        OA<< BOOST_SERIALIZATION_NVP(聚);
}

有关如何得到这个工作的任何想法?

编辑:全功能code约多边形系列化

 的#include<&的fstream GT;
#包括LT&;矢量>#包括LT&;升压/系列化/ vector.hpp>
#包括LT&;升压/系列化/ nvp.hpp>
#包括LT&;升压/系列化/ version.hpp>
#包括LT&;升压/系列化/ tracking.hpp>
#包括LT&;升压/ foreach.hpp>#包括LT&;升压/几何/ geometry.hpp>
#包括LT&;升压/几何/几何/ polygon.hpp>
#包括LT&;升压/几何/几何/ point_xy.hpp>
#包括LT&;升压/几何/几何/ ring.hpp>#包括LT&;升压/存档/ binary_oarchive.hpp>
#包括LT&;升压/存档/ binary_iarchive.hpp>TYPEDEF提振::几何模型:: :: D2 :: point_xy<双>点;
TYPEDEF提振::几何模型:: ::环LT;点>环;
TYPEDEF提振::几何模型:: ::多边形<点>多边形;名字空间boost {
        命名空间序列{                模板<类归档和GT;
                内嵌无效连载(归档和放大器; AR,点和放大器;点,const的无符号整型FILE_VERSION)
                {
                        性病::法院LT&;< 点:序列化点<<的std :: ENDL;
                        AR&安培; const_cast会<双&安培;>(point.x());
                        AR&安培; const_cast会<双&安培;>(point.y());
                }                模板<类归档和GT;
                内嵌无效连载(归档和放大器; AR,环放大器;环,const的无符号整型FILE_VERSION)
                {
                        性病::法院LT&;< 环:环序列化<<的std :: ENDL;
                        AR&安培;的static_cast<的std ::矢量<&点GT;&安培; >(环);
                }                模板<类归档和GT;
                内嵌无效连载(归档和放大器; AR,多边形& T公司,const的无符号整型FILE_VERSION)
                {
                        性病::法院LT&;< 多边形:序列化外环<<的std :: ENDL;
                        AR&安培; t.outer();                        性病::法院LT&;< 多边形:序列化内圈<<的std :: ENDL;
                        AR&安培; t.inners();
                }
        }
}使用空间boost ::几何形状;
使用空间boost ::存档;
使用命名空间std;诠释的main()
{
        多边形聚;
        追加(聚,使<点和GT(0.0,0.0));
        追加(聚,使<&点GT;(5.0,5.0));
        追加(聚,使<点和GT(5.0,0.0));
        正确的(多);        BOOST_FOREACH(点&安培; P,poly.outer())
        {
                性病::法院LT&;< 点<< p.x()&所述;&下; ,&所述;&下; p.y()&所述;&下;的std :: ENDL;
        }        的ofstream OFS(polygon.dat);
        binary_oarchive OA(OFS);
        OA<<聚;
        ofs.close();        多边形polyFromFile;
        ifstream的IFS(polygon.dat);
        binary_iarchive IA(IFS);
        IA>> polyFromFile;        BOOST_FOREACH(点&安培; P,polyFromFile.outer())
        {
                性病::法院LT&;< 点<< p.x()&所述;&下; ,&所述;&下; p.y()&所述;&下;的std :: ENDL;
        }
        ifs.close();
}


解决方案

有关serialzation即使在偏特存在 STD:矢量< T> 这并不意味着它会子类工作,所以你必须添加一个序列化方法为戒指

 模板<类归档和GT;
内嵌无效连载(归档和放大器; AR,环& T公司,const的无符号整型FILE_VERSION)
{
    //默认地将Impl
}

那么,在执行?由于几何未建被序列化,你不能访问类型将是序列化有用的(例如,以选择容器<$ C $正确的默认实现C>戒指继承),所以你可以强制使用某种方式。例如,这似乎工作:

 模板&LT;类归档和GT;
内嵌无效连载(归档和放大器; AR,环&amp; T公司,const的无符号整型FILE_VERSION)
{
     性病::法院LT&;&LT; 环:序列化环&LT;&LT;的std :: ENDL;
     序列化(AR,与的static_cast LT;的std ::矢量&lt;&点GT;&安培;&GT;(T),FILE_VERSION);
}

您也可以尝试写一些基础类的序列化的呼叫:

 模板&LT;类归档和GT;
内嵌无效连载(归档和放大器; AR,环&amp; T公司,const的无符号整型FILE_VERSION)
{
     性病::法院LT&;&LT; 环:序列化环&LT;&LT;的std :: ENDL;
     AR&安培;提高::系列化:: make_nvp(基地
              提高::系列化:: base_object&LT;的std ::矢量&lt;&点GT; &GT;(t))的;
}

但同样的问题是,你应该能够从访问戒指内的继承类。事实上,它的戒指,定义为 base_type 的,但它是私有的类。如果它是公共的,你可以用写没有那么糟糕code 环:: base_type 作为序列化(而不是裸的std ::矢量&lt;点方式&gt; 以上)

也许知道序列化库的胆可以绑序列化机制,使两个电话都没有neccessary,专门为戒指类本身的一些偏专业化,但我怀疑这将是便携式的。

According to this related question (Boost Polygon Serialization). I am trying to serialize polygons with Boost. The problem I have now is that I am trying to compile an example using polygons of custom X, Y, points, but the compiler throws this error at compile time:

error: 'class boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> >' has no member named 'serialize'

Like there is no defined any function to serialize a ring. Since a Ring extends from std::vector, and as stated in the related question, it is not necessary to define a method for its serialization. But the compiler complains.

Here I have a full example about defining polygons, and its serialization:

#include <fstream>

#include <boost/serialization/vector.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/tracking.hpp>

#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/ring.hpp>

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

typedef boost::geometry::model::d2::point_xy< double > point;
typedef boost::geometry::model::ring< point > ring;
typedef boost::geometry::model::polygon< point > polygon;

namespace boost{
        namespace serialization{

                template<class Archive>
                inline void serialize(Archive & ar, point &point, const unsigned int file_version)
                {
                        std::cout << "Point: Serializing point" << std::endl;
                        ar & boost::serialization::make_nvp("x", point.x());
                        ar & boost::serialization::make_nvp("y", point.y());
                }

                template<class Archive>
                inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
                {
                        std::cout << "Polygon: Serializing outer ring" << std::endl;
                        ar & boost::serialization::make_nvp("outer", t.outer());

                        std::cout << "Polygon: Serializing inner rings" << std::endl;
                        ar & boost::serialization::make_nvp("inners", t.inners());
                }
        }
}

using namespace boost::geometry;
using namespace boost::archive;
using namespace std;

int main()
{
        polygon poly;
        append(poly, make<point>(0.0, 0.0));
        append(poly, make<point>(5.0, 5.0));
        append(poly, make<point>(5.0, 0.0));
        correct(poly);

        ofstream ofs("polygon.xml");
        xml_oarchive oa(ofs);
        oa << BOOST_SERIALIZATION_NVP(poly);
}

Any idea about how to get this working?

EDIT: Fully functional code about polygon serialization

#include <fstream>
#include <vector>

#include <boost/serialization/vector.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/foreach.hpp>

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

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

typedef boost::geometry::model::d2::point_xy< double > point;
typedef boost::geometry::model::ring< point > ring;
typedef boost::geometry::model::polygon< point > polygon;

namespace boost{
        namespace serialization{

                template<class Archive>
                inline void serialize(Archive & ar, point &point, const unsigned int file_version)
                {
                        std::cout << "Point: Serializing point" << std::endl;
                        ar & const_cast<double &>(point.x());
                        ar & const_cast<double &>(point.y());
                }

                template<class Archive>
                inline void serialize(Archive & ar, ring &ring, const unsigned int file_version)
                {
                        std::cout << "Ring: Serializing ring" << std::endl;
                        ar & static_cast<std::vector<point>& >(ring);
                }

                template<class Archive>
                inline void serialize(Archive & ar, polygon &t, const unsigned int file_version)
                {
                        std::cout << "Polygon: Serializing outer ring" << std::endl;
                        ar & t.outer();

                        std::cout << "Polygon: Serializing inner rings" << std::endl;
                        ar & t.inners();
                }
        }
}

using namespace boost::geometry;
using namespace boost::archive;
using namespace std;

int main()
{
        polygon poly;
        append(poly, make<point>(0.0, 0.0));
        append(poly, make<point>(5.0, 5.0));
        append(poly, make<point>(5.0, 0.0));
        correct(poly);

        BOOST_FOREACH(point& p, poly.outer())
        {
                std::cout << "point " << p.x() << "," << p.y() << std::endl;
        }

        ofstream ofs("polygon.dat");
        binary_oarchive oa(ofs);
        oa << poly;
        ofs.close();

        polygon polyFromFile;
        ifstream ifs("polygon.dat");
        binary_iarchive ia(ifs);
        ia >> polyFromFile;

        BOOST_FOREACH(point& p, polyFromFile.outer())
        {
                std::cout << "point " << p.x() << "," << p.y() << std::endl;
        }
        ifs.close();
}

解决方案

Even when partial specialization for serialzation exists for std:vector<T> this does not imply it will work for subclasses, so you have to add a serialization method for ring:

template<class Archive>
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
    // Impl
}

So what's in the implementation? As geometry is not built to be serialized, you cannot access types that would be useful for the serialization (for example, to select the correct default implementation for the container ring inherits), so you can force this somehow. For example, this seems to work:

template<class Archive>
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
     std::cout << "Ring: Serializing a ring" << std::endl;
     serialize(ar, static_cast< std::vector<point>& >(t), file_version);
}

You can also try to write some base class serialization call:

template<class Archive>
inline void serialize(Archive & ar, ring &t, const unsigned int file_version)
{
     std::cout << "Ring: Serializing a ring" << std::endl;
     ar & boost::serialization::make_nvp( "Base",
              boost::serialization::base_object<std::vector<point> >(t));
}

but again, the problem is that you should be able to access that inherited class from within ring. In fact, it is in the definition of ring, as base_type, but it is private to the class. If it was public, you could write the not so bad code using ring::base_type as a parameter for the serialization (instead of the bare std::vector<point> above).

Maybe knowing the inners of the serialization library you can "tie" the serialization machinery so that two calls are not neccessary, specializing some partial specialization for the ring class itself, but I doubt this would be portable.

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

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