gtsam
Loading...
Searching...
No Matches
KnownLandmarkFactor.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010-2026, 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
19
20#pragma once
21
22#include <gtsam/constrained/QcqpProblem.h>
23#include <gtsam/constrained/QpCost.h>
27
28#include <memory>
29#include <stdexcept>
30#include <type_traits>
31#include <vector>
32
33namespace gtsam {
34
49template <class T>
51 public:
52 static_assert(std::is_same_v<T, Pose2> || std::is_same_v<T, Pose3>,
53 "KnownLandmarkFactor supports only Pose2 and Pose3");
54
55 using Base = NoiseModelFactorN<T>;
56 using This = KnownLandmarkFactor<T>;
57 using Point = typename T::Translation;
58
59 private:
60 Point wL_;
61 Point measured_kP_;
62
63 public:
65
67 KnownLandmarkFactor(Key key, const Point& wL, const Point& measured_kP,
68 const SharedNoiseModel& model)
69 : Base(model, key), wL_(wL), measured_kP_(measured_kP) {}
70
71 NonlinearFactor::shared_ptr clone() const override {
72 return std::make_shared<This>(*this);
73 }
74
75 bool equals(const NonlinearFactor& expected,
76 double tol = 1e-9) const override {
77 const auto* e = dynamic_cast<const This*>(&expected);
78 return e != nullptr && Base::equals(*e, tol) &&
79 traits<Point>::Equals(wL_, e->wL_, tol) &&
80 traits<Point>::Equals(measured_kP_, e->measured_kP_, tol);
81 }
82
84 Vector evaluateError(const T& wTk, OptionalMatrixType H) const override {
85 const Point predicted_kP = wTk.transformTo(wL_, H);
86 return predicted_kP - measured_kP_;
87 }
88};
89
103template <class T>
105 public:
106 static_assert(std::is_same_v<T, Pose2> || std::is_same_v<T, Pose3>,
107 "KnownLandmarkFactor2 supports only Pose2 and Pose3");
108
109 using Base = NoiseModelFactorN<T>;
110 using This = KnownLandmarkFactor2<T>;
111 using Point = typename T::Translation;
112
113 private:
114 Point wL_;
115 Point measured_kP_;
116
117 public:
119
121 KnownLandmarkFactor2(Key key, const Point& wL, const Point& measured_kP,
122 const SharedNoiseModel& model)
123 : Base(model, key), wL_(wL), measured_kP_(measured_kP) {}
124
125 NonlinearFactor::shared_ptr clone() const override {
126 return std::make_shared<This>(*this);
127 }
128
129 bool equals(const NonlinearFactor& expected,
130 double tol = 1e-9) const override {
131 const auto* e = dynamic_cast<const This*>(&expected);
132 return e != nullptr && Base::equals(*e, tol) &&
133 traits<Point>::Equals(wL_, e->wL_, tol) &&
134 traits<Point>::Equals(measured_kP_, e->measured_kP_, tol);
135 }
136
138 Vector evaluateError(const T& kTw, OptionalMatrixType H) const override {
139 const Point predicted_kP = kTw.transformFrom(wL_, H);
140 return predicted_kP - measured_kP_;
141 }
142
145 NonlinearEqualityConstraints* constraints,
146 size_t columnDimension = 1) const override {
147 if (columnDimension != 1) {
148 throw std::invalid_argument(
149 "KnownLandmarkFactor2::qcqpFactors only supports column dimension "
150 "1");
151 }
152 if (!costs) {
153 throw std::invalid_argument(
154 "KnownLandmarkFactor2::qcqpFactors costs is null");
155 }
156 if (!this->noiseModel_ ||
157 std::dynamic_pointer_cast<noiseModel::Robust>(this->noiseModel_) ||
158 this->noiseModel_->isConstrained()) {
159 throw std::runtime_error(
160 "KnownLandmarkFactor2::qcqpFactors requires a non-null, "
161 "non-robust/non-hard quadratic noise model");
162 }
163
164 constexpr int PointDim = Point::RowsAtCompileTime;
165 constexpr int N = T::LieAlgebra::RowsAtCompileTime;
166 constexpr int LiftedDim = 1 + PointDim * N;
167 static_assert(N == PointDim || N == PointDim + 1,
168 "Unsupported transform and point dimensions");
169
170 Matrix B = Matrix::Zero(PointDim, LiftedDim);
171 B.col(0) = -measured_kP_;
172 for (int column = 0; column < N; ++column) {
173 const double coefficient = column < PointDim ? wL_(column) : 1.0;
174 B.block(0, 1 + column * PointDim, PointDim, PointDim)
175 .diagonal()
176 .setConstant(coefficient);
177 }
178
179 const Matrix whitenedB = this->noiseModel_->Whiten(B);
180 const Matrix Q = whitenedB.transpose() * whitenedB;
181
182 InsertQcqpConstraints<T, 1>(this->key(), constraints);
183 const SymmetricBlockMatrix blockQ(std::vector<DenseIndex>{LiftedDim}, Q);
184 costs->push_back(std::make_shared<QpCost>(KeyVector{this->key()}, blockQ));
185 }
186};
187
188} // namespace gtsam
3D Pose manifold SO(3) x R^3 and group SE(3)
2D Pose
Base class for noise model factors with N variables.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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
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
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition FactorGraph.h:147
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
Definition NonlinearFactorGraph.h:57
NonlinearFactor::shared_ptr clone() const override
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition KnownLandmarkFactor.h:71
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Check if two factors are equal.
Definition KnownLandmarkFactor.h:75
Vector evaluateError(const T &wTk, OptionalMatrixType H) const override
Evaluate the conventional world-from-k prediction minus measured_kP.
Definition KnownLandmarkFactor.h:84
KnownLandmarkFactor(Key key, const Point &wL, const Point &measured_kP, const SharedNoiseModel &model)
Construct from a key, world landmark, frame-k measurement, and noise model.
Definition KnownLandmarkFactor.h:67
bool equals(const NonlinearFactor &expected, double tol=1e-9) const override
Check if two factors are equal.
Definition KnownLandmarkFactor.h:129
KnownLandmarkFactor2(Key key, const Point &wL, const Point &measured_kP, const SharedNoiseModel &model)
Construct from a key, world landmark, frame-k measurement, and noise model.
Definition KnownLandmarkFactor.h:121
Vector evaluateError(const T &kTw, OptionalMatrixType H) const override
Evaluate the k-from-world prediction minus measured_kP.
Definition KnownLandmarkFactor.h:138
NonlinearFactor::shared_ptr clone() const override
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition KnownLandmarkFactor.h:125
void qcqpFactors(NonlinearFactorGraph *costs, NonlinearEqualityConstraints *constraints, size_t columnDimension=1) const override
Add the exact D=1 homogeneous known-landmark cost to a QCQP.
Definition KnownLandmarkFactor.h:144