MRPT  1.9.9
matrix_serialization.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/math/CMatrix.h>
13 #include <mrpt/math/CMatrixD.h>
14 
17 
18 /** \file matrix_serialization.h
19  * This file implements matrix/vector text and binary serialization */
20 namespace mrpt::math
21 {
22 /** \addtogroup container_ops_grp
23  * @{ */
24 
25 /** @name Operators for binary streaming of MRPT matrices
26  @{ */
27 
28 /** Read operator from a CStream. The format is compatible with that of CMatrix
29  * & CMatrixD */
30 template <size_t NROWS, size_t NCOLS>
34 {
35  CMatrix aux;
36  in.ReadObject(&aux);
37  ASSERTMSG_(
38  M.cols() == aux.cols() && M.rows() == aux.rows(),
39  format(
40  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
41  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
42  (unsigned)NCOLS));
43  M = aux;
44  return in;
45 }
46 /** Read operator from a CStream. The format is compatible with that of CMatrix
47  * & CMatrixD */
48 template <size_t NROWS, size_t NCOLS>
52 {
53  CMatrixD aux;
54  in.ReadObject(&aux);
55  ASSERTMSG_(
56  M.cols() == aux.cols() && M.rows() == aux.rows(),
57  format(
58  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
59  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
60  (unsigned)NCOLS));
61  M = aux;
62  return in;
63 }
64 
65 /** Write operator for writing into a CStream. The format is compatible with
66  * that of CMatrix & CMatrixD */
67 template <size_t NROWS, size_t NCOLS>
71 {
72  CMatrix aux = CMatrixFloat(M);
73  out.WriteObject(&aux);
74  return out;
75 }
76 /** Write operator for writing into a CStream. The format is compatible with
77  * that of CMatrix & CMatrixD */
78 template <size_t NROWS, size_t NCOLS>
82 {
83  CMatrixD aux = CMatrixDouble(M);
84  out.WriteObject(&aux);
85  return out;
86 }
87 
88 /** @} */ // end MRPT matrices stream operators
89 
90 /** @name Operators for text streaming of MRPT matrices
91  @{ */
92 
93 /** Dumps the matrix to a text ostream, adding a final "\n" to Eigen's default
94  * output. */
95 template <typename T, size_t NROWS, size_t NCOLS>
96 inline std::ostream& operator<<(
97  std::ostream& s, const CMatrixFixedNumeric<T, NROWS, NCOLS>& m)
98 {
99  Eigen::IOFormat fmt;
100  fmt.matSuffix = "\n";
101  return s << m.format(fmt);
102 }
103 
104 /** Dumps the matrix to a text ostream, adding a final "\n" to Eigen's default
105  * output. */
106 template <typename T>
107 inline std::ostream& operator<<(
108  std::ostream& s, const CMatrixTemplateNumeric<T>& m)
109 {
110  Eigen::IOFormat fmt;
111  fmt.matSuffix = "\n";
112  return s << m.format(fmt);
113 }
114 
115 /** Binary serialization of symmetric matrices, saving the space of duplicated
116  * values. \sa serializeSymmetricMatrixTo() */
117 template <typename MAT>
119 {
120  ASSERT_EQUAL_(m.rows(), m.cols());
121  auto N = m.cols();
122  for (decltype(N) i = 0; i < N; i++) in >> m(i, i);
123  for (decltype(N) r = 0; r < N - 1; r++)
124  {
125  for (decltype(N) c = r + 1; c < N; c++)
126  {
127  typename MAT::Scalar x;
128  in >> x;
129  m(r, c) = m(c, r) = x;
130  }
131  }
132 }
133 
134 /** Binary serialization of symmetric matrices, saving the space of duplicated
135  * values. \sa deserializeSymmetricMatrixFrom() */
136 template <typename MAT>
138 {
139  ASSERT_EQUAL_(m.rows(), m.cols());
140  auto N = m.cols();
141  for (decltype(N) i = 0; i < N; i++) out << m(i, i);
142  for (decltype(N) r = 0; r < N - 1; r++)
143  for (decltype(N) c = r + 1; c < N; c++) out << m(r, c);
144 }
145 
146 /** @} */ // end MRPT matrices stream operators
147 
148 /** @} */ // end of grouping
149 }
150 
void serializeSymmetricMatrixTo(MAT &m, mrpt::serialization::CArchive &out)
Binary serialization of symmetric matrices, saving the space of duplicated values.
double Scalar
Definition: KmUtils.h:44
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Definition: CMatrixD.h:22
GLdouble s
Definition: glext.h:3676
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:153
std::ostream & operator<<(std::ostream &o, const TPoint2D &p)
const GLubyte * c
Definition: glext.h:6313
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLuint in
Definition: glext.h:7274
void WriteObject(const CSerializable *o)
Writes an object to the stream.
Definition: CArchive.cpp:154
GLenum GLint x
Definition: glext.h:3538
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:22
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrix::Ptr &pObj)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020