gtsam/gtsam/slam/GeneralSFMFactor.h

115 lines
3.5 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------------
* 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
*
2011-09-08 06:05:13 +08:00
* @brief a general SFM factor with an unknown calibration
*
* @date Dec 15, 2010
* @author Kai Ni
*/
#pragma once
#include <gtsam/geometry/Point2.h>
2010-12-29 06:59:24 +08:00
#include <gtsam/nonlinear/NonlinearFactor.h>
namespace gtsam {
/**
* Non-linear factor for a constraint derived from a 2D measurement. The calibration is unknown here compared to GenericProjectionFactor
*/
template <class CAMERA, class LANDMARK>
class GeneralSFMFactor:
public NoiseModelFactor2<CAMERA, LANDMARK> {
protected:
Point2 measured_; ///< the 2D measurement
public:
typedef CAMERA Cam; ///< typedef for camera type
typedef GeneralSFMFactor<CAMERA, LANDMARK> This; ///< typedef for this object
typedef NoiseModelFactor2<CAMERA, LANDMARK> Base; ///< typedef for the base class
typedef Point2 Measurement; ///< typedef for the measurement
// shorthand for a smart pointer to a factor
typedef boost::shared_ptr<This> shared_ptr;
/**
* 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
*/
GeneralSFMFactor(const Point2& measured, const SharedNoiseModel& model, Key cameraKey, Key landmarkKey) :
Base(model, cameraKey, landmarkKey), measured_(measured) {}
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
ADD_CLONE_NONLINEAR_FACTOR(This)
/**
* print
* @param s optional string naming the factor
*/
void print(const std::string& s = "SFMFactor", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
Base::print(s, keyFormatter);
measured_.print(s + ".z");
}
/**
* equals
*/
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) ;
}
/** 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 16:11:44 +08:00
/** return the measured */
inline const Point2 measured() const {
return measured_;
2010-12-16 16:11:44 +08:00
}
private:
/** Serialization function */
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(measured_);
}
};
} //namespace