Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
Vector.hpp
Go to the documentation of this file.
1
14#pragma once
15
16#include <AH/Arduino-Wrapper.h> // Print
17#include <AH/STL/cmath> // std::sqrt
18
19#ifndef ARDUINO
20#include <iosfwd> // std::ostream
21#endif
22
24
27
39struct Vec2f {
40 float x = 0.0;
41 float y = 0.0;
42
44 Vec2f() = default;
46 Vec2f(float x, float y) : x(x), y(y) {}
47
50 x += rhs.x;
51 y += rhs.y;
52 return *this;
53 }
56 Vec2f result = *this;
57 result += rhs;
58 return result;
59 }
60
62 Vec2f operator-() const { return {-x, -y}; }
64 Vec2f &operator-=(Vec2f rhs) { return *this += -rhs; }
67 Vec2f result = *this;
68 result -= rhs;
69 return result;
70 }
71
74 x *= rhs;
75 y *= rhs;
76 return *this;
77 }
79 Vec2f operator*(float rhs) const {
80 Vec2f result = *this;
81 result *= rhs;
82 return result;
83 }
84
87 x /= rhs;
88 y /= rhs;
89 return *this;
90 }
92 Vec2f operator/(float rhs) const {
93 Vec2f result = *this;
94 result /= rhs;
95 return result;
96 }
97
99 float operator*(Vec2f rhs) const {
100 return this->x * rhs.x + this->y * rhs.y;
101 }
102
104 float normSquared() const { return (*this) * (*this); }
106 float norm() const {
107 // return std::sqrt(normSquared()); // faster but less accurate
108 return std::hypot(x, y);
109 }
111 Vec2f &normalize() { return *this /= norm(); }
113 Vec2f normalized() const { return *this / norm(); }
114
116 bool operator==(Vec2f rhs) const {
117 return this->x == rhs.x && this->y == rhs.y;
118 }
120 bool operator!=(Vec2f rhs) const { return !(*this == rhs); }
121};
122
125inline Vec2f operator*(float lhs, Vec2f rhs) {
126 return {lhs * rhs.x, lhs * rhs.y};
127}
128
140struct Vec3f {
141 float x = 0.0;
142 float y = 0.0;
143 float z = 0.0;
144
146 Vec3f() = default;
148 Vec3f(float x, float y, float z) : x(x), y(y), z(z) {}
149 Vec3f(const Vec3f &) = default;
150 Vec3f(const volatile Vec3f &other) : x(other.x), y(other.y), z(other.z) {}
151 void operator=(const Vec3f &other) volatile {
152 this->x = other.x;
153 this->y = other.y;
154 this->z = other.z;
155 }
156
159 x += rhs.x;
160 y += rhs.y;
161 z += rhs.z;
162 return *this;
163 }
166 Vec3f result = *this;
167 result += rhs;
168 return result;
169 }
170
172 Vec3f operator-() const { return {-x, -y, -z}; }
174 Vec3f &operator-=(Vec3f rhs) { return *this += -rhs; }
177 Vec3f result = *this;
178 result -= rhs;
179 return result;
180 }
181
184 x *= rhs;
185 y *= rhs;
186 z *= rhs;
187 return *this;
188 }
190 Vec3f operator*(float rhs) const {
191 Vec3f result = *this;
192 result *= rhs;
193 return result;
194 }
195
198 x /= rhs;
199 y /= rhs;
200 z /= rhs;
201 return *this;
202 }
204 Vec3f operator/(float rhs) const {
205 Vec3f result = *this;
206 result /= rhs;
207 return result;
208 }
209
211 float operator*(Vec3f rhs) const {
212 return this->x * rhs.x + this->y * rhs.y + this->z * rhs.z;
213 }
214
216 float normSquared() const { return (*this) * (*this); }
217
219 float norm() const {
220 return std::sqrt(normSquared());
221 // return std::hypot(x, y, z); // C++17
222 }
223
225 Vec3f &normalize() { return *this /= norm(); }
227 Vec3f normalized() const { return *this / norm(); }
228
230 bool operator==(Vec3f rhs) const {
231 return this->x == rhs.x && this->y == rhs.y && this->z == rhs.z;
232 }
234 bool operator!=(Vec3f rhs) const { return !(*this == rhs); }
235};
236
239inline Vec3f operator*(float lhs, Vec3f rhs) {
240 return {lhs * rhs.x, lhs * rhs.y, lhs * rhs.z};
241}
242
243#ifndef ARDUINO
244
247std::ostream &operator<<(std::ostream &os, Vec2f v);
248
251std::ostream &operator<<(std::ostream &os, Vec3f v);
252
253#endif
254
257Print &operator<<(Print &os, Vec2f v);
258
261Print &operator<<(Print &os, Vec3f v);
262
264
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Print & operator<<(Print &os, Cable c)
Definition Cable.cpp:6
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
Print & operator<<(Print &os, Vec3f v)
Printing.
Vec2f operator*(float lhs, Vec2f rhs)
Scalar multiplication.
Definition Vector.hpp:125
Print & operator<<(Print &os, Vec2f v)
Printing.
Vec3f operator*(float lhs, Vec3f rhs)
Scalar multiplication.
Definition Vector.hpp:239
Type for 2D vectors of floating point numbers.
Definition Vector.hpp:39
float operator*(Vec2f rhs) const
Inner product.
Definition Vector.hpp:99
bool operator!=(Vec2f rhs) const
Inequality check.
Definition Vector.hpp:120
Vec2f operator-() const
Negation.
Definition Vector.hpp:62
Vec2f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition Vector.hpp:113
Vec2f & normalize()
Normalize this vector.
Definition Vector.hpp:111
bool operator==(Vec2f rhs) const
Equality check.
Definition Vector.hpp:116
Vec2f & operator*=(float rhs)
Scalar multiplication.
Definition Vector.hpp:73
float normSquared() const
Norm squared.
Definition Vector.hpp:104
Vec2f operator-(Vec2f rhs) const
Subtraction.
Definition Vector.hpp:66
Vec2f & operator+=(Vec2f rhs)
Addition.
Definition Vector.hpp:49
Vec2f operator*(float rhs) const
Scalar multiplication.
Definition Vector.hpp:79
Vec2f & operator/=(float rhs)
Scalar division.
Definition Vector.hpp:86
Vec2f operator+(Vec2f rhs) const
Addition.
Definition Vector.hpp:55
Vec2f operator/(float rhs) const
Scalar division.
Definition Vector.hpp:92
float norm() const
Norm.
Definition Vector.hpp:106
Vec2f(float x, float y)
Create a vector with the given x and y coordinates.
Definition Vector.hpp:46
Vec2f & operator-=(Vec2f rhs)
Subtraction.
Definition Vector.hpp:64
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:140
Vec3f operator+(Vec3f rhs) const
Addition.
Definition Vector.hpp:165
float operator*(Vec3f rhs) const
Inner product.
Definition Vector.hpp:211
bool operator!=(Vec3f rhs) const
Inequality check.
Definition Vector.hpp:234
Vec3f(const volatile Vec3f &other)
Definition Vector.hpp:150
Vec3f operator*(float rhs) const
Scalar multiplication.
Definition Vector.hpp:190
Vec3f(float x, float y, float z)
Create a vector with the given x, y and z coordinates.
Definition Vector.hpp:148
void operator=(const Vec3f &other) volatile
Definition Vector.hpp:151
Vec3f(const Vec3f &)=default
Vec3f operator-(Vec3f rhs) const
Subtraction.
Definition Vector.hpp:176
float normSquared() const
Norm squared.
Definition Vector.hpp:216
Vec3f & operator/=(float rhs)
Scalar division.
Definition Vector.hpp:197
Vec3f & operator-=(Vec3f rhs)
Subtraction.
Definition Vector.hpp:174
Vec3f & normalize()
Normalize this vector.
Definition Vector.hpp:225
Vec3f & operator*=(float rhs)
Scalar multiplication.
Definition Vector.hpp:183
Vec3f()=default
Create a vector that is initialized to the zero vector (0,0,0).
Vec3f & operator+=(Vec3f rhs)
Addition.
Definition Vector.hpp:158
Vec3f operator-() const
Negation.
Definition Vector.hpp:172
float norm() const
Norm.
Definition Vector.hpp:219
Vec3f operator/(float rhs) const
Scalar division.
Definition Vector.hpp:204
bool operator==(Vec3f rhs) const
Equality check.
Definition Vector.hpp:230
Vec3f normalized() const
Normalize a copy of this vector (doesn't change the original vector).
Definition Vector.hpp:227