Main MRPT website > C++ reference for MRPT 1.9.9
CPolygon.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/math/CPolygon.h>
13 #include <mrpt/utils/CStream.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::math;
17 using namespace mrpt::utils;
18 using namespace std;
19 
20 // This must be added to any CSerializable class implementation file.
22 
23 /*---------------------------------------------------------------
24  Implements the writing to a CStream capability of
25  CSerializable objects
26  ---------------------------------------------------------------*/
27 void CPolygon::writeToStream(mrpt::utils::CStream& out, int* version) const
28 {
29  if (version)
30  *version = 2;
31  else
32  {
33  // The number of vertexs:
35 
36  // Size:
37  out << n;
38 
39  // Vertices:
40  if (n)
41  out.WriteBufferFixEndianness<double>(
42  (double*)&TPolygon2D::operator[](0), 2 * n);
43  }
44 }
45 
46 /*---------------------------------------------------------------
47  Implements the reading from a CStream capability of
48  CSerializable objects
49  ---------------------------------------------------------------*/
51 {
52  switch (version)
53  {
54  case 0: // floats
55  {
56  // The number of vertexs:
57  uint32_t i, n;
58  float f;
59 
60  // All elemental typed variables:
61  in >> n;
62  in >> f; // max_x=f;
63  in >> f; // max_y=f;
64  in >> f; // min_x=f;
65  in >> f; // min_y=f;
66  in >> f; // cx=f;
67  in >> f; // cy=f;
68 
69  TPolygon2D::resize(n);
70 
71  // The vertexs arrays:
72  for (i = 0; i < n; i++)
73  {
74  in >> f;
75  TPolygon2D::operator[](i).x = f;
76  }
77  for (i = 0; i < n; i++)
78  {
79  in >> f;
80  TPolygon2D::operator[](i).y = f;
81  }
82  }
83  break;
84 
85  case 1:
86  {
87  // The number of vertexs:
88  uint32_t n;
89  double dumm;
90 
91  // All elemental typed variables:
92  in >> n >> dumm >> dumm >> dumm >> dumm >> dumm >> dumm;
93  // max_x >> max_y >> min_x >> min_y >> cx >> cy;
94 
95  TPolygon2D::resize(n);
96 
97  // The vertexs arrays:
98  for (size_t i = 0; i < n; i++)
99  {
100  in >> dumm;
101  TPolygon2D::operator[](i).x = dumm;
102  }
103  for (size_t i = 0; i < n; i++)
104  {
105  in >> dumm;
106  TPolygon2D::operator[](i).y = dumm;
107  }
108  }
109  break;
110  case 2:
111  {
112  // The number of vertexs:
113  uint32_t n;
114 
115  // All elemental typed variables:
116  in >> n;
117 
118  TPolygon2D::resize(n);
119 
120  // The vertexs arrays:
121  if (n > 0)
122  in.ReadBufferFixEndianness<double>(
123  (double*)&TPolygon2D::operator[](0), 2 * n);
124  }
125  break;
126  default:
128  };
129 }
130 
131 /*---------------------------------------------------------------
132  Set all vertices at once, not to be used normally.
133  ---------------------------------------------------------------*/
135  const std::vector<double>& x, const std::vector<double>& y)
136 {
137  ASSERT_(x.size() == y.size() && !x.empty());
138  setAllVertices(x.size(), &x[0], &y[0]);
139 }
140 
141 /*---------------------------------------------------------------
142  Set all vertices at once, not to be used normally.
143  ---------------------------------------------------------------*/
145  size_t nVertices, const double* xs, const double* ys)
146 {
147  // Resize:
148  TPolygon2D::resize(nVertices);
149  for (size_t i = 0; i < nVertices; i++)
150  {
151  TPolygon2D::operator[](i).x = xs[i];
152  TPolygon2D::operator[](i).y = ys[i];
153  }
154 }
155 
156 /*---------------------------------------------------------------
157  Set all vertices at once, not to be used normally.
158  ---------------------------------------------------------------*/
160  size_t nVertices, const float* xs, const float* ys)
161 {
162  // Resize:
163  TPolygon2D::resize(nVertices);
164  for (size_t i = 0; i < nVertices; i++)
165  {
166  TPolygon2D::operator[](i).x = xs[i];
167  TPolygon2D::operator[](i).y = ys[i];
168  }
169 }
170 
172  std::vector<double>& x, std::vector<double>& y) const
173 {
174  // Resize:
175  const size_t n = TPolygon2D::size();
176  x.resize(n);
177  y.resize(n);
178  for (size_t i = 0; i < n; i++)
179  {
180  x[i] = TPolygon2D::operator[](i).x;
181  y[i] = TPolygon2D::operator[](i).y;
182  }
183 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLenum GLsizei n
Definition: glext.h:5074
void setAllVertices(const std::vector< double > &x, const std::vector< double > &y)
Set all vertices at once.
Definition: CPolygon.cpp:134
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:22
STL namespace.
void getAllVertices(std::vector< double > &x, std::vector< double > &y) const
Get all vertices at once.
Definition: CPolygon.cpp:171
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
Definition: CPolygon.cpp:50
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
VALUE & operator[](const KEY &key)
Write/read via [i] operator, that creates an element if it didn&#39;t exist already.
Definition: ts_hash_map.h:200
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
GLenum GLint GLint y
Definition: glext.h:3538
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLsizeiptr size
Definition: glext.h:3923
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019