Main MRPT website > C++ reference for 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
21 {
22 namespace math
23 {
24 /** \addtogroup container_ops_grp
25  * @{ */
26 
27 /** @name Operators for binary streaming of MRPT matrices
28  @{ */
29 
30 /** Read operator from a CStream. The format is compatible with that of CMatrix
31  * & CMatrixD */
32 template <size_t NROWS, size_t NCOLS>
36 {
37  CMatrix aux;
38  in.ReadObject(&aux);
39  ASSERTMSG_(
40  M.cols() == aux.cols() && M.rows() == aux.rows(),
41  format(
42  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
43  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
44  (unsigned)NCOLS));
45  M = aux;
46  return in;
47 }
48 /** Read operator from a CStream. The format is compatible with that of CMatrix
49  * & CMatrixD */
50 template <size_t NROWS, size_t NCOLS>
54 {
55  CMatrixD aux;
56  in.ReadObject(&aux);
57  ASSERTMSG_(
58  M.cols() == aux.cols() && M.rows() == aux.rows(),
59  format(
60  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
61  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
62  (unsigned)NCOLS));
63  M = aux;
64  return in;
65 }
66 
67 /** Write operator for writing into a CStream. The format is compatible with
68  * that of CMatrix & CMatrixD */
69 template <size_t NROWS, size_t NCOLS>
73 {
74  CMatrix aux = CMatrixFloat(M);
75  out.WriteObject(&aux);
76  return out;
77 }
78 /** Write operator for writing into a CStream. The format is compatible with
79  * that of CMatrix & CMatrixD */
80 template <size_t NROWS, size_t NCOLS>
84 {
85  CMatrixD aux = CMatrixDouble(M);
86  out.WriteObject(&aux);
87  return out;
88 }
89 
90 /** @} */ // end MRPT matrices stream operators
91 
92 /** @name Operators for text streaming of MRPT matrices
93  @{ */
94 
95 /** Dumps the matrix to a text ostream, adding a final "\n" to Eigen's default
96  * output. */
97 template <typename T, size_t NROWS, size_t NCOLS>
98 inline std::ostream& operator<<(
99  std::ostream& s, const CMatrixFixedNumeric<T, NROWS, NCOLS>& m)
100 {
101  Eigen::IOFormat fmt;
102  fmt.matSuffix = "\n";
103  return s << m.format(fmt);
104 }
105 
106 /** Dumps the matrix to a text ostream, adding a final "\n" to Eigen's default
107  * output. */
108 template <typename T>
109 inline std::ostream& operator<<(
110  std::ostream& s, const CMatrixTemplateNumeric<T>& m)
111 {
112  Eigen::IOFormat fmt;
113  fmt.matSuffix = "\n";
114  return s << m.format(fmt);
115 }
116 
117 /** Binary serialization of symmetric matrices, saving the space of duplicated
118  * values. \sa serializeSymmetricMatrixTo() */
119 template <typename MAT>
121 {
122  ASSERT_EQUAL_(m.rows(), m.cols());
123  auto N = m.cols();
124  for (decltype(N) i = 0; i < N; i++) in >> m(i, i);
125  for (decltype(N) r = 0; r < N - 1; r++)
126  {
127  for (decltype(N) c = r + 1; c < N; c++)
128  {
129  typename MAT::Scalar x;
130  in >> x;
131  m(r, c) = m(c, r) = x;
132  }
133  }
134 }
135 
136 /** Binary serialization of symmetric matrices, saving the space of duplicated
137  * values. \sa deserializeSymmetricMatrixFrom() */
138 template <typename MAT>
140 {
141  ASSERT_EQUAL_(m.rows(), m.cols());
142  auto N = m.cols();
143  for (decltype(N) i = 0; i < N; i++) out << m(i, i);
144  for (decltype(N) r = 0; r < N - 1; r++)
145  for (decltype(N) c = r + 1; c < N; c++) out << m(r, c);
146 }
147 
148 /** @} */ // end MRPT matrices stream operators
149 
150 /** @} */ // end of grouping
151 } // namespace math
152 } // namespace mrpt
mrpt::math::CMatrixDouble
CMatrixTemplateNumeric< double > CMatrixDouble
Declares a matrix of double numbers (non serializable).
Definition: CMatrixTemplateNumeric.h:144
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
mrpt::math::CMatrixFloat
CMatrixTemplateNumeric< float > CMatrixFloat
Declares a matrix of float numbers (non serializable).
Definition: CMatrixTemplateNumeric.h:138
mrpt::math::operator<<
std::ostream & operator<<(std::ostream &o, const TPoint2D &p)
Definition: lightweight_geom_data.cpp:439
s
GLdouble s
Definition: glext.h:3676
CMatrixFixedNumeric.h
ASSERT_EQUAL_
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:153
c
const GLubyte * c
Definition: glext.h:6313
CMatrix.h
mrpt::math::serializeSymmetricMatrixTo
void serializeSymmetricMatrixTo(MAT &m, mrpt::serialization::CArchive &out)
Binary serialization of symmetric matrices, saving the space of duplicated values.
Definition: matrix_serialization.h:139
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
Scalar
double Scalar
Definition: KmUtils.h:44
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:37
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::math::operator>>
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrix::Ptr &pObj)
mrpt::math::CMatrix
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrix.h:24
CMatrixTemplateNumeric.h
mrpt::math::deserializeSymmetricMatrixFrom
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
Definition: matrix_serialization.h:120
CMatrixD.h
mrpt::math::CMatrixFixedNumeric
A numeric matrix of compile-time fixed size.
Definition: CMatrixFixedNumeric.h:40
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
mrpt::serialization::CArchive::WriteObject
void WriteObject(const CSerializable *o)
Writes an object to the stream.
Definition: CArchive.cpp:142
in
GLuint in
Definition: glext.h:7274
mrpt::math::CMatrixD
This class is a "CSerializable" wrapper for "CMatrixTemplateNumeric<double>".
Definition: CMatrixD.h:24
CArchive.h
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST