gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SmartProjectionPoseFactor.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #pragma once
21 
22 #include "SmartProjectionFactor.h"
23 
24 namespace gtsam {
40 template<class POSE, class LANDMARK, class CALIBRATION>
41 class SmartProjectionPoseFactor: public SmartProjectionFactor<POSE, LANDMARK, CALIBRATION, 6> {
42 protected:
43 
44  LinearizationMode linearizeTo_;
45 
46  std::vector<boost::shared_ptr<CALIBRATION> > K_all_;
47 
48 public:
49 
52 
55 
57  typedef boost::shared_ptr<This> shared_ptr;
58 
68  SmartProjectionPoseFactor(const double rankTol = 1,
69  const double linThreshold = -1, const bool manageDegeneracy = false,
70  const bool enableEPI = false, boost::optional<POSE> body_P_sensor = boost::none,
71  LinearizationMode linearizeTo = HESSIAN, double landmarkDistanceThreshold = 1e10,
72  double dynamicOutlierRejectionThreshold = -1) :
73  Base(rankTol, linThreshold, manageDegeneracy, enableEPI, body_P_sensor,
74  landmarkDistanceThreshold, dynamicOutlierRejectionThreshold), linearizeTo_(linearizeTo) {}
75 
78 
86  void add(const Point2 measured_i, const Key poseKey_i,
87  const SharedNoiseModel noise_i,
88  const boost::shared_ptr<CALIBRATION> K_i) {
89  Base::add(measured_i, poseKey_i, noise_i);
90  K_all_.push_back(K_i);
91  }
92 
100  void add(std::vector<Point2> measurements, std::vector<Key> poseKeys,
101  std::vector<SharedNoiseModel> noises,
102  std::vector<boost::shared_ptr<CALIBRATION> > Ks) {
103  Base::add(measurements, poseKeys, noises);
104  for (size_t i = 0; i < measurements.size(); i++) {
105  K_all_.push_back(Ks.at(i));
106  }
107  }
108 
116  void add(std::vector<Point2> measurements, std::vector<Key> poseKeys,
117  const SharedNoiseModel noise, const boost::shared_ptr<CALIBRATION> K) {
118  for (size_t i = 0; i < measurements.size(); i++) {
119  Base::add(measurements.at(i), poseKeys.at(i), noise);
120  K_all_.push_back(K);
121  }
122  }
123 
129  void print(const std::string& s = "", const KeyFormatter& keyFormatter =
130  DefaultKeyFormatter) const {
131  std::cout << s << "SmartProjectionPoseFactor, z = \n ";
132  BOOST_FOREACH(const boost::shared_ptr<CALIBRATION>& K, K_all_)
133  K->print("calibration = ");
134  Base::print("", keyFormatter);
135  }
136 
138  virtual bool equals(const NonlinearFactor& p, double tol = 1e-9) const {
139  const This *e = dynamic_cast<const This*>(&p);
140 
141  return e && Base::equals(p, tol);
142  }
143 
145  virtual size_t dim() const {
146  return 6 * this->keys_.size();
147  }
148 
155  typename Base::Cameras cameras(const Values& values) const {
156  typename Base::Cameras cameras;
157  size_t i=0;
158  BOOST_FOREACH(const Key& k, this->keys_) {
159  Pose3 pose = values.at<Pose3>(k);
160  typename Base::Camera camera(pose, *K_all_[i++]);
161  cameras.push_back(camera);
162  }
163  return cameras;
164  }
165 
171  virtual boost::shared_ptr<GaussianFactor> linearize(
172  const Values& values) const {
173  // depending on flag set on construction we may linearize to different linear factors
174  switch(linearizeTo_){
175  case JACOBIAN_SVD :
176  return this->createJacobianSVDFactor(cameras(values), 0.0);
177  break;
178  case JACOBIAN_Q :
179  return this->createJacobianQFactor(cameras(values), 0.0);
180  break;
181  default:
182  return this->createHessianFactor(cameras(values));
183  break;
184  }
185  }
186 
190  virtual double error(const Values& values) const {
191  if (this->active(values)) {
192  return this->totalReprojectionError(cameras(values));
193  } else { // else of active flag
194  return 0.0;
195  }
196  }
197 
199  inline const std::vector<boost::shared_ptr<CALIBRATION> > calibration() const {
200  return K_all_;
201  }
202 
203 private:
204 
207  template<class ARCHIVE>
208  void serialize(ARCHIVE & ar, const unsigned int version) {
209  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
210  ar & BOOST_SERIALIZATION_NVP(K_all_);
211  }
212 
213 }; // end of class declaration
214 
215 } // \ namespace gtsam
virtual bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: SmartFactorBase.h:166
const std::vector< SharedNoiseModel > & noise() const
return the noise model
Definition: SmartFactorBase.h:144
boost::shared_ptr< RegularHessianFactor< D > > createHessianFactor(const Cameras &cameras, const double lambda=0.0) const
linearize returns a Hessianfactor that is an approximation of error(p)
Definition: SmartProjectionFactor.h:316
SmartProjectionFactor: triangulates point TODO: why LANDMARK parameter?
Definition: SmartProjectionFactor.h:65
PinholeCamera< CALIBRATION > Camera
shorthand for a pinhole camera
Definition: SmartProjectionFactor.h:113
virtual ~SmartProjectionPoseFactor()
Virtual destructor.
Definition: SmartProjectionPoseFactor.h:77
virtual double error(const Values &values) const
error calculates the error of the factor.
Definition: SmartProjectionPoseFactor.h:190
LinearizationMode linearizeTo_
How to linearize the factor (HESSIAN, JACOBIAN_SVD, JACOBIAN_Q)
Definition: SmartProjectionPoseFactor.h:44
void add(const Point2 measured_i, const Key poseKey_i, const SharedNoiseModel noise_i, const boost::shared_ptr< CALIBRATION > K_i)
add a new measurement and pose key
Definition: SmartProjectionPoseFactor.h:86
virtual bool equals(const NonlinearFactor &p, double tol=1e-9) const
equals
Definition: SmartProjectionPoseFactor.h:138
void add(std::vector< Point2 > measurements, std::vector< Key > poseKeys, const SharedNoiseModel noise, const boost::shared_ptr< CALIBRATION > K)
Variant of the previous one in which we include a set of measurements with the same noise and calibra...
Definition: SmartProjectionPoseFactor.h:116
Definition: Point2.h:35
FastVector< Key > keys_
The keys involved in this factor.
Definition: Factor.h:69
Definition: Pose3.h:42
This is the base class for all factor types.
Definition: Factor.h:51
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &values) const
Linearize to Gaussian Factor.
Definition: SmartProjectionPoseFactor.h:171
void add(std::vector< Point2 > measurements, std::vector< Key > poseKeys, std::vector< SharedNoiseModel > noises, std::vector< boost::shared_ptr< CALIBRATION > > Ks)
Variant of the previous one in which we include a set of measurements.
Definition: SmartProjectionPoseFactor.h:100
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
friend class boost::serialization::access
Serialization function.
Definition: SmartProjectionPoseFactor.h:206
double totalReprojectionError(const Cameras &cameras, boost::optional< Point3 > externalPoint=boost::none) const
Calculate the error of the factor.
Definition: SmartProjectionFactor.h:613
const ValueType & at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:219
Definition: SmartProjectionPoseFactor.h:41
const std::vector< boost::shared_ptr< CALIBRATION > > calibration() const
return the calibration object
Definition: SmartProjectionPoseFactor.h:199
SmartProjectionFactor< POSE, LANDMARK, CALIBRATION, 6 > Base
shorthand for base class type
Definition: SmartProjectionPoseFactor.h:51
Base class to create smart factors on poses or cameras.
size_t Key
Integer nonlinear key type.
Definition: types.h:59
virtual size_t dim() const
get the dimension of the factor
Definition: SmartProjectionPoseFactor.h:145
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: SmartProjectionPoseFactor.h:129
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:125
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: SmartProjectionFactor.h:148
boost::shared_ptr< This > shared_ptr
shorthand for a smart pointer to a factor
Definition: SmartProjectionPoseFactor.h:57
Base::Cameras cameras(const Values &values) const
Collect all cameras involved in this factor.
Definition: SmartProjectionPoseFactor.h:155
void add(const Point2 &measured_i, const Key &poseKey_i, const SharedNoiseModel &noise_i)
add a new measurement and pose key
Definition: SmartFactorBase.h:92
SmartProjectionPoseFactor< POSE, LANDMARK, CALIBRATION > This
shorthand for this class
Definition: SmartProjectionPoseFactor.h:54
std::vector< boost::shared_ptr< CALIBRATION > > K_all_
shared pointer to calibration object (one for each camera)
Definition: SmartProjectionPoseFactor.h:46
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:884
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
boost::shared_ptr< JacobianFactorQ< D > > createJacobianQFactor(const Cameras &cameras, double lambda) const
create factor
Definition: SmartProjectionFactor.h:421
boost::shared_ptr< JacobianFactor > createJacobianSVDFactor(const Cameras &cameras, double lambda) const
different (faster) way to compute Jacobian factor
Definition: SmartProjectionFactor.h:442
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
SmartProjectionPoseFactor(const double rankTol=1, const double linThreshold=-1, const bool manageDegeneracy=false, const bool enableEPI=false, boost::optional< POSE > body_P_sensor=boost::none, LinearizationMode linearizeTo=HESSIAN, double landmarkDistanceThreshold=1e10, double dynamicOutlierRejectionThreshold=-1)
Constructor.
Definition: SmartProjectionPoseFactor.h:68