gtsam/gtsam_unstable/nonlinear/BADFactor.h

77 lines
2.0 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
* -------------------------------------------------------------------------- */
/**
* @file Expression.h
* @date September 18, 2014
* @author Frank Dellaert
* @author Paul Furgale
* @brief Expressions for Block Automatic Differentiation
*/
2014-09-30 18:34:03 +08:00
#include <gtsam_unstable/nonlinear/Expression.h>
#include <gtsam/nonlinear/NonlinearFactor.h>
#include <gtsam/base/Testable.h>
namespace gtsam {
/**
* BAD Factor that supports arbitrary expressions via AD
*/
template<class T>
class BADFactor: public NoiseModelFactor {
const T measurement_;
const Expression<T> expression_;
public:
/// Constructor
BADFactor(const SharedNoiseModel& noiseModel, //
const T& measurement, const Expression<T>& expression) :
NoiseModelFactor(noiseModel, expression.keys()), //
measurement_(measurement), expression_(expression) {
}
/// get the dimension of the factor (number of rows on linearization)
size_t dim() const {
2014-10-01 16:45:57 +08:00
return measurement_.dim();
}
/**
* Error function *without* the NoiseModel, \f$ z-h(x) \f$.
* We override this method to provide
* both the function evaluation and its derivative(s) in H.
*/
virtual Vector unwhitenedError(const Values& x,
boost::optional<std::vector<Matrix>&> H = boost::none) const {
if (H) {
typedef std::map<Key, Matrix> MapType;
MapType terms;
const T& value = expression_.value(x, terms);
// copy terms to H
H->clear();
H->reserve(terms.size());
for (MapType::iterator it = terms.begin(); it != terms.end(); ++it)
H->push_back(it->second);
return measurement_.localCoordinates(value);
} else {
const T& value = expression_.value(x);
return measurement_.localCoordinates(value);
}
}
};
// BADFactor
}