NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
pose_rotation_factor.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef GRAPH_FACTORS_POSE_ROTATION_FACTOR_H_
20 #define GRAPH_FACTORS_POSE_ROTATION_FACTOR_H_
21 
22 #include <gtsam/geometry/Pose3.h>
23 #include <gtsam/geometry/Rot3.h>
24 #include <gtsam/nonlinear/NonlinearFactor.h>
25 
26 #include <string>
27 
28 namespace gtsam {
29 class PoseRotationFactor : public NoiseModelFactor2<gtsam::Pose3, gtsam::Pose3> {
30  typedef NoiseModelFactor2<Pose3, Pose3> Base;
31  typedef PoseRotationFactor This;
32 
33  public:
35 
36  PoseRotationFactor(const Rot3& rotation, const SharedNoiseModel& model, Key pose_key_1, Key pose_key_2)
37  : Base(model, pose_key_1, pose_key_2), rotation_(rotation) {}
38 
39  gtsam::NonlinearFactor::shared_ptr clone() const override {
40  return boost::static_pointer_cast<gtsam::NonlinearFactor>(gtsam::NonlinearFactor::shared_ptr(new This(*this)));
41  }
42 
43  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
44  std::cout << s << "PoseRotationFactor, z = ";
45  traits<Rot3>::Print(rotation_);
46  Base::print("", keyFormatter);
47  }
48 
49  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
50  const This* e = dynamic_cast<const This*>(&p);
51  return e && Base::equals(p, tol) && traits<Rot3>::Equals(this->rotation_, e->rotation(), tol);
52  }
53 
54  Vector evaluateError(const Pose3& pose1, const Pose3& pose2, boost::optional<Matrix&> H1 = boost::none,
55  boost::optional<Matrix&> H2 = boost::none) const override {
56  const auto& rot1 = pose1.rotation(H1);
57  const auto& rot2 = pose2.rotation(H2);
58  // TODO(rsoussan): How to use ref to block of H1 and H2 instead of making new matrices and copying?
59  Matrix H1_rot_matrix;
60  Matrix H2_rot_matrix;
61  boost::optional<Matrix&> H1_rot = H1_rot_matrix;
62  boost::optional<Matrix&> H2_rot = H2_rot_matrix;
63  if (!H1) H1_rot = boost::none;
64  if (!H2) H2_rot = boost::none;
65  // Adapted from BetweenFactor.h
66  const auto relative_rotation = traits<Rot3>::Between(rot1, rot2, H1_rot, H2_rot);
67  // manifold equivalent of h(x)-z -> log(z,h(x))
68  traits<Rot3>::ChartJacobian::Jacobian Hlocal;
69  Vector error = traits<Rot3>::Local(rotation_, relative_rotation, boost::none, (H1_rot || H2_rot) ? &Hlocal : 0);
70  if (H1_rot) {
71  *H1_rot = Hlocal * (*H1_rot);
72  H1->block<3, 3>(0, 0) = H1_rot_matrix;
73  }
74  if (H2_rot) {
75  *H2_rot = Hlocal * (*H2_rot);
76  H2->block<3, 3>(0, 0) = H2_rot_matrix;
77  }
78  return error;
79  }
80 
81  const Rot3& rotation() const { return rotation_; }
82 
83  private:
85  template <class ARCHIVE>
86  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
87  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
88  ar& BOOST_SERIALIZATION_NVP(rotation_);
89  }
90 
91  Rot3 rotation_;
92 
93  public:
94  GTSAM_MAKE_ALIGNED_OPERATOR_NEW
95 };
96 } // namespace gtsam
97 
98 #endif // GRAPH_FACTORS_POSE_ROTATION_FACTOR_H_
gtsam::PoseRotationFactor::rotation
const Rot3 & rotation() const
Definition: pose_rotation_factor.h:81
gtsam::PoseRotationFactor
Definition: pose_rotation_factor.h:29
gtsam::PoseRotationFactor::evaluateError
Vector evaluateError(const Pose3 &pose1, const Pose3 &pose2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Definition: pose_rotation_factor.h:54
gtsam::PoseRotationFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
Definition: pose_rotation_factor.h:49
gtsam::PoseRotationFactor::clone
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: pose_rotation_factor.h:39
gtsam::PoseRotationFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: pose_rotation_factor.h:43
Print
int Print(std::string const &msg)
Definition: eps_driver_tool.cc:86
gtsam::PoseRotationFactor::access
friend class boost::serialization::access
Definition: pose_rotation_factor.h:84
gtsam
Definition: cumulative_factor.h:26
error
uint8_t error
Definition: signal_lights.h:89
gtsam::PoseRotationFactor::PoseRotationFactor
PoseRotationFactor(const Rot3 &rotation, const SharedNoiseModel &model, Key pose_key_1, Key pose_key_2)
Definition: pose_rotation_factor.h:36
gtsam::PoseRotationFactor::PoseRotationFactor
PoseRotationFactor()
Definition: pose_rotation_factor.h:34