farm-ng-core
pose3.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
10 
11 #include "sophus/lie/isometry3.h"
12 
13 namespace sophus {
14 
15 template <class TScalar>
16 class Pose3 {
17  public:
18  using Scalar = TScalar;
20  using Tangent = typename Isometry::Tangent;
21  using Rotation = typename Isometry::Rotation;
22  using Params = typename Isometry::Params;
24  Isometry const& a_from_b,
25  std::string const& frame_a,
26  std::string const& frame_b,
27  Tangent const& tangent_in_b = Tangent::Zero())
28  : a_from_b_(a_from_b),
29  frame_a_(frame_a),
30  frame_b_(frame_b),
31  tangent_in_b_(tangent_in_b) {}
32 
33  std::string const& frameA() const { return frame_a_; }
34  std::string& frameA() { return frame_a_; }
35 
36  std::string const& frameB() const { return frame_b_; }
37  std::string& frameB() { return frame_b_; }
38 
39  Isometry const& aFromB() const { return a_from_b_; }
40  Isometry& aFromB() { return a_from_b_; }
41 
42  /// Rate of change of the pose ``a_from_b`` represented in frame ``b``.
43  ///
44  /// In other words tangent_in_b_ is the relative linear and angular velocity
45  /// of the rigid body ``b`` (with respect to frame ``a``) and this
46  /// velocity is expressed in frame ``b``.
47  Tangent const& tangentInB() const { return tangent_in_b_; }
48 
49  /// Mutable version of tangentInB.
50  Tangent& tangentInB() { return tangent_in_b_; }
51 
52  // deprecated naming convention
53  Tangent const& tangentOfBInA() const { return tangentInB(); }
54 
55  // deprecated naming convention
56  Tangent& tangentOfBInA() { return tangentInB(); }
57 
58  /// Pose of entity ``b`` in the ``a`` frame.
59  Isometry bFromA() const { return a_from_b_.inverse(); }
60 
61  /// Changes origin frame of tangent vector from frame ``bar`` to frame
62  /// ``foo``.
64  sophus::Isometry3F64 const& foo_from_bar, Tangent const& tangent_in_bar) {
65  return foo_from_bar.adj() * tangent_in_bar;
66  }
67 
68  Tangent log() const { return a_from_b_.log(); }
69 
70  Eigen::Vector<Scalar, 3> translation() const {
71  return aFromB().translation();
72  }
73 
74  Eigen::VectorBlock<Params, 3> translation() {
75  return a_from_b_.translation();
76  }
77 
78  Rotation rotation() const { return aFromB().rotation(); }
79  void setRotation(Rotation const& rotation) {
80  return aFromB().setRotation(rotation);
81  }
82 
83  /// Inverse of the pose (and its velocity).
84  ///
85  Pose3 inverse() const {
86  return Pose3(
87  bFromA(),
88  frame_b_,
89  frame_a_,
90  -changeTangentOrigin(aFromB(), tangent_in_b_));
91  }
92 
93  /// Evolves the pose by a small increment ``dt``.
94  ///
95  /// It is assumed that the egocentric velocity of the rigid body ``b`` stays
96  /// constant.
97  Pose3 evolve(double dt) const {
98  return Pose3(
99  aFromB() * Isometry::exp(tangent_in_b_ * dt),
100  frame_a_,
101  frame_b_,
102  tangent_in_b_);
103  }
104 
105  friend Expected<Tangent> error(
106  Pose3 const& lhs_a_from_b, Pose3 const& rhs_a_from_b) {
107  return (lhs_a_from_b.inverse() * rhs_a_from_b)
108  .and_then([](Expected<Pose3> const& pose) -> Expected<Tangent> {
109  return pose->log();
110  });
111  }
112 
113  friend Expected<Pose3> operator*(Pose3 const& lhs, Pose3 const& rhs) {
114  if (lhs.frameB() != rhs.frameA()) {
115  return FARM_UNEXPECTED(
116  "Pose frame error: lhs a={} b={} rhs a={} b={}",
117  lhs.frameA(),
118  lhs.frameB(),
119  rhs.frameA(),
120  rhs.frameB());
121  }
122 
123  /// for notation simplicity, let's introduce the following three frames:
124  ///
125  /// a := lhs.frameA()
126  /// mid := lhs.frameB() == rhs.frameA()
127  /// b := rhs.frameB()
128 
129  Isometry a_from_mid = lhs.aFromB();
130  Isometry mid_from_b = rhs.aFromB();
131  // Relative velocity of the rigid body ``mid`` (with respect to frame
132  // a) in frame ``mid``.
133  Tangent velocity_of_mid_wrt_a_in_mid = lhs.tangentInB();
134  // Relative velocity of the rigid body ``b`` (with respect to frame
135  // mid) in frame ``b``.
136  Tangent velocity_of_b_wrt_mid_in_b = rhs.tangentInB();
137 
138  /// We aim to calculate the pose of ``a_from_b`` and its velocity
139  /// ``velocity_of_b_wrt_a_in_b``.
140  Isometry a_from_b = a_from_mid * mid_from_b;
141 
142  // Given relative velocity of the rigid body ``mid`` (with respect to frame
143  // a) in frame ``mid``,
144  // we calculate the relative velocity of the rigid body ``mid`` (with
145  // respect to frame a) in frame ``b``.
146  Eigen::Vector<double, 6> velocity_of_mid_wrt_a_in_b =
147  changeTangentOrigin(mid_from_b.inverse(), velocity_of_mid_wrt_a_in_mid);
148 
149  // This is basically just vec(a,mid) + vec(mid,b) = vec(a,b). This is valid
150  // because all velocities are expressed in the same frame b.
151  Tangent velocity_of_b_wrt_a_in_b =
152  velocity_of_mid_wrt_a_in_b + velocity_of_b_wrt_mid_in_b;
153 
154  return Pose3(
155  a_from_b, lhs.frameA(), rhs.frameB(), velocity_of_b_wrt_a_in_b);
156  }
157 
158  friend Expected<Pose3> operator*(
159  Pose3 const& lhs, Expected<Pose3> const& rhs) {
160  if (!rhs) {
161  return rhs;
162  }
163  return lhs * (*rhs);
164  }
165 
166  friend Expected<Pose3> operator*(
167  Expected<Pose3> const& lhs, Expected<Pose3> const& rhs) {
168  if (!lhs) {
169  return lhs;
170  }
171  if (!rhs) {
172  return rhs;
173  }
174  return (*lhs) * (*rhs);
175  }
176 
177  friend Expected<Pose3> operator*(
178  Expected<Pose3> const& lhs, Pose3 const& rhs) {
179  if (!lhs) {
180  return lhs;
181  }
182  return (*lhs) * rhs;
183  }
184 
185  private:
186  Isometry a_from_b_;
187  std::string frame_a_;
188  std::string frame_b_;
189  Tangent tangent_in_b_;
190 };
191 
194 
195 } // namespace sophus
sophus::Pose3::tangentInB
Tangent & tangentInB()
Mutable version of tangentInB.
Definition: pose3.h:50
sophus::Pose3::aFromB
Isometry & aFromB()
Definition: pose3.h:40
sophus::Pose3::Pose3
Pose3(Isometry const &a_from_b, std::string const &frame_a, std::string const &frame_b, Tangent const &tangent_in_b=Tangent::Zero())
Definition: pose3.h:23
sophus::Isometry3< Scalar >::Params
typename Base::Params Params
Definition: isometry3.h:37
sophus::Pose3::bFromA
Isometry bFromA() const
Pose of entity b in the a frame.
Definition: pose3.h:59
sophus::Pose3::frameA
std::string const & frameA() const
Definition: pose3.h:33
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::Pose3::operator*
friend Expected< Pose3 > operator*(Pose3 const &lhs, Expected< Pose3 > const &rhs)
Definition: pose3.h:158
sophus::Pose3::operator*
friend Expected< Pose3 > operator*(Expected< Pose3 > const &lhs, Expected< Pose3 > const &rhs)
Definition: pose3.h:166
sophus::lie::Group::adj
auto adj() const -> Eigen::Matrix< Scalar, kDof, kDof >
Definition: lie_group.h:147
sophus::Pose3::translation
Eigen::Vector< Scalar, 3 > translation() const
Definition: pose3.h:70
sophus::Pose3::evolve
Pose3 evolve(double dt) const
Evolves the pose by a small increment dt.
Definition: pose3.h:97
sophus::Pose3::Tangent
typename Isometry::Tangent Tangent
Definition: pose3.h:20
sophus::Pose3::frameB
std::string & frameB()
Definition: pose3.h:37
sophus::Pose3::inverse
Pose3 inverse() const
Inverse of the pose (and its velocity).
Definition: pose3.h:85
isometry3.h
sophus::Pose3::Rotation
typename Isometry::Rotation Rotation
Definition: pose3.h:21
sophus::Pose3::operator*
friend Expected< Pose3 > operator*(Pose3 const &lhs, Pose3 const &rhs)
Definition: pose3.h:113
sophus::Pose3::changeTangentOrigin
static Tangent changeTangentOrigin(sophus::Isometry3F64 const &foo_from_bar, Tangent const &tangent_in_bar)
Changes origin frame of tangent vector from frame bar to frame foo.
Definition: pose3.h:63
sophus::lie::Group::inverse
auto inverse() const -> Derived
Definition: lie_group.h:127
sophus::Pose3::log
Tangent log() const
Definition: pose3.h:68
sophus::Pose3::operator*
friend Expected< Pose3 > operator*(Expected< Pose3 > const &lhs, Pose3 const &rhs)
Definition: pose3.h:177
sophus::Pose3::Scalar
TScalar Scalar
Definition: pose3.h:18
sophus::Isometry3::translation
auto translation() -> Eigen::VectorBlock< Params, 3 >
Definition: isometry3.h:132
FARM_UNEXPECTED
#define FARM_UNEXPECTED(cstr,...)
Definition: expected.h:86
sophus::Pose3::frameB
std::string const & frameB() const
Definition: pose3.h:36
sophus::Pose3
Definition: pose3.h:16
sophus::Pose3::translation
Eigen::VectorBlock< Params, 3 > translation()
Definition: pose3.h:74
sophus::lie::Group::log
auto log() const -> Tangent
Definition: lie_group.h:97
sophus::Isometry3::setRotation
auto setRotation(Rotation3< Scalar > const &rotation)
Definition: isometry3.h:146
sophus::Pose3::rotation
Rotation rotation() const
Definition: pose3.h:78
sophus::Isometry3< Scalar >::Tangent
typename Base::Tangent Tangent
Definition: isometry3.h:36
sophus::Pose3::aFromB
Isometry const & aFromB() const
Definition: pose3.h:39
sophus::Pose3::Isometry
Isometry3< Scalar > Isometry
Definition: pose3.h:19
sophus::Pose3::tangentOfBInA
Tangent const & tangentOfBInA() const
Definition: pose3.h:53
sophus::Pose3::tangentOfBInA
Tangent & tangentOfBInA()
Definition: pose3.h:56
sophus::Isometry3< Scalar >
sophus::Pose3::error
friend Expected< Tangent > error(Pose3 const &lhs_a_from_b, Pose3 const &rhs_a_from_b)
Definition: pose3.h:105
sophus::Isometry3< Scalar >::Rotation
Rotation3< Scalar > Rotation
Definition: isometry3.h:34
sophus::lie::Group< Isometry3, Scalar, lie::WithDimAndSubgroup< 3, lie::Rotation3Impl >::SemiDirectProduct >::exp
static auto exp(Tangent const &tangent) -> Derived
Definition: lie_group.h:93
sophus::Pose3::frameA
std::string & frameA()
Definition: pose3.h:34
sophus::Pose3::Params
typename Isometry::Params Params
Definition: pose3.h:22
sophus::Pose3::tangentInB
Tangent const & tangentInB() const
Rate of change of the pose a_from_b represented in frame b.
Definition: pose3.h:47
sophus::Pose3::setRotation
void setRotation(Rotation const &rotation)
Definition: pose3.h:79
sophus::Isometry3::rotation
auto rotation() const -> Rotation3< Scalar > const
Definition: isometry3.h:141