2010-10-14 12:54:38 +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
|
|
|
|
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
/**
|
|
|
|
* @file BetweenFactor.h
|
2011-10-21 14:01:50 +08:00
|
|
|
* @author Frank Dellaert, Viorela Ila
|
2010-01-08 23:32:45 +08:00
|
|
|
**/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
|
2011-10-16 02:38:49 +08:00
|
|
|
#include <gtsam/base/Testable.h>
|
|
|
|
#include <gtsam/base/Lie.h>
|
2010-08-20 01:23:19 +08:00
|
|
|
#include <gtsam/nonlinear/NonlinearFactor.h>
|
|
|
|
#include <gtsam/linear/GaussianFactor.h>
|
2010-01-08 23:32:45 +08:00
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class for a measurement predicted by "between(config[key1],config[key2])"
|
2012-02-07 07:32:59 +08:00
|
|
|
* @tparam VALUE the Value type
|
2010-01-08 23:32:45 +08:00
|
|
|
*/
|
2012-02-06 08:44:25 +08:00
|
|
|
template<class VALUE>
|
2012-02-21 05:52:47 +08:00
|
|
|
class BetweenFactor: public NoiseModelFactor2<VALUE, VALUE> {
|
2010-01-08 23:32:45 +08:00
|
|
|
|
2012-02-07 07:32:59 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef VALUE T;
|
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
private:
|
|
|
|
|
2012-02-06 08:44:25 +08:00
|
|
|
typedef BetweenFactor<VALUE> This;
|
2012-02-21 05:52:47 +08:00
|
|
|
typedef NoiseModelFactor2<VALUE, VALUE> Base;
|
Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just says something like
if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
2010-01-14 06:25:03 +08:00
|
|
|
|
2012-02-06 08:44:25 +08:00
|
|
|
VALUE measured_; /** The measurement */
|
2010-01-08 23:32:45 +08:00
|
|
|
|
2011-10-16 02:38:49 +08:00
|
|
|
/** concept check by type */
|
2011-11-10 06:15:40 +08:00
|
|
|
GTSAM_CONCEPT_LIE_TYPE(T)
|
2011-10-16 02:38:49 +08:00
|
|
|
GTSAM_CONCEPT_TESTABLE_TYPE(T)
|
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
public:
|
|
|
|
|
|
|
|
// shorthand for a smart pointer to a factor
|
|
|
|
typedef typename boost::shared_ptr<BetweenFactor> shared_ptr;
|
|
|
|
|
2011-02-24 04:31:19 +08:00
|
|
|
/** default constructor - only use for serialization */
|
|
|
|
BetweenFactor() {}
|
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
/** Constructor */
|
2012-02-19 09:02:07 +08:00
|
|
|
BetweenFactor(Key key1, Key key2, const VALUE& measured,
|
2011-08-27 05:41:01 +08:00
|
|
|
const SharedNoiseModel& model) :
|
2010-01-18 13:38:53 +08:00
|
|
|
Base(model, key1, key2), measured_(measured) {
|
2010-01-08 23:32:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-04 01:16:13 +08:00
|
|
|
virtual ~BetweenFactor() {}
|
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
/** implement functions needed for Testable */
|
|
|
|
|
|
|
|
/** print */
|
2010-10-27 04:21:52 +08:00
|
|
|
virtual void print(const std::string& s) const {
|
2011-11-04 12:28:07 +08:00
|
|
|
std::cout << s << "BetweenFactor("
|
2012-02-07 07:32:59 +08:00
|
|
|
<< (std::string) this->key1() << ","
|
|
|
|
<< (std::string) this->key2() << ")\n";
|
2011-10-23 03:56:53 +08:00
|
|
|
measured_.print(" measured");
|
|
|
|
this->noiseModel_->print(" noise model");
|
2010-01-08 23:32:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** equals */
|
2012-01-29 04:47:43 +08:00
|
|
|
virtual bool equals(const NonlinearFactor& expected, double tol=1e-9) const {
|
2011-02-24 04:31:19 +08:00
|
|
|
const This *e = dynamic_cast<const This*> (&expected);
|
2010-10-27 04:21:52 +08:00
|
|
|
return e != NULL && Base::equals(*e, tol) && this->measured_.equals(e->measured_, tol);
|
2010-01-08 23:32:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** implement functions needed to derive from Factor */
|
|
|
|
|
|
|
|
/** vector of errors */
|
2012-02-07 07:32:59 +08:00
|
|
|
Vector evaluateError(const T& p1, const T& p2,
|
2011-03-17 12:36:27 +08:00
|
|
|
boost::optional<Matrix&> H1 = boost::none, boost::optional<Matrix&> H2 =
|
|
|
|
boost::none) const {
|
2010-08-27 03:55:40 +08:00
|
|
|
T hx = p1.between(p2, H1, H2); // h(x)
|
Large gtsam refactoring
To support faster development *and* better performance Richard and I pushed through a large refactoring of NonlinearFactors.
The following are the biggest changes:
1) NonLinearFactor1 and NonLinearFactor2 are now templated on Config, Key type, and X type, where X is the argument to the measurement function.
2) The measurement itself is no longer kept in the nonlinear factor. Instead, a derived class (see testVSLAMFactor, testNonlinearEquality, testPose3Factor etc...) has to implement a function to compute the errors, "evaluateErrors". Instead of (h(x)-z), it needs to return (z-h(x)), so Ax-b is an approximation of the error. IMPORTANT: evaluateErrors needs - if asked - *combine* the calculation of the function value h(x) and the derivatives dh(x)/dx. This was a major performance issue. To do this, boost::optional<Matrix&> arguments are provided, and tin EvaluateErrors you just says something like
if (H) *H = Matrix_(3,6,....);
3) We are no longer using int or strings for nonlinear factors. Instead, the preferred key type is now Symbol, defined in Key.h. This is both fast and cool: you can construct it from an int, and cast it to a strong. It also does type checking: a Symbol<Pose3,'x'> will not match a Symbol<Pose2,'x'>
4) minor: take a look at LieConfig.h: it help you avoid writing a lot of code bu automatically creating configs for a certain type. See e.g. Pose3Config.h. A "double" LieConfig is on the way - Thanks Richard and Manohar !
2010-01-14 06:25:03 +08:00
|
|
|
// manifold equivalent of h(x)-z -> log(z,h(x))
|
2011-11-06 07:01:43 +08:00
|
|
|
return measured_.localCoordinates(hx);
|
2010-01-13 00:11:24 +08:00
|
|
|
}
|
|
|
|
|
2010-01-13 00:12:25 +08:00
|
|
|
/** return the measured */
|
2012-02-06 08:44:25 +08:00
|
|
|
const VALUE& measured() const {
|
2010-01-17 00:46:57 +08:00
|
|
|
return measured_;
|
|
|
|
}
|
2010-01-13 00:12:25 +08:00
|
|
|
|
2010-01-08 23:32:45 +08:00
|
|
|
/** number of variables attached to this factor */
|
2012-02-06 08:44:25 +08:00
|
|
|
std::size_t size() const {
|
2010-01-17 00:46:57 +08:00
|
|
|
return 2;
|
|
|
|
}
|
2011-02-24 04:31:19 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/** Serialization function */
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template<class ARCHIVE>
|
|
|
|
void serialize(ARCHIVE & ar, const unsigned int version) {
|
2012-02-21 05:52:47 +08:00
|
|
|
ar & boost::serialization::make_nvp("NoiseModelFactor2",
|
2011-02-24 04:31:19 +08:00
|
|
|
boost::serialization::base_object<Base>(*this));
|
|
|
|
ar & BOOST_SERIALIZATION_NVP(measured_);
|
|
|
|
}
|
2011-11-10 06:15:40 +08:00
|
|
|
}; // \class BetweenFactor
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binary between constraint - forces between to a given value
|
|
|
|
* This constraint requires the underlying type to a Lie type
|
|
|
|
*
|
|
|
|
*/
|
2012-02-06 08:44:25 +08:00
|
|
|
template<class VALUE>
|
|
|
|
class BetweenConstraint : public BetweenFactor<VALUE> {
|
2011-11-10 06:15:40 +08:00
|
|
|
public:
|
2012-02-06 08:44:25 +08:00
|
|
|
typedef boost::shared_ptr<BetweenConstraint<VALUE> > shared_ptr;
|
2011-11-10 06:15:40 +08:00
|
|
|
|
|
|
|
/** Syntactic sugar for constrained version */
|
2012-02-19 09:02:07 +08:00
|
|
|
BetweenConstraint(const VALUE& measured, Key key1, Key key2, double mu = 1000.0) :
|
2012-02-07 07:32:59 +08:00
|
|
|
BetweenFactor<VALUE>(key1, key2, measured, noiseModel::Constrained::All(VALUE::Dim(), fabs(mu))) {}
|
2011-11-10 06:15:40 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/** Serialization function */
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
template<class ARCHIVE>
|
|
|
|
void serialize(ARCHIVE & ar, const unsigned int version) {
|
|
|
|
ar & boost::serialization::make_nvp("BetweenFactor",
|
2012-02-06 08:44:25 +08:00
|
|
|
boost::serialization::base_object<BetweenFactor<VALUE> >(*this));
|
2011-11-10 06:15:40 +08:00
|
|
|
}
|
|
|
|
}; // \class BetweenConstraint
|
2010-01-08 23:32:45 +08:00
|
|
|
|
|
|
|
} /// namespace gtsam
|