YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
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
19using namespace yarp::math;
20
21namespace {
22YARP_LOG_COMPONENT(VEC2D, "yarp.math.Vec2D")
23}
24
25
28{
29public:
33};
35
36namespace yarp::math {
37template<>
39{
40 // auto-convert text mode interaction
41 connection.convertTextMode();
43 bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
44 if (!ok) {
45 return false;
46 }
47
48 if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_FLOAT64))
49 {
50 this->x = connection.expectFloat64();
51 this->y = connection.expectFloat64();
52 }
53 else
54 {
55 return false;
56 }
57
58 return !connection.isError();
59}
60
61template<>
63{
64 // auto-convert text mode interaction
65 connection.convertTextMode();
67 bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
68 if (!ok) {
69 return false;
70 }
71
72 if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_INT32))
73 {
74 this->x = connection.expectInt32();
75 this->y = connection.expectInt32();
76 }
77 else
78 {
79 return false;
80 }
81
82 return !connection.isError();
83}
84
85template<>
87{
88 // auto-convert text mode interaction
89 connection.convertTextMode();
91 bool ok = connection.expectBlock(reinterpret_cast<char*>(&header), sizeof(header));
92 if (!ok) {
93 return false;
94 }
95
96 if (header.listLen == 2 && header.listTag == (BOTTLE_TAG_LIST | BOTTLE_TAG_INT64))
97 {
98 this->x = connection.expectInt64();
99 this->y = connection.expectInt64();
100 }
101 else
102 {
103 return false;
104 }
105
106 return !connection.isError();
107}
108
109template<>
111{
113
115 header.listLen = 2;
116
117 connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
118
119 connection.appendFloat64(this->x);
120 connection.appendFloat64(this->y);
121
122 connection.convertTextMode();
123
124 return !connection.isError();
125}
126
127template<>
129{
131
133 header.listLen = 2;
134
135 connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
136
137 connection.appendInt32(this->x);
138 connection.appendInt32(this->y);
139
140 connection.convertTextMode();
141
142 return !connection.isError();
143}
144
145template<>
147{
149
151 header.listLen = 2;
152
153 connection.appendBlock(reinterpret_cast<char*>(&header), sizeof(header));
154
155 connection.appendInt64(this->x);
156 connection.appendInt64(this->y);
157
158 connection.convertTextMode();
159
160 return !connection.isError();
161}
162
163} // namespace yarp::math
164
165
166template <typename T>
167std::string yarp::math::Vec2D<T>::toString(int precision, int width) const
168{
169 std::ostringstream stringStream;
170 stringStream.precision(precision);
171 stringStream.width(width);
172 stringStream << std::string("x:") << x << std::string(" y:") << y;
173 return stringStream.str();
174}
175
176template <typename T>
178{
179 return T(sqrt(x*x + y*y));
180}
181
182//constructors
183template <typename T>
185{
186}
187
188template <typename T>
190{
191 yCAssert(VEC2D, v.size() == 2);
192 x = T(v[0]);
193 y = T(v[1]);
194}
195
196template <typename T>
197yarp::math::Vec2D<T>::Vec2D(const T& x_value, const T& y_value)
198{
199 x = x_value;
200 y = y_value;
201}
202
203template <typename T>
205{
206 yCAssert(VEC2D, lhs.rows() == 2 && lhs.cols() == 2);
207 T x = rhs.x; T y = rhs.y;
208 rhs.x = T(lhs[0][0] * x + lhs[0][1] * y);
209 rhs.y = T(lhs[1][0] * x + lhs[1][1] * y);
210 return rhs;
211}
212
213template <typename T>
215{
216 lhs += rhs;
217 return lhs;
218}
219
220template <typename T>
222{
223 lhs -= rhs;
224 return lhs;
225}
226
227template <typename T>
229{
230 this->x += rhs.x;
231 this->y += rhs.y;
232 return *this;
233}
234
235template <typename T>
237{
238 this->x -= rhs.x;
239 this->y -= rhs.y;
240 return *this;
241}
242
243template <typename T>
245{
246 if (this->x == rhs.x &&
247 this->y == rhs.y) {
248 return true;
249 }
250 return false;
251}
252
253template <typename T>
255{
256 if (this->x == rhs.x &&
257 this->y == rhs.y) {
258 return false;
259 }
260 return true;
261}
262
269
270template class yarp::math::Vec2D<double>;
271template class yarp::math::Vec2D<int>;
272template 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::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:167
yarp::math::Vec2D< T > & operator-=(const yarp::math::Vec2D< T > &rhs)
Definition Vec2D.cpp:236
bool operator==(const yarp::math::Vec2D< T > &rhs) const
Definition Vec2D.cpp:244
T norm() const
Returns the Euclidean norm of the Vec2D, i.e.
Definition Vec2D.cpp:177
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:254
yarp::math::Vec2D< T > & operator+=(const yarp::math::Vec2D< T > &rhs)
Definition Vec2D.cpp:228
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:39
size_t cols() const
Return number of columns.
Definition Matrix.h:94
size_t rows() const
Return number of rows.
Definition Matrix.h:88
size_t size() const
Definition Vector.h:341
#define yCAssert(component, x)
#define YARP_LOG_COMPONENT(name,...)
Vector operator+(const Vector &a, const double &s)
Mathematical operations.
Definition math.cpp:27
Vector operator-(const Vector &a, const double &s)
Subtraction operator between a vector and a scalar (defined in Math.h).
Definition math.cpp:82
Vector operator*(double k, const Vector &b)
Scalar-vector product operator (defined in Math.h).
Definition math.cpp:143
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition NetInt32.h:29
#define YARP_END_PACK
Ends 1 byte packing for structs/classes.
Definition system.h:193
#define YARP_BEGIN_PACK
Starts 1 byte packing for structs/classes.
Definition system.h:192
#define YARP_math_API
Definition api.h:17