gpt4 book ai didi

c++ - boost 多边形序列化 : Ring

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:46 66 4
gpt4 key购买 nike

根据这个相关问题(Boost Polygon Serialization)。我正在尝试使用 Boost 序列化多边形。我现在遇到的问题是,我正在尝试使用自定义 X、Y、点的多边形来编译示例,但编译器在编译时抛出此错误:

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

就像没有定义任何函数来序列化一个环。由于 Ring 从 std::vector 扩展,并且如相关问题中所述,因此没有必要为其序列化定义方法。但是编译器会提示。

这里有一个关于定义多边形及其序列化的完整示例:

#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);
}

关于如何让它工作的任何想法?

编辑:关于多边形序列化的全功能代码

#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();
}

最佳答案

即使 std:vector<T> 存在序列化的部分特化这并不意味着它适用于子类,因此您必须为 ring 添加一个序列化方法。 :

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

那么实现中有什么?作为geometry不是为序列化而构建的,您无法访问对序列化有用的类型(例如,为容器选择正确的默认实现 ring 继承),因此您可以以某种方式强制执行此操作。例如,这似乎有效:

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);
}

你也可以尝试写一些基类序列化调用:

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));
}

但同样,问题是您应该能够从 ring 中访问该继承的类.事实上,它ring的定义中, 作为 base_type ,但它是类(class)私有(private)的。如果它是公开的,您可以使用 ring::base_type 编写还不错的代码作为序列化的参数(而不是上面的 std::vector<point>)。

也许了解序列化库的内部结构,您可以“绑定(bind)”序列化机制,这样就不需要两次调用,为 ring 专门化一些部分特化。类本身,但我怀疑这是否可移植。

关于c++ - boost 多边形序列化 : Ring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21438732/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com