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



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019