YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Vec2D.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/math/Vec2D.h>
7
9#include <yarp/math/Math.h>
10#include <sstream>
11#include <cmath>
12#include <cstdio>
13
14using namespace yarp::math;
15using namespace yarp::sig;
16
17namespace {
18YARP_LOG_COMPONENT(VEC2D, "yarp.math.Vec2D")
19}
20
21template <typename Derived, typename Scalar>
22bool Vec2DCommon<Derived, Scalar>::operator==(const Derived& other) const
23{
24 const auto& v = static_cast<const Derived&>(*this);
25
26 return v.x == other.x &&
27 v.y == other.y;
28}
29
30template <typename Derived, typename Scalar>
31bool Vec2DCommon<Derived, Scalar>::operator!=(const Derived& other) const
32{
33 const auto& v = static_cast<const Derived&>(*this);
34
35 return v.x != other.x ||
36 v.y != other.y;
37}
38
39template <typename Derived, typename Scalar>
40Derived Vec2DCommon<Derived, Scalar>::operator+(const Derived& other) const
41{
42 const auto& v = static_cast<const Derived&>(*this);
43
44 Derived result;
45 result.x = v.x + other.x;
46 result.y = v.y + other.y;
47
48 return result;
49}
50
51template <typename Derived, typename Scalar>
52Derived Vec2DCommon<Derived, Scalar>::operator-(const Derived& other) const
53{
54 const auto& v = static_cast<const Derived&>(*this);
55
56 Derived result;
57 result.x = v.x - other.x;
58 result.y = v.y - other.y;
59
60 return result;
61}
62
63template <typename Derived, typename Scalar>
64Derived Vec2DCommon<Derived, Scalar>::operator*(const Derived& other) const
65{
66 const auto& v = static_cast<const Derived&>(*this);
67
68 Derived result;
69 result.x = v.x * other.x;
70 result.y = v.y * other.y;
71
72 return result;
73}
74
75template <typename Derived, typename Scalar>
76Derived Vec2DCommon<Derived, Scalar>::operator+=(const Derived& other)
77{
78 auto& v = static_cast<Derived&>(*this);
79 Derived result;
80 result.x = v.x += other.x;
81 result.y = v.y += other.y;
82 return result;
83}
84
85template <typename Derived, typename Scalar>
86Derived Vec2DCommon<Derived, Scalar>::operator-=(const Derived& other)
87{
88 auto& v = static_cast<Derived&>(*this);
89 Derived result;
90 result.x = v.x -= other.x;
91 result.y = v.y -= other.y;
92 return result;
93}
94
95template <typename Derived, typename Scalar>
96Vec2DCommon<Derived, Scalar>::Vec2DCommon(const yarp::sig::Vector& v)
97{
98 yCAssert(VEC2D, v.size() == 2);
99
100 auto& ccv = static_cast<Derived&>(*this);
101 ccv.x = decltype(ccv.x)(v[0]);
102 ccv.y = decltype(ccv.y)(v[1]);
103}
104
105
106namespace yarp::math
107{
108 template <typename Derived>
109 Derived operator*(const yarp::sig::Matrix& lhs, const Derived& rhs)
110 {
111 yCAssert(VEC2D, lhs.rows() == 2 && lhs.cols() == 2);
112 Derived result;
113 result.x = static_cast<decltype(result.x)>(lhs[0][0] * rhs.x + lhs[0][1] * rhs.y);
114 result.y = static_cast<decltype(result.y)>(lhs[1][0] * rhs.x + lhs[1][1] * rhs.y);
115 return result;
116 }
117
118 // Explicit instantiations: these are what actually define, and export, the
119 // three concrete overloads that the friend declarations in Vec2DCommon bind to.
123} // namespace yarp::math
124
125
126template <typename Derived, typename Scalar>
127std::string yarp::math::Vec2DCommon<Derived, Scalar>::PrintToString(int precision, int width) const
128{
129 const auto& ccv = static_cast<const Derived&>(*this);
130
131 std::ostringstream stringStream;
132 stringStream.precision(precision);
133 stringStream.width(width);
134 stringStream << std::string("x:") << ccv.x << std::string(" y:") << ccv.y;
135 return stringStream.str();
136}
137
138
139template <typename Derived, typename Scalar>
140Scalar yarp::math::Vec2DCommon<Derived, Scalar>::norm() const
141{
142 const auto& v = static_cast<const Derived&>(*this);
143 return static_cast<Scalar>(std::sqrt(static_cast<double>(v.x * v.x + v.y * v.y)));
144}
145
146template <typename Derived, typename Scalar>
147yarp::math::Vec2DCommon<Derived, Scalar>::operator yarp::sig::Vector() const
148{
149 const auto& ccv = static_cast<const Derived&>(*this);
150
152 v[0] = double(ccv.x);
153 v[1] = double(ccv.y);
154 return v;
155}
156
157// Explicit instances
158template class YARP_math_API yarp::math::Vec2DCommon<Vec2DOfInt, int>;
159template class YARP_math_API yarp::math::Vec2DCommon<Vec2DOfDouble, double>;
160template class YARP_math_API yarp::math::Vec2DCommon<Vec2DOfSizet, size_t>;
A class for a Matrix.
Definition Matrix.h:40
size_t cols() const
Return number of columns.
Definition Matrix.h:91
size_t rows() const
Return number of rows.
Definition Matrix.h:85
size_t size() const
Returns the number of elements in the vector.
Definition VectorBase.h:167
#define yCAssert(component, x)
#define YARP_LOG_COMPONENT(name,...)
Derived operator*(const yarp::sig::Matrix &lhs, const Derived &rhs)
Definition Vec2D.cpp:109
#define YARP_math_API
Definition api.h:17