RIT VEXU Core API
Loading...
Searching...
No Matches
translation2d.h
1#pragma once
2
3#include <cmath>
4#include <vector>
5
6#include "cevalm.hpp"
7#include "core/math/eigen_interface.h"
8#include "core/math/geometry/rotation2d.h"
9#include "core/math/units.h"
10
11class Rotation2d;
12
13template <typename Q>
14concept LinearKinematicQuantity =
15 units::KinematicQuantity<Q> && std::ratio_equal_v<typename Q::length, std::ratio<1>> &&
16 std::ratio_equal_v<typename Q::angle, std::ratio<0>>;
17
28template <LinearKinematicQuantity Q>
30 private:
31 Q x_;
32 Q y_;
33
34 public:
36 constexpr LinearVector2d() : x_{0}, y_{0} {}
37
44 constexpr LinearVector2d(Q x, Q y) : x_{x}, y_{y} {}
45
53 : x_{r * theta.f_cos()}, y_{r * theta.f_sin()} {}
54
61 constexpr LinearVector2d(Eigen::Vector2d vector, Q unit)
62 : x_{vector[0], unit}, y_{vector[1], unit} {}
63
65 constexpr Q x() const { return x_; }
66
68 constexpr double x(Q unit) const { return x_.to(unit); }
69
71 constexpr void set_x(Q val) { x_ = val; }
72
74 constexpr Q y() const { return y_; }
75
77 constexpr double y(Q unit) const { return y_.to(unit); }
78
80 constexpr void set_y(Q val) { y_ = val; }
81
83 constexpr Rotation2d theta() const { return Rotation2d(x_.internal(), y_.internal()); }
84
91 constexpr Eigen::Vector2d as_vector(Q unit) const { return EVec<2>{x_.to(unit), y_.to(unit)}; }
92
94 constexpr Q norm() const { return units::hypot(x_, y_); }
95
103 constexpr LinearVector2d normalize(Q magnitude = Q(1.0)) const {
104 return LinearVector2d(magnitude, theta());
105 }
106
108 constexpr Q distance(LinearVector2d other) const {
109 return units::hypot(x_ - other.x_, y_ - other.y_);
110 }
111
120 constexpr LinearVector2d interpolate(LinearVector2d end, double fraction) const {
121 if (fraction <= 0) {
122 return *this;
123 }
124 if (fraction >= 1) {
125 return end;
126 }
127 return *this + (end - *this) * fraction;
128 }
129
140 constexpr LinearVector2d rotate_by(Rotation2d rotation) const {
141 return {x_ * rotation.f_cos() - y_ * rotation.f_sin(),
142 x_ * rotation.f_sin() + y_ * rotation.f_cos()};
143 }
144
155 constexpr LinearVector2d rotate_around(LinearVector2d other, Rotation2d rotation) const {
156 LinearVector2d diff = *this - other;
157 return diff.rotate_by(rotation) + other;
158 }
159
167 constexpr LinearVector2d inverse() const { return LinearVector2d(-x_, -y_); }
168
178 constexpr units::Multiplied<Q, Q> dot(LinearVector2d other) const {
179 return (x_ * other.x_) + (y_ * other.y_);
180 }
181
183 constexpr LinearVector2d operator+(LinearVector2d other) const {
184 return {x_ + other.x_, y_ + other.y_};
185 }
186
188 constexpr LinearVector2d& operator+=(LinearVector2d other) { return *this = *this + other; }
189
191 constexpr LinearVector2d operator-(LinearVector2d other) const {
192 return {x_ - other.x_, y_ - other.y_};
193 }
194
196 constexpr LinearVector2d& operator-=(LinearVector2d other) { return *this = *this - other; }
197
199 constexpr LinearVector2d operator-() const { return inverse(); }
200
202 constexpr LinearVector2d operator*(double scalar) const { return {x_ * scalar, y_ * scalar}; }
203
205 friend constexpr LinearVector2d operator*(double scalar, LinearVector2d vector) {
206 return vector * scalar;
207 }
208
213 template <units::IsQuantity S>
214 requires LinearKinematicQuantity<units::Multiplied<Q, S>>
216 return LinearVector2d<units::Multiplied<Q, S>>{x_ * scalar, y_ * scalar};
217 }
218
223 template <units::IsQuantity S>
224 requires LinearKinematicQuantity<units::Multiplied<Q, S>>
225 friend constexpr auto operator*(S scalar, LinearVector2d vector) {
226 return vector * scalar;
227 }
228
238 constexpr units::Multiplied<Q, Q> operator*(LinearVector2d other) const { return dot(other); }
239
241 constexpr LinearVector2d& operator*=(double scalar) { return *this = *this * scalar; }
242
244 constexpr LinearVector2d operator/(double scalar) const { return {x_ / scalar, y_ / scalar}; }
245
246 template <units::IsQuantity S>
247 requires LinearKinematicQuantity<units::Divided<Q, S>>
248 constexpr LinearVector2d<units::Divided<Q, S>> operator/(S scalar) const {
249 return LinearVector2d<units::Divided<Q, S>>{x_ / scalar, y_ / scalar};
250 }
251
253 constexpr LinearVector2d& operator/=(double scalar) { return *this = *this / scalar; }
254
256 constexpr bool operator==(LinearVector2d other) const {
257 return cevalm::abs(x_.internal() - other.x_.internal()) < 1e-6 &&
258 cevalm::abs(y_.internal() - other.y_.internal()) < 1e-6;
259 }
260
265 constexpr bool is_near(LinearVector2d other, Q tolerance = Q(1e-6)) const {
266 return distance(other) <= tolerance;
267 }
268
275 static constexpr LinearVector2d mean(const std::vector<LinearVector2d>& list) {
276 if (list.size() == 0) {
277 return LinearVector2d{};
278 }
279
280 Q sumx;
281 Q sumy;
282
283 for (LinearVector2d& vec : list) {
284 sumx += vec.x_;
285 sumy += vec.y_;
286 }
287
288 return LinearVector2d(sumx / list.size(), sumy / list.size());
289 }
290};
291
293using Translation2d = LinearVector2d<units::Length>;
295using Velocity2d = LinearVector2d<units::Velocity>;
297using Acceleration2d = LinearVector2d<units::Acceleration>;
299using Jerk2d = LinearVector2d<units::Jerk>;
Definition translation2d.h:29
constexpr bool operator==(LinearVector2d other) const
Checks exact equality between this and another vector.
Definition translation2d.h:256
constexpr LinearVector2d & operator-=(LinearVector2d other)
Subtracts another vector from this vector component wise.
Definition translation2d.h:196
constexpr Rotation2d theta() const
Definition translation2d.h:83
constexpr LinearVector2d operator-() const
Inverts this vector (flipped across origin).
Definition translation2d.h:199
constexpr LinearVector2d & operator*=(double scalar)
Multiplies this vector by a scalar.
Definition translation2d.h:241
constexpr double x(Q unit) const
Gets x in the supplied unit.
Definition translation2d.h:68
constexpr LinearVector2d operator-(LinearVector2d other) const
Subtracts another vector from this vector component wise.
Definition translation2d.h:191
constexpr LinearVector2d interpolate(LinearVector2d end, double fraction) const
Definition translation2d.h:120
constexpr units::Multiplied< Q, Q > dot(LinearVector2d other) const
Definition translation2d.h:178
constexpr LinearVector2d normalize(Q magnitude=Q(1.0)) const
Definition translation2d.h:103
constexpr LinearVector2d & operator/=(double scalar)
Divides this vector by a scalar.
Definition translation2d.h:253
constexpr Q x() const
Definition translation2d.h:65
constexpr LinearVector2d operator+(LinearVector2d other) const
Adds another vector to this vector component wise.
Definition translation2d.h:183
friend constexpr auto operator*(S scalar, LinearVector2d vector)
Definition translation2d.h:225
friend constexpr LinearVector2d operator*(double scalar, LinearVector2d vector)
Multiplies a scalar by this vector.
Definition translation2d.h:205
constexpr Q distance(LinearVector2d other) const
Gets the distance between two vectors.
Definition translation2d.h:108
constexpr double y(Q unit) const
Gets y in the supplied unit.
Definition translation2d.h:77
constexpr void set_x(Q val)
Sets the x component.
Definition translation2d.h:71
constexpr units::Multiplied< Q, Q > operator*(LinearVector2d other) const
Definition translation2d.h:238
constexpr LinearVector2d operator*(double scalar) const
Multiplies this vector by a scalar.
Definition translation2d.h:202
static constexpr LinearVector2d mean(const std::vector< LinearVector2d > &list)
Definition translation2d.h:275
constexpr void set_y(Q val)
Sets the y component.
Definition translation2d.h:80
constexpr LinearVector2d rotate_around(LinearVector2d other, Rotation2d rotation) const
Definition translation2d.h:155
constexpr LinearVector2d()
Default Constructor for LinearVector2d valued (0, 0).
Definition translation2d.h:36
constexpr LinearVector2d(Q x, Q y)
Definition translation2d.h:44
constexpr LinearVector2d(Q r, Rotation2d theta)
Definition translation2d.h:52
constexpr LinearVector2d(Eigen::Vector2d vector, Q unit)
Definition translation2d.h:61
constexpr LinearVector2d< units::Multiplied< Q, S > > operator*(S scalar) const
Definition translation2d.h:215
constexpr LinearVector2d & operator+=(LinearVector2d other)
Adds another vector to this vector component wise.
Definition translation2d.h:188
constexpr bool is_near(LinearVector2d other, Q tolerance=Q(1e-6)) const
Definition translation2d.h:265
constexpr LinearVector2d rotate_by(Rotation2d rotation) const
Definition translation2d.h:140
constexpr LinearVector2d inverse() const
Definition translation2d.h:167
constexpr Eigen::Vector2d as_vector(Q unit) const
Definition translation2d.h:91
constexpr Q norm() const
Gets the norm of the vector.
Definition translation2d.h:94
constexpr Q y() const
Definition translation2d.h:74
constexpr LinearVector2d operator/(double scalar) const
Divides this vector by a scalar.
Definition translation2d.h:244
Definition rotation2d.h:16
constexpr double f_sin() const
Gets the sine of the rotation.
Definition rotation2d.h:87
constexpr Rotation2d()=default
Default Constructor for Rotation2d.
constexpr double f_cos() const
Gets the cosine of the rotation.
Definition rotation2d.h:84
Only allows nonzero length, time, angle for the next two functions.
Definition units.h:963