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 }
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1166
GLvoid *typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1745
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.
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
GLuint in
Definition: glew.h:7146
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
GLsizei n
Definition: glew.h:5051
#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't exist already.
Definition: ts_hash_map.h:123
GLint GLint GLint GLint GLint x
Definition: glew.h:1166
int version
Definition: mrpt_jpeglib.h:898
void getAllVertices(std::vector< double > &x, std::vector< double > &y) const
Get all vertices at once.
Definition: CPolygon.cpp:154
GLsizeiptr size
Definition: glew.h:1586
size_t ReadBufferFixEndianness(T *ptr, size_t ElementCount)
Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream s...
Definition: CStream.h:95
#define ASSERT_(f)
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
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f
Definition: glew.h:5092



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018