2020-12-26 14:12:05 -08:00
|
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
2024-10-14 16:08:10 -07:00
|
|
|
|
#include <type_traits>
|
2024-12-07 21:29:02 -08:00
|
|
|
|
#include <utility>
|
2024-10-14 16:08:10 -07:00
|
|
|
|
|
2024-12-07 21:29:02 -08:00
|
|
|
|
#include <Eigen/Core>
|
2024-10-14 16:08:10 -07:00
|
|
|
|
#include <gcem.hpp>
|
2021-08-20 09:02:01 -07:00
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
|
#include "wpi/math/linalg/ct_matrix.hpp"
|
|
|
|
|
|
#include "wpi/math/util/MathShared.hpp"
|
2025-11-07 19:57:55 -05:00
|
|
|
|
#include "wpi/units/angle.hpp"
|
|
|
|
|
|
#include "wpi/util/StackTrace.hpp"
|
|
|
|
|
|
#include "wpi/util/SymbolExports.hpp"
|
|
|
|
|
|
#include "wpi/util/json_fwd.hpp"
|
2019-07-31 22:15:22 -07:00
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
|
namespace wpi::math {
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* A rotation in a 2D coordinate frame represented by a point on the unit circle
|
2019-07-24 02:57:39 -04:00
|
|
|
|
* (cosine and sine).
|
|
|
|
|
|
*/
|
2021-08-20 09:02:01 -07:00
|
|
|
|
class WPILIB_DLLEXPORT Rotation2d {
|
2019-07-24 02:57:39 -04:00
|
|
|
|
public:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Rotation2d with a default angle of 0 degrees.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr Rotation2d() = default;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2024-01-26 12:49:22 -08:00
|
|
|
|
* Constructs a Rotation2d with the given angle.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2024-01-26 12:49:22 -08:00
|
|
|
|
* @param value The value of the angle.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2025-11-07 20:00:05 -05:00
|
|
|
|
constexpr Rotation2d(wpi::units::angle_unit auto value) // NOLINT
|
|
|
|
|
|
: m_cos{gcem::cos(value.template convert<wpi::units::radian>().value())},
|
2025-11-07 20:01:58 -05:00
|
|
|
|
m_sin{gcem::sin(value.template convert<wpi::units::radian>().value())} {
|
|
|
|
|
|
}
|
2020-07-03 21:58:19 -07:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Rotation2d with the given x and y (cosine and sine)
|
|
|
|
|
|
* components. The x and y don't have to be normalized.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param x The x component or cosine of the rotation.
|
|
|
|
|
|
* @param y The y component or sine of the rotation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d(double x, double y) {
|
|
|
|
|
|
double magnitude = gcem::hypot(x, y);
|
|
|
|
|
|
if (magnitude > 1e-6) {
|
|
|
|
|
|
m_cos = x / magnitude;
|
2025-01-03 22:44:17 -08:00
|
|
|
|
m_sin = y / magnitude;
|
2024-10-14 16:08:10 -07:00
|
|
|
|
} else {
|
|
|
|
|
|
m_cos = 1.0;
|
2025-01-03 22:44:17 -08:00
|
|
|
|
m_sin = 0.0;
|
2024-10-14 16:08:10 -07:00
|
|
|
|
if (!std::is_constant_evaluated()) {
|
|
|
|
|
|
wpi::math::MathSharedStore::ReportError(
|
|
|
|
|
|
"x and y components of Rotation2d are zero\n{}",
|
2025-11-07 20:00:05 -05:00
|
|
|
|
wpi::util::GetStackTrace(1));
|
2024-10-14 16:08:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2024-12-07 21:29:02 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Rotation2d from a rotation matrix.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param rotationMatrix The rotation matrix.
|
|
|
|
|
|
* @throws std::domain_error if the rotation matrix isn't special orthogonal.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr explicit Rotation2d(const Eigen::Matrix2d& rotationMatrix) {
|
|
|
|
|
|
auto impl =
|
|
|
|
|
|
[]<typename Matrix2d>(const Matrix2d& R) -> std::pair<double, double> {
|
|
|
|
|
|
// Require that the rotation matrix is special orthogonal. This is true if
|
|
|
|
|
|
// the matrix is orthogonal (RRᵀ = I) and normalized (determinant is 1).
|
|
|
|
|
|
if ((R * R.transpose() - Matrix2d::Identity()).norm() > 1e-9) {
|
|
|
|
|
|
throw std::domain_error("Rotation matrix isn't orthogonal");
|
|
|
|
|
|
}
|
2024-12-28 14:03:29 -08:00
|
|
|
|
// HACK: Uses ct_matrix instead of <Eigen/LU> for determinant because
|
|
|
|
|
|
// including <Eigen/LU> doubles compilation times on MSVC, even if
|
|
|
|
|
|
// this constructor is unused. MSVC's frontend inefficiently parses
|
|
|
|
|
|
// large headers; GCC and Clang are largely unaffected.
|
|
|
|
|
|
if (gcem::abs(ct_matrix{R}.determinant() - 1.0) > 1e-9) {
|
2024-12-07 21:29:02 -08:00
|
|
|
|
throw std::domain_error(
|
|
|
|
|
|
"Rotation matrix is orthogonal but not special orthogonal");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// R = [cosθ −sinθ]
|
|
|
|
|
|
// [sinθ cosθ]
|
|
|
|
|
|
return {R(0, 0), R(1, 0)};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (std::is_constant_evaluated()) {
|
|
|
|
|
|
auto cossin = impl(ct_matrix2d{rotationMatrix});
|
|
|
|
|
|
m_cos = std::get<0>(cossin);
|
|
|
|
|
|
m_sin = std::get<1>(cossin);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
auto cossin = impl(rotationMatrix);
|
|
|
|
|
|
m_cos = std::get<0>(cossin);
|
|
|
|
|
|
m_sin = std::get<1>(cossin);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
2024-11-01 17:16:18 -07:00
|
|
|
|
* Adds two rotations together, with the result being bounded between -π and
|
|
|
|
|
|
* π.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-02-27 16:56:56 -08:00
|
|
|
|
* For example, <code>Rotation2d{30_deg} + Rotation2d{60_deg}</code> equals
|
2025-11-07 20:00:05 -05:00
|
|
|
|
* <code>Rotation2d{wpi::units::radian_t{std::numbers::pi/2.0}}</code>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The rotation to add.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The sum of the two rotations.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d operator+(const Rotation2d& other) const {
|
|
|
|
|
|
return RotateBy(other);
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-06 15:15:00 -08:00
|
|
|
|
* Returns this rotation relative to another rotation.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-02-27 16:56:56 -08:00
|
|
|
|
* For example, <code>Rotation2d{10_deg} - Rotation2d{100_deg}</code> equals
|
2025-11-07 20:00:05 -05:00
|
|
|
|
* <code>Rotation2d{wpi::units::radian_t{-std::numbers::pi/2.0}}</code>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The rotation to subtract.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The difference between the two rotations.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d operator-(const Rotation2d& other) const {
|
|
|
|
|
|
return *this + -other;
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Takes the inverse of the current rotation. This is simply the negative of
|
|
|
|
|
|
* the current angular value.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The inverse of the current rotation.
|
|
|
|
|
|
*/
|
2024-12-06 21:00:09 -08:00
|
|
|
|
constexpr Rotation2d operator-() const { return Rotation2d{m_cos, -m_sin}; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2019-09-28 18:40:56 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Multiplies the current rotation by a scalar.
|
2022-09-28 21:34:29 -07:00
|
|
|
|
*
|
2019-09-28 18:40:56 -04:00
|
|
|
|
* @param scalar The scalar.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The new scaled Rotation2d.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d operator*(double scalar) const {
|
2024-12-06 21:00:09 -08:00
|
|
|
|
return Rotation2d{Radians() * scalar};
|
2024-10-14 16:08:10 -07:00
|
|
|
|
}
|
2019-09-28 18:40:56 -04:00
|
|
|
|
|
2022-09-28 21:34:29 -07:00
|
|
|
|
/**
|
|
|
|
|
|
* Divides the current rotation by a scalar.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param scalar The scalar.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The new scaled Rotation2d.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d operator/(double scalar) const {
|
|
|
|
|
|
return *this * (1.0 / scalar);
|
|
|
|
|
|
}
|
2022-09-28 21:34:29 -07:00
|
|
|
|
|
2019-09-08 14:20:26 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Checks equality between this Rotation2d and another object.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The other object.
|
|
|
|
|
|
* @return Whether the two objects are equal.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr bool operator==(const Rotation2d& other) const {
|
|
|
|
|
|
return gcem::hypot(Cos() - other.Cos(), Sin() - other.Sin()) < 1E-9;
|
|
|
|
|
|
}
|
2019-09-08 14:20:26 -04:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Adds the new rotation to the current rotation using a rotation matrix.
|
|
|
|
|
|
*
|
2021-02-16 21:06:01 -05:00
|
|
|
|
* <pre>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
* [cos_new] [other.cos, -other.sin][cos]
|
|
|
|
|
|
* [sin_new] = [other.sin, other.cos][sin]
|
2021-02-16 21:06:01 -05:00
|
|
|
|
* value_new = std::atan2(sin_new, cos_new)
|
|
|
|
|
|
* </pre>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The rotation to rotate by.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The new rotated Rotation2d.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d RotateBy(const Rotation2d& other) const {
|
|
|
|
|
|
return {Cos() * other.Cos() - Sin() * other.Sin(),
|
|
|
|
|
|
Cos() * other.Sin() + Sin() * other.Cos()};
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
[wpimath] Fix Rotation3d interpolation and document extrinsic vs intrinsic (#8544)
Documents the extrinsic vs intrinsic semantics of `plus()` and
`minus()`. (`rotateBy()` was documented in [a previous
PR](https://github.com/wpilibsuite/allwpilib/pull/5508))
Fixes usage of `plus()` and `minus()` in `Rotation3d.interpolate()`.
(Fixes #8523)
Fixes incorrect usages of `plus()`, `minus()`, and `rotateBy()`
throughout `Odometry3d`.
Adds explanatory comments for some `plus()`, `minus()`, and `rotateBy()`
operations.
Fixes `TimeInterpolatableBuffer` not using twists for `Pose3d` (this was
just because I happened to notice it, it isn't really related to the PR)
To check all of our usages of `plus()`, `minus()`, and `rotateBy()`, I
marked them as deprecated, checked compile errors from `./gradlew
compileJava`, and then undeprecated them. You can see all of the spots
that showed up (at least on the Java side) by viewing the diff for
241109c.
I wanted to present this alternative to #8526 because the change has its
own quirks, there's little time before kickoff, and there would be no
code-side warning to teams (and mentors) already used to the current
behavior.
2026-01-14 20:16:24 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the current rotation relative to the given rotation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The rotation describing the orientation of the new coordinate
|
|
|
|
|
|
* frame that the current rotation will be converted into.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The current rotation relative to the new orientation of the
|
|
|
|
|
|
* coordinate frame.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr Rotation2d RelativeTo(const Rotation2d& other) const {
|
|
|
|
|
|
return RotateBy(-other);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-07 21:29:02 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns matrix representation of this rotation.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr Eigen::Matrix2d ToMatrix() const {
|
|
|
|
|
|
// R = [cosθ −sinθ]
|
|
|
|
|
|
// [sinθ cosθ]
|
|
|
|
|
|
return Eigen::Matrix2d{{m_cos, -m_sin}, {m_sin, m_cos}};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
2024-12-06 21:00:09 -08:00
|
|
|
|
* Returns the radian value of the rotation constrained within [-π, π].
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2024-12-06 21:00:09 -08:00
|
|
|
|
* @return The radian value of the rotation constrained within [-π, π].
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2025-11-07 20:00:05 -05:00
|
|
|
|
constexpr wpi::units::radian_t Radians() const {
|
|
|
|
|
|
return wpi::units::radian_t{gcem::atan2(m_sin, m_cos)};
|
2024-12-06 21:00:09 -08:00
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2024-12-06 21:00:09 -08:00
|
|
|
|
* Returns the degree value of the rotation constrained within [-180, 180].
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2024-12-06 21:00:09 -08:00
|
|
|
|
* @return The degree value of the rotation constrained within [-180, 180].
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2025-11-07 20:00:05 -05:00
|
|
|
|
constexpr wpi::units::degree_t Degrees() const { return Radians(); }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the cosine of the rotation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The cosine of the rotation.
|
|
|
|
|
|
*/
|
2022-10-31 11:17:00 -05:00
|
|
|
|
constexpr double Cos() const { return m_cos; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the sine of the rotation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The sine of the rotation.
|
|
|
|
|
|
*/
|
2022-10-31 11:17:00 -05:00
|
|
|
|
constexpr double Sin() const { return m_sin; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the tangent of the rotation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The tangent of the rotation.
|
|
|
|
|
|
*/
|
2022-10-31 11:17:00 -05:00
|
|
|
|
constexpr double Tan() const { return Sin() / Cos(); }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
double m_cos = 1;
|
|
|
|
|
|
double m_sin = 0;
|
|
|
|
|
|
};
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
|
WPILIB_DLLEXPORT
|
2025-11-07 20:00:05 -05:00
|
|
|
|
void to_json(wpi::util::json& json, const Rotation2d& rotation);
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
|
WPILIB_DLLEXPORT
|
2025-11-07 20:00:05 -05:00
|
|
|
|
void from_json(const wpi::util::json& json, Rotation2d& rotation);
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
|
} // namespace wpi::math
|
2022-10-31 11:17:00 -05:00
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
|
#include "wpi/math/geometry/proto/Rotation2dProto.hpp"
|
|
|
|
|
|
#include "wpi/math/geometry/struct/Rotation2dStruct.hpp"
|