8#include <initializer_list>
18#include "units/length.h"
41 : m_translation{
std::move(translation)},
42 m_rotation{
std::move(rotation)} {}
53 : m_translation{x, y}, m_rotation{
std::move(rotation)} {}
61 constexpr explicit Pose2d(
const Eigen::Matrix3d& matrix)
62 : m_translation{
Eigen::Vector2d{{matrix(0, 2)}, {matrix(1, 2)}}},
63 m_rotation{Eigen::Matrix2d{{matrix(0, 0), matrix(0, 1)},
64 {matrix(1, 0), matrix(1, 1)}}} {
65 if (matrix(2, 0) != 0.0 || matrix(2, 1) != 0.0 || matrix(2, 2) != 1.0) {
66 throw std::domain_error(
"Affine transformation matrix is invalid");
85 return TransformBy(other);
113 constexpr units::meter_t
X()
const {
return m_translation.X(); }
120 constexpr units::meter_t
Y()
const {
return m_translation.Y(); }
137 return Pose2d{m_translation * scalar, m_rotation * scalar};
148 return *
this * (1.0 / scalar);
159 return {m_translation.RotateBy(other), m_rotation.RotateBy(other)};
196 return {m_translation.RotateAround(point, rot), m_rotation.RotateBy(rot)};
203 auto vec = m_translation.ToVector();
204 auto mat = m_rotation.ToMatrix();
205 return Eigen::Matrix3d{{mat(0, 0), mat(0, 1), vec(0)},
206 {mat(1, 0), mat(1, 1), vec(1)},
220 return *std::min_element(
221 poses.begin(), poses.end(), [
this](
const Pose2d& a,
const Pose2d& b) {
222 auto aDistance = this->Translation().Distance(a.Translation());
223 auto bDistance = this->Translation().Distance(b.Translation());
226 if (aDistance == bDistance) {
228 (this->Rotation() - a.Rotation()).Radians().value()) <
230 (this->Rotation() - b.Rotation()).Radians().value());
232 return aDistance < bDistance;
246 return *std::min_element(
247 poses.begin(), poses.end(), [
this](
const Pose2d& a,
const Pose2d& b) {
248 auto aDistance = this->Translation().Distance(a.Translation());
249 auto bDistance = this->Translation().Distance(b.Translation());
252 if (aDistance == bDistance) {
254 (this->Rotation() - a.Rotation()).Radians().value()) <
256 (this->Rotation() - b.Rotation()).Radians().value());
258 return aDistance < bDistance;
263 Translation2d m_translation;
264 Rotation2d m_rotation;
284 return Transform2d{pose.Translation(), pose.Rotation()};
288 return {m_translation + (other.Translation().
RotateBy(m_rotation)),
289 other.Rotation() + m_rotation};
294 return {transform.Translation(), transform.Rotation()};
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
namespace for Niels Lohmann
Definition json.h:99
Represents a 2D pose containing translational and rotational elements.
Definition Pose2d.h:27
constexpr Pose2d TransformBy(const Transform2d &other) const
Transforms the pose by the given transformation and returns the new pose.
Definition Pose2d.h:287
constexpr Transform2d operator-(const Pose2d &other) const
Returns the Transform2d that maps the one pose to another.
Definition Pose2d.h:282
constexpr Pose2d operator+(const Transform2d &other) const
Transforms the pose by the given transformation and returns the new transformed pose.
Definition Pose2d.h:84
constexpr Pose2d(Translation2d translation, Rotation2d rotation)
Constructs a pose with the specified translation and rotation.
Definition Pose2d.h:40
constexpr Pose2d RotateAround(const Translation2d &point, const Rotation2d &rot) const
Rotates the current pose around a point in 2D space.
Definition Pose2d.h:194
constexpr Pose2d()=default
Constructs a pose at the origin facing toward the positive X axis.
constexpr const Rotation2d & Rotation() const
Returns the underlying rotation.
Definition Pose2d.h:127
constexpr Pose2d operator*(double scalar) const
Multiplies the current pose by a scalar.
Definition Pose2d.h:136
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition Pose2d.h:120
constexpr Pose2d(units::meter_t x, units::meter_t y, Rotation2d rotation)
Constructs a pose with x and y translations instead of a separate Translation2d.
Definition Pose2d.h:52
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition Pose2d.h:113
constexpr bool operator==(const Pose2d &) const =default
Checks equality between this Pose2d and another object.
constexpr Pose2d RotateBy(const Rotation2d &other) const
Rotates the pose around the origin and returns the new pose.
Definition Pose2d.h:158
constexpr Pose2d RelativeTo(const Pose2d &other) const
Returns the current pose relative to the given pose.
Definition Pose2d.h:292
constexpr Pose2d(const Eigen::Matrix3d &matrix)
Constructs a pose with the specified affine transformation matrix.
Definition Pose2d.h:61
constexpr Pose2d operator/(double scalar) const
Divides the current pose by a scalar.
Definition Pose2d.h:147
constexpr Pose2d Nearest(std::span< const Pose2d > poses) const
Returns the nearest Pose2d from a collection of poses.
Definition Pose2d.h:219
constexpr const Translation2d & Translation() const
Returns the underlying translation.
Definition Pose2d.h:106
constexpr Pose2d Nearest(std::initializer_list< Pose2d > poses) const
Returns the nearest Pose2d from a collection of poses.
Definition Pose2d.h:245
constexpr Eigen::Matrix3d ToMatrix() const
Returns an affine transformation matrix representation of this pose.
Definition Pose2d.h:202
A rotation in a 2D coordinate frame represented by a point on the unit circle (cosine and sine).
Definition Rotation2d.h:26
Represents a translation in 2D space.
Definition Translation2d.h:30
Definition variable.hpp:742
Definition SystemServer.h:9
WPILIB_DLLEXPORT void to_json(wpi::json &json, const Rotation3d &rotation)
WPILIB_DLLEXPORT void from_json(const wpi::json &json, Rotation3d &rotation)
Definition PointerIntPair.h:280