Control Surface main
MIDI Control Surface library for Arduino
Vector.hpp
Go to the documentation of this file.
1
14#pragma once
15
17AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
18
19#include <AH/Arduino-Wrapper.h> // Print
20#include <AH/STL/cmath> // std::sqrt
21
22#ifndef ARDUINO
23#include <iosfwd> // std::ostream
24#endif
25
27
30
42struct Vec2f {
43 float x = 0.0;
44 float y = 0.0;
45
47 Vec2f() = default;
49 Vec2f(float x, float y) : x(x), y(y) {}
50
53 x += rhs.x;
54 y += rhs.y;
55 return *this;
56 }
58 Vec2f operator+(Vec2f rhs) const {
59 Vec2f result = *this;
60 result += rhs;
61 return result;
62 }
63
65 Vec2f operator-() const { return {-x, -y}; }
67 Vec2f &operator-=(Vec2f rhs) { return *this += -rhs; }
69 Vec2f operator-(Vec2f rhs) const {
70 Vec2f result = *this;
71 result -= rhs;
72 return result;
73 }
74
76 Vec2f &operator*=(float rhs) {
77 x *= rhs;
78 y *= rhs;
79 return *this;
80 }
82 Vec2f operator*(float rhs) const {
83 Vec2f result = *this;
84 result *= rhs;
85 return result;
86 }
87
89 Vec2f &operator/=(float rhs) {
90 x /= rhs;
91 y /= rhs;
92 return *this;
93 }
95 Vec2f operator/(float rhs) const {
96 Vec2f result = *this;
97 result /= rhs;
98 return result;
99 }
100
102 float operator*(Vec2f rhs) const {
103 return this->x * rhs.x + this->y * rhs.y;
104 }
105
107 float normSquared() const { return (*this) * (*this); }
109 float norm() const {
110 // return std::sqrt(normSquared()); // faster but less accurate
111 return std::hypot(x, y);
112 }
114 Vec2f &normalize() { return *this /= norm(); }
116 Vec2f normalized() const { return *this / norm(); }
117
119 bool operator==(Vec2f rhs) const {
120 return this->x == rhs.x && this->y == rhs.y;
121 }
123 bool operator!=(Vec2f rhs) const { return !(*this == rhs); }
124};
125
128inline Vec2f operator*(float lhs, Vec2f rhs) {
129 return {lhs * rhs.x, lhs * rhs.y};
130}
131
143struct Vec3f {
144 float x = 0.0;
145 float y = 0.0;
146 float z = 0.0;
147
149 Vec3f() = default;
151 Vec3f(float x, float y, float z) : x(x), y(y), z(z) {}
152 Vec3f(const Vec3f &) = default;
153 Vec3f(const volatile Vec3f &other) : x(other.x), y(other.y), z(other.z) {}
154 void operator=(const Vec3f &other) volatile {
155 this->x = other.x;
156 this->y = other.y;
157 this->z = other.z;
158 }
159
162 x += rhs.x;
163 y += rhs.y;
164 z += rhs.z;
165 return *this;
166 }
168 Vec3f operator+(Vec3f rhs) const {
169 Vec3f result = *this;
170 result += rhs;
171 return result;
172 }
173
175 Vec3f operator-() const { return {-x, -y, -z}; }
177 Vec3f &operator-=(Vec3f rhs) { return *this += -rhs; }
179 Vec3f operator-(Vec3f rhs) const {
180 Vec3f result = *this;
181 result -= rhs;
182 return result;
183 }
184
186 Vec3f &operator*=(float rhs) {
187 x *= rhs;
188 y *= rhs;
189 z *= rhs;
190 return *this;
191 }
193 Vec3f operator*(float rhs) const {
194 Vec3f result = *this;
195 result *= rhs;
196 return result;
197 }
198
200 Vec3f &operator/=(float rhs) {
201 x /= rhs;
202 y /= rhs;
203 z /= rhs;
204 return *this;
205 }
207 Vec3f operator/(float rhs) const {
208 Vec3f result = *this;
209 result /= rhs;
210 return result;
211 }
212
214 float operator*(Vec3f rhs) const {
215 return this->x * rhs.x + this->y * rhs.y + this->z * rhs.z;
216 }
217
219 float normSquared() const { return (*this) * (*this); }
220
222 float norm() const {
223 return std::sqrt(normSquared());
224 // return std::hypot(x, y, z); // C++17
225 }
226
228 Vec3f &normalize() { return *this /= norm(); }
230 Vec3f normalized() const { return *this / norm(); }
231
233 bool operator==(Vec3f rhs) const {
234 return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z;
235 }
237 bool operator!=(Vec3f rhs) const { return !(*this == rhs); }
238};
239
242inline Vec3f operator*(float lhs, Vec3f rhs) {
243 return {lhs * rhs.x, lhs * rhs.y, lhs * rhs.z};
244}
245
246#ifndef ARDUINO
247
250std::ostream &operator<<(std::ostream &os, Vec2f v);
251
254std::ostream &operator<<(std::ostream &os, Vec3f v);
255
256#endif
257
260Print &operator<<(Print &os, Vec2f v);
261
264Print &operator<<(Print &os, Vec3f v);
265
267
269
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:36
Print & operator<<(Print &os, Vec3f v)
Printing.
Vec2f operator*(float lhs, Vec2f rhs)
Scalar multiplication.
Definition: Vector.hpp:128
Print & operator<<(Print &os, Vec2f v)
Printing.
Print & operator<<(Print &os, Quaternion e)
Printing.
Definition: Quaternion.cpp:28
Vec3f operator*(float lhs, Vec3f rhs)
Scalar multiplication.
Definition: Vector.hpp:242
Type for 2D vectors of floating point numbers.
Definition: Vector.hpp:42
float operator*(Vec2f rhs) const
Inner product.
Definition: Vector.hpp:102
bool operator!=(Vec2f rhs) const
Inequality check.
Definition: Vector.hpp:123
Vec2f operator-() const
Negation.
Definition: Vector.hpp:65
Vec2f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition: Vector.hpp:116
Vec2f & normalize()
Normalize this vector.
Definition: Vector.hpp:114
bool operator==(Vec2f rhs) const
Equality check.
Definition: Vector.hpp:119
Vec2f & operator*=(float rhs)
Scalar multiplication.
Definition: Vector.hpp:76
float normSquared() const
Norm squared.
Definition: Vector.hpp:107
Vec2f operator-(Vec2f rhs) const
Subtraction.
Definition: Vector.hpp:69
Vec2f & operator+=(Vec2f rhs)
Addition.
Definition: Vector.hpp:52
Vec2f operator*(float rhs) const
Scalar multiplication.
Definition: Vector.hpp:82
Vec2f & operator/=(float rhs)
Scalar division.
Definition: Vector.hpp:89
float y
The y component of the vector.
Definition: Vector.hpp:44
float x
The x component of the vector.
Definition: Vector.hpp:43
Vec2f operator+(Vec2f rhs) const
Addition.
Definition: Vector.hpp:58
Vec2f operator/(float rhs) const
Scalar division.
Definition: Vector.hpp:95
float norm() const
Norm.
Definition: Vector.hpp:109
Vec2f(float x, float y)
Create a vector with the given x and y coordinates.
Definition: Vector.hpp:49
Vec2f & operator-=(Vec2f rhs)
Subtraction.
Definition: Vector.hpp:67
Vec2f()=default
Create a vector that is initialized to the zero vector (0,0).
Type for 3D vectors of floating point numbers.
Definition: Vector.hpp:143
Vec3f operator+(Vec3f rhs) const
Addition.
Definition: Vector.hpp:168
float operator*(Vec3f rhs) const
Inner product.
Definition: Vector.hpp:214
bool operator!=(Vec3f rhs) const
Inequality check.
Definition: Vector.hpp:237
Vec3f(const volatile Vec3f &other)
Definition: Vector.hpp:153
Vec3f operator*(float rhs) const
Scalar multiplication.
Definition: Vector.hpp:193
Vec3f(float x, float y, float z)
Create a vector with the given x, y and z coordinates.
Definition: Vector.hpp:151
void operator=(const Vec3f &other) volatile
Definition: Vector.hpp:154
Vec3f(const Vec3f &)=default
Vec3f operator-(Vec3f rhs) const
Subtraction.
Definition: Vector.hpp:179
float normSquared() const
Norm squared.
Definition: Vector.hpp:219
Vec3f & operator/=(float rhs)
Scalar division.
Definition: Vector.hpp:200
Vec3f & operator-=(Vec3f rhs)
Subtraction.
Definition: Vector.hpp:177
Vec3f & normalize()
Normalize this vector.
Definition: Vector.hpp:228
float y
The y component of the vector.
Definition: Vector.hpp:145
Vec3f & operator*=(float rhs)
Scalar multiplication.
Definition: Vector.hpp:186
Vec3f()=default
Create a vector that is initialized to the zero vector (0,0,0).
Vec3f & operator+=(Vec3f rhs)
Addition.
Definition: Vector.hpp:161
Vec3f operator-() const
Negation.
Definition: Vector.hpp:175
float x
The x component of the vector.
Definition: Vector.hpp:144
float norm() const
Norm.
Definition: Vector.hpp:222
Vec3f operator/(float rhs) const
Scalar division.
Definition: Vector.hpp:207
bool operator==(Vec3f rhs) const
Equality check.
Definition: Vector.hpp:233
Vec3f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition: Vector.hpp:230
float z
The z component of the vector.
Definition: Vector.hpp:146