RIT VEXU Core API
Loading...
Searching...
No Matches
rotation2d.h
1#pragma once
2
3#include "cevalm.hpp"
4#include "core/math/eigen_interface.h"
5#include "core/math/units.h"
6
17 private:
18 double cos_ = 1;
19 double sin_ = 0;
20
21 public:
23 constexpr Rotation2d() = default;
24
26 constexpr Rotation2d(units::Angle value) : cos_(units::cos(value)), sin_(units::sin(value)) {}
27
29 constexpr Rotation2d(double value) : cos_(cevalm::cos(value)), sin_(cevalm::sin(value)) {}
30
40 constexpr Rotation2d(double x, double y) {
41 double mag = cevalm::hypot(x, y);
42 if (x != 0 || y != 0) {
43 cos_ = x / mag;
44 sin_ = y / mag;
45 } else {
46 cos_ = 1;
47 sin_ = 0;
48 }
49 }
50
60 constexpr Rotation2d(units::Length x, units::Length y)
61 : Rotation2d(x.to(units::inches), y.to(units::inches)) {}
62
64 constexpr units::Angle angle() const {
65 return units::Angle(cevalm::atan2(sin_, cos_), units::radians);
66 }
67
69 constexpr double angle(units::Angle unit) const { return angle().to(unit); }
70
72 constexpr double radians() const { return angle().to(units::radians); }
73
75 constexpr double degrees() const { return angle().to(units::degrees); }
76
78 constexpr double revolutions() const { return angle().to(units::revolutions); }
79
81 constexpr double gradians() const { return angle().to(units::gradians); }
82
84 constexpr double f_cos() const { return cos_; }
85
87 constexpr double f_sin() const { return sin_; }
88
90 constexpr double f_tan() const { return sin_ / cos_; }
91
93 constexpr Rotation2d inverse() const { return Rotation2d(cos_, -sin_); }
94
96 constexpr Rotation2d opposite() const { return Rotation2d(-cos_, -sin_); }
97
99 constexpr EMat<2, 2> rotation_matrix() const { return EMat<2, 2>{{cos_, -sin_}, {sin_, cos_}}; }
100
102 constexpr double wrapped_radians_180() const { return wrap_radians_180(radians()); }
103
105 constexpr double wrapped_degrees_180() const { return wrap_degrees_180(degrees()); }
106
108 constexpr double wrapped_revolutions_180() const { return wrap_revolutions_180(revolutions()); }
109
111 constexpr double wrapped_gradians_180() const { return wrap_gradians_180(gradians()); }
112
114 constexpr double wrapped_radians_360() const { return wrap_radians_360(radians()); }
115
117 constexpr double wrapped_degrees_360() const { return wrap_degrees_360(degrees()); }
118
120 constexpr double wrapped_revolutions_360() const { return wrap_revolutions_360(revolutions()); }
121
123 constexpr double wrapped_gradians_360() const { return wrap_gradians_360(gradians()); }
124
126 constexpr Rotation2d operator+(Rotation2d other) const {
127 return Rotation2d(
128 cos_ * other.cos_ - sin_ * other.sin_, cos_ * other.sin_ + sin_ * other.cos_
129 );
130 }
131
133 constexpr Rotation2d& operator+=(Rotation2d other) { return *this = *this + other; }
134
136 constexpr Rotation2d operator-(Rotation2d other) const { return *this + -other; }
137
139 constexpr Rotation2d& operator-=(Rotation2d other) { return *this = *this - other; }
140
142 constexpr Rotation2d operator-() const { return inverse(); }
143
145 constexpr Rotation2d operator*(double scalar) const { return Rotation2d(radians() * scalar); }
146
148 friend constexpr Rotation2d operator*(double scalar, Rotation2d rotation) {
149 return rotation * scalar;
150 }
151
153 constexpr Rotation2d& operator*=(double scalar) { return *this = *this * scalar; }
154
156 constexpr Rotation2d operator/(double scalar) const { return *this * (1.0 / scalar); }
157
159 constexpr Rotation2d& operator/=(double scalar) { return *this = *this / scalar; }
160
162 constexpr bool operator==(Rotation2d other) const {
163 return cos_ == other.cos_ && sin_ == other.sin_;
164 }
165
167 constexpr bool is_near(Rotation2d other, units::Angle tolerance = units::Angle(1e-6)) const {
168 return units::abs((*this - other).angle()) <= tolerance;
169 }
170
177 static constexpr double deg2rad(double deg) { return deg * (std::numbers::pi / 180.0); }
178
185 static constexpr double rad2deg(double rad) { return rad * (180.0 / std::numbers::pi); }
186
193 static constexpr double wrap_radians_180(double angle) {
194 if (angle >= -std::numbers::pi && angle <= std::numbers::pi) {
195 return angle;
196 }
197 double x = cevalm::fmod(angle, 2 * std::numbers::pi);
198 if (x > std::numbers::pi) {
199 x -= 2 * std::numbers::pi;
200 } else if (x < -std::numbers::pi) {
201 x += 2 * std::numbers::pi;
202 }
203 return x;
204 }
205
212 static constexpr double wrap_degrees_180(double angle) {
213 if (angle >= -180.0 && angle <= 180.0) {
214 return angle;
215 }
216 double x = cevalm::fmod(angle, 360.0);
217 if (x > 180.0) {
218 x -= 360.0;
219 } else if (x < -180.0) {
220 x += 360.0;
221 }
222 return x;
223 }
224
231 static constexpr double wrap_revolutions_180(double angle) {
232 if (angle >= -0.5 && angle <= 0.5) {
233 return angle;
234 }
235 double x = cevalm::fmod(angle, 1.0);
236 if (x > 0.5) {
237 x -= 1.0;
238 } else if (x < -0.5) {
239 x += 1.0;
240 }
241 return x;
242 }
243
250 static constexpr double wrap_gradians_180(double angle) {
251 if (angle >= -200.0 && angle <= 200.0) {
252 return angle;
253 }
254 double x = cevalm::fmod(angle, 400.0);
255 if (x > 200.0) {
256 x -= 400.0;
257 } else if (x < -200.0) {
258 x += 400.0;
259 }
260 return x;
261 }
262
269 static constexpr double wrap_radians_360(double angle) {
270 if (angle >= 0.0 && angle < 2 * std::numbers::pi) {
271 return angle;
272 }
273 double x = cevalm::fmod(angle, 2 * std::numbers::pi);
274 if (x < 0.0) {
275 x += 2 * std::numbers::pi;
276 }
277 return (x >= 2 * std::numbers::pi) ? 0.0 : x;
278 }
279
286 static constexpr double wrap_degrees_360(double angle) {
287 if (angle >= 0.0 && angle < 360.0) {
288 return angle;
289 }
290 double x = cevalm::fmod(angle, 360.0);
291 if (x < 0.0) {
292 x += 360.0;
293 }
294 return (x >= 360.0) ? 0.0 : x;
295 }
296
303 static constexpr double wrap_revolutions_360(double angle) {
304 if (angle >= 0.0 && angle < 1.0) {
305 return angle;
306 }
307 double x = cevalm::fmod(angle, 1.0);
308 if (x < 0.0) {
309 x += 1.0;
310 }
311 return (x >= 1.0) ? 0.0 : x;
312 }
313
320 static constexpr double wrap_gradians_360(double angle) {
321 if (angle >= 0.0 && angle < 400.0) {
322 return angle;
323 }
324 double x = cevalm::fmod(angle, 400.0);
325 if (x < 0.0) {
326 x += 400.0;
327 }
328 return (x >= 400.0) ? 0.0 : x;
329 }
330};
constexpr double wrapped_revolutions_180() const
Gets the value of this rotation in revolutions from [-0.5, 0.5].
Definition rotation2d.h:108
constexpr Rotation2d & operator*=(double scalar)
Multiplies this rotation by a scalar.
Definition rotation2d.h:153
constexpr double radians() const
Gets the rotation in radians.
Definition rotation2d.h:72
constexpr double f_sin() const
Gets the sine of the rotation.
Definition rotation2d.h:87
static constexpr double wrap_degrees_180(double angle)
Definition rotation2d.h:212
constexpr double wrapped_gradians_180() const
Gets the value of this rotation in gradians from [-200, 200].
Definition rotation2d.h:111
constexpr Rotation2d(units::Length x, units::Length y)
Definition rotation2d.h:60
static constexpr double wrap_revolutions_360(double angle)
Definition rotation2d.h:303
static constexpr double wrap_gradians_360(double angle)
Definition rotation2d.h:320
constexpr Rotation2d(double x, double y)
Definition rotation2d.h:40
constexpr Rotation2d & operator/=(double scalar)
Divides this rotation by a scalar.
Definition rotation2d.h:159
constexpr Rotation2d operator-(Rotation2d other) const
Subtracts another rotation from this rotation.
Definition rotation2d.h:136
constexpr double revolutions() const
Gets the rotation in revolutions.
Definition rotation2d.h:78
constexpr double wrapped_degrees_180() const
Gets the value of this rotation in degrees from [-180, 180].
Definition rotation2d.h:105
constexpr double degrees() const
Gets the rotation in degrees.
Definition rotation2d.h:75
constexpr double f_tan() const
Gets the tangent of the rotation.
Definition rotation2d.h:90
static constexpr double wrap_gradians_180(double angle)
Definition rotation2d.h:250
constexpr double wrapped_degrees_360() const
Gets the value of this rotation in degrees from [0, 360).
Definition rotation2d.h:117
constexpr double wrapped_gradians_360() const
Gets the value of this rotation in gradians from [0, 400).
Definition rotation2d.h:123
constexpr Rotation2d(units::Angle value)
Constructs a rotation given a units::Angle.
Definition rotation2d.h:26
constexpr Rotation2d operator*(double scalar) const
Multiplies this rotation by a scalar.
Definition rotation2d.h:145
static constexpr double wrap_radians_180(double angle)
Definition rotation2d.h:193
constexpr units::Angle angle() const
Gets an Angle equal to this rotation.
Definition rotation2d.h:64
constexpr Rotation2d & operator+=(Rotation2d other)
Adds another rotation to this rotation.
Definition rotation2d.h:133
constexpr double angle(units::Angle unit) const
Gets the angle in the supplied unit.
Definition rotation2d.h:69
static constexpr double wrap_revolutions_180(double angle)
Definition rotation2d.h:231
constexpr Rotation2d operator+(Rotation2d other) const
Adds another rotation to this rotation.
Definition rotation2d.h:126
constexpr Rotation2d operator-() const
Inverts this rotation (conjugate).
Definition rotation2d.h:142
static constexpr double rad2deg(double rad)
Definition rotation2d.h:185
constexpr bool operator==(Rotation2d other) const
Checks exact equality between this rotation and another rotation.
Definition rotation2d.h:162
constexpr Rotation2d()=default
Default Constructor for Rotation2d.
constexpr double gradians() const
Gets the rotation in gradians.
Definition rotation2d.h:81
friend constexpr Rotation2d operator*(double scalar, Rotation2d rotation)
Multiplies a scalar by this rotation.
Definition rotation2d.h:148
constexpr double wrapped_radians_180() const
Gets the value of this rotation in radians from [-pi, pi].
Definition rotation2d.h:102
constexpr Rotation2d & operator-=(Rotation2d other)
Subtracts another rotation from this rotation.
Definition rotation2d.h:139
static constexpr double deg2rad(double deg)
Definition rotation2d.h:177
constexpr Rotation2d inverse() const
Inverts this rotation (conjugate).
Definition rotation2d.h:93
constexpr Rotation2d(double value)
Constructs a rotation given an angle in radians.
Definition rotation2d.h:29
constexpr Rotation2d opposite() const
Flips this rotation across the origin.
Definition rotation2d.h:96
constexpr EMat< 2, 2 > rotation_matrix() const
Gets the rotation matrix corresponding to this rotation.
Definition rotation2d.h:99
constexpr bool is_near(Rotation2d other, units::Angle tolerance=units::Angle(1e-6)) const
Compares two angles with a default tolerance of 1e-6 radians.
Definition rotation2d.h:167
static constexpr double wrap_radians_360(double angle)
Definition rotation2d.h:269
constexpr double wrapped_radians_360() const
Gets the value of this rotation in radians from [0, 2pi).
Definition rotation2d.h:114
static constexpr double wrap_degrees_360(double angle)
Definition rotation2d.h:286
constexpr double wrapped_revolutions_360() const
Gets the value of this rotation in revolutions from [0, 1).
Definition rotation2d.h:120
constexpr Rotation2d operator/(double scalar) const
Divides this rotation by a scalar.
Definition rotation2d.h:156
constexpr double f_cos() const
Gets the cosine of the rotation.
Definition rotation2d.h:84