MRPT  1.9.9
TPixelLabelInfo.cpp
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 
10 #include "obs-precomp.h" // Precompiled headers
11 
15 
16 using namespace mrpt::obs;
18 
19 // Define here, not in the .h to allow the vtable to be created here.
21 
23 {
24  const uint8_t version = 1; // for possible future changes.
25  out << version;
26  // 1st: Save number MAX_NUM_DIFFERENT_LABELS so we can reconstruct the
27  // object in the class factory later on.
28  out << BITFIELD_BYTES;
29  // 2nd: data-specific serialization:
30  this->internal_writeToStream(out);
31 }
32 
33 template <unsigned int BYTES_REQUIRED_>
36 {
37  {
38  uint32_t nR, nC;
39  in >> nR >> nC;
40  pixelLabels.resize(nR, nC);
41  for (uint32_t c = 0; c < nC; c++)
42  for (uint32_t r = 0; r < nR; r++) in >> pixelLabels.coeffRef(r, c);
43  }
44  in >> pixelLabelNames;
45 }
46 template <unsigned int BYTES_REQUIRED_>
49 {
50  {
51  const auto nR = static_cast<uint32_t>(pixelLabels.rows());
52  const auto nC = static_cast<uint32_t>(pixelLabels.cols());
53  out << nR << nC;
54  for (uint32_t c = 0; c < nC; c++)
55  for (uint32_t r = 0; r < nR; r++) out << pixelLabels.coeff(r, c);
56  }
57  out << pixelLabelNames;
58 }
59 
60 // Deserialization and class factory. All in one, ladies and gentlemen
63 {
64  uint8_t version;
65  in >> version;
66 
67  switch (version)
68  {
69  case 1:
70  {
71  // 1st: Read NUM BYTES
72  uint8_t bitfield_bytes;
73  in >> bitfield_bytes;
74 
75  // Hand-made class factory. May be a good solution if there will be
76  // not too many different classes:
77  TPixelLabelInfoBase* new_obj = nullptr;
78  switch (bitfield_bytes)
79  {
80  case 1:
81  new_obj = new TPixelLabelInfo<1>();
82  break;
83  case 2:
84  new_obj = new TPixelLabelInfo<2>();
85  break;
86  case 3:
87  case 4:
88  new_obj = new TPixelLabelInfo<4>();
89  break;
90  case 5:
91  case 6:
92  case 7:
93  case 8:
94  new_obj = new TPixelLabelInfo<8>();
95  break;
96  default:
97  throw std::runtime_error(
98  "Unknown type of pixelLabel inner class while "
99  "deserializing!");
100  };
101  // 2nd: data-specific serialization:
102  new_obj->internal_readFromStream(in);
103 
104  return new_obj;
105  }
106  break;
107 
108  default:
110  break;
111  };
112 }
113 
114 template <unsigned int BYTES_REQUIRED_>
116 {
117  {
118  const auto nR = static_cast<uint32_t>(pixelLabels.rows());
119  const auto nC = static_cast<uint32_t>(pixelLabels.cols());
120  out << "Number of rows: " << nR << "\n";
121  out << "Number of cols: " << nC << "\n";
122  out << "Matrix of labels:\n";
123  for (uint32_t c = 0; c < nC; c++)
124  {
125  for (uint32_t r = 0; r < nR; r++)
126  out << pixelLabels.coeff(r, c) << " ";
127 
128  out << std::endl;
129  }
130  }
131  out << std::endl;
132  out << "Label indices and names: " << std::endl;
133  std::map<uint32_t, std::string>::const_iterator it;
134  for (it = pixelLabelNames.begin(); it != pixelLabelNames.end(); it++)
135  out << it->first << " " << it->second << std::endl;
136 }
137 
138 // Explicit instantiations:
139 template void TPixelLabelInfo<1>::Print(std::ostream& out) const;
140 template void TPixelLabelInfo<2>::Print(std::ostream& out) const;
141 template void TPixelLabelInfo<4>::Print(std::ostream& out) const;
142 template void TPixelLabelInfo<8>::Print(std::ostream& out) const;
Virtual interface to all pixel-label semantic information structs.
void writeToStream(mrpt::serialization::CArchive &out) const
void internal_writeToStream(mrpt::serialization::CArchive &out) const override
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)=0
void Print(std::ostream &out) const override
static TPixelLabelInfoBase * readAndBuildFromStream(mrpt::serialization::CArchive &in)
This namespace contains representation of robot actions and observations.
void internal_readFromStream(mrpt::serialization::CArchive &in) override
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
Pixel-wise semantic label struct.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020