NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
loc_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_LOC_PROJECTION_FACTOR_H_
20 #define GRAPH_FACTORS_LOC_PROJECTION_FACTOR_H_
21 
22 #include <gtsam/geometry/Point2.h>
23 #include <gtsam/geometry/Pose3.h>
24 #include <gtsam/geometry/SimpleCamera.h>
25 #include <gtsam/nonlinear/NonlinearFactor.h>
26 #include <boost/optional.hpp>
27 
28 #include <string>
29 
30 namespace gtsam {
31 
37 template <class POSE = Pose3, class LANDMARK = Point3, class CALIBRATION = Cal3_S2>
38 class LocProjectionFactor : public NoiseModelFactor1<POSE> {
39  protected:
40  // Keep a copy of measurement and calibration for I/O
41  Point2 measured_;
42  LANDMARK landmark_point_;
43  boost::shared_ptr<CALIBRATION> K_;
44  boost::optional<POSE> body_P_sensor_;
45 
46  // verbosity handling for Cheirality Exceptions
48  bool verboseCheirality_;
50 
52  public:
54  typedef NoiseModelFactor1<POSE> Base;
55 
58 
60  typedef boost::shared_ptr<This> shared_ptr;
61 
64  : measured_(0, 0), landmark_point_(0, 0, 0), throwCheirality_(false), verboseCheirality_(false) {}
65 
77  LocProjectionFactor(const Point2& measured, const LANDMARK& landmark_point, const SharedNoiseModel& model,
78  Key poseKey, const boost::shared_ptr<CALIBRATION>& K,
79  boost::optional<POSE> body_P_sensor = boost::none)
80  : Base(model, poseKey),
83  K_(K),
85  throwCheirality_(false),
86  verboseCheirality_(false) {}
87 
103  LocProjectionFactor(const Point2& measured, const LANDMARK& landmark_point, const SharedNoiseModel& model,
104  Key poseKey, const boost::shared_ptr<CALIBRATION>& K, bool throwCheirality,
105  bool verboseCheirality, boost::optional<POSE> body_P_sensor = boost::none)
106  : Base(model, poseKey),
109  K_(K),
113 
115  virtual ~LocProjectionFactor() {}
116 
118  virtual NonlinearFactor::shared_ptr clone() const {
119  return boost::static_pointer_cast<NonlinearFactor>(NonlinearFactor::shared_ptr(new This(*this)));
120  }
121 
127  void print(const std::string& s = "", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
128  std::cout << s << "LocProjectionFactor, z = ";
130  std::cout << " landmark_point = ";
132  if (this->body_P_sensor_) this->body_P_sensor_->print(" sensor pose in body frame: ");
133  Base::print("", keyFormatter);
134  }
135 
137  virtual bool equals(const NonlinearFactor& p, double tol = 1e-9) const {
138  const This* e = dynamic_cast<const This*>(&p);
139  return e && Base::equals(p, tol) && traits<Point2>::Equals(this->measured_, e->measured_, tol) &&
140  traits<LANDMARK>::Equals(this->landmark_point_, e->landmark_point_, tol) && this->K_->equals(*e->K_, tol) &&
141  ((!body_P_sensor_ && !e->body_P_sensor_) ||
143  }
144 
146  // TODO(rsoussan): Replace PinholeCamera with PinholePose?
147  Vector evaluateError(const Pose3& pose, boost::optional<Matrix&> H1 = boost::none) const {
148  try {
149  if (body_P_sensor_) {
150  if (H1) {
151  Matrix H0;
152  PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_, H0), *K_);
153  Point2 reprojectionError(camera.project(landmark_point_, H1, boost::none, boost::none) - measured_);
154  *H1 = *H1 * H0;
155  return reprojectionError;
156  } else {
157  PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_), *K_);
158  return camera.project(landmark_point_, H1, boost::none, boost::none) - measured_;
159  }
160  } else {
161  PinholeCamera<CALIBRATION> camera(pose, *K_);
162  return camera.project(landmark_point_, H1, boost::none, boost::none) - measured_;
163  }
164  } catch (CheiralityException& e) {
165  if (H1) *H1 = Matrix::Zero(2, 6);
166  if (verboseCheirality_)
167  std::cout << e.what() << ": Landmark moved behind camera " << DefaultKeyFormatter(this->key()) << std::endl;
168  if (throwCheirality_) throw CheiralityException(this->key());
169  }
170  return Vector2::Constant(0.0);
171  }
172 
173  bool cheiralityError(const Pose3& pose) const {
174  try {
175  if (body_P_sensor_) {
176  PinholeCamera<CALIBRATION> camera(pose.compose(*body_P_sensor_), *K_);
177  camera.project(landmark_point_) - measured_;
178  } else {
179  PinholeCamera<CALIBRATION> camera(pose, *K_);
180  camera.project(landmark_point_) - measured_;
181  }
182  } catch (CheiralityException& e) {
183  return true;
184  }
185  return false;
186  }
187 
189  const Point2& measured() const { return measured_; }
190 
191  const LANDMARK& landmark_point() const { return landmark_point_; }
192 
193  LANDMARK& landmark_point() { return landmark_point_; }
194 
196  inline const boost::shared_ptr<CALIBRATION> calibration() const { return K_; }
197 
199  inline bool verboseCheirality() const { return verboseCheirality_; }
200 
202  inline bool throwCheirality() const { return throwCheirality_; }
203 
204  boost::optional<POSE> body_P_sensor() const { return body_P_sensor_; }
205 
206  private:
209  template <class ARCHIVE>
210  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
211  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
212  ar& BOOST_SERIALIZATION_NVP(measured_);
213  ar& BOOST_SERIALIZATION_NVP(landmark_point_);
214  ar& BOOST_SERIALIZATION_NVP(K_);
215  ar& BOOST_SERIALIZATION_NVP(body_P_sensor_);
216  ar& BOOST_SERIALIZATION_NVP(throwCheirality_);
217  ar& BOOST_SERIALIZATION_NVP(verboseCheirality_);
218  }
219 
220  public:
221  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
222 };
223 
225 template <class POSE, class LANDMARK, class CALIBRATION>
226 struct traits<LocProjectionFactor<POSE, LANDMARK, CALIBRATION>>
227  : public Testable<LocProjectionFactor<POSE, LANDMARK, CALIBRATION>> {};
228 
229 } // namespace gtsam
230 
231 #endif // GRAPH_FACTORS_LOC_PROJECTION_FACTOR_H_
gtsam::LocProjectionFactor::landmark_point
LANDMARK & landmark_point()
Definition: loc_projection_factor.h:193
gtsam::LocProjectionFactor::LocProjectionFactor
LocProjectionFactor()
Default constructor.
Definition: loc_projection_factor.h:63
gtsam::LocProjectionFactor::~LocProjectionFactor
virtual ~LocProjectionFactor()
Definition: loc_projection_factor.h:115
camera
Definition: camera_model.h:26
gtsam::LocProjectionFactor::Base
NoiseModelFactor1< POSE > Base
shorthand for base class type
Definition: loc_projection_factor.h:54
gtsam::LocProjectionFactor
Definition: loc_projection_factor.h:38
gtsam::LocProjectionFactor::cheiralityError
bool cheiralityError(const Pose3 &pose) const
Definition: loc_projection_factor.h:173
gtsam::LocProjectionFactor::measured
const Point2 & measured() const
Definition: loc_projection_factor.h:189
gtsam::LocProjectionFactor::LocProjectionFactor
LocProjectionFactor(const Point2 &measured, const LANDMARK &landmark_point, const SharedNoiseModel &model, Key poseKey, const boost::shared_ptr< CALIBRATION > &K, bool throwCheirality, bool verboseCheirality, boost::optional< POSE > body_P_sensor=boost::none)
Definition: loc_projection_factor.h:103
gtsam::LocProjectionFactor::body_P_sensor_
boost::optional< POSE > body_P_sensor_
The pose of the sensor in the body frame.
Definition: loc_projection_factor.h:44
gtsam::LocProjectionFactor::K_
boost::shared_ptr< CALIBRATION > K_
shared pointer to calibration object
Definition: loc_projection_factor.h:43
gtsam::LocProjectionFactor::access
friend class boost::serialization::access
Serialization function.
Definition: loc_projection_factor.h:208
gtsam::LocProjectionFactor::evaluateError
Vector evaluateError(const Pose3 &pose, boost::optional< Matrix & > H1=boost::none) const
Evaluate error h(x)-z and optionally derivatives.
Definition: loc_projection_factor.h:147
gtsam::LocProjectionFactor::verboseCheirality_
bool verboseCheirality_
Definition: loc_projection_factor.h:49
gtsam::LocProjectionFactor::LocProjectionFactor
LocProjectionFactor(const Point2 &measured, const LANDMARK &landmark_point, const SharedNoiseModel &model, Key poseKey, const boost::shared_ptr< CALIBRATION > &K, boost::optional< POSE > body_P_sensor=boost::none)
Definition: loc_projection_factor.h:77
gtsam::LocProjectionFactor::equals
virtual bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: loc_projection_factor.h:137
Print
int Print(std::string const &msg)
Definition: eps_driver_tool.cc:86
gtsam::LocProjectionFactor::body_P_sensor
boost::optional< POSE > body_P_sensor() const
Definition: loc_projection_factor.h:204
gtsam::LocProjectionFactor::shared_ptr
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: loc_projection_factor.h:60
gtsam::LocProjectionFactor::landmark_point_
LANDMARK landmark_point_
Landmark point.
Definition: loc_projection_factor.h:42
gtsam::LocProjectionFactor::clone
virtual NonlinearFactor::shared_ptr clone() const
Definition: loc_projection_factor.h:118
gtsam::LocProjectionFactor::throwCheirality_
bool throwCheirality_
Definition: loc_projection_factor.h:47
gtsam::LocProjectionFactor::calibration
const boost::shared_ptr< CALIBRATION > calibration() const
Definition: loc_projection_factor.h:196
gtsam
Definition: cumulative_factor.h:26
gtsam::LocProjectionFactor::throwCheirality
bool throwCheirality() const
Definition: loc_projection_factor.h:202
gtsam::LocProjectionFactor::This
LocProjectionFactor< POSE, LANDMARK, CALIBRATION > This
shorthand for this class
Definition: loc_projection_factor.h:57
gtsam::LocProjectionFactor::measured_
Point2 measured_
2D measurement
Definition: loc_projection_factor.h:41
gtsam::LocProjectionFactor::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: loc_projection_factor.h:127
gtsam::LocProjectionFactor::verboseCheirality
bool verboseCheirality() const
Definition: loc_projection_factor.h:199
gtsam::LocProjectionFactor::landmark_point
const LANDMARK & landmark_point() const
Definition: loc_projection_factor.h:191