31#include <gtsam/constrained/QcqpProblem.h>
32#include <gtsam/constrained/QpCost.h>
48 typename std::conditional<d == 2, Rot2, Rot3>::type,
49 Eigen::Matrix<double, d, 1>, Eigen::Matrix<double, d, 1>> {
50 static_assert(d == 2 || d == 3,
51 "RelativeTranslationFactor supports d = 2 or 3.");
53 using Rot =
typename std::conditional<d == 2, Rot2, Rot3>::type;
54 using Point = Eigen::Matrix<double, d, 1>;
71 Key translationKey2,
const Point& measured,
73 : Base(
noiseModel::Unit::Create(d), rotationKey, translationKey1,
78 throw std::invalid_argument(
79 "RelativeTranslationFactor: weight must be positive.");
83 const Point& measured()
const {
return measured_; }
84 double weight()
const {
return weight_; }
87 const std::string& s =
"",
89 std::cout << s <<
"RelativeTranslationFactor(" << keyFormatter(this->key1())
90 <<
"," << keyFormatter(this->key2()) <<
","
91 << keyFormatter(this->key3()) <<
") weight=" << weight_
92 <<
" measured=[" << measured_.transpose() <<
"]\n";
99 std::abs(weight_ - e->weight_) < tol;
106 const double sw = std::sqrt(weight_);
108 Matrix rotationJacobian;
109 const Point rotated =
110 Ri.rotate(measured_, H1 ? &rotationJacobian :
nullptr);
112 if (H1) *H1 = -sw * rotationJacobian;
113 if (H2) *H2 = -sw * Matrix::Identity(d, d);
114 if (H3) *H3 = sw * Matrix::Identity(d, d);
115 return sw * (tj - ti - rotated);
118 gtsam::NonlinearFactor::shared_ptr
clone()
const override {
119 return std::static_pointer_cast<gtsam::NonlinearFactor>(
120 gtsam::NonlinearFactor::shared_ptr(
126 NonlinearEqualityConstraints* constraints,
127 size_t columnDimension = 1)
const override {
128 if (columnDimension == 0) {
129 throw std::invalid_argument(
130 "RelativeTranslationFactor::qcqpFactors requires a positive "
133 if (columnDimension == 1) {
134 qcqpFactorsForVector(costs, constraints);
137 if (columnDimension <
static_cast<size_t>(d)) {
138 throw std::invalid_argument(
139 "RelativeTranslationFactor::qcqpFactors requires columnDimension "
143 throw std::invalid_argument(
144 "RelativeTranslationFactor::qcqpFactors: costs is null.");
147 const double sw = std::sqrt(weight_);
148 Matrix B = Matrix::Zero(1, d + 2);
149 B.block(0, 0, 1, d) = -sw * measured_.transpose();
153 const Matrix Q = B.transpose() * B;
155 costs->
push_back(std::make_shared<QpCost>(
156 KeyVector{this->key1(), this->key2(), this->key3()}, blockQ,
163 NonlinearEqualityConstraints* constraints)
const {
165 throw std::invalid_argument(
166 "RelativeTranslationFactor::qcqpFactors: costs is null.");
169 constexpr int kRotationDim = traits<Rot>::QcqpVectorDim;
170 constexpr int kPointDim = traits<Point>::QcqpVectorDim;
171 const double sw = std::sqrt(weight_);
174 Matrix rotateMeasurement;
175 if constexpr (d == 2) {
177 rotateMeasurement.resize(2, 2);
178 rotateMeasurement << measured_(0), -measured_(1), measured_(1),
182 rotateMeasurement = Matrix::Zero(d, d * d);
183 for (
int column = 0; column < d; ++column) {
184 rotateMeasurement.block(0, column * d, d, d) =
185 measured_(column) * Matrix::Identity(d, d);
189 Matrix B = Matrix::Zero(d, kRotationDim + 2 * kPointDim);
190 B.block(0, 1, d, kRotationDim - 1) = -sw * rotateMeasurement;
191 B.block(0, kRotationDim + 1, d, d) = -sw * Matrix::Identity(d, d);
192 B.block(0, kRotationDim + kPointDim + 1, d, d) =
193 sw * Matrix::Identity(d, d);
195 InsertQcqpConstraints<Rot, 1>(this->key1(), constraints);
196 InsertQcqpConstraints<Point, 1>(this->key2(), constraints);
197 InsertQcqpConstraints<Point, 1>(this->key3(), constraints);
199 const Matrix Q = B.transpose() * B;
200 const SymmetricBlockMatrix blockQ(
201 std::vector<DenseIndex>{kRotationDim, kPointDim, kPointDim}, Q);
202 costs->
push_back(std::make_shared<QpCost>(
203 KeyVector{this->key1(), this->key2(), this->key3()}, blockQ));
typedef and functions to augment Eigen's MatrixXd
3D rotation represented as a rotation matrix or quaternion
Base class for noise model factors with N variables.
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
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
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
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
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
Nonlinear factor base class.
Definition NonlinearFactor.h:70
Definition NonlinearFactorGraph.h:57
Definition RelativeTranslationFactor.h:49
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
Check if two factors are equal.
Definition RelativeTranslationFactor.h:95
Vector evaluateError(const Rot &Ri, const Point &ti, const Point &tj, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Weighted residual sqrt(weight) * (t_j - t_i - R_i * measured).
Definition RelativeTranslationFactor.h:103
RelativeTranslationFactor(Key rotationKey, Key translationKey1, Key translationKey2, const Point &measured, double weight)
Definition RelativeTranslationFactor.h:70
void qcqpFactors(NonlinearFactorGraph *costs, NonlinearEqualityConstraints *constraints, size_t columnDimension=1) const override
Add this translation factor as a QCQP cost when traits exist.
Definition RelativeTranslationFactor.h:125
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition RelativeTranslationFactor.h:86
gtsam::NonlinearFactor::shared_ptr clone() const override
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition RelativeTranslationFactor.h:118