Skip to content

Commit

Permalink
[wpimath] Make Rotation2d implicitly convert from any angle unit
Browse files Browse the repository at this point in the history
Fixes #6312.
Supersedes #6315.
  • Loading branch information
calcmogul committed Jan 26, 2024
1 parent 68736d8 commit f9047fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
15 changes: 5 additions & 10 deletions wpimath/src/main/native/include/frc/geometry/Rotation2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,13 @@ class WPILIB_DLLEXPORT Rotation2d {
constexpr Rotation2d() = default;

/**
* Constructs a Rotation2d with the given radian value.
* Constructs a Rotation2d with the given angle.
*
* @param value The value of the angle in radians.
* @param value The value of the angle.
*/
constexpr Rotation2d(units::radian_t value); // NOLINT

/**
* Constructs a Rotation2d with the given degree value.
*
* @param value The value of the angle in degrees.
*/
constexpr Rotation2d(units::degree_t value); // NOLINT
template <typename Angle>
requires units::traits::is_angle_unit_v<Angle>
constexpr Rotation2d(Angle value); // NOLINT

/**
* Constructs a Rotation2d with the given x and y (cosine and sine)
Expand Down
11 changes: 5 additions & 6 deletions wpimath/src/main/native/include/frc/geometry/Rotation2d.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@

namespace frc {

constexpr Rotation2d::Rotation2d(units::radian_t value)
template <typename Angle>
requires units::traits::is_angle_unit_v<Angle>
constexpr Rotation2d::Rotation2d(Angle value)
: m_value(value),
m_cos(gcem::cos(value.to<double>())),
m_sin(gcem::sin(value.to<double>())) {}

constexpr Rotation2d::Rotation2d(units::degree_t value)
: Rotation2d(units::radian_t{value}) {}
m_cos(gcem::cos(value.template convert<units::radian>().value())),
m_sin(gcem::sin(value.template convert<units::radian>().value())) {}

constexpr Rotation2d::Rotation2d(double x, double y) {
double magnitude = gcem::hypot(x, y);
Expand Down

0 comments on commit f9047fb

Please sign in to comment.