rigid_transform.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_
18 #define CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_
19 
20 #include <cmath>
21 #include <iostream>
22 #include <string>
23 
24 #include "Eigen/Core"
25 #include "Eigen/Geometry"
29 
30 namespace cartographer {
31 namespace transform {
32 
33 template <typename FloatType>
34 class Rigid2 {
35  public:
36  using Vector = Eigen::Matrix<FloatType, 2, 1>;
37  using Rotation2D = Eigen::Rotation2D<FloatType>;
38 
41  : translation_(translation), rotation_(rotation) {}
42  Rigid2(const Vector& translation, const double rotation)
43  : translation_(translation), rotation_(rotation) {}
44 
45  static Rigid2 Rotation(const double rotation) {
46  return Rigid2(Vector::Zero(), rotation);
47  }
48 
50  return Rigid2(Vector::Zero(), rotation);
51  }
52 
53  static Rigid2 Translation(const Vector& vector) {
54  return Rigid2(vector, Rotation2D::Identity());
55  }
56 
58 
59  template <typename OtherType>
61  return Rigid2<OtherType>(translation_.template cast<OtherType>(),
62  rotation_.template cast<OtherType>());
63  }
64 
65  const Vector& translation() const { return translation_; }
66 
67  Rotation2D rotation() const { return rotation_; }
68 
69  double normalized_angle() const {
71  }
72 
73  Rigid2 inverse() const {
74  const Rotation2D rotation = rotation_.inverse();
75  const Vector translation = -(rotation * translation_);
76  return Rigid2(translation, rotation);
77  }
78 
79  std::string DebugString() const {
80  std::string out;
81  out.append("{ t: [");
82  out.append(std::to_string(translation().x()));
83  out.append(", ");
84  out.append(std::to_string(translation().y()));
85  out.append("], r: [");
86  out.append(std::to_string(rotation().angle()));
87  out.append("] }");
88  return out;
89  }
90 
91  private:
94 };
95 
96 template <typename FloatType>
98  const Rigid2<FloatType>& rhs) {
99  return Rigid2<FloatType>(
100  lhs.rotation() * rhs.translation() + lhs.translation(),
101  lhs.rotation() * rhs.rotation());
102 }
103 
104 template <typename FloatType>
106  const Rigid2<FloatType>& rigid,
107  const typename Rigid2<FloatType>::Vector& point) {
108  return rigid.rotation() * point + rigid.translation();
109 }
110 
111 // This is needed for gmock.
112 template <typename T>
113 std::ostream& operator<<(std::ostream& os,
114  const cartographer::transform::Rigid2<T>& rigid) {
115  os << rigid.DebugString();
116  return os;
117 }
118 
121 
122 template <typename FloatType>
123 class Rigid3 {
124  public:
125  using Vector = Eigen::Matrix<FloatType, 3, 1>;
126  using Quaternion = Eigen::Quaternion<FloatType>;
127  using AngleAxis = Eigen::AngleAxis<FloatType>;
128 
131  : translation_(translation), rotation_(rotation) {}
133  : translation_(translation), rotation_(rotation) {}
134 
135  static Rigid3 Rotation(const AngleAxis& angle_axis) {
136  return Rigid3(Vector::Zero(), Quaternion(angle_axis));
137  }
138 
140  return Rigid3(Vector::Zero(), rotation);
141  }
142 
143  static Rigid3 Translation(const Vector& vector) {
144  return Rigid3(vector, Quaternion::Identity());
145  }
146 
147  static Rigid3 FromArrays(const std::array<FloatType, 4>& rotation,
148  const std::array<FloatType, 3>& translation) {
149  return Rigid3(Eigen::Map<const Vector>(translation.data()),
150  Eigen::Quaternion<FloatType>(rotation[0], rotation[1],
151  rotation[2], rotation[3]));
152  }
153 
155 
156  template <typename OtherType>
158  return Rigid3<OtherType>(translation_.template cast<OtherType>(),
159  rotation_.template cast<OtherType>());
160  }
161 
162  const Vector& translation() const { return translation_; }
163  const Quaternion& rotation() const { return rotation_; }
164 
165  Rigid3 inverse() const {
166  const Quaternion rotation = rotation_.conjugate();
167  const Vector translation = -(rotation * translation_);
168  return Rigid3(translation, rotation);
169  }
170 
171  std::string DebugString() const {
172  std::string out;
173  out.append("{ t: [");
174  out.append(std::to_string(translation().x()));
175  out.append(", ");
176  out.append(std::to_string(translation().y()));
177  out.append(", ");
178  out.append(std::to_string(translation().z()));
179  out.append("], q: [");
180  out.append(std::to_string(rotation().w()));
181  out.append(", ");
182  out.append(std::to_string(rotation().x()));
183  out.append(", ");
184  out.append(std::to_string(rotation().y()));
185  out.append(", ");
186  out.append(std::to_string(rotation().z()));
187  out.append("] }");
188  return out;
189  }
190 
191  bool IsValid() const {
192  return !std::isnan(translation_.x()) && !std::isnan(translation_.y()) &&
193  !std::isnan(translation_.z()) &&
194  std::abs(FloatType(1) - rotation_.norm()) < FloatType(1e-3);
195  }
196 
197  private:
200 };
201 
202 template <typename FloatType>
204  const Rigid3<FloatType>& rhs) {
205  return Rigid3<FloatType>(
206  lhs.rotation() * rhs.translation() + lhs.translation(),
207  (lhs.rotation() * rhs.rotation()).normalized());
208 }
209 
210 template <typename FloatType>
212  const Rigid3<FloatType>& rigid,
213  const typename Rigid3<FloatType>::Vector& point) {
214  return rigid.rotation() * point + rigid.translation();
215 }
216 
217 // This is needed for gmock.
218 template <typename T>
219 std::ostream& operator<<(std::ostream& os,
220  const cartographer::transform::Rigid3<T>& rigid) {
221  os << rigid.DebugString();
222  return os;
223 }
224 
227 
228 // Converts (roll, pitch, yaw) to a unit length quaternion. Based on the URDF
229 // specification http://wiki.ros.org/urdf/XML/joint.
230 Eigen::Quaterniond RollPitchYaw(double roll, double pitch, double yaw);
231 
232 // Returns an transform::Rigid3d given a 'dictionary' containing 'translation'
233 // (x, y, z) and 'rotation' which can either we an array of (roll, pitch, yaw)
234 // or a dictionary with (w, x, y, z) values as a quaternion.
236 
237 } // namespace transform
238 } // namespace cartographer
239 
240 #endif // CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_
static Rigid3 Rotation(const AngleAxis &angle_axis)
Rigid2< OtherType > cast() const
Eigen::Matrix< FloatType, 2, 1 > Vector
Rigid3(const Vector &translation, const Quaternion &rotation)
static Rigid2 Rotation(const double rotation)
Rigid3< OtherType > cast() const
Rigid2(const Vector &translation, const Rotation2D &rotation)
const Quaternion & rotation() const
static Rigid3 FromArrays(const std::array< FloatType, 4 > &rotation, const std::array< FloatType, 3 > &translation)
static Rigid2< FloatType > Identity()
Rigid2< FloatType > operator*(const Rigid2< FloatType > &lhs, const Rigid2< FloatType > &rhs)
static Rigid3< FloatType > Identity()
Eigen::Quaternion< double > Quaternion
Rigid3(const Vector &translation, const AngleAxis &rotation)
const Vector & translation() const
Eigen::Rotation2D< FloatType > Rotation2D
static Rigid3 Rotation(const Quaternion &rotation)
std::ostream & operator<<(std::ostream &os, const cartographer::transform::Rigid2< T > &rigid)
transform::Rigid3d FromDictionary(common::LuaParameterDictionary *dictionary)
const Vector & translation() const
Rigid2(const Vector &translation, const double rotation)
T NormalizeAngleDifference(T difference)
Definition: math.h:62
static Rigid2 Rotation(const Rotation2D &rotation)
static Rigid3 Translation(const Vector &vector)
std::string DebugString() const
Eigen::Quaterniond RollPitchYaw(const double roll, const double pitch, const double yaw)
Eigen::Matrix< double, 3, 1 > Vector
static Rigid2 Translation(const Vector &vector)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58