MRPT  2.0.1
matrix_serialization.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/CMatrixD.h>
13 #include <mrpt/math/CMatrixF.h>
14 #include <mrpt/math/CMatrixFixed.h>
16 
17 /** \file matrix_serialization.h
18  * This file implements matrix/vector text and binary serialization */
19 namespace mrpt::math
20 {
21 /** \addtogroup container_ops_grp
22  * @{ */
23 
24 /** @name Operators for binary streaming of MRPT matrices
25  @{ */
26 
27 /** Read operator from a CStream. The format is compatible with that of CMatrixF
28  * & CMatrixD */
29 template <size_t NROWS, size_t NCOLS>
32 {
33  CMatrixF aux;
34  in.ReadObject(&aux);
35  ASSERTMSG_(
36  M.cols() == aux.cols() && M.rows() == aux.rows(),
37  format(
38  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
39  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
40  (unsigned)NCOLS));
41  M = aux;
42  return in;
43 }
44 /** Read operator from a CStream. The format is compatible with that of CMatrixF
45  * & CMatrixD */
46 template <size_t NROWS, size_t NCOLS>
49 {
50  CMatrixD aux;
51  in.ReadObject(&aux);
52  ASSERTMSG_(
53  M.cols() == aux.cols() && M.rows() == aux.rows(),
54  format(
55  "Size mismatch: deserialized is %ux%u, expected is %ux%u",
56  (unsigned)aux.rows(), (unsigned)aux.cols(), (unsigned)NROWS,
57  (unsigned)NCOLS));
58  M = aux;
59  return in;
60 }
61 
62 /** Write operator for writing into a CStream. The format is compatible with
63  * that of CMatrixF & CMatrixD */
64 template <size_t NROWS, size_t NCOLS>
68 {
69  CMatrixF aux;
70  aux = M;
71  out.WriteObject(&aux);
72  return out;
73 }
74 /** Write operator for writing into a CStream. The format is compatible with
75  * that of CMatrixF & CMatrixD */
76 template <size_t NROWS, size_t NCOLS>
80 {
81  CMatrixD aux;
82  aux = M;
83  out.WriteObject(&aux);
84  return out;
85 }
86 
87 /** @} */ // end MRPT matrices stream operators
88 
89 /** @name Operators for text streaming of MRPT matrices
90  @{ */
91 
92 /** Binary serialization of symmetric matrices, saving the space of duplicated
93  * values. \sa serializeSymmetricMatrixTo() */
94 template <typename MAT>
96 {
97  ASSERT_EQUAL_(m.rows(), m.cols());
98  auto N = m.cols();
99  for (decltype(N) i = 0; i < N; i++) in >> m(i, i);
100  for (decltype(N) r = 0; r < N - 1; r++)
101  {
102  for (decltype(N) c = r + 1; c < N; c++)
103  {
104  typename MAT::Scalar x;
105  in >> x;
106  m(r, c) = m(c, r) = x;
107  }
108  }
109 }
110 
111 /** Binary serialization of symmetric matrices, saving the space of duplicated
112  * values. \sa deserializeSymmetricMatrixFrom() */
113 template <typename MAT>
115 {
116  ASSERT_EQUAL_(m.rows(), m.cols());
117  auto N = m.cols();
118  for (decltype(N) i = 0; i < N; i++) out << m(i, i);
119  for (decltype(N) r = 0; r < N - 1; r++)
120  for (decltype(N) c = r + 1; c < N; c++) out << m(r, c);
121 }
122 
123 /** @} */ // end MRPT matrices stream operators
124 
125 /** @} */ // end of grouping
126 } // namespace mrpt::math
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
void serializeSymmetricMatrixTo(MAT &m, mrpt::serialization::CArchive &out)
Binary serialization of symmetric matrices, saving the space of duplicated values.
double Scalar
Definition: KmUtils.h:43
This class is a "CSerializable" wrapper for "CMatrixDynamic<double>".
Definition: CMatrixD.h:23
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
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:137
void deserializeSymmetricMatrixFrom(MAT &m, mrpt::serialization::CArchive &in)
Binary serialization of symmetric matrices, saving the space of duplicated values.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
size_type rows() const
Number of rows in the matrix.
size_type cols() const
Number of columns in the matrix.
constexpr size_type rows() const
Number of rows in the matrix.
Definition: CMatrixFixed.h:227
This class is a "CSerializable" wrapper for "CMatrixFloat".
Definition: CMatrixF.h:22
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:626
CSerializable::Ptr ReadObject()
Reads an object from stream, its class determined at runtime, and returns a smart pointer to the obje...
Definition: CArchive.h:178
constexpr size_type cols() const
Number of columns in the matrix.
Definition: CMatrixFixed.h:230



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020