WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
Pose3d.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdexcept>
8#include <type_traits>
9#include <utility>
10
11#include <Eigen/Core>
12#include <wpi/SymbolExports.h>
13#include <wpi/json_fwd.h>
14
15#include "frc/ct_matrix.h"
16#include "frc/geometry/Pose2d.h"
20
21namespace frc {
22
23class Transform3d;
24
25/**
26 * Represents a 3D pose containing translational and rotational elements.
27 */
29 public:
30 /**
31 * Constructs a pose at the origin facing toward the positive X axis.
32 */
33 constexpr Pose3d() = default;
34
35 /**
36 * Constructs a pose with the specified translation and rotation.
37 *
38 * @param translation The translational component of the pose.
39 * @param rotation The rotational component of the pose.
40 */
41 constexpr Pose3d(Translation3d translation, Rotation3d rotation)
42 : m_translation{std::move(translation)},
43 m_rotation{std::move(rotation)} {}
44
45 /**
46 * Constructs a pose with x, y, and z translations instead of a separate
47 * Translation3d.
48 *
49 * @param x The x component of the translational component of the pose.
50 * @param y The y component of the translational component of the pose.
51 * @param z The z component of the translational component of the pose.
52 * @param rotation The rotational component of the pose.
53 */
54 constexpr Pose3d(units::meter_t x, units::meter_t y, units::meter_t z,
55 Rotation3d rotation)
56 : m_translation{x, y, z}, m_rotation{std::move(rotation)} {}
57
58 /**
59 * Constructs a pose with the specified affine transformation matrix.
60 *
61 * @param matrix The affine transformation matrix.
62 * @throws std::domain_error if the affine transformation matrix is invalid.
63 */
64 constexpr explicit Pose3d(const Eigen::Matrix4d& matrix)
65 : m_translation{Eigen::Vector3d{
66 {matrix(0, 3)}, {matrix(1, 3)}, {matrix(2, 3)}}},
67 m_rotation{
68 Eigen::Matrix3d{{matrix(0, 0), matrix(0, 1), matrix(0, 2)},
69 {matrix(1, 0), matrix(1, 1), matrix(1, 2)},
70 {matrix(2, 0), matrix(2, 1), matrix(2, 2)}}} {
71 if (matrix(3, 0) != 0.0 || matrix(3, 1) != 0.0 || matrix(3, 2) != 0.0 ||
72 matrix(3, 3) != 1.0) {
73 throw std::domain_error("Affine transformation matrix is invalid");
74 }
75 }
76
77 /**
78 * Constructs a 3D pose from a 2D pose in the X-Y plane.
79 *
80 * @param pose The 2D pose.
81 * @see Rotation3d(Rotation2d)
82 * @see Translation3d(Translation2d)
83 */
84 constexpr explicit Pose3d(const Pose2d& pose)
85 : m_translation{pose.X(), pose.Y(), 0_m},
86 m_rotation{0_rad, 0_rad, pose.Rotation().Radians()} {}
87
88 /**
89 * Transforms the pose by the given transformation and returns the new
90 * transformed pose. The transform is applied relative to the pose's frame.
91 * Note that this differs from Pose3d::RotateBy(const Rotation3d&), which is
92 * applied relative to the global frame and around the origin.
93 *
94 * @param other The transform to transform the pose by.
95 *
96 * @return The transformed pose.
97 */
98 constexpr Pose3d operator+(const Transform3d& other) const {
99 return TransformBy(other);
100 }
101
102 /**
103 * Returns the Transform3d that maps the one pose to another.
104 *
105 * @param other The initial pose of the transformation.
106 * @return The transform that maps the other pose to the current pose.
107 */
108 constexpr Transform3d operator-(const Pose3d& other) const;
109
110 /**
111 * Checks equality between this Pose3d and another object.
112 */
113 constexpr bool operator==(const Pose3d&) const = default;
114
115 /**
116 * Returns the underlying translation.
117 *
118 * @return Reference to the translational component of the pose.
119 */
120 constexpr const Translation3d& Translation() const { return m_translation; }
121
122 /**
123 * Returns the X component of the pose's translation.
124 *
125 * @return The x component of the pose's translation.
126 */
127 constexpr units::meter_t X() const { return m_translation.X(); }
128
129 /**
130 * Returns the Y component of the pose's translation.
131 *
132 * @return The y component of the pose's translation.
133 */
134 constexpr units::meter_t Y() const { return m_translation.Y(); }
135
136 /**
137 * Returns the Z component of the pose's translation.
138 *
139 * @return The z component of the pose's translation.
140 */
141 constexpr units::meter_t Z() const { return m_translation.Z(); }
142
143 /**
144 * Returns the underlying rotation.
145 *
146 * @return Reference to the rotational component of the pose.
147 */
148 constexpr const Rotation3d& Rotation() const { return m_rotation; }
149
150 /**
151 * Multiplies the current pose by a scalar.
152 *
153 * @param scalar The scalar.
154 *
155 * @return The new scaled Pose2d.
156 */
157 constexpr Pose3d operator*(double scalar) const {
158 return Pose3d{m_translation * scalar, m_rotation * scalar};
159 }
160
161 /**
162 * Divides the current pose by a scalar.
163 *
164 * @param scalar The scalar.
165 *
166 * @return The new scaled Pose2d.
167 */
168 constexpr Pose3d operator/(double scalar) const {
169 return *this * (1.0 / scalar);
170 }
171
172 /**
173 * Rotates the pose around the origin and returns the new pose.
174 *
175 * @param other The rotation to transform the pose by, which is applied
176 * extrinsically (from the global frame).
177 *
178 * @return The rotated pose.
179 */
180 constexpr Pose3d RotateBy(const Rotation3d& other) const {
181 return {m_translation.RotateBy(other), m_rotation.RotateBy(other)};
182 }
183
184 /**
185 * Transforms the pose by the given transformation and returns the new
186 * transformed pose. The transform is applied relative to the pose's frame.
187 * Note that this differs from Pose3d::RotateBy(const Rotation3d&), which is
188 * applied relative to the global frame and around the origin.
189 *
190 * @param other The transform to transform the pose by.
191 *
192 * @return The transformed pose.
193 */
194 constexpr Pose3d TransformBy(const Transform3d& other) const;
195
196 /**
197 * Returns the current pose relative to the given pose.
198 *
199 * This function can often be used for trajectory tracking or pose
200 * stabilization algorithms to get the error between the reference and the
201 * current pose.
202 *
203 * @param other The pose that is the origin of the new coordinate frame that
204 * the current pose will be converted into.
205 *
206 * @return The current pose relative to the new origin pose.
207 */
208 constexpr Pose3d RelativeTo(const Pose3d& other) const;
209
210 /**
211 * Obtain a new Pose3d from a (constant curvature) velocity.
212 *
213 * The twist is a change in pose in the robot's coordinate frame since the
214 * previous pose update. When the user runs exp() on the previous known
215 * field-relative pose with the argument being the twist, the user will
216 * receive the new field-relative pose.
217 *
218 * "Exp" represents the pose exponential, which is solving a differential
219 * equation moving the pose forward in time.
220 *
221 * @param twist The change in pose in the robot's coordinate frame since the
222 * previous pose update. For example, if a non-holonomic robot moves forward
223 * 0.01 meters and changes angle by 0.5 degrees since the previous pose
224 * update, the twist would be Twist3d{0.01_m, 0_m, 0_m, Rotation3d{0.0, 0.0,
225 * 0.5_deg}}.
226 *
227 * @return The new pose of the robot.
228 */
229 constexpr Pose3d Exp(const Twist3d& twist) const;
230
231 /**
232 * Returns a Twist3d that maps this pose to the end pose. If c is the output
233 * of a.Log(b), then a.Exp(c) would yield b.
234 *
235 * @param end The end pose for the transformation.
236 *
237 * @return The twist that maps this to end.
238 */
239 constexpr Twist3d Log(const Pose3d& end) const;
240
241 /**
242 * Returns an affine transformation matrix representation of this pose.
243 */
244 constexpr Eigen::Matrix4d ToMatrix() const {
245 auto vec = m_translation.ToVector();
246 auto mat = m_rotation.ToMatrix();
247 return Eigen::Matrix4d{{mat(0, 0), mat(0, 1), mat(0, 2), vec(0)},
248 {mat(1, 0), mat(1, 1), mat(1, 2), vec(1)},
249 {mat(2, 0), mat(2, 1), mat(2, 2), vec(2)},
250 {0.0, 0.0, 0.0, 1.0}};
251 }
252
253 /**
254 * Returns a Pose2d representing this Pose3d projected into the X-Y plane.
255 */
256 constexpr Pose2d ToPose2d() const {
257 return Pose2d{m_translation.X(), m_translation.Y(), m_rotation.Z()};
258 }
259
260 private:
261 Translation3d m_translation;
262 Rotation3d m_rotation;
263};
264
266void to_json(wpi::json& json, const Pose3d& pose);
267
269void from_json(const wpi::json& json, Pose3d& pose);
270
271} // namespace frc
272
275
277
278namespace frc {
279
280namespace detail {
281
282/**
283 * Applies the hat operator to a rotation vector.
284 *
285 * It takes a rotation vector and returns the corresponding matrix
286 * representation of the Lie algebra element (a 3x3 rotation matrix).
287 *
288 * @param rotation The rotation vector.
289 * @return The rotation vector as a 3x3 rotation matrix.
290 */
292 // Given a rotation vector <a, b, c>,
293 // [ 0 -c b]
294 // Omega = [ c 0 -a]
295 // [-b a 0]
296 return ct_matrix3d{{0.0, -rotation(2), rotation(1)},
297 {rotation(2), 0.0, -rotation(0)},
298 {-rotation(1), rotation(0), 0.0}};
299}
300
301/**
302 * Applies the hat operator to a rotation vector.
303 *
304 * It takes a rotation vector and returns the corresponding matrix
305 * representation of the Lie algebra element (a 3x3 rotation matrix).
306 *
307 * @param rotation The rotation vector.
308 * @return The rotation vector as a 3x3 rotation matrix.
309 */
310constexpr Eigen::Matrix3d RotationVectorToMatrix(
311 const Eigen::Vector3d& rotation) {
312 // Given a rotation vector <a, b, c>,
313 // [ 0 -c b]
314 // Omega = [ c 0 -a]
315 // [-b a 0]
316 return Eigen::Matrix3d{{0.0, -rotation(2), rotation(1)},
317 {rotation(2), 0.0, -rotation(0)},
318 {-rotation(1), rotation(0), 0.0}};
319}
320
321} // namespace detail
322
323constexpr Transform3d Pose3d::operator-(const Pose3d& other) const {
324 const auto pose = this->RelativeTo(other);
325 return Transform3d{pose.Translation(), pose.Rotation()};
326}
327
328constexpr Pose3d Pose3d::TransformBy(const Transform3d& other) const {
329 return {m_translation + (other.Translation().RotateBy(m_rotation)),
330 other.Rotation() + m_rotation};
331}
332
333constexpr Pose3d Pose3d::RelativeTo(const Pose3d& other) const {
334 const Transform3d transform{other, *this};
335 return {transform.Translation(), transform.Rotation()};
336}
337
338constexpr Pose3d Pose3d::Exp(const Twist3d& twist) const {
339 // Implementation from Section 3.2 of https://ethaneade.org/lie.pdf
340
341 auto impl = [this]<typename Matrix3d, typename Vector3d>(
342 const Twist3d& twist) -> Pose3d {
343 Vector3d u{{twist.dx.value(), twist.dy.value(), twist.dz.value()}};
344 Vector3d rvec{{twist.rx.value(), twist.ry.value(), twist.rz.value()}};
345 Matrix3d omega = detail::RotationVectorToMatrix(rvec);
346 Matrix3d omegaSq = omega * omega;
347 double theta = rvec.norm();
348 double thetaSq = theta * theta;
349
350 double A;
351 double B;
352 double C;
353 if (gcem::abs(theta) < 1E-7) {
354 // Taylor Expansions around θ = 0
355 // A = 1/1! - θ²/3! + θ⁴/5!
356 // B = 1/2! - θ²/4! + θ⁴/6!
357 // C = 1/3! - θ²/5! + θ⁴/7!
358 // sources:
359 // A:
360 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5Bsin%5C%2840%29x%5C%2841%29%2Cx%5D+at+x%3D0
361 // B:
362 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5B1-cos%5C%2840%29x%5C%2841%29%2CPower%5Bx%2C2%5D%5D+at+x%3D0
363 // C:
364 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5B1-Divide%5Bsin%5C%2840%29x%5C%2841%29%2Cx%5D%2CPower%5Bx%2C2%5D%5D+at+x%3D0
365 A = 1 - thetaSq / 6 + thetaSq * thetaSq / 120;
366 B = 1 / 2.0 - thetaSq / 24 + thetaSq * thetaSq / 720;
367 C = 1 / 6.0 - thetaSq / 120 + thetaSq * thetaSq / 5040;
368 } else {
369 // A = std::sin(θ)/θ
370 // B = (1 - std::cos(θ)) / θ²
371 // C = (1 - A) / θ²
372 A = gcem::sin(theta) / theta;
373 B = (1 - gcem::cos(theta)) / thetaSq;
374 C = (1 - A) / thetaSq;
375 }
376
377 Matrix3d R = Matrix3d::Identity() + A * omega + B * omegaSq;
378 Matrix3d V = Matrix3d::Identity() + B * omega + C * omegaSq;
379
380 Vector3d translation_component = V * u;
381
382 const Transform3d transform{
383 Translation3d{units::meter_t{translation_component(0)},
384 units::meter_t{translation_component(1)},
385 units::meter_t{translation_component(2)}},
386 Rotation3d{R}};
387
388 return *this + transform;
389 };
390
391 if (std::is_constant_evaluated()) {
392 return impl.template operator()<ct_matrix3d, ct_vector3d>(twist);
393 } else {
394 return impl.template operator()<Eigen::Matrix3d, Eigen::Vector3d>(twist);
395 }
396}
397
398constexpr Twist3d Pose3d::Log(const Pose3d& end) const {
399 // Implementation from Section 3.2 of https://ethaneade.org/lie.pdf
400
401 auto impl = [this]<typename Matrix3d, typename Vector3d>(
402 const Pose3d& end) -> Twist3d {
403 const auto transform = end.RelativeTo(*this);
404
405 Vector3d u{
406 {transform.X().value(), transform.Y().value(), transform.Z().value()}};
407 Vector3d rvec = transform.Rotation().ToVector();
408 Matrix3d omega = detail::RotationVectorToMatrix(rvec);
409 Matrix3d omegaSq = omega * omega;
410 double theta = rvec.norm();
411 double thetaSq = theta * theta;
412
413 double C;
414 if (gcem::abs(theta) < 1E-7) {
415 // Taylor Expansions around θ = 0
416 // A = 1/1! - θ²/3! + θ⁴/5!
417 // B = 1/2! - θ²/4! + θ⁴/6!
418 // C = 1/6 * (1/2 + θ²/5! + θ⁴/7!)
419 // sources:
420 // A:
421 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5Bsin%5C%2840%29x%5C%2841%29%2Cx%5D+at+x%3D0
422 // B:
423 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5B1-cos%5C%2840%29x%5C%2841%29%2CPower%5Bx%2C2%5D%5D+at+x%3D0
424 // C:
425 // https://www.wolframalpha.com/input?i2d=true&i=series+expansion+of+Divide%5B1-Divide%5BDivide%5Bsin%5C%2840%29x%5C%2841%29%2Cx%5D%2C2Divide%5B1-cos%5C%2840%29x%5C%2841%29%2CPower%5Bx%2C2%5D%5D%5D%2CPower%5Bx%2C2%5D%5D+at+x%3D0
426 C = 1 / 12.0 + thetaSq / 720 + thetaSq * thetaSq / 30240;
427 } else {
428 // A = std::sin(θ)/θ
429 // B = (1 - std::cos(θ)) / θ²
430 // C = (1 - A/(2*B)) / θ²
431 double A = gcem::sin(theta) / theta;
432 double B = (1 - gcem::cos(theta)) / thetaSq;
433 C = (1 - A / (2 * B)) / thetaSq;
434 }
435
436 Matrix3d V_inv = Matrix3d::Identity() - 0.5 * omega + C * omegaSq;
437
438 Vector3d translation_component = V_inv * u;
439
440 return Twist3d{units::meter_t{translation_component(0)},
441 units::meter_t{translation_component(1)},
442 units::meter_t{translation_component(2)},
443 units::radian_t{rvec(0)},
444 units::radian_t{rvec(1)},
445 units::radian_t{rvec(2)}};
446 };
447
448 if (std::is_constant_evaluated()) {
449 return impl.template operator()<ct_matrix3d, ct_vector3d>(end);
450 } else {
451 return impl.template operator()<Eigen::Matrix3d, Eigen::Vector3d>(end);
452 }
453}
454
455} // namespace frc
#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:28
Represents a 3D pose containing translational and rotational elements.
Definition Pose3d.h:28
constexpr Pose3d(const Pose2d &pose)
Constructs a 3D pose from a 2D pose in the X-Y plane.
Definition Pose3d.h:84
constexpr Pose3d(units::meter_t x, units::meter_t y, units::meter_t z, Rotation3d rotation)
Constructs a pose with x, y, and z translations instead of a separate Translation3d.
Definition Pose3d.h:54
constexpr Pose3d TransformBy(const Transform3d &other) const
Transforms the pose by the given transformation and returns the new transformed pose.
Definition Pose3d.h:328
constexpr Pose3d RotateBy(const Rotation3d &other) const
Rotates the pose around the origin and returns the new pose.
Definition Pose3d.h:180
constexpr const Translation3d & Translation() const
Returns the underlying translation.
Definition Pose3d.h:120
constexpr Pose3d(Translation3d translation, Rotation3d rotation)
Constructs a pose with the specified translation and rotation.
Definition Pose3d.h:41
constexpr Twist3d Log(const Pose3d &end) const
Returns a Twist3d that maps this pose to the end pose.
Definition Pose3d.h:398
constexpr units::meter_t Z() const
Returns the Z component of the pose's translation.
Definition Pose3d.h:141
constexpr Pose3d operator/(double scalar) const
Divides the current pose by a scalar.
Definition Pose3d.h:168
constexpr Eigen::Matrix4d ToMatrix() const
Returns an affine transformation matrix representation of this pose.
Definition Pose3d.h:244
constexpr Pose2d ToPose2d() const
Returns a Pose2d representing this Pose3d projected into the X-Y plane.
Definition Pose3d.h:256
constexpr const Rotation3d & Rotation() const
Returns the underlying rotation.
Definition Pose3d.h:148
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition Pose3d.h:134
constexpr Pose3d operator*(double scalar) const
Multiplies the current pose by a scalar.
Definition Pose3d.h:157
constexpr Pose3d RelativeTo(const Pose3d &other) const
Returns the current pose relative to the given pose.
Definition Pose3d.h:333
constexpr Pose3d(const Eigen::Matrix4d &matrix)
Constructs a pose with the specified affine transformation matrix.
Definition Pose3d.h:64
constexpr bool operator==(const Pose3d &) const =default
Checks equality between this Pose3d and another object.
constexpr Transform3d operator-(const Pose3d &other) const
Returns the Transform3d that maps the one pose to another.
Definition Pose3d.h:323
constexpr Pose3d operator+(const Transform3d &other) const
Transforms the pose by the given transformation and returns the new transformed pose.
Definition Pose3d.h:98
constexpr Pose3d()=default
Constructs a pose at the origin facing toward the positive X axis.
constexpr Pose3d Exp(const Twist3d &twist) const
Obtain a new Pose3d from a (constant curvature) velocity.
Definition Pose3d.h:338
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition Pose3d.h:127
A rotation in a 3D coordinate frame represented by a quaternion.
Definition Rotation3d.h:29
Represents a transformation for a Pose3d in the pose's frame.
Definition Transform3d.h:21
Represents a translation in 3D space.
Definition Translation3d.h:26
Compile-time wrapper for Eigen::Matrix.
Definition ct_matrix.h:26
Definition Variable.hpp:739
detail namespace with internal helper functions
Definition input_adapters.h:32
constexpr ct_matrix3d RotationVectorToMatrix(const ct_vector3d &rotation)
Applies the hat operator to a rotation vector.
Definition Pose3d.h:291
Definition CAN.h:11
WPILIB_DLLEXPORT void to_json(wpi::json &json, const Rotation3d &rotation)
ct_matrix< double, 3, 3 > ct_matrix3d
Definition ct_matrix.h:385
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition abs.hpp:40
constexpr return_t< T > cos(const T x) noexcept
Compile-time cosine function.
Definition cos.hpp:83
constexpr return_t< T > sin(const T x) noexcept
Compile-time sine function.
Definition sin.hpp:85
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
A change in distance along a 3D arc since the last pose update.
Definition Twist3d.h:22
units::meter_t dx
Linear "dx" component.
Definition Twist3d.h:26
units::radian_t ry
Rotation vector y component.
Definition Twist3d.h:46
units::meter_t dy
Linear "dy" component.
Definition Twist3d.h:31
units::radian_t rz
Rotation vector z component.
Definition Twist3d.h:51
units::meter_t dz
Linear "dz" component.
Definition Twist3d.h:36
units::radian_t rx
Rotation vector x component.
Definition Twist3d.h:41
Type representing an arbitrary unit.
Definition base.h:888