YARP
Yet Another Robot Platform
Vec2D.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_MATH_VEC2D_H
10 #define YARP_MATH_VEC2D_H
11 
12 #include <yarp/math/api.h>
13 
14 #include <yarp/os/Portable.h>
15 
16 #include <yarp/sig/Matrix.h>
17 #include <yarp/sig/Vector.h>
18 
19 #include <type_traits>
20 
21 namespace yarp {
22 namespace math {
23 
24 template <typename T>
26 {
27  static_assert(std::is_same<size_t, T>::value || std::is_same<int, T>::value || std::is_same<double, T>::value, "Vec2D can be specialized only as size_t, int, double");
28 
29 public:
30  T x;
31  T y;
32 
33 public:
34  Vec2D<T>();
35  Vec2D<T>(const T& x_value, const T& y_value);
36  Vec2D<T>(const yarp::sig::Vector& v);
37  explicit operator yarp::sig::Vector() const
38  {
39  yarp::sig::Vector v(2);
40  v[0] = double(x);
41  v[1] = double(y);
42  return v;
43  }
44 
49  T norm() const;
50 
55  std::string toString(int precision = -1, int width = -1) const;
56 
58  /*
59  * Read vector from a connection.
60  * return true if a Vec2D was read correctly
61  */
62  bool read(yarp::os::ConnectionReader& connection) override;
63 
68  bool write(yarp::os::ConnectionWriter& connection) const override;
69 
70  yarp::os::Type getType() const override
71  {
72  return yarp::os::Type::byName("yarp/vec2D");
73  }
74 
75  //operators
78  bool operator==(const yarp::math::Vec2D<T>& rhs) const;
79  bool operator!=(const yarp::math::Vec2D<T>& rhs) const;
80 };
81 
82 } // namespace math
83 } // namespace yarp
84 
85 //operators
86 template <typename T>
88 
89 template <typename T>
91 
92 template <typename T>
94 
95 #endif // YARP_MATH_VEC2D_H
contains the definition of a Matrix type
yarp::math::Vec2D< T > operator*(const yarp::sig::Matrix &lhs, yarp::math::Vec2D< T > rhs)
Definition: Vec2D.cpp:203
yarp::math::Vec2D< T > operator-(yarp::math::Vec2D< T > lhs, const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:220
yarp::math::Vec2D< T > operator+(yarp::math::Vec2D< T > lhs, const yarp::math::Vec2D< T > &rhs)
Definition: Vec2D.cpp:213
contains the definition of a Vector type
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.
yarp::os::Type getType() const override
Definition: Vec2D.h:70
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
static Type byName(const char *name)
Definition: Type.cpp:174
A class for a Matrix.
Definition: Matrix.h:46
std::string toString(const T &value)
convert an arbitrary type to string.
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition: math.cpp:41
Vector & operator-=(Vector &a, const double &s)
Subtraction operator between a vector and a scalar (defined in Math.h).
Definition: math.cpp:96
double norm(const yarp::sig::Vector &v)
Returns the Euclidean norm of the vector (defined in Math.h).
Definition: math.cpp:520
VectorOf< double > Vector
Definition: Vector.h:33
The main, catch-all namespace for YARP.
Definition: environment.h:25
#define YARP_math_API
Definition: api.h:18