Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] Make Rotation2d implicitly convert from any angle unit #6316

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 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,11 @@ 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
constexpr Rotation2d(units::angle_unit auto value); // NOLINT

/**
* Constructs a Rotation2d with the given x and y (cosine and sine)
Expand Down
9 changes: 3 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,10 @@

namespace frc {

constexpr Rotation2d::Rotation2d(units::radian_t value)
constexpr Rotation2d::Rotation2d(units::angle_unit auto 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
18 changes: 3 additions & 15 deletions wpimath/src/main/native/include/units/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

#if !defined(_MSC_VER) || _MSC_VER > 1800
# define UNIT_HAS_LITERAL_SUPPORT
# define UNIT_HAS_VARIADIC_TEMPLATE_SUPPORT
#endif

#ifndef UNIT_LIB_DEFAULT_TYPE
Expand Down Expand Up @@ -358,25 +357,14 @@ template<> inline constexpr const char* abbreviation(const namespaceName::nameSi
/** @endcond */\
}

#if defined(UNIT_HAS_VARIADIC_TEMPLATE_SUPPORT)
#define UNIT_ADD_IS_UNIT_CATEGORY_TRAIT(unitCategory)\
namespace traits\
{\
template<typename... T> struct is_ ## unitCategory ## _unit : std::integral_constant<bool, units::all_true<units::traits::detail::is_ ## unitCategory ## _unit_impl<std::decay_t<T>>::value...>::value> {};\
template<typename... T> inline constexpr bool is_ ## unitCategory ## _unit_v = is_ ## unitCategory ## _unit<T...>::value;\
}
#else
#define UNIT_ADD_IS_UNIT_CATEGORY_TRAIT(unitCategory)\
namespace traits\
{\
template<typename T1, typename T2 = T1, typename T3 = T1>\
struct is_ ## unitCategory ## _unit : std::integral_constant<bool, units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T1>::type>::value &&\
units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T2>::type>::value &&\
units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T3>::type>::value>{};\
template<typename T1, typename T2 = T1, typename T3 = T1>\
inline constexpr bool is_ ## unitCategory ## _unit_v = is_ ## unitCategory ## _unit<T1, T2, T3>::value;\
}
#endif
}\
template <typename T>\
concept unitCategory ## _unit = traits::is_ ## unitCategory ## _unit_v<T>;

#define UNIT_ADD_CATEGORY_TRAIT(unitCategory)\
UNIT_ADD_CATEGORY_TRAIT_DETAIL(unitCategory)\
Expand Down
Loading