InvDepthFactor3.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file InvDepthFactor3.h
  3. * @brief Inverse Depth Factor based on Civera09tro, Montiel06rss.
  4. * Landmarks are initialized from the first camera observation with
  5. * (x,y,z,theta,phi,inv_depth), where x,y,z are the coordinates of
  6. * the camera. InvDepthCamera provides methods to initialize inverse
  7. * depth landmarks (backproject), and to convert inverse depth
  8. * landmarks to cartesian coordinates (Point3) for visualization, etc.
  9. * The inverse depth parameterization is split into (x,y,z,theta,phi),
  10. * (inv_depth) to make it easy to add a prior on inverse depth alone
  11. * @author Chris Beall
  12. */
  13. #pragma once
  14. #include <gtsam/geometry/Cal3_S2.h>
  15. #include <gtsam/nonlinear/NonlinearFactor.h>
  16. #include <gtsam_unstable/geometry/InvDepthCamera3.h>
  17. namespace gtsam {
  18. /**
  19. * Ternary factor representing a visual measurement that includes inverse depth
  20. */
  21. template<class POSE, class LANDMARK, class INVDEPTH>
  22. class InvDepthFactor3: public NoiseModelFactor3<POSE, LANDMARK, INVDEPTH> {
  23. protected:
  24. // Keep a copy of measurement and calibration for I/O
  25. Point2 measured_; ///< 2D measurement
  26. boost::shared_ptr<Cal3_S2> K_; ///< shared pointer to calibration object
  27. public:
  28. /// shorthand for base class type
  29. typedef NoiseModelFactor3<POSE, LANDMARK, INVDEPTH> Base;
  30. /// shorthand for this class
  31. typedef InvDepthFactor3<POSE, LANDMARK, INVDEPTH> This;
  32. /// shorthand for a smart pointer to a factor
  33. typedef boost::shared_ptr<This> shared_ptr;
  34. /// Default constructor
  35. InvDepthFactor3() :
  36. measured_(0.0, 0.0), K_(new Cal3_S2(444, 555, 666, 777, 888)) {
  37. }
  38. /**
  39. * Constructor
  40. * TODO: Mark argument order standard (keys, measurement, parameters)
  41. * @param measured is the 2 dimensional location of point in image (the measurement)
  42. * @param model is the standard deviation
  43. * @param poseKey is the index of the camera pose
  44. * @param pointKey is the index of the landmark
  45. * @param invDepthKey is the index of inverse depth
  46. * @param K shared pointer to the constant calibration
  47. */
  48. InvDepthFactor3(const Point2& measured, const SharedNoiseModel& model,
  49. const Key poseKey, Key pointKey, Key invDepthKey, const Cal3_S2::shared_ptr& K) :
  50. Base(model, poseKey, pointKey, invDepthKey), measured_(measured), K_(K) {}
  51. /** Virtual destructor */
  52. ~InvDepthFactor3() override {}
  53. /**
  54. * print
  55. * @param s optional string naming the factor
  56. * @param keyFormatter optional formatter useful for printing Symbols
  57. */
  58. void print(const std::string& s = "InvDepthFactor3",
  59. const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
  60. Base::print(s, keyFormatter);
  61. traits<Point2>::Print(measured_, s + ".z");
  62. }
  63. /// equals
  64. bool equals(const NonlinearFactor& p, double tol = 1e-9) const override {
  65. const This *e = dynamic_cast<const This*>(&p);
  66. return e && Base::equals(p, tol) && traits<Point2>::Equals(this->measured_, e->measured_, tol) && this->K_->equals(*e->K_, tol);
  67. }
  68. /// Evaluate error h(x)-z and optionally derivatives
  69. Vector evaluateError(const POSE& pose, const Vector5& point, const INVDEPTH& invDepth,
  70. boost::optional<Matrix&> H1=boost::none,
  71. boost::optional<Matrix&> H2=boost::none,
  72. boost::optional<Matrix&> H3=boost::none) const override {
  73. try {
  74. InvDepthCamera3<Cal3_S2> camera(pose, K_);
  75. return camera.project(point, invDepth, H1, H2, H3) - measured_;
  76. } catch( CheiralityException& e) {
  77. if (H1) *H1 = Matrix::Zero(2,6);
  78. if (H2) *H2 = Matrix::Zero(2,5);
  79. if (H3) *H2 = Matrix::Zero(2,1);
  80. std::cout << e.what() << ": Landmark "<< DefaultKeyFormatter(this->key2()) <<
  81. " moved behind camera " << DefaultKeyFormatter(this->key1()) << std::endl;
  82. return Vector::Ones(2) * 2.0 * K_->fx();
  83. }
  84. return (Vector(1) << 0.0).finished();
  85. }
  86. /** return the measurement */
  87. const Point2& imagePoint() const {
  88. return measured_;
  89. }
  90. /** return the calibration object */
  91. inline const Cal3_S2::shared_ptr calibration() const {
  92. return K_;
  93. }
  94. private:
  95. /// Serialization function
  96. friend class boost::serialization::access;
  97. template<class ARCHIVE>
  98. void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
  99. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
  100. ar & BOOST_SERIALIZATION_NVP(measured_);
  101. ar & BOOST_SERIALIZATION_NVP(K_);
  102. }
  103. };
  104. } // \ namespace gtsam