NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
inverse_depth_projection_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_INVERSE_DEPTH_PROJECTION_FACTOR_H_
20 #define GRAPH_FACTORS_INVERSE_DEPTH_PROJECTION_FACTOR_H_
21 
23 
24 #include <gtsam/geometry/Pose3.h>
25 #include <gtsam/nonlinear/NonlinearFactor.h>
26 #include <boost/optional.hpp>
27 
28 #include <string>
29 
30 namespace gtsam {
31 
36 class InverseDepthProjectionFactor : public NoiseModelFactor3<Pose3, vision_common::InverseDepthMeasurement, Pose3> {
37  protected:
38  Point2 measured_;
40 
41  public:
42  typedef NoiseModelFactor3<Pose3, vision_common::InverseDepthMeasurement, Pose3> Base;
43  typedef boost::shared_ptr<InverseDepthProjectionFactor> shared_ptr;
45 
47 
57  InverseDepthProjectionFactor(const Point2& measured, const SharedNoiseModel& model, Key sourcePoseKey,
58  Key inverseDepthMeasurementKey, Key targetPoseKey)
59  : Base(model, sourcePoseKey, inverseDepthMeasurementKey, targetPoseKey),
61  verboseCheirality_(false) {}
62 
74  InverseDepthProjectionFactor(const Point2& measured, const SharedNoiseModel& model, Key sourcePoseKey,
75  Key inverseDepthCameraPoseKey, Key targetPoseKey, bool verboseCheirality)
76  : Base(model, sourcePoseKey, inverseDepthCameraPoseKey, targetPoseKey),
79 
81 
83  virtual NonlinearFactor::shared_ptr clone() const {
84  return boost::static_pointer_cast<NonlinearFactor>(NonlinearFactor::shared_ptr(new This(*this)));
85  }
86 
92  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
93  std::cout << s << "InverseDepthProjectionFactor, z = ";
95  Base::print("", keyFormatter);
96  }
97 
98  bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
99  const This* e = dynamic_cast<const This*>(&p);
100  return e && Base::equals(p, tol) && traits<Point2>::Equals(this->measured_, e->measured_, tol);
101  }
102 
104  Vector evaluateError(const Pose3& world_T_source,
105  const vision_common::InverseDepthMeasurement& inverseDepthMeasurement,
106  const Pose3& world_T_target,
107  boost::optional<Matrix&> d_projected_point_d_world_T_source = boost::none,
108  boost::optional<Matrix&> d_projected_point_d_inverse_depth = boost::none,
109  boost::optional<Matrix&> d_projected_point_d_world_T_target = boost::none) const override {
110  const auto projected_measurement =
111  inverseDepthMeasurement.Project(world_T_source, world_T_target, d_projected_point_d_world_T_source,
112  d_projected_point_d_world_T_target, d_projected_point_d_inverse_depth);
113  if (!projected_measurement) {
114  if (d_projected_point_d_world_T_source) *d_projected_point_d_world_T_source = Matrix::Zero(2, 6);
115  if (d_projected_point_d_world_T_target) *d_projected_point_d_world_T_target = Matrix::Zero(2, 6);
116  if (d_projected_point_d_inverse_depth) *d_projected_point_d_inverse_depth = Matrix::Zero(2, 1);
117  if (verboseCheirality_)
118  std::cout << "Landmark moved behind camera " << DefaultKeyFormatter(this->key2()) << std::endl;
119  return Vector2::Constant(0.0);
120  }
121  return *projected_measurement - measured_;
122  }
123 
124  const Point2& measured() const { return measured_; }
125 
126  inline bool verboseCheirality() const { return verboseCheirality_; }
127 
128  private:
131  template <class ARCHIVE>
132  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
133  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
134  ar& BOOST_SERIALIZATION_NVP(measured_);
135  ar& BOOST_SERIALIZATION_NVP(verboseCheirality_);
136  }
137 
138  public:
139  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
140 };
141 
143 template <>
144 struct traits<InverseDepthProjectionFactor> : public Testable<InverseDepthProjectionFactor> {};
145 
146 } // namespace gtsam
147 
148 #endif // GRAPH_FACTORS_INVERSE_DEPTH_PROJECTION_FACTOR_H_
gtsam::InverseDepthProjectionFactor::equals
bool equals(const NonlinearFactor &p, double tol=1e-9) const override
Definition: inverse_depth_projection_factor.h:98
gtsam::InverseDepthProjectionFactor::Base
NoiseModelFactor3< Pose3, vision_common::InverseDepthMeasurement, Pose3 > Base
Definition: inverse_depth_projection_factor.h:42
vision_common::InverseDepthMeasurement::Project
boost::optional< Eigen::Vector2d > Project(const gtsam::Pose3 &world_T_source_body, const gtsam::Pose3 &world_T_target_body, boost::optional< gtsam::Matrix & > d_projected_point_d_world_T_source_body=boost::none, boost::optional< gtsam::Matrix & > d_projected_point_d_world_T_target_body=boost::none, boost::optional< gtsam::Matrix & > d_projected_point_d_inverse_depth=boost::none) const
Definition: inverse_depth_measurement.h:58
gtsam::InverseDepthProjectionFactor::verboseCheirality
bool verboseCheirality() const
Definition: inverse_depth_projection_factor.h:126
inverse_depth_measurement.h
gtsam::InverseDepthProjectionFactor::measured_
Point2 measured_
Definition: inverse_depth_projection_factor.h:38
gtsam::InverseDepthProjectionFactor::evaluateError
Vector evaluateError(const Pose3 &world_T_source, const vision_common::InverseDepthMeasurement &inverseDepthMeasurement, const Pose3 &world_T_target, boost::optional< Matrix & > d_projected_point_d_world_T_source=boost::none, boost::optional< Matrix & > d_projected_point_d_inverse_depth=boost::none, boost::optional< Matrix & > d_projected_point_d_world_T_target=boost::none) const override
Evaluate error h(x)-z and optionally derivatives.
Definition: inverse_depth_projection_factor.h:104
gtsam::InverseDepthProjectionFactor::InverseDepthProjectionFactor
InverseDepthProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key sourcePoseKey, Key inverseDepthMeasurementKey, Key targetPoseKey)
Definition: inverse_depth_projection_factor.h:57
gtsam::InverseDepthProjectionFactor::InverseDepthProjectionFactor
InverseDepthProjectionFactor(const Point2 &measured, const SharedNoiseModel &model, Key sourcePoseKey, Key inverseDepthCameraPoseKey, Key targetPoseKey, bool verboseCheirality)
Definition: inverse_depth_projection_factor.h:74
gtsam::InverseDepthProjectionFactor
Definition: inverse_depth_projection_factor.h:36
gtsam::InverseDepthProjectionFactor::measured
const Point2 & measured() const
Definition: inverse_depth_projection_factor.h:124
Print
int Print(std::string const &msg)
Definition: eps_driver_tool.cc:86
gtsam::InverseDepthProjectionFactor::~InverseDepthProjectionFactor
virtual ~InverseDepthProjectionFactor()
Definition: inverse_depth_projection_factor.h:80
gtsam::InverseDepthProjectionFactor::access
friend class boost::serialization::access
Serialization function.
Definition: inverse_depth_projection_factor.h:130
gtsam
Definition: cumulative_factor.h:26
gtsam::InverseDepthProjectionFactor::This
InverseDepthProjectionFactor This
Definition: inverse_depth_projection_factor.h:44
gtsam::InverseDepthProjectionFactor::shared_ptr
boost::shared_ptr< InverseDepthProjectionFactor > shared_ptr
Definition: inverse_depth_projection_factor.h:43
gtsam::InverseDepthProjectionFactor::verboseCheirality_
bool verboseCheirality_
Definition: inverse_depth_projection_factor.h:39
gtsam::InverseDepthProjectionFactor::clone
virtual NonlinearFactor::shared_ptr clone() const
Definition: inverse_depth_projection_factor.h:83
gtsam::InverseDepthProjectionFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: inverse_depth_projection_factor.h:92
vision_common::InverseDepthMeasurement
Definition: inverse_depth_measurement.h:40
gtsam::InverseDepthProjectionFactor::InverseDepthProjectionFactor
InverseDepthProjectionFactor()
Definition: inverse_depth_projection_factor.h:46