WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
CoordinateSystem.hpp
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 <gcem.hpp>
8
15
16namespace wpi::math {
17
18/**
19 * A helper class that converts Pose3d objects between different standard
20 * coordinate frames.
21 */
23 public:
24 /**
25 * Constructs a coordinate system with the given cardinal directions for each
26 * axis.
27 *
28 * @param positiveX The cardinal direction of the positive x-axis.
29 * @param positiveY The cardinal direction of the positive y-axis.
30 * @param positiveZ The cardinal direction of the positive z-axis.
31 * @throws std::domain_error if the coordinate system isn't special orthogonal
32 */
33 constexpr CoordinateSystem(const CoordinateAxis& positiveX,
34 const CoordinateAxis& positiveY,
35 const CoordinateAxis& positiveZ) {
36 // Construct a change of basis matrix from the source coordinate system to
37 // the NWU coordinate system. Each column vector in the change of basis
38 // matrix is one of the old basis vectors mapped to its representation in
39 // the new basis.
40 Eigen::Matrix3d R{
41 {positiveX.m_axis(0), positiveY.m_axis(0), positiveZ.m_axis(0)},
42 {positiveX.m_axis(1), positiveY.m_axis(1), positiveZ.m_axis(1)},
43 {positiveX.m_axis(2), positiveY.m_axis(2), positiveZ.m_axis(2)}};
44
45 // If determinant is -1, coordinate system is left-handed
46 if (gcem::abs(ct_matrix{R}.determinant() + 1.0) < 1e-9) {
47 throw std::domain_error(
48 "CoordinateSystem requires a right-handed system, but a left-handed "
49 "one was provided");
50 }
51
52 // The change of basis matrix should be a pure rotation. The Rotation3d
53 // constructor will verify this by checking for special orthogonality.
54 m_rotation = Rotation3d{R};
55 }
56
57 /**
58 * Returns an instance of the North-West-Up (NWU) coordinate system.
59 *
60 * The +X axis is north, the +Y axis is west, and the +Z axis is up.
61 */
66
67 /**
68 * Returns an instance of the East-Down-North (EDN) coordinate system.
69 *
70 * The +X axis is east, the +Y axis is down, and the +Z axis is north.
71 */
76
77 /**
78 * Returns an instance of the NED coordinate system.
79 *
80 * The +X axis is north, the +Y axis is east, and the +Z axis is down.
81 */
86
87 /**
88 * Converts the given translation from one coordinate system to another.
89 *
90 * @param translation The translation to convert.
91 * @param from The coordinate system the translation starts in.
92 * @param to The coordinate system to which to convert.
93 * @return The given translation in the desired coordinate system.
94 */
95 constexpr static Translation3d Convert(const Translation3d& translation,
97 const CoordinateSystem& to) {
98 // Convert to NWU, then convert to the new coordinate system
99 return translation.RotateBy(from.m_rotation)
100 .RotateBy(to.m_rotation.Inverse());
101 }
102
103 /**
104 * Converts the given rotation from one coordinate system to another.
105 *
106 * @param rotation The rotation to convert.
107 * @param from The coordinate system the rotation starts in.
108 * @param to The coordinate system to which to convert.
109 * @return The given rotation in the desired coordinate system.
110 */
111 constexpr static Rotation3d Convert(const Rotation3d& rotation,
112 const CoordinateSystem& from,
113 const CoordinateSystem& to) {
114 // Convert to NWU, then convert to the new coordinate system
115 return rotation.RotateBy(from.m_rotation).RotateBy(to.m_rotation.Inverse());
116 }
117
118 /**
119 * Converts the given pose from one coordinate system to another.
120 *
121 * @param pose The pose to convert.
122 * @param from The coordinate system the pose starts in.
123 * @param to The coordinate system to which to convert.
124 * @return The given pose in the desired coordinate system.
125 */
126 constexpr static Pose3d Convert(const Pose3d& pose,
127 const CoordinateSystem& from,
128 const CoordinateSystem& to) {
129 return Pose3d{Convert(pose.Translation(), from, to),
130 Convert(pose.Rotation(), from, to)};
131 }
132
133 /**
134 * Converts the given transform from one coordinate system to another.
135 *
136 * @param transform The transform to convert.
137 * @param from The coordinate system the transform starts in.
138 * @param to The coordinate system to which to convert.
139 * @return The given transform in the desired coordinate system.
140 */
141 constexpr static Transform3d Convert(const Transform3d& transform,
142 const CoordinateSystem& from,
143 const CoordinateSystem& to) {
144 // coordRot is the rotation that converts between the coordinate systems
145 // when applied extrinsically. It first converts to NWU, then converts to
146 // the new coordinate system.
147 const auto coordRot = from.m_rotation.RotateBy(to.m_rotation.Inverse());
148 // The new rotation is the extrinsic rotation from convert(zero) to
149 // convert(transformRot). That is, applying convertedRot extrinsically to
150 // convert(zero) produces convert(transformRot). In the below snippet, we
151 // use matrix notation, so rotA rotB applies rotA extrinsically to rotB.
152 //
153 // convertedRot convert(zero) = convert(transformRot)
154 // convertedRot = convert(transformRot) convert(zero)⁻¹
155 // = (coordRot transformRot) (coordRot zero)⁻¹
156 // = (coordRot transformRot) coordRot⁻¹
157 //
158 // In code, the equivalent for rotA rotB is rotB.RotateBy(rotA) (note the
159 // change in order).
160 return Transform3d{
161 Convert(transform.Translation(), from, to),
162 coordRot.Inverse().RotateBy(transform.Rotation().RotateBy(coordRot))};
163 }
164
165 private:
166 // Rotation from this coordinate system to NWU coordinate system when applied
167 // extrinsically
168 Rotation3d m_rotation;
169};
170
171} // namespace wpi::math
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or glfw and nanopb were modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition ThirdPartyNotices.txt:136
A class representing a coordinate system axis within the NWU coordinate system.
Definition CoordinateAxis.hpp:18
static constexpr CoordinateAxis N()
Returns a coordinate axis corresponding to +X in the NWU coordinate system.
Definition CoordinateAxis.hpp:42
static constexpr CoordinateAxis D()
Returns a coordinate axis corresponding to -Z in the NWU coordinate system.
Definition CoordinateAxis.hpp:67
static constexpr CoordinateAxis W()
Returns a coordinate axis corresponding to +Y in the NWU coordinate system.
Definition CoordinateAxis.hpp:57
static constexpr CoordinateAxis U()
Returns a coordinate axis corresponding to +Z in the NWU coordinate system.
Definition CoordinateAxis.hpp:62
static constexpr CoordinateAxis E()
Returns a coordinate axis corresponding to -Y in the NWU coordinate system.
Definition CoordinateAxis.hpp:52
static constexpr CoordinateSystem NED()
Returns an instance of the NED coordinate system.
Definition CoordinateSystem.hpp:82
static constexpr Pose3d Convert(const Pose3d &pose, const CoordinateSystem &from, const CoordinateSystem &to)
Converts the given pose from one coordinate system to another.
Definition CoordinateSystem.hpp:126
static constexpr Transform3d Convert(const Transform3d &transform, const CoordinateSystem &from, const CoordinateSystem &to)
Converts the given transform from one coordinate system to another.
Definition CoordinateSystem.hpp:141
constexpr CoordinateSystem(const CoordinateAxis &positiveX, const CoordinateAxis &positiveY, const CoordinateAxis &positiveZ)
Constructs a coordinate system with the given cardinal directions for each axis.
Definition CoordinateSystem.hpp:33
static constexpr Rotation3d Convert(const Rotation3d &rotation, const CoordinateSystem &from, const CoordinateSystem &to)
Converts the given rotation from one coordinate system to another.
Definition CoordinateSystem.hpp:111
static constexpr Translation3d Convert(const Translation3d &translation, const CoordinateSystem &from, const CoordinateSystem &to)
Converts the given translation from one coordinate system to another.
Definition CoordinateSystem.hpp:95
static constexpr CoordinateSystem EDN()
Returns an instance of the East-Down-North (EDN) coordinate system.
Definition CoordinateSystem.hpp:72
static constexpr CoordinateSystem NWU()
Returns an instance of the North-West-Up (NWU) coordinate system.
Definition CoordinateSystem.hpp:62
Represents a 3D pose containing translational and rotational elements.
Definition Pose3d.hpp:31
constexpr const Translation3d & Translation() const
Returns the underlying translation.
Definition Pose3d.hpp:123
constexpr const Rotation3d & Rotation() const
Returns the underlying rotation.
Definition Pose3d.hpp:151
A rotation in a 3D coordinate frame, represented by a quaternion.
Definition Rotation3d.hpp:72
constexpr Rotation3d Inverse() const
Takes the inverse of the current rotation.
Definition Rotation3d.hpp:293
constexpr Rotation3d RotateBy(const Rotation3d &other) const
Adds the new rotation to the current rotation.
Definition Rotation3d.hpp:336
Represents a transformation for a Pose3d in the pose's frame.
Definition Transform3d.hpp:23
constexpr const Rotation3d & Rotation() const
Returns the rotational component of the transformation.
Definition Transform3d.hpp:137
constexpr const Translation3d & Translation() const
Returns the translation component of the transformation.
Definition Transform3d.hpp:96
Represents a translation in 3D space.
Definition Translation3d.hpp:34
constexpr Translation3d RotateBy(const Rotation3d &other) const
Applies a rotation to the translation in 3D space.
Definition Translation3d.hpp:183
Compile-time wrapper for Eigen::Matrix.
Definition ct_matrix.hpp:26
constexpr Scalar determinant() const
Constexpr version of Eigen's 2x2 matrix determinant member function.
Definition ct_matrix.hpp:316
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition abs.hpp:40
Definition LinearSystem.hpp:20