2011-06-14 04:01:58 +08:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
|
|
|
|
|
* Atlanta, Georgia 30332-0415
|
|
|
|
|
* All Rights Reserved
|
|
|
|
|
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
|
|
|
|
|
|
|
|
|
|
* See LICENSE for the license information
|
|
|
|
|
|
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-09-08 06:05:13 +08:00
|
|
|
/**
|
|
|
|
|
* @file GeneralSFMFactor.h
|
2010-12-16 07:53:10 +08:00
|
|
|
*
|
2011-09-08 06:05:13 +08:00
|
|
|
* @brief a general SFM factor with an unknown calibration
|
|
|
|
|
*
|
|
|
|
|
* @date Dec 15, 2010
|
|
|
|
|
* @author Kai Ni
|
2010-12-16 07:53:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2012-01-28 22:59:58 +08:00
|
|
|
#include <gtsam/geometry/Point2.h>
|
2010-12-29 06:59:24 +08:00
|
|
|
#include <gtsam/nonlinear/NonlinearFactor.h>
|
|
|
|
|
|
2010-12-16 07:53:10 +08:00
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Non-linear factor for a constraint derived from a 2D measurement. The calibration is unknown here compared to GenericProjectionFactor
|
|
|
|
|
*/
|
2012-02-06 08:44:25 +08:00
|
|
|
template <class CAMERA, class LANDMARK>
|
2010-12-16 07:53:10 +08:00
|
|
|
class GeneralSFMFactor:
|
2012-02-21 05:52:47 +08:00
|
|
|
public NoiseModelFactor2<CAMERA, LANDMARK> {
|
2010-12-16 07:53:10 +08:00
|
|
|
protected:
|
2012-02-06 08:44:25 +08:00
|
|
|
Point2 measured_; ///< the 2D measurement
|
2010-12-16 07:53:10 +08:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2012-02-25 05:09:20 +08:00
|
|
|
typedef CAMERA Cam; ///< typedef for camera type
|
2012-02-07 07:32:59 +08:00
|
|
|
typedef GeneralSFMFactor<CAMERA, LANDMARK> This; ///< typedef for this object
|
2012-02-21 05:52:47 +08:00
|
|
|
typedef NoiseModelFactor2<CAMERA, LANDMARK> Base; ///< typedef for the base class
|
2012-02-25 05:09:20 +08:00
|
|
|
typedef Point2 Measurement; ///< typedef for the measurement
|
2010-12-16 07:53:10 +08:00
|
|
|
|
|
|
|
|
// shorthand for a smart pointer to a factor
|
2012-02-06 08:44:25 +08:00
|
|
|
typedef boost::shared_ptr<This> shared_ptr;
|
2010-12-16 07:53:10 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* @param z is the 2 dimensional location of point in image (the measurement)
|
2011-09-08 06:05:13 +08:00
|
|
|
* @param model is the standard deviation of the measurements
|
|
|
|
|
* @param i is basically the frame number
|
|
|
|
|
* @param j is the index of the landmark
|
2010-12-16 07:53:10 +08:00
|
|
|
*/
|
2012-02-19 09:02:07 +08:00
|
|
|
GeneralSFMFactor(const Point2& measured, const SharedNoiseModel& model, Key cameraKey, Key landmarkKey) :
|
2012-02-07 12:02:20 +08:00
|
|
|
Base(model, cameraKey, landmarkKey), measured_(measured) {}
|
2010-12-16 07:53:10 +08:00
|
|
|
|
2012-02-06 08:44:25 +08:00
|
|
|
GeneralSFMFactor():measured_(0.0,0.0) {} ///< default constructor
|
|
|
|
|
GeneralSFMFactor(const Point2 & p):measured_(p) {} ///< constructor that takes a Point2
|
|
|
|
|
GeneralSFMFactor(double x, double y):measured_(x,y) {} ///< constructor that takes doubles x,y to make a Point2
|
2011-09-08 06:05:13 +08:00
|
|
|
|
|
|
|
|
virtual ~GeneralSFMFactor() {} ///< destructor
|
2011-03-04 01:16:13 +08:00
|
|
|
|
2012-05-22 04:54:40 +08:00
|
|
|
ADD_CLONE_NONLINEAR_FACTOR(This)
|
|
|
|
|
|
2010-12-16 07:53:10 +08:00
|
|
|
/**
|
|
|
|
|
* print
|
|
|
|
|
* @param s optional string naming the factor
|
|
|
|
|
*/
|
2012-02-21 08:53:35 +08:00
|
|
|
void print(const std::string& s = "SFMFactor", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
|
|
|
|
|
Base::print(s, keyFormatter);
|
2012-02-07 07:32:59 +08:00
|
|
|
measured_.print(s + ".z");
|
2010-12-16 07:53:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* equals
|
|
|
|
|
*/
|
2012-02-07 07:32:59 +08:00
|
|
|
bool equals(const NonlinearFactor &p, double tol = 1e-9) const {
|
|
|
|
|
const This* e = dynamic_cast<const This*>(&p);
|
|
|
|
|
return e && Base::equals(p, tol) && this->measured_.equals(e->measured_, tol) ;
|
2010-12-16 07:53:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** h(x)-z */
|
2012-02-18 07:33:29 +08:00
|
|
|
Vector evaluateError(const Cam& camera, const Point3& point,
|
|
|
|
|
boost::optional<Matrix&> H1=boost::none, boost::optional<Matrix&> H2=boost::none) const {
|
|
|
|
|
|
|
|
|
|
try {
|
2012-02-25 05:09:24 +08:00
|
|
|
Point2 reprojError(camera.project2(point,H1,H2) - measured_);
|
2012-02-18 07:33:29 +08:00
|
|
|
return reprojError.vector();
|
|
|
|
|
}
|
|
|
|
|
catch( CheiralityException& e) {
|
|
|
|
|
if (H1) *H1 = zeros(2, camera.dim());
|
|
|
|
|
if (H2) *H2 = zeros(2, point.dim());
|
|
|
|
|
// cout << e.what() << ": Landmark "<< this->key2_.index()
|
|
|
|
|
// << " behind Camera " << this->key1_.index() << endl;
|
|
|
|
|
return zero(2);
|
|
|
|
|
}
|
2010-12-16 07:53:10 +08:00
|
|
|
}
|
|
|
|
|
|
2010-12-16 16:11:44 +08:00
|
|
|
/** return the measured */
|
|
|
|
|
inline const Point2 measured() const {
|
2012-02-06 08:44:25 +08:00
|
|
|
return measured_;
|
2010-12-16 16:11:44 +08:00
|
|
|
}
|
|
|
|
|
|
2010-12-16 07:53:10 +08:00
|
|
|
private:
|
|
|
|
|
/** Serialization function */
|
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
|
template<class Archive>
|
|
|
|
|
void serialize(Archive & ar, const unsigned int version) {
|
2012-02-06 08:44:25 +08:00
|
|
|
ar & BOOST_SERIALIZATION_NVP(measured_);
|
2010-12-16 07:53:10 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} //namespace
|