gtsam
Loading...
Searching...
No Matches
VectorNormFactor.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
17
18#pragma once
19
21
22namespace gtsam {
23
53template <int N>
54class VectorNormFactor : public NoiseModelFactorN<Eigen::Matrix<double, N, 1>> {
55 static_assert(N > 0, "VectorNormFactor requires a fixed positive dimension");
56
57 public:
58 typedef Eigen::Matrix<double, N, 1> VectorN;
59
60 private:
61 typedef VectorNormFactor<N> This;
62 typedef NoiseModelFactorN<VectorN> Base;
63
64 double norm_;
65
66 public:
69 static constexpr double kMinNorm = 1e-10;
70
71 // Provide access to the Matrix& version of evaluateError:
73
75 typedef std::shared_ptr<This> shared_ptr;
76
78 VectorNormFactor() : norm_(0.0) {}
79
87 : Base(model, key), norm_(norm) {}
88
89 ~VectorNormFactor() override {}
90
92 NonlinearFactor::shared_ptr clone() const override {
93 return std::make_shared<This>(*this);
94 }
95
98 void print(const std::string& s = "",
99 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
100 std::cout << (s.empty() ? s : s + " ") << "VectorNormFactor on "
101 << keyFormatter(this->template key<1>()) << ", norm = " << norm_
102 << std::endl;
103 this->noiseModel_->print(" noise model: ");
104 }
105
106 bool equals(const NonlinearFactor& expected, double tol = 1e-9) const override {
107 const This* e = dynamic_cast<const This*>(&expected);
108 return e != nullptr && Base::equals(*e, tol) &&
109 std::abs(norm_ - e->norm_) < tol;
110 }
111
112
114 double norm() const { return norm_; }
115
117 Vector evaluateError(const VectorN& v, OptionalMatrixType H) const override {
118 const double vnorm = v.norm();
119 if (H) {
120 if (vnorm > kMinNorm) {
121 *H = v.transpose() / vnorm;
122 } else {
123 // The norm is not differentiable at the origin. Return a zero
124 // Jacobian: the factor exerts no pull on a (near-)zero vector.
125 H->setZero(1, N);
126 }
127 }
128 return Vector1(vnorm - norm_);
129 }
130
131 private:
132#if GTSAM_ENABLE_BOOST_SERIALIZATION
134 friend class boost::serialization::access;
135 template <class ARCHIVE>
136 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
137 ar& boost::serialization::make_nvp(
138 "NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
139 ar& BOOST_SERIALIZATION_NVP(norm_);
140 }
141#endif
142};
143
145template <int N>
146struct traits<VectorNormFactor<N>> : public Testable<VectorNormFactor<N>> {};
147
148} // namespace gtsam
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
NoiseModelFactorT< Vector, ValueTypes... > NoiseModelFactorN
Noise model factor with N value types and dynamic-sized error vector.
Definition NoiseModelFactorN.h:561
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
virtual Vector evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
static constexpr auto N
Definition NoiseModelFactorN.h:158
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
Unary factor constraining the norm of an N-dimensional vector-space variable to a given value,...
Definition VectorNormFactor.h:54
Vector evaluateError(const VectorN &v, OptionalMatrixType H) const override
vector of errors: ||v|| - norm
Definition VectorNormFactor.h:117
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition VectorNormFactor.h:98
VectorNormFactor(Key key, double norm, const SharedNoiseModel &model)
Constructor.
Definition VectorNormFactor.h:86
NonlinearFactor::shared_ptr clone() const override
Definition VectorNormFactor.h:92
VectorNormFactor()
Default constructor - only use for serialization.
Definition VectorNormFactor.h:78
double norm() const
The desired norm.
Definition VectorNormFactor.h:114
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Check if two factors are equal.
Definition VectorNormFactor.h:106
static constexpr double kMinNorm
Below this norm the error is treated as non-differentiable and the Jacobian is zero (see class warnin...
Definition VectorNormFactor.h:69
std::shared_ptr< This > shared_ptr
Shorthand for a smart pointer to a factor.
Definition VectorNormFactor.h:75