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-18 16:08:41 -07:00
|
|
|
|
#include <algorithm>
|
2023-02-03 18:27:16 -05:00
|
|
|
|
#include <initializer_list>
|
|
|
|
|
|
#include <span>
|
|
|
|
|
|
|
2024-02-10 13:43:58 -05:00
|
|
|
|
#include <Eigen/Core>
|
2021-08-20 09:02:01 -07:00
|
|
|
|
#include <wpi/SymbolExports.h>
|
2023-09-22 16:01:27 -04:00
|
|
|
|
#include <wpi/json_fwd.h>
|
2021-08-20 09:02:01 -07:00
|
|
|
|
|
2023-07-31 19:17:02 -07:00
|
|
|
|
#include "frc/geometry/Rotation2d.h"
|
2025-07-31 23:05:39 -05:00
|
|
|
|
#include "units/area.h"
|
2020-08-08 11:54:03 -07:00
|
|
|
|
#include "units/length.h"
|
2024-06-04 21:27:32 -04:00
|
|
|
|
#include "units/math.h"
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
namespace frc {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Represents a translation in 2D space.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
* This object can be used to represent a point or a vector.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This assumes that you are using conventional mathematical axes.
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* When the robot is at the origin facing in the positive X direction, forward
|
|
|
|
|
|
* is positive X and left is positive Y.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2021-08-20 09:02:01 -07:00
|
|
|
|
class WPILIB_DLLEXPORT Translation2d {
|
2019-07-24 02:57:39 -04:00
|
|
|
|
public:
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Translation2d with X and Y components equal to zero.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr Translation2d() = default;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Translation2d with the X and Y components equal to the
|
|
|
|
|
|
* provided values.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param x The x component of the translation.
|
|
|
|
|
|
* @param y The y component of the translation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d(units::meter_t x, units::meter_t y)
|
|
|
|
|
|
: m_x{x}, m_y{y} {}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2020-10-21 00:22:41 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Constructs a Translation2d with the provided distance and angle. This is
|
|
|
|
|
|
* essentially converting from polar coordinates to Cartesian coordinates.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param distance The distance from the origin to the end of the translation.
|
|
|
|
|
|
* @param angle The angle between the x-axis and the translation vector.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d(units::meter_t distance, const Rotation2d& angle)
|
|
|
|
|
|
: m_x{distance * angle.Cos()}, m_y{distance * angle.Sin()} {}
|
2020-10-21 00:22:41 -04:00
|
|
|
|
|
2024-02-10 13:43:58 -05:00
|
|
|
|
/**
|
2024-12-07 15:49:17 -08:00
|
|
|
|
* Constructs a Translation2d from a 2D translation vector. The values are
|
|
|
|
|
|
* assumed to be in meters.
|
2024-02-10 13:43:58 -05:00
|
|
|
|
*
|
2024-12-07 15:49:17 -08:00
|
|
|
|
* @param vector The translation vector.
|
2024-02-10 13:43:58 -05:00
|
|
|
|
*/
|
2024-10-18 16:08:41 -07:00
|
|
|
|
constexpr explicit Translation2d(const Eigen::Vector2d& vector)
|
2024-12-07 15:49:17 -08:00
|
|
|
|
: m_x{units::meter_t{vector.x()}}, m_y{units::meter_t{vector.y()}} {}
|
2024-02-10 13:43:58 -05:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Calculates the distance between two translations in 2D space.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* The distance between translations is defined as √((x₂−x₁)²+(y₂−y₁)²).
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to compute the distance to.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The distance between the two translations.
|
|
|
|
|
|
*/
|
2024-06-04 21:27:32 -04:00
|
|
|
|
constexpr units::meter_t Distance(const Translation2d& other) const {
|
|
|
|
|
|
return units::math::hypot(other.m_x - m_x, other.m_y - m_y);
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2025-07-31 23:05:39 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Calculates the square of the distance between two translations in 2D space.
|
|
|
|
|
|
* This is equivalent to squaring the result of Distance(Translation2d), but
|
|
|
|
|
|
* avoids computing a square root.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The square of the distance between translations is defined as
|
|
|
|
|
|
* (x₂−x₁)²+(y₂−y₁)².
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to compute the squared distance to.
|
|
|
|
|
|
* @return The square of the distance between the two translations.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr units::square_meter_t SquaredDistance(
|
|
|
|
|
|
const Translation2d& other) const {
|
|
|
|
|
|
return units::math::pow<2>(other.m_x - m_x) +
|
|
|
|
|
|
units::math::pow<2>(other.m_y - m_y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the X component of the translation.
|
|
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* @return The X component of the translation.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2022-10-31 11:17:00 -05:00
|
|
|
|
constexpr units::meter_t X() const { return m_x; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the Y component of the translation.
|
|
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* @return The Y component of the translation.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*/
|
2022-10-31 11:17:00 -05:00
|
|
|
|
constexpr units::meter_t Y() const { return m_y; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2024-02-10 13:43:58 -05:00
|
|
|
|
/**
|
2024-12-07 15:49:17 -08:00
|
|
|
|
* Returns a 2D translation vector representation of this translation.
|
2024-02-10 13:43:58 -05:00
|
|
|
|
*
|
2024-12-07 15:49:17 -08:00
|
|
|
|
* @return A 2D translation vector representation of this translation.
|
2024-02-10 13:43:58 -05:00
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Eigen::Vector2d ToVector() const {
|
|
|
|
|
|
return Eigen::Vector2d{{m_x.value(), m_y.value()}};
|
|
|
|
|
|
}
|
2024-02-10 13:43:58 -05:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the norm, or distance from the origin to the translation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The norm of the translation.
|
|
|
|
|
|
*/
|
2024-10-18 16:08:41 -07:00
|
|
|
|
constexpr units::meter_t Norm() const { return units::math::hypot(m_x, m_y); }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2025-07-31 23:05:39 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the squared norm, or squared distance from the origin to the
|
|
|
|
|
|
* translation. This is equivalent to squaring the result of Norm(), but
|
|
|
|
|
|
* avoids computing a square root.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The squared norm of the translation.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr units::square_meter_t SquaredNorm() const {
|
|
|
|
|
|
return units::math::pow<2>(m_x) + units::math::pow<2>(m_y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-15 00:22:00 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the angle this translation forms with the positive X axis.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The angle of the translation
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Rotation2d Angle() const {
|
|
|
|
|
|
return Rotation2d{m_x.value(), m_y.value()};
|
|
|
|
|
|
}
|
2022-05-15 00:22:00 -04:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Applies a rotation to the translation in 2D space.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* This multiplies the translation vector by a counterclockwise rotation
|
|
|
|
|
|
* matrix of the given angle.
|
|
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* <pre>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
* [x_new] [other.cos, -other.sin][x]
|
|
|
|
|
|
* [y_new] = [other.sin, other.cos][y]
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* </pre>
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* For example, rotating a Translation2d of <2, 0> by 90 degrees will
|
|
|
|
|
|
* return a Translation2d of <0, 2>.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The rotation to rotate the translation by.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The new rotated translation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d RotateBy(const Rotation2d& other) const {
|
|
|
|
|
|
return {m_x * other.Cos() - m_y * other.Sin(),
|
|
|
|
|
|
m_x * other.Sin() + m_y * other.Cos()};
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2024-06-04 21:27:32 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Rotates this translation around another translation in 2D space.
|
|
|
|
|
|
*
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
* [x_new] [rot.cos, -rot.sin][x - other.x] [other.x]
|
|
|
|
|
|
* [y_new] = [rot.sin, rot.cos][y - other.y] + [other.y]
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The other translation to rotate around.
|
|
|
|
|
|
* @param rot The rotation to rotate the translation by.
|
|
|
|
|
|
* @return The new rotated translation.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr Translation2d RotateAround(const Translation2d& other,
|
2024-10-14 16:08:10 -07:00
|
|
|
|
const Rotation2d& rot) const {
|
|
|
|
|
|
return {(m_x - other.X()) * rot.Cos() - (m_y - other.Y()) * rot.Sin() +
|
|
|
|
|
|
other.X(),
|
|
|
|
|
|
(m_x - other.X()) * rot.Sin() + (m_y - other.Y()) * rot.Cos() +
|
|
|
|
|
|
other.Y()};
|
|
|
|
|
|
}
|
2024-06-04 21:27:32 -04:00
|
|
|
|
|
2025-07-31 23:05:39 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Computes the dot product between this translation and another translation
|
|
|
|
|
|
* in 2D space.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The dot product between two translations is defined as x₁x₂+y₁y₂.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to compute the dot product with.
|
|
|
|
|
|
* @return The dot product between the two translations.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr units::square_meter_t Dot(const Translation2d& other) const {
|
|
|
|
|
|
return m_x * other.X() + m_y * other.Y();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Computes the cross product between this translation and another translation
|
|
|
|
|
|
* in 2D space.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The 2D cross product between two translations is defined as x₁y₂-x₂y₁.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to compute the cross product with.
|
|
|
|
|
|
* @return The cross product between the two translations.
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr units::square_meter_t Cross(const Translation2d& other) const {
|
|
|
|
|
|
return m_x * other.Y() - m_y * other.X();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Returns the sum of two translations in 2D space.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* For example, Translation3d{1.0, 2.5} + Translation3d{2.0, 5.5} =
|
|
|
|
|
|
* Translation3d{3.0, 8.0}.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to add.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The sum of the translations.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d operator+(const Translation2d& other) const {
|
|
|
|
|
|
return {X() + other.X(), Y() + other.Y()};
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Returns the difference between two translations.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* For example, Translation2d{5.0, 4.0} - Translation2d{1.0, 2.0} =
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Translation2d{4.0, 2.0}.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param other The translation to subtract.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The difference between the two translations.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d operator-(const Translation2d& other) const {
|
|
|
|
|
|
return *this + -other;
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the inverse of the current translation. This is equivalent to
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* rotating by 180 degrees, flipping the point over both axes, or negating all
|
|
|
|
|
|
* components of the translation.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @return The inverse of the current translation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d operator-() const { return {-m_x, -m_y}; }
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Returns the translation multiplied by a scalar.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* For example, Translation2d{2.0, 2.5} * 2 = Translation2d{4.0, 5.0}.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param scalar The scalar to multiply by.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The scaled translation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d operator*(double scalar) const {
|
|
|
|
|
|
return {scalar * m_x, scalar * m_y};
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* Returns the translation divided by a scalar.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
2022-05-06 08:41:23 -07:00
|
|
|
|
* For example, Translation2d{2.0, 2.5} / 2 = Translation2d{1.0, 1.25}.
|
2019-07-24 02:57:39 -04:00
|
|
|
|
*
|
|
|
|
|
|
* @param scalar The scalar to divide by.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return The scaled translation.
|
|
|
|
|
|
*/
|
2024-10-14 16:08:10 -07:00
|
|
|
|
constexpr Translation2d operator/(double scalar) const {
|
|
|
|
|
|
return operator*(1.0 / scalar);
|
|
|
|
|
|
}
|
2019-07-24 02:57:39 -04:00
|
|
|
|
|
2019-09-08 14:20:26 -04:00
|
|
|
|
/**
|
|
|
|
|
|
* Checks equality between this Translation2d 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 Translation2d& other) const {
|
|
|
|
|
|
return units::math::abs(m_x - other.m_x) < 1E-9_m &&
|
|
|
|
|
|
units::math::abs(m_y - other.m_y) < 1E-9_m;
|
|
|
|
|
|
}
|
2019-09-08 14:20:26 -04:00
|
|
|
|
|
2023-02-03 18:27:16 -05:00
|
|
|
|
/**
|
|
|
|
|
|
* Returns the nearest Translation2d from a collection of translations
|
|
|
|
|
|
* @param translations The collection of translations.
|
|
|
|
|
|
* @return The nearest Translation2d from the collection.
|
|
|
|
|
|
*/
|
2024-10-18 16:08:41 -07:00
|
|
|
|
constexpr Translation2d Nearest(
|
|
|
|
|
|
std::span<const Translation2d> translations) const {
|
2025-06-13 18:50:05 -07:00
|
|
|
|
return *std::min_element(
|
|
|
|
|
|
translations.begin(), translations.end(),
|
|
|
|
|
|
[this](const Translation2d& a, const Translation2d& b) {
|
|
|
|
|
|
return this->Distance(a) < this->Distance(b);
|
|
|
|
|
|
});
|
2024-10-18 16:08:41 -07:00
|
|
|
|
}
|
2023-02-03 18:27:16 -05:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the nearest Translation2d from a collection of translations
|
|
|
|
|
|
* @param translations The collection of translations.
|
|
|
|
|
|
* @return The nearest Translation2d from the collection.
|
|
|
|
|
|
*/
|
2024-10-18 16:08:41 -07:00
|
|
|
|
constexpr Translation2d Nearest(
|
|
|
|
|
|
std::initializer_list<Translation2d> translations) const {
|
2025-06-13 18:50:05 -07:00
|
|
|
|
return *std::min_element(
|
|
|
|
|
|
translations.begin(), translations.end(),
|
|
|
|
|
|
[this](const Translation2d& a, const Translation2d& b) {
|
|
|
|
|
|
return this->Distance(a) < this->Distance(b);
|
|
|
|
|
|
});
|
2024-10-18 16:08:41 -07:00
|
|
|
|
}
|
2023-02-03 18:27:16 -05:00
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
private:
|
2019-08-17 01:00:33 -04:00
|
|
|
|
units::meter_t m_x = 0_m;
|
|
|
|
|
|
units::meter_t m_y = 0_m;
|
2019-07-24 02:57:39 -04:00
|
|
|
|
};
|
2019-11-02 13:35:03 -05:00
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
|
WPILIB_DLLEXPORT
|
2019-11-02 13:35:03 -05:00
|
|
|
|
void to_json(wpi::json& json, const Translation2d& state);
|
|
|
|
|
|
|
2021-08-20 09:02:01 -07:00
|
|
|
|
WPILIB_DLLEXPORT
|
2019-11-02 13:35:03 -05:00
|
|
|
|
void from_json(const wpi::json& json, Translation2d& state);
|
|
|
|
|
|
|
2019-07-24 02:57:39 -04:00
|
|
|
|
} // namespace frc
|
2022-10-31 11:17:00 -05:00
|
|
|
|
|
2023-11-21 13:11:57 -05:00
|
|
|
|
#include "frc/geometry/proto/Translation2dProto.h"
|
|
|
|
|
|
#include "frc/geometry/struct/Translation2dStruct.h"
|