ThunderLib
Loading...
Searching...
No Matches
CanonicalAngle.hpp
1#pragma once
2
3#include <frc/geometry/Rotation2d.h>
4#include <units/angle.h>
5#include <cassert>
6
7namespace thunder {
8namespace types {
9
17 double m_cos = 1.0;
18 double m_sin = 0.0;
19
20 public:
24 CanonicalAngle() = default;
25
30 /*implicit*/ CanonicalAngle(const frc::Rotation2d& rotation)
31 : m_cos(rotation.Cos()), m_sin(rotation.Sin()) {}
32
37 /*implicit*/ CanonicalAngle(units::angle_unit auto value) // NOLINT
38 : m_cos{gcem::cos(value.template convert<units::radian>().value())},
39 m_sin{gcem::sin(value.template convert<units::radian>().value())} {}
40
46 CanonicalAngle(double x, double y) {
47 double magnitude = gcem::hypot(x, y);
48 assert((magnitude > 1e-6) && "Cannot create CanonicalAngle with zero magnitude");
49 m_cos = x / magnitude;
50 m_sin = y / magnitude;
51 }
52
53 double cos() const { return m_cos; }
54 double sin() const { return m_sin; }
55 double tan() const { return m_sin / m_cos; }
56
57 units::radian_t radians() const { return units::radian_t{gcem::atan2(m_sin, m_cos)}; }
58
59 units::degree_t degrees() const { return radians().convert<units::degree>(); }
60
61 /*implicit*/ operator frc::Rotation2d() const { return frc::Rotation2d{m_cos, m_sin}; }
62
63 /*implicit*/ operator units::angle_unit auto() const { return radians(); }
64
65 bool operator==(const CanonicalAngle& other) const {
66 return gcem::hypot(cos() - other.cos(), sin() - other.sin()) < 1E-9;
67 }
68
69 bool operator!=(const CanonicalAngle& other) const { return !(*this == other); }
70
75 CanonicalAngle operator-() const { return CanonicalAngle{-radians()}; }
76
77 CanonicalAngle operator+(const CanonicalAngle& other) const { return rotateBy(other); }
78
79 CanonicalAngle operator-(const CanonicalAngle& other) const { return rotateBy(-other); }
80
81 CanonicalAngle operator*(double scalar) const { return CanonicalAngle(radians() * scalar); }
82
83 CanonicalAngle operator/(double scalar) const { return CanonicalAngle(radians() / scalar); }
84
91 return {cos() * other.cos() - sin() * other.sin(), cos() * other.sin() + sin() * other.cos()};
92 }
93
98 CanonicalAngle supplementary() const { return CanonicalAngle{-m_cos, -m_sin}; }
99
105 bool isSupplementaryTo(const CanonicalAngle& other) const { return supplementary() == other; }
106};
107
108} // namespace types
109
110using types::CanonicalAngle;
111
112} // namespace thunder
113
Definition CanonicalAngle.hpp:16
CanonicalAngle(double x, double y)
Definition CanonicalAngle.hpp:46
CanonicalAngle rotateBy(const CanonicalAngle &other) const
Definition CanonicalAngle.hpp:90
bool isSupplementaryTo(const CanonicalAngle &other) const
Definition CanonicalAngle.hpp:105
CanonicalAngle(const frc::Rotation2d &rotation)
Definition CanonicalAngle.hpp:30
CanonicalAngle operator-() const
Definition CanonicalAngle.hpp:75
CanonicalAngle supplementary() const
Definition CanonicalAngle.hpp:98
CanonicalAngle(units::angle_unit auto value)
Definition CanonicalAngle.hpp:37