29 #include <boost/serialization/base_object.hpp> 30 #include <boost/assign/list_of.hpp> 36 #define ADD_CLONE_NONLINEAR_FACTOR(Derived) \ 37 virtual gtsam::NonlinearFactor::shared_ptr clone() const { \ 38 return boost::static_pointer_cast<gtsam::NonlinearFactor>( \ 39 gtsam::NonlinearFactor::shared_ptr(new Derived(*this))); } 43 using boost::assign::cref_list_of;
62 typedef boost::shared_ptr<This> shared_ptr;
73 template<
typename CONTAINER>
82 virtual void print(
const std::string& s =
"",
83 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const;
100 virtual double error(
const Values& c)
const = 0;
103 virtual size_t dim()
const = 0;
118 virtual boost::shared_ptr<GaussianFactor>
119 linearize(
const Values& c)
const = 0;
129 throw std::runtime_error(
"NonlinearFactor::clone(): Attempting to clone factor with no clone() implemented!");
138 shared_ptr rekey(
const std::map<Key,Key>& rekey_mapping)
const;
144 shared_ptr rekey(
const std::vector<Key>& new_keys)
const;
168 typedef NonlinearFactor
Base;
186 template<
typename CONTAINER>
188 Base(keys), noiseModel_(noiseModel) {}
200 virtual void print(
const std::string& s =
"",
201 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const;
204 virtual bool equals(
const NonlinearFactor& f,
double tol = 1e-9)
const;
207 virtual size_t dim()
const {
208 return noiseModel_->dim();
227 virtual Vector unwhitenedError(
const Values& x,
228 boost::optional<std::vector<Matrix>&> H = boost::none)
const = 0;
234 Vector whitenedError(
const Values& c)
const;
242 virtual double error(
const Values& c)
const;
249 boost::shared_ptr<GaussianFactor> linearize(
const Values& x)
const;
254 friend class boost::serialization::access;
255 template<
class ARCHIVE>
256 void serialize(ARCHIVE & ar,
const unsigned int ) {
257 ar & boost::serialization::make_nvp(
"NonlinearFactor",
258 boost::serialization::base_object<Base>(*
this));
259 ar & BOOST_SERIALIZATION_NVP(noiseModel_);
275 template<
class VALUE>
295 inline Key key()
const {
return keys_[0]; }
303 Base(noiseModel, cref_list_of<1>(key1)) {}
309 if(this->active(x)) {
310 const X& x1 = x.
at<X>(keys_[0]);
312 return evaluateError(x1, (*H)[0]);
314 return evaluateError(x1);
317 return Vector::Zero(this->dim());
326 virtual Vector evaluateError(
const X& x, boost::optional<Matrix&> H =
327 boost::none)
const = 0;
332 friend class boost::serialization::access;
333 template<
class ARCHIVE>
334 void serialize(ARCHIVE & ar,
const unsigned int ) {
335 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
336 boost::serialization::base_object<Base>(*
this));
344 template<
class VALUE1,
class VALUE2>
372 Base(noiseModel, cref_list_of<2>(j1)(j2)) {}
378 inline Key key2()
const {
return keys_[1]; }
383 if(this->active(x)) {
384 const X1& x1 = x.
at<X1>(keys_[0]);
385 const X2& x2 = x.
at<X2>(keys_[1]);
387 return evaluateError(x1, x2, (*H)[0], (*H)[1]);
389 return evaluateError(x1, x2);
392 return Vector::Zero(this->dim());
402 evaluateError(
const X1&,
const X2&, boost::optional<Matrix&> H1 =
403 boost::none, boost::optional<Matrix&> H2 = boost::none)
const = 0;
408 friend class boost::serialization::access;
409 template<
class ARCHIVE>
410 void serialize(ARCHIVE & ar,
const unsigned int ) {
411 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
412 boost::serialization::base_object<Base>(*
this));
419 template<
class VALUE1,
class VALUE2,
class VALUE3>
449 Base(noiseModel, cref_list_of<3>(j1)(j2)(j3)) {}
455 inline Key key2()
const {
return keys_[1]; }
456 inline Key key3()
const {
return keys_[2]; }
461 if(this->active(x)) {
463 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), (*H)[0], (*H)[1], (*H)[2]);
465 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]));
467 return Vector::Zero(this->dim());
477 evaluateError(
const X1&,
const X2&,
const X3&,
478 boost::optional<Matrix&> H1 = boost::none,
479 boost::optional<Matrix&> H2 = boost::none,
480 boost::optional<Matrix&> H3 = boost::none)
const = 0;
485 friend class boost::serialization::access;
486 template<
class ARCHIVE>
487 void serialize(ARCHIVE & ar,
const unsigned int ) {
488 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
489 boost::serialization::base_object<Base>(*
this));
496 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4>
528 Base(noiseModel, cref_list_of<4>(j1)(j2)(j3)(j4)) {}
534 inline Key key2()
const {
return keys_[1]; }
535 inline Key key3()
const {
return keys_[2]; }
536 inline Key key4()
const {
return keys_[3]; }
541 if(this->active(x)) {
543 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]), (*H)[0], (*H)[1], (*H)[2], (*H)[3]);
545 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]));
547 return Vector::Zero(this->dim());
557 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
558 boost::optional<Matrix&> H1 = boost::none,
559 boost::optional<Matrix&> H2 = boost::none,
560 boost::optional<Matrix&> H3 = boost::none,
561 boost::optional<Matrix&> H4 = boost::none)
const = 0;
566 friend class boost::serialization::access;
567 template<
class ARCHIVE>
568 void serialize(ARCHIVE & ar,
const unsigned int ) {
569 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
570 boost::serialization::base_object<Base>(*
this));
577 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5>
611 Base(noiseModel, cref_list_of<5>(j1)(j2)(j3)(j4)(j5)) {}
617 inline Key key2()
const {
return keys_[1]; }
618 inline Key key3()
const {
return keys_[2]; }
619 inline Key key4()
const {
return keys_[3]; }
620 inline Key key5()
const {
return keys_[4]; }
625 if(this->active(x)) {
627 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]), x.
at<X5>(keys_[4]), (*H)[0], (*H)[1], (*H)[2], (*H)[3], (*H)[4]);
629 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]), x.
at<X5>(keys_[4]));
631 return Vector::Zero(this->dim());
641 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
const X5&,
642 boost::optional<Matrix&> H1 = boost::none,
643 boost::optional<Matrix&> H2 = boost::none,
644 boost::optional<Matrix&> H3 = boost::none,
645 boost::optional<Matrix&> H4 = boost::none,
646 boost::optional<Matrix&> H5 = boost::none)
const = 0;
651 friend class boost::serialization::access;
652 template<
class ARCHIVE>
653 void serialize(ARCHIVE & ar,
const unsigned int ) {
654 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
655 boost::serialization::base_object<Base>(*
this));
662 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5,
class VALUE6>
698 Base(noiseModel, cref_list_of<6>(j1)(j2)(j3)(j4)(j5)(j6)) {}
704 inline Key key2()
const {
return keys_[1]; }
705 inline Key key3()
const {
return keys_[2]; }
706 inline Key key4()
const {
return keys_[3]; }
707 inline Key key5()
const {
return keys_[4]; }
708 inline Key key6()
const {
return keys_[5]; }
713 if(this->active(x)) {
715 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]), x.
at<X5>(keys_[4]), x.
at<X6>(keys_[5]), (*H)[0], (*H)[1], (*H)[2], (*H)[3], (*H)[4], (*H)[5]);
717 return evaluateError(x.
at<X1>(keys_[0]), x.
at<X2>(keys_[1]), x.
at<X3>(keys_[2]), x.
at<X4>(keys_[3]), x.
at<X5>(keys_[4]), x.
at<X6>(keys_[5]));
719 return Vector::Zero(this->dim());
729 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
const X5&,
const X6&,
730 boost::optional<Matrix&> H1 = boost::none,
731 boost::optional<Matrix&> H2 = boost::none,
732 boost::optional<Matrix&> H3 = boost::none,
733 boost::optional<Matrix&> H4 = boost::none,
734 boost::optional<Matrix&> H5 = boost::none,
735 boost::optional<Matrix&> H6 = boost::none)
const = 0;
740 friend class boost::serialization::access;
741 template<
class ARCHIVE>
742 void serialize(ARCHIVE & ar,
const unsigned int ) {
743 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
744 boost::serialization::base_object<Base>(*
this));
NoiseModelFactor2()
Default Constructor for I/O.
Definition: NonlinearFactor.h:363
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 2-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:382
Nonlinear factor base class.
Definition: NonlinearFactor.h:52
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:163
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:616
A non-templated config holding any types of Manifold-group elements.
This is the base class for all factor types.
Definition: Factor.h:51
virtual size_t dim() const
get the dimension of the factor (number of rows on linearization)
Definition: NonlinearFactor.h:207
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
A convenient base class for creating your own NoiseModelFactor with 3 variables.
Definition: NonlinearFactor.h:420
NonlinearFactor(const CONTAINER &keys)
Constructor from a collection of the keys involved in this factor.
Definition: NonlinearFactor.h:74
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:1072
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 5-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:624
NoiseModelFactor1(const SharedNoiseModel &noiseModel, Key key1)
Constructor.
Definition: NonlinearFactor.h:302
NoiseModelFactor3()
Default Constructor for I/O.
Definition: NonlinearFactor.h:439
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 3-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:460
A convenient base class for creating your own NoiseModelFactor with 6 variables.
Definition: NonlinearFactor.h:663
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:454
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 1-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:308
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 6-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:712
The base class for all factors.
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:70
NoiseModelFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:178
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:212
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:533
Template to create a binary predicate.
Definition: Testable.h:110
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:115
NoiseModelFactor4(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4)
Constructor.
Definition: NonlinearFactor.h:527
NoiseModelFactor(const SharedNoiseModel &noiseModel)
Constructor - only for subclasses, as this does not set keys.
Definition: NonlinearFactor.h:195
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:345
NoiseModelFactor6(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5, Key j6)
Constructor.
Definition: NonlinearFactor.h:697
NoiseModelFactor(const SharedNoiseModel &noiseModel, const CONTAINER &keys)
Constructor.
Definition: NonlinearFactor.h:187
ValueType at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:343
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:377
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
A convenient base class for creating your own NoiseModelFactor with 5 variables.
Definition: NonlinearFactor.h:578
NoiseModelFactor1()
Default constructor for I/O only.
Definition: NonlinearFactor.h:291
virtual shared_ptr clone() const
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition: NonlinearFactor.h:127
virtual ~NonlinearFactor()
Destructor.
Definition: NonlinearFactor.h:92
NonlinearFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:68
NoiseModelFactor5()
Default Constructor for I/O.
Definition: NonlinearFactor.h:599
NoiseModelFactor2(const SharedNoiseModel &noiseModel, Key j1, Key j2)
Constructor.
Definition: NonlinearFactor.h:371
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:276
virtual ~NoiseModelFactor()
Destructor.
Definition: NonlinearFactor.h:181
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 4-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:540
Special class for optional Jacobian arguments.
NoiseModelFactor3(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3)
Constructor.
Definition: NonlinearFactor.h:448
boost::shared_ptr< This > shared_ptr
Noise model.
Definition: NonlinearFactor.h:175
SharedNoiseModel get_noiseModel() const
Definition: NonlinearFactor.h:217
NoiseModelFactor5(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5)
Constructor.
Definition: NonlinearFactor.h:610
NoiseModelFactor6()
Default Constructor for I/O.
Definition: NonlinearFactor.h:685
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
A convenient base class for creating your own NoiseModelFactor with 4 variables.
Definition: NonlinearFactor.h:497
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:703
NoiseModelFactor4()
Default Constructor for I/O.
Definition: NonlinearFactor.h:517
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33