YARP
Yet Another Robot Platform
Vec2D.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/math/Vec2D.h>
7 
10 #include <yarp/os/LogComponent.h>
11 #include <yarp/math/Math.h>
12 #include <sstream>
13 #include <cmath>
14 #include <cstdio>
15 
16 // network stuff
17 #include <yarp/os/NetInt32.h>
18 
19 using namespace yarp::math;
20 
21 namespace {
22 YARP_LOG_COMPONENT(VEC2D, "yarp.math.Vec2D")
23 }
24 
25 
28 {
29 public:
30  yarp::os::NetInt32 listTag{0};
31  yarp::os::NetInt32 listLen{0};
33 };
35 
36 namespace yarp {
37 namespace math {
38 template<>
40 {
41  // auto-convert text mode interaction
42  connection.convertTextMode();
44  bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
45  if (!ok) {
46  return false;
47  }
48 
49  if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_FLOAT64))
50  {
51  this->x = connection.expectFloat64();
52  this->y = connection.expectFloat64();
53  }
54  else
55  {
56  return false;
57  }
58 
59  return !connection.isError();
60 }
61 
62 template<>
64 {
65  // auto-convert text mode interaction
66  connection.convertTextMode();
68  bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
69  if (!ok) {
70  return false;
71  }
72 
73  if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_INT32))
74  {
75  this->x = connection.expectInt32();
76  this->y = connection.expectInt32();
77  }
78  else
79  {
80  return false;
81  }
82 
83  return !connection.isError();
84 }
85 
86 template<>
88 {
89  // auto-convert text mode interaction
90  connection.convertTextMode();
92  bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
93  if (!ok) {
94  return false;
95  }
96 
97  if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_INT64))
98  {
99  this->x = connection.expectInt64();
100  this->y = connection.expectInt64();
101  }
102  else
103  {
104  return false;
105  }
106 
107  return !connection.isError();
108 }
109 
110 template<>
112 {
113  Vec2DPortContentHeader header;
114 
116  header.listLen = 2;
117 
118  connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
119 
120  connection.appendFloat64(this->x);
121  connection.appendFloat64(this->y);
122 
123  connection.convertTextMode();
124 
125  return !connection.isError();
126 }
127 
128 template<>
130 {
131  Vec2DPortContentHeader header;
132 
134  header.listLen = 2;
135 
136  connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
137 
138  connection.appendInt32(this->x);
139  connection.appendInt32(this->y);
140 
141  connection.convertTextMode();
142 
143  return !connection.isError();
144 }
145 
146 template<>
148 {
149  Vec2DPortContentHeader header;
150 
152  header.listLen = 2;
153 
154  connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
155 
156  connection.appendInt64(this->x);
157  connection.appendInt64(this->y);
158 
159  connection.convertTextMode();
160 
161  return !connection.isError();
162 }
163 
164 } // namespace math
165 } // namespace yarp
166 
167 
168 template <typename T>
169 std::string yarp::math::Vec2D<T>::toString(int precision, int width) const
170 {
171  std::ostringstream stringStream;
172  stringStream.precision(precision);
173  stringStream.width(width);
174  stringStream << std::string("x:") << x << std::string(" y:") << y;
175  return stringStream.str();
176 }
177 
178 template <typename T>
180 {
181  return T(sqrt(x*x + y*y));
182 }
183 
184 //constructors
185 template <typename T>
187 {
188 }
189 
190 template <typename T>
192 {
193  yCAssert(VEC2D, v.size() == 2);
194  x = T(v[0]);
195  y = T(v[1]);
196 }
197 
198 template <typename T>
199 yarp::math::Vec2D<T>::Vec2D(const T& x_value, const T& y_value)
200 {
201  x = x_value;
202  y = y_value;
203 }
204 
205 template <typename T>
207 {
208  yCAssert(VEC2D, lhs.rows() == 2 && lhs.cols() == 2);
209  T x = rhs.x; T y = rhs.y;
210  rhs.x = T(lhs[0][0] * x + lhs[0][1] * y);
211  rhs.y = T(lhs[1][0] * x + lhs[1][1] * y);
212  return rhs;
213 }
214 
215 template <typename T>
217 {
218  lhs += rhs;
219  return lhs;
220 }
221 
222 template <typename T>
224 {
225  lhs -= rhs;
226  return lhs;
227 }
228 
229 template <typename T>
231 {
232  this->x += rhs.x;
233  this->y += rhs.y;
234  return *this;
235 }
236 
237 template <typename T>
239 {
240  this->x -= rhs.x;
241  this->y -= rhs.y;
242  return *this;
243 }
244 
245 template <typename T>
247 {
248  if (this->x == rhs.x &&
249  this->y == rhs.y) {
250  return true;
251  }
252  return false;
253 }
254 
255 template <typename T>
257 {
258  if (this->x == rhs.x &&
259  this->y == rhs.y) {
260  return false;
261  }
262  return true;
263 }
264 
271 
272 template class yarp::math::Vec2D<double>;
273 template class yarp::math::Vec2D<int>;
274 template class yarp::math::Vec2D<size_t>;
#define BOTTLE_TAG_FLOAT64
Definition: Bottle.h:25
#define BOTTLE_TAG_INT64
Definition: Bottle.h:22
#define BOTTLE_TAG_INT32
Definition: Bottle.h:21
#define BOTTLE_TAG_LIST
Definition: Bottle.h:28
yarp::math::Vec2D< T > operator*(const yarp::sig::Matrix &lhs, yarp::math::Vec2D< T > rhs)
Definition: Vec2D.cpp:206
yarp::math::Vec2D< T > operator-(yarp::math::Vec2D< T > lhs, const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:223
yarp::math::Vec2D< T > operator+(yarp::math::Vec2D< T > lhs, const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:216
yarp::os::NetInt32 listLen
Definition: Vec2D.cpp:31
yarp::os::NetInt32 listTag
Definition: Vec2D.cpp:30
Vec2DPortContentHeader()=default
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition: Vec2D.cpp:169
yarp::math::Vec2D< T > & operator-=(const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:238
bool operator==(const yarp::math::Vec2D< T > &rhs) const
Definition: Vec2D.cpp:246
T norm() const
Returns the Euclidean norm of the Vec2D, i.e.
Definition: Vec2D.cpp:179
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
bool operator!=(const yarp::math::Vec2D< T > &rhs) const
Definition: Vec2D.cpp:256
yarp::math::Vec2D< T > & operator+=(const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:230
An interface for reading from a network connection.
virtual bool expectBlock(char *data, size_t len)=0
Read a block of data from the network connection.
virtual std::int32_t expectInt32()=0
Read a 32-bit integer from the network connection.
virtual bool convertTextMode()=0
Reads in a standard description in text mode, and converts it to a standard description in binary.
virtual std::int64_t expectInt64()=0
Read a 64-bit integer from the network connection.
virtual bool isError() const =0
virtual yarp::conf::float64_t expectFloat64()=0
Read a 64-bit floating point number from the network connection.
An interface for writing to a network connection.
virtual bool isError() const =0
virtual void appendInt64(std::int64_t data)=0
Send a representation of a 64-bit integer to the network connection.
virtual bool convertTextMode()=0
Converts a standard description in binary into a textual description, if the connection is in text-mo...
virtual void appendInt32(std::int32_t data)=0
Send a representation of a 32-bit integer to the network connection.
virtual void appendFloat64(yarp::conf::float64_t data)=0
Send a representation of a 64-bit floating point number to the network connection.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
A class for a Matrix.
Definition: Matrix.h:43
size_t cols() const
Return number of columns.
Definition: Matrix.h:98
size_t rows() const
Return number of rows.
Definition: Matrix.h:92
size_t size() const
Definition: Vector.h:323
#define yCAssert(component, x)
Definition: LogComponent.h:169
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:30
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition: system.h:191
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition: system.h:190
#define YARP_math_API
Definition: api.h:17