使用boost.serialization序列化本征矩阵 [英] serializing Eigen's Matrix using boost.serialization

查看:175
本文介绍了使用boost.serialization序列化本征矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化本征矩阵。这样我就可以序列化一个更复杂的对象。
我使用Matrix作为基类,并将序列化包含在派生类中。我对如何处理Matrix.data()感到困惑,该方法会返回C样式的数组(如果我正确的话)。
这是我的尝试:

I'm trying to serialize Eigen's matrix. So that I can serialize a more complex object. I'm using Matrix as a base class and include the serialization in the derived class. I'm confused on how to address Matrix.data(), which returns a c-style array (if i'm correct). This is my attempt:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

template < class TEigenMatrix>
class VariableType : public TEigenMatrix {
private:
  friend class boost::serialization::access;
  template<class Archive>
  void serialize(Archive & ar, const unsigned int version)
  {
      ar & this.data();
  }
public:
};

我想将其用作包装纸:

VariableType<Matrix<double,3,1>> serializableVector;

代替

Matrix<double,3,1> vector;


推荐答案

通过将以下免费函数放入编译单元,您可以有效地使Boost.Serialization知道如何序列化本征类型:

By placing the following free function into your compilation unit, you effectively make Boost.Serialization aware of how to serialize Eigen types:

namespace boost
{
    template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
    inline void serialize(
        Archive & ar, 
        Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & t, 
        const unsigned int file_version
    ) 
    {
        for(size_t i=0; i<t.size(); i++)
            ar & t.data()[i];
    }
}

在您给出的示例中,您应该能够要做(未试用):

In the example you gave, you should then be able to do (untested):

void serialize(Archive & ar, const unsigned int version)
{
    ar & *this;
}

看看我的先前的答案有关使用Boost.Serialization对本征类型进行序列化的详细示例。

Have a look at my previous answer on serialization of Eigen types using Boost.Serialization for a more detailed example.

这篇关于使用boost.serialization序列化本征矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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