6#include "core/math/geometry/rotation2d.h"
7#include "core/math/geometry/transform2d.h"
8#include "core/math/geometry/translation2d.h"
9#include "core/math/geometry/twist2d.h"
10#include "core/math/units.h"
23 Translation2d translation_;
56 constexpr Pose2d(units::Length
x, units::Length
y,
double radians)
57 : translation_{
x,
y}, rotation_{radians} {}
68 const Eigen::Vector3d& pose_vector, units::Length length_unit,
69 units::Angle angle_unit = units::radians
72 units::Length(pose_vector[0], length_unit),
units::Length(pose_vector[1], length_unit)
74 rotation_{
units::Angle(pose_vector[2], angle_unit)} {}
77 constexpr units::Length
x()
const {
return translation_.x(); }
80 constexpr double x(units::Length unit)
const {
return translation_.x(unit); }
83 constexpr void set_x(units::Length val) { translation_.set_x(val); }
86 constexpr units::Length
y()
const {
return translation_.y(); }
89 constexpr double y(units::Length unit)
const {
return translation_.y(unit); }
92 constexpr void set_y(units::Length val) { translation_.set_y(val); }
95 constexpr Translation2d
translation()
const {
return translation_; }
107 constexpr units::Angle
angle()
const {
return rotation_.angle(); }
110 constexpr double angle(units::Angle unit)
const {
return rotation_.angle(unit); }
116 EVec<3>
as_vector(units::Length length_unit, units::Angle angle_unit = units::radians)
const {
118 translation_.x().to(length_unit), translation_.y().to(length_unit),
119 rotation_.angle().to(angle_unit)
124 constexpr units::Length
distance(Translation2d point)
const {
125 return translation_.distance(point);
130 return distance(other.translation_);
135 const auto delta = point - translation_;
136 if (delta.x().internal() == 0 && delta.y().internal() == 0) {
139 return delta.
theta();
143 constexpr units::Angle
angle_to(Translation2d point)
const {
144 return (
bearing_to(point) - rotation_).angle();
149 return translation_ + point.
rotate_by(rotation_);
154 return (point - translation_).
rotate_by(-rotation_);
178 translation_ + (transform.
translation().rotate_by(rotation_)),
197 translation_ + (end.translation_ - translation_) * fraction,
198 rotation_ + (end.rotation_ - rotation_) * fraction
222 const units::Length dx = twist.
dx();
223 const units::Length dy = twist.
dy();
224 const double dtheta = twist.
dtheta().to(units::radians);
226 const double sin_theta = cevalm::sin(dtheta);
227 const double cos_theta = cevalm::cos(dtheta);
230 if (cevalm::abs(dtheta) < 1e-9) {
231 s = 1.0 - 1.0 / 6.0 * dtheta * dtheta;
234 s = sin_theta / dtheta;
235 c = (1 - cos_theta) / dtheta;
239 Translation2d{dx * s - dy * c, dx * c + dy * s},
Rotation2d{cos_theta, sin_theta}
242 return *
this + transform;
255 const double half_dtheta = dtheta / 2.0;
257 const double cos_minus_one = transform.
rotation().
f_cos() - 1;
259 double half_theta_by_tan_of_half_dtheta;
261 if (cevalm::abs(cos_minus_one) < 1e-9) {
262 half_theta_by_tan_of_half_dtheta = 1.0 - 1.0 / 12.0 * dtheta * dtheta;
264 half_theta_by_tan_of_half_dtheta =
265 -(half_dtheta * transform.
rotation().f_sin()) / cos_minus_one;
268 const Translation2d translation_part =
270 cevalm::hypot(half_theta_by_tan_of_half_dtheta, half_dtheta);
273 translation_part.
x(), translation_part.
y(), units::Angle(dtheta, units::radians)
284 if (list.size() == 0) {
294 for (
Pose2d pose : list) {
298 sum_sin += pose.rotation_.f_sin();
299 sum_cos += pose.rotation_.f_cos();
303 Translation2d{sumx / list.size(), sumy / list.size()},
304 Rotation2d{sum_cos / list.size(), sum_sin / list.size()}
311 translation_ + (transform.
translation().rotate_by(rotation_)),
322 (translation_ - other.translation_).rotate_by(-other.rotation_), rotation_ - other.rotation_
328 return (translation_ == other.translation_) && (rotation_ == other.rotation_);
336 const Pose2d& other, units::Length distance_tolerance = units::Length(1e-6),
337 units::Angle angle_tolerance = units::Angle(1e-6)
339 return translation_.is_near(other.translation_, distance_tolerance) &&
340 rotation_.is_near(other.rotation_, angle_tolerance);
constexpr Rotation2d theta() const
Gets the angle of the vector from the x axis.
Definition translation2d.h:82
constexpr Q x() const
Gets the x component.
Definition translation2d.h:64
constexpr LinearVector2d rotate_by(Rotation2d rotation) const
Definition translation2d.h:139
constexpr Q y() const
Gets the y component.
Definition translation2d.h:73
constexpr Pose2d & operator+=(const Transform2d &transform)
Adds a transform to this pose by rotating it into the pose frame then adding.
Definition pose2d.h:317
constexpr void set_angle(units::Angle val)
Sets the rotation as an angle.
Definition pose2d.h:113
constexpr Rotation2d rotation() const
Gets the rotation.
Definition pose2d.h:101
constexpr Pose2d exp(const Twist2d &twist) const
Definition pose2d.h:221
constexpr void set_rotation(Rotation2d val)
Sets the rotation.
Definition pose2d.h:104
constexpr units::Length distance(Translation2d point) const
Calculates the distance from the pose to another point.
Definition pose2d.h:124
constexpr Pose2d(units::Length x, units::Length y, Rotation2d rotation)
Definition pose2d.h:46
constexpr Pose2d transform_by(const Transform2d &transform) const
Definition pose2d.h:176
constexpr void set_y(units::Length val)
Sets the y value of the translation.
Definition pose2d.h:92
constexpr units::Angle angle() const
Gets the heading as an angle.
Definition pose2d.h:107
constexpr void set_x(units::Length val)
Sets the x value of the translation.
Definition pose2d.h:83
constexpr Rotation2d bearing_to(Translation2d point) const
Calculates the bearing from this to another point in the world frame.
Definition pose2d.h:134
EVec< 3 > as_vector(units::Length length_unit, units::Angle angle_unit=units::radians) const
Gets [x, y, theta] in the supplied units. Angles default to radians.
Definition pose2d.h:116
constexpr units::Length y() const
Gets the y value of the translation.
Definition pose2d.h:86
constexpr void set_translation(Translation2d val)
Sets the translation.
Definition pose2d.h:98
constexpr Pose2d(Translation2d translation, Rotation2d rotation)
Definition pose2d.h:36
constexpr Twist2d log(const Transform2d &transform) const
Definition pose2d.h:253
static Pose2d wrapped_mean(const std::vector< Pose2d > &list)
Definition pose2d.h:283
constexpr Pose2d(const Eigen::Vector3d &pose_vector, units::Length length_unit, units::Angle angle_unit=units::radians)
Definition pose2d.h:67
constexpr Pose2d interpolate(const Pose2d &end, double fraction) const
Definition pose2d.h:189
constexpr Translation2d world_to_local(Translation2d point) const
Converts a point from the world frame to this pose's local frame.
Definition pose2d.h:153
constexpr units::Length distance(const Pose2d &other) const
Calculates the distance from this to another pose, ignoring rotation.
Definition pose2d.h:129
constexpr Translation2d translation() const
Gets the translation.
Definition pose2d.h:95
constexpr Translation2d local_to_world(Translation2d point) const
Converts a point from this pose's local frame to the world frame.
Definition pose2d.h:148
constexpr double angle(units::Angle unit) const
Gets the angle in the supplied unit.
Definition pose2d.h:110
constexpr double y(units::Length unit) const
Gets y in the supplied length unit.
Definition pose2d.h:89
constexpr units::Angle angle_to(Translation2d point) const
Calculates the smallest angle to another point in the local frame.
Definition pose2d.h:143
constexpr double x(units::Length unit) const
Gets x in the supplied length unit.
Definition pose2d.h:80
constexpr Pose2d operator+(const Transform2d &transform) const
Adds a transform to this pose by rotating it into the pose frame then adding.
Definition pose2d.h:309
constexpr bool operator==(const Pose2d &other) const
Checks exact equality between this and another pose.
Definition pose2d.h:327
constexpr units::Length x() const
Gets the x value of the translation.
Definition pose2d.h:77
constexpr Transform2d operator-(const Pose2d &other) const
Subtracts another pose from this to find the transform between them.
Definition pose2d.h:320
constexpr Pose2d relative_to(const Pose2d &other) const
Definition pose2d.h:163
constexpr bool is_near(const Pose2d &other, units::Length distance_tolerance=units::Length(1e-6), units::Angle angle_tolerance=units::Angle(1e-6)) const
Definition pose2d.h:335
constexpr Pose2d()
Default Constructor for Pose2d.
Definition pose2d.h:28
constexpr Pose2d(units::Length x, units::Length y, double radians)
Definition pose2d.h:56
Definition rotation2d.h:16
constexpr double radians() const
Gets the rotation in radians.
Definition rotation2d.h:72
constexpr Rotation2d()=default
Default Constructor for Rotation2d.
constexpr double f_cos() const
Gets the cosine of the rotation.
Definition rotation2d.h:84
constexpr units::Length dx() const
Gets the x displacement.
Definition twist2d.h:56
constexpr units::Length dy() const
Gets the y displacement.
Definition twist2d.h:65
constexpr units::Angle dtheta() const
Gets the Angle displacement.
Definition twist2d.h:74