transform_delta_cost_functor.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 COST_FUNCTOR_H
18 #define COST_FUNCTOR_H
19 
20 #include <iostream>
21 #include <string>
22 
23 #include "Eigen/Core"
24 #include "Eigen/Geometry"
25 #include "ceres/ceres.h"
26 
27 template <typename FloatType>
28 class Rigid3 {
29 public:
30  using Affine = Eigen::Transform<FloatType, 3, Eigen::Affine>;
31  using Vector = Eigen::Matrix<FloatType, 3, 1>;
32  using Quaternion = Eigen::Quaternion<FloatType>;
33  using AngleAxis = Eigen::AngleAxis<FloatType>;
34 
37  // TODO(damonkohler): Remove
38  explicit Rigid3(const Affine& affine)
39  : translation_(affine.translation()), rotation_(affine.rotation()) {}
41  : translation_(translation), rotation_(rotation) {}
43  : translation_(translation), rotation_(rotation) {}
44 
45  static Rigid3 Rotation(const AngleAxis& angle_axis) {
46  return Rigid3(Vector::Zero(), Quaternion(angle_axis));
47  }
48 
50  return Rigid3(Vector::Zero(), rotation);
51  }
52 
53  static Rigid3 Translation(const Vector& vector) {
54  return Rigid3(vector, Quaternion::Identity());
55  }
56 
58  return Rigid3<FloatType>(Vector::Zero(), Quaternion::Identity());
59  }
60 
61  template <typename OtherType>
63  return Rigid3<OtherType>(translation_.template cast<OtherType>(),
64  rotation_.template cast<OtherType>());
65  }
66 
67  const Vector& translation() const { return translation_; }
68  const Quaternion& rotation() const { return rotation_; }
69 
70  Rigid3 inverse() const {
71  const Quaternion rotation = rotation_.conjugate();
72  const Vector translation = -(rotation * translation_);
73  return Rigid3(translation, rotation);
74  }
75 
76  std::string DebugString() const {
77  std::string out;
78  out.append("{ t: [");
79  out.append(std::to_string(translation().x()));
80  out.append(", ");
81  out.append(std::to_string(translation().y()));
82  out.append(", ");
83  out.append(std::to_string(translation().z()));
84  out.append("], q: [");
85  out.append(std::to_string(rotation().w()));
86  out.append(", ");
87  out.append(std::to_string(rotation().x()));
88  out.append(", ");
89  out.append(std::to_string(rotation().y()));
90  out.append(", ");
91  out.append(std::to_string(rotation().z()));
92  out.append("] }");
93  return out;
94  }
95 
96 private:
99 };
100 
101 template <typename FloatType>
103  const Rigid3<FloatType>& rhs) {
104  return Rigid3<FloatType>(
105  lhs.rotation() * rhs.translation() + lhs.translation(),
106  (lhs.rotation() * rhs.rotation()).normalized());
107 }
108 
109 template <typename FloatType>
111  const Rigid3<FloatType>& rigid,
112  const typename Rigid3<FloatType>::Vector& point) {
113  return rigid.rotation() * point + rigid.translation();
114 }
115 
116 // This is needed for gmock.
117 template <typename T>
118 std::ostream& operator<<(std::ostream& os,
119  Rigid3<T>& rigid) {
120  os << rigid.DebugString();
121  return os;
122 }
123 
126 
128 public:
129  // Creates an OccupiedSpaceCostFunctor using the specified grid, 'rotation' to
130  // add to all poses, and point cloud.
131  TransformDeltaCostFunctor(Eigen::Matrix<double, 2, 1> pos_world,
132  Eigen::Matrix<double, 2, 1> pos_gps,
133  double covariance)
134  : pos_world_(pos_world),
135  pos_gps_(pos_gps),
136  inv_covariance_(1.0/covariance){
137  if(covariance < 1e-7)
138  {
139  LOG(WARNING)<<"TransformDeltaCostFunctor covariance too small: "<<covariance;
140  inv_covariance_ = 1.0;
141  }
142  }
143 
145  TransformDeltaCostFunctor& operator=(const TransformDeltaCostFunctor&) = delete;
146 
147  template <typename T>
148  bool operator()(const T* const translation, const T* const rotation,
149  T* const residual) const {
150  const Rigid3<T> transform(
151  Eigen::Matrix<T, 3, 1>(translation[0], translation[1], T(0)),
152  Eigen::Quaternion<T>(ceres::cos(rotation[0]/T(2)), T(0), T(0),
153  ceres::sin(rotation[0]/T(2))));
154  return Evaluate(transform, residual);
155  }
156 
157  template <typename T>
158  bool Evaluate(const Rigid3<T>& transform,
159  T* const residual) const {
160  const Eigen::Matrix<T, 3, 1> delta =
161  Eigen::Matrix<T, 3, 1>(T(pos_gps_[0]), T(pos_gps_[1]), T(0))
162  - transform * Eigen::Matrix<T, 3, 1>(T(pos_world_[0]), T(pos_world_[1]), T(0));
163  residual[0] = T(inv_covariance_)*delta[0];
164  residual[1] = T(inv_covariance_)*delta[1];
165  return true;
166  }
167 
168 private:
169  Eigen::Matrix<double, 2, 1> pos_world_;
170  Eigen::Matrix<double, 2, 1> pos_gps_;
172 };
173 
174 
175 
176 
177 #endif // COST_FUNCTOR_H
Rigid3(const Affine &affine)
const Quaternion & rotation() const
Rigid3(const Vector &translation, const Quaternion &rotation)
static Rigid3 Rotation(const AngleAxis &angle_axis)
bool Evaluate(const Rigid3< T > &transform, T *const residual) const
Eigen::Quaternion< FloatType > Quaternion
Rigid3(const Vector &translation, const AngleAxis &rotation)
TFSIMD_FORCE_INLINE const tfScalar & y() const
Eigen::Matrix< FloatType, 3, 1 > Vector
Rigid3< FloatType > operator*(const Rigid3< FloatType > &lhs, const Rigid3< FloatType > &rhs)
TFSIMD_FORCE_INLINE Vector3 normalized() const
Eigen::Matrix< double, 2, 1 > pos_gps_
bool operator()(const T *const translation, const T *const rotation, T *const residual) const
static Rigid3< FloatType > Identity()
std::ostream & operator<<(std::ostream &os, Rigid3< T > &rigid)
TFSIMD_FORCE_INLINE const tfScalar & x() const
std::string DebugString() const
Eigen::Matrix< double, 2, 1 > pos_world_
const Vector & translation() const
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & w() const
TransformDeltaCostFunctor(Eigen::Matrix< double, 2, 1 > pos_world, Eigen::Matrix< double, 2, 1 > pos_gps, double covariance)
Rigid3< OtherType > cast() const
Rigid3 inverse() const
static Rigid3 Translation(const Vector &vector)
static Rigid3 Rotation(const Quaternion &rotation)
Eigen::AngleAxis< FloatType > AngleAxis
Eigen::Transform< FloatType, 3, Eigen::Affine > Affine


hector_gps_calibration
Author(s):
autogenerated on Mon Jun 10 2019 13:34:46