Brick
Loading...
Searching...
No Matches
transform2d.h
1#pragma once
2
3#include "core/math/geometry/rotation2d.h"
4#include "core/math/geometry/translation2d.h"
5#include "core/math/units.h"
6
7/**
8 * Class representing a transformation of a pose2d, by rotating a translation into the frame
9 * of the pose2d, then adding the translation, then rotating by theta.
10 *
11 * Assumes conventional cartesian coordinate system:
12 * Looking down at the coordinate plane,
13 * +X is right
14 * +Y is up
15 * +Theta is counterclockwise
16 */
18 private:
19 Translation2d translation_;
20 Rotation2d rotation_;
21
22 public:
23 /// Default Constructor for Transform2d
24 constexpr Transform2d() = default;
25
26 /**
27 * Constructs a transform given translation and rotation components.
28 *
29 * @param translation the translational component of the transform.
30 * @param rotation the rotational component of the transform.
31 */
32 constexpr Transform2d(Translation2d translation, Rotation2d rotation)
33 : translation_(translation), rotation_(rotation) {}
34
35 /**
36 * Constructs a transform given translation and rotation components.
37 *
38 * @param x the x component of the transform.
39 * @param y the y component of the transform.
40 * @param rotation the rotational component of the transform.
41 */
42 constexpr Transform2d(units::Length x, units::Length y, Rotation2d rotation)
43 : translation_(x, y), rotation_(rotation) {}
44
45 /**
46 * Constructs a transform given translation and rotation components.
47 *
48 * @param x the x component of the transform.
49 * @param y the y component of the transform.
50 * @param angle the rotational component of the transform.
51 */
52 constexpr Transform2d(units::Length x, units::Length y, units::Angle angle)
53 : translation_(x, y), rotation_(angle) {}
54
55 /**
56 * Constructs a transform given translation and rotation components.
57 *
58 * @param translation the translational component of the transform.
59 * @param angle the rotational component of the transform.
60 */
61 constexpr Transform2d(Translation2d translation, units::Angle angle)
62 : translation_(translation), rotation_(angle) {}
63
64 /**
65 * Constructs a transform given translation and rotation components given as a
66 * vector using the supplied units.
67 *
68 * @param transform_vector vector of the form [x, y, theta]
69 * @param unit The length unit to use when assigning the translation.
70 * @param angle_unit The angle unit, defaults to radians.
71 */
72 constexpr Transform2d(
73 const Eigen::Vector3d& transform_vector, units::Length unit,
74 units::Angle angle_unit = units::radians
75 )
76 : translation_({transform_vector(0), transform_vector(1)}, unit),
77 rotation_(units::Angle(transform_vector(2), angle_unit)) {}
78
79 /// Gets the x component
80 constexpr units::Length x() const { return translation_.x(); }
81
82 /// Gets x in the supplied length unit.
83 constexpr double x(units::Length unit) const { return translation_.x(unit); }
84
85 /// Sets the x component.
86 constexpr void set_x(units::Length val) { translation_.set_x(val); }
87
88 /// Gets the y component
89 constexpr units::Length y() const { return translation_.y(); }
90
91 /// Gets y in the supplied length unit.
92 constexpr double y(units::Length unit) const { return translation_.y(unit); }
93
94 /// Sets the y component.
95 constexpr void set_y(units::Length val) { translation_.set_y(val); }
96
97 /// Gets the translation.
98 constexpr Translation2d translation() const { return translation_; }
99
100 /// Sets the translation.
101 constexpr void set_translation(Translation2d val) { translation_ = val; }
102
103 /// Gets the rotation.
104 constexpr Rotation2d rotation() const { return rotation_; }
105
106 /// Sets the rotation.
107 constexpr void set_rotation(Rotation2d val) { rotation_ = val; }
108
109 /// Gets the rotation as an Angle
110 constexpr units::Angle angle() const { return rotation_.angle(); }
111
112 /// Gets the angle in the supplied unit.
113 constexpr double angle(units::Angle unit) const { return rotation_.angle(unit); }
114
115 /// Sets the rotation as an angle.
116 constexpr void set_angle(units::Angle val) { rotation_ = Rotation2d(val); }
117
118 /**
119 * Returns [x, y, theta] in the supplied units. Angles default to radians.
120 *
121 * @param length_unit the unit of length to get the values as
122 * @param angle_unit the unit of angle to get the rotation as, default radians
123 * @return EVec<3> containing the values.
124 */
125 EVec<3> as_vector(units::Length length_unit, units::Angle angle_unit = units::radians) const {
126 return {
127 translation_.x().to(length_unit), translation_.y().to(length_unit),
128 rotation_.angle().to(angle_unit)
129 };
130 }
131
132 /// Inverts this transform
133 constexpr Transform2d inverse() const {
134 return Transform2d(-translation_.rotate_by(-rotation_), -rotation_);
135 }
136
137 /// Composes this transform and another transform.
138 constexpr Transform2d operator+(const Transform2d& other) const {
139 return {translation_ + other.translation_.rotate_by(rotation_), rotation_ + other.rotation_};
140 }
141
142 /// Composes this transform and another transform.
143 constexpr Transform2d& operator+=(const Transform2d& other) { return *this = *this + other; }
144
145 /// Inverts this transform.
146 constexpr Transform2d operator-() const { return inverse(); }
147
148 /// Multiplies this transform by a scalar.
149 constexpr Transform2d operator*(double scalar) const {
150 return Transform2d(translation_ * scalar, rotation_ * scalar);
151 }
152
153 /// Multiplies this transform by a scalar reverse order.
154 friend constexpr Transform2d operator*(double scalar, const Transform2d& transform) {
155 return transform * scalar;
156 }
157
158 /// Multiplies this transform by a scalar.
159 constexpr Transform2d& operator*=(double scalar) { return *this = *this * scalar; }
160
161 /// Divides this transform by a scalar.
162 constexpr Transform2d operator/(double scalar) const {
163 return Transform2d(translation_ / scalar, rotation_ / scalar);
164 }
165
166 /// Divides this transform by a scalar.
167 constexpr Transform2d& operator/=(double scalar) { return *this = *this / scalar; }
168
169 /// Checks exact equality between this and another transform.
170 constexpr bool operator==(const Transform2d& other) const {
171 return (translation_ == other.translation_) && (rotation_ == other.rotation_);
172 }
173
174 /**
175 * Checks translation distance and the smallest angle against tolerances.
176 * Defaults to 1um and 1e-6 radians.
177 */
178 constexpr bool is_near(
179 const Transform2d& other, units::Length distance_tolerance = units::Length(1e-6),
180 units::Angle angle_tolerance = units::Angle(1e-6)
181 ) const {
182 return translation_.is_near(other.translation_, distance_tolerance) &&
183 rotation_.is_near(other.rotation_, angle_tolerance);
184 }
185};
constexpr LinearVector2d rotate_by(Rotation2d rotation) const
Definition translation2d.h:139
Definition rotation2d.h:16
constexpr Rotation2d()=default
Default Constructor for Rotation2d.
constexpr double x(units::Length unit) const
Gets x in the supplied length unit.
Definition transform2d.h:83
constexpr Transform2d()=default
Default Constructor for Transform2d.
constexpr Transform2d operator/(double scalar) const
Divides this transform by a scalar.
Definition transform2d.h:162
constexpr void set_angle(units::Angle val)
Sets the rotation as an angle.
Definition transform2d.h:116
constexpr Transform2d & operator*=(double scalar)
Multiplies this transform by a scalar.
Definition transform2d.h:159
constexpr units::Angle angle() const
Gets the rotation as an Angle.
Definition transform2d.h:110
constexpr Transform2d operator+(const Transform2d &other) const
Composes this transform and another transform.
Definition transform2d.h:138
constexpr Translation2d translation() const
Gets the translation.
Definition transform2d.h:98
constexpr void set_translation(Translation2d val)
Sets the translation.
Definition transform2d.h:101
constexpr double y(units::Length unit) const
Gets y in the supplied length unit.
Definition transform2d.h:92
constexpr void set_y(units::Length val)
Sets the y component.
Definition transform2d.h:95
constexpr Transform2d(Translation2d translation, units::Angle angle)
Definition transform2d.h:61
constexpr Transform2d inverse() const
Inverts this transform.
Definition transform2d.h:133
constexpr Transform2d(const Eigen::Vector3d &transform_vector, units::Length unit, units::Angle angle_unit=units::radians)
Definition transform2d.h:72
constexpr void set_x(units::Length val)
Sets the x component.
Definition transform2d.h:86
constexpr Rotation2d rotation() const
Gets the rotation.
Definition transform2d.h:104
constexpr Transform2d(units::Length x, units::Length y, Rotation2d rotation)
Definition transform2d.h:42
constexpr bool is_near(const Transform2d &other, units::Length distance_tolerance=units::Length(1e-6), units::Angle angle_tolerance=units::Angle(1e-6)) const
Definition transform2d.h:178
constexpr Transform2d & operator/=(double scalar)
Divides this transform by a scalar.
Definition transform2d.h:167
constexpr Transform2d(Translation2d translation, Rotation2d rotation)
Definition transform2d.h:32
friend constexpr Transform2d operator*(double scalar, const Transform2d &transform)
Multiplies this transform by a scalar reverse order.
Definition transform2d.h:154
constexpr Transform2d(units::Length x, units::Length y, units::Angle angle)
Definition transform2d.h:52
constexpr Transform2d operator-() const
Inverts this transform.
Definition transform2d.h:146
constexpr bool operator==(const Transform2d &other) const
Checks exact equality between this and another transform.
Definition transform2d.h:170
EVec< 3 > as_vector(units::Length length_unit, units::Angle angle_unit=units::radians) const
Definition transform2d.h:125
constexpr void set_rotation(Rotation2d val)
Sets the rotation.
Definition transform2d.h:107
constexpr units::Length x() const
Gets the x component.
Definition transform2d.h:80
constexpr double angle(units::Angle unit) const
Gets the angle in the supplied unit.
Definition transform2d.h:113
constexpr units::Length y() const
Gets the y component.
Definition transform2d.h:89
constexpr Transform2d operator*(double scalar) const
Multiplies this transform by a scalar.
Definition transform2d.h:149
constexpr Transform2d & operator+=(const Transform2d &other)
Composes this transform and another transform.
Definition transform2d.h:143