Control Surface  1.2.0
MIDI Control Surface library for Arduino
Vector.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5 
6 #include <AH/Arduino-Wrapper.h> // Print
7 #include <AH/STL/cmath> // std::sqrt
8 
9 #ifndef ARDUINO
10 #include <iosfwd> // std::ostream
11 #endif
12 
14 
17 
43 struct Vec2f {
44  float x = 0.0;
45  float y = 0.0;
46 
48  Vec2f() = default;
50  Vec2f(float x, float y) : x(x), y(y) {}
51 
54  x += rhs.x;
55  y += rhs.y;
56  return *this;
57  }
59  Vec2f operator+(Vec2f rhs) const {
60  Vec2f result = *this;
61  result += rhs;
62  return result;
63  }
64 
66  Vec2f operator-() const { return {-x, -y}; }
68  Vec2f &operator-=(Vec2f rhs) { return *this += -rhs; }
70  Vec2f operator-(Vec2f rhs) const {
71  Vec2f result = *this;
72  result -= rhs;
73  return result;
74  }
75 
77  Vec2f &operator*=(float rhs) {
78  x *= rhs;
79  y *= rhs;
80  return *this;
81  }
83  Vec2f operator*(float rhs) const {
84  Vec2f result = *this;
85  result *= rhs;
86  return result;
87  }
88 
90  Vec2f &operator/=(float rhs) {
91  x /= rhs;
92  y /= rhs;
93  return *this;
94  }
96  Vec2f operator/(float rhs) const {
97  Vec2f result = *this;
98  result /= rhs;
99  return result;
100  }
101 
103  float operator*(Vec2f rhs) const {
104  return this->x * rhs.x + this->y * rhs.y;
105  }
106 
108  float normSquared() const { return (*this) * (*this); }
110  float norm() const {
111  // return std::sqrt(normSquared()); // faster but less accurate
112  return std::hypot(x, y);
113  }
115  Vec2f &normalize() { return *this /= norm(); }
117  Vec2f normalized() const { return *this / norm(); }
118 
120  bool operator==(Vec2f rhs) const {
121  return this->x == rhs.x && this->y == rhs.y;
122  }
124  bool operator!=(Vec2f rhs) const { return !(*this == rhs); }
125 };
126 
129 inline Vec2f operator*(float lhs, Vec2f rhs) {
130  return {lhs * rhs.x, lhs * rhs.y};
131 }
132 
144 struct Vec3f {
145  float x = 0.0;
146  float y = 0.0;
147  float z = 0.0;
148 
150  Vec3f() = default;
152  Vec3f(float x, float y, float z) : x(x), y(y), z(z) {}
153  Vec3f(const Vec3f &) = default;
154  Vec3f(const volatile Vec3f &other) : x(other.x), y(other.y), z(other.z) {}
155  void operator=(const Vec3f &other) volatile {
156  this->x = other.x;
157  this->y = other.y;
158  this->z = other.z;
159  }
160 
163  x += rhs.x;
164  y += rhs.y;
165  z += rhs.z;
166  return *this;
167  }
169  Vec3f operator+(Vec3f rhs) const {
170  Vec3f result = *this;
171  result += rhs;
172  return result;
173  }
174 
176  Vec3f operator-() const { return {-x, -y, -z}; }
178  Vec3f &operator-=(Vec3f rhs) { return *this += -rhs; }
180  Vec3f operator-(Vec3f rhs) const {
181  Vec3f result = *this;
182  result -= rhs;
183  return result;
184  }
185 
187  Vec3f &operator*=(float rhs) {
188  x *= rhs;
189  y *= rhs;
190  z *= rhs;
191  return *this;
192  }
194  Vec3f operator*(float rhs) const {
195  Vec3f result = *this;
196  result *= rhs;
197  return result;
198  }
199 
201  Vec3f &operator/=(float rhs) {
202  x /= rhs;
203  y /= rhs;
204  z /= rhs;
205  return *this;
206  }
208  Vec3f operator/(float rhs) const {
209  Vec3f result = *this;
210  result /= rhs;
211  return result;
212  }
213 
215  float operator*(Vec3f rhs) const {
216  return this->x * rhs.x + this->y * rhs.y + this->z * rhs.z;
217  }
218 
220  float normSquared() const { return (*this) * (*this); }
221 
223  float norm() const {
224  return std::sqrt(normSquared());
225  // return std::hypot(x, y, z); // C++17
226  }
227 
229  Vec3f &normalize() { return *this /= norm(); }
231  Vec3f normalized() const { return *this / norm(); }
232 
234  bool operator==(Vec3f rhs) const {
235  return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z;
236  }
238  bool operator!=(Vec3f rhs) const { return !(*this == rhs); }
239 };
240 
243 inline Vec3f operator*(float lhs, Vec3f rhs) {
244  return {lhs * rhs.x, lhs * rhs.y, lhs * rhs.z};
245 }
246 
247 #ifndef ARDUINO
248 
251 std::ostream &operator<<(std::ostream &os, Vec2f v);
252 
255 std::ostream &operator<<(std::ostream &os, Vec3f v);
256 
257 #endif
258 
261 Print &operator<<(Print &os, Vec2f v);
262 
265 Print &operator<<(Print &os, Vec3f v);
266 
268 
270 
AH::Vec2f::y
float y
The y component of the vector.
Definition: Vector.hpp:45
AH::Vec3f::norm
float norm() const
Norm.
Definition: Vector.hpp:223
AH::Vec2f::normalize
Vec2f & normalize()
Normalize this vector.
Definition: Vector.hpp:115
AH::Vec2f::normSquared
float normSquared() const
Norm squared.
Definition: Vector.hpp:108
AH::Vec2f::operator+
Vec2f operator+(Vec2f rhs) const
Addition.
Definition: Vector.hpp:59
Warnings.hpp
AH::Vec2f::operator/=
Vec2f & operator/=(float rhs)
Scalar division.
Definition: Vector.hpp:90
AH::Vec2f::x
float x
The x component of the vector.
Definition: Vector.hpp:44
AH::Vec3f::normSquared
float normSquared() const
Norm squared.
Definition: Vector.hpp:220
AH::Vec3f::operator!=
bool operator!=(Vec3f rhs) const
Inequality check.
Definition: Vector.hpp:238
AH::Vec2f::Vec2f
Vec2f()=default
Create a vector that is initialized to the zero vector (0,0).
AH::Vec3f::operator+=
Vec3f & operator+=(Vec3f rhs)
Addition.
Definition: Vector.hpp:162
AH::Vec3f::operator-=
Vec3f & operator-=(Vec3f rhs)
Subtraction.
Definition: Vector.hpp:178
AH::Vec2f::operator==
bool operator==(Vec2f rhs) const
Equality check.
Definition: Vector.hpp:120
AH::Vec2f::operator*
Vec2f operator*(float lhs, Vec2f rhs)
Scalar multiplication.
Definition: Vector.hpp:129
AH::Vec2f::Vec2f
Vec2f(float x, float y)
Create a vector with the given x and y coordinates.
Definition: Vector.hpp:50
AH::Vec3f::operator-
Vec3f operator-() const
Negation.
Definition: Vector.hpp:176
AH::Vec3f::Vec3f
Vec3f(const volatile Vec3f &other)
Definition: Vector.hpp:154
AH::Vec2f::operator+=
Vec2f & operator+=(Vec2f rhs)
Addition.
Definition: Vector.hpp:53
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
AH::Vec3f
Type for 3D vectors of floating point numbers.
Definition: Vector.hpp:144
AH::Vec2f::operator<<
Print & operator<<(Print &os, Vec2f v)
Printing.
AH::Vec3f::operator/
Vec3f operator/(float rhs) const
Scalar division.
Definition: Vector.hpp:208
AH::Vec2f::operator/
Vec2f operator/(float rhs) const
Scalar division.
Definition: Vector.hpp:96
AH::Vec3f::x
float x
The x component of the vector.
Definition: Vector.hpp:145
AH::Vec3f::operator==
bool operator==(Vec3f rhs) const
Equality check.
Definition: Vector.hpp:234
AH::Vec2f::operator-
Vec2f operator-(Vec2f rhs) const
Subtraction.
Definition: Vector.hpp:70
AH::Vec3f::Vec3f
Vec3f(const Vec3f &)=default
AH::Vec3f::Vec3f
Vec3f()=default
Create a vector that is initialized to the zero vector (0,0,0).
AH::Vec3f::operator*=
Vec3f & operator*=(float rhs)
Scalar multiplication.
Definition: Vector.hpp:187
AH::Vec3f::operator*
Vec3f operator*(float lhs, Vec3f rhs)
Scalar multiplication.
Definition: Vector.hpp:243
AH::Vec3f::operator*
Vec3f operator*(float rhs) const
Scalar multiplication.
Definition: Vector.hpp:194
AH::Vec2f::operator-=
Vec2f & operator-=(Vec2f rhs)
Subtraction.
Definition: Vector.hpp:68
AH::Vec3f::operator-
Vec3f operator-(Vec3f rhs) const
Subtraction.
Definition: Vector.hpp:180
AH::Vec3f::operator<<
Print & operator<<(Print &os, Vec3f v)
Printing.
AH::Vec3f::operator+
Vec3f operator+(Vec3f rhs) const
Addition.
Definition: Vector.hpp:169
AH::Vec2f::operator*=
Vec2f & operator*=(float rhs)
Scalar multiplication.
Definition: Vector.hpp:77
AH::Vec3f::operator*
float operator*(Vec3f rhs) const
Inner product.
Definition: Vector.hpp:215
AH::Vec2f::operator*
Vec2f operator*(float rhs) const
Scalar multiplication.
Definition: Vector.hpp:83
AH::Vec3f::operator/=
Vec3f & operator/=(float rhs)
Scalar division.
Definition: Vector.hpp:201
AH::Vec2f::normalized
Vec2f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition: Vector.hpp:117
AH::Vec3f::operator=
void operator=(const Vec3f &other) volatile
Definition: Vector.hpp:155
AH::Vec2f
Type for 2D vectors of floating point numbers.
Definition: Vector.hpp:43
AH::Vec3f::Vec3f
Vec3f(float x, float y, float z)
Create a vector with the given x, y and z coordinates.
Definition: Vector.hpp:152
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
AH::Vec3f::z
float z
The z component of the vector.
Definition: Vector.hpp:147
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::Vec2f::operator!=
bool operator!=(Vec2f rhs) const
Inequality check.
Definition: Vector.hpp:124
AH::Vec2f::operator*
float operator*(Vec2f rhs) const
Inner product.
Definition: Vector.hpp:103
AH::Vec3f::y
float y
The y component of the vector.
Definition: Vector.hpp:146
AH::Vec3f::normalize
Vec3f & normalize()
Normalize this vector.
Definition: Vector.hpp:229
AH::Quaternion::operator<<
Print & operator<<(Print &os, Quaternion e)
Printing.
Definition: Quaternion.cpp:28
AH::Vec2f::norm
float norm() const
Norm.
Definition: Vector.hpp:110
AH::Vec2f::operator-
Vec2f operator-() const
Negation.
Definition: Vector.hpp:66
AH::Vec3f::normalized
Vec3f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition: Vector.hpp:231