gtsam
Loading...
Searching...
No Matches
FastSync.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
16
17#pragma once
18
19#include <gtsam/base/FastMap.h>
21#include <gtsam/dllexport.h>
24#include <gtsam/geometry/Rot2.h>
25#include <gtsam/geometry/Rot3.h>
26#include <gtsam/geometry/SL4.h>
35
36#include <Eigen/SVD>
37#include <cmath>
38#include <limits>
39#include <stdexcept>
40#include <vector>
41
42namespace gtsam {
43
45
53template <class T>
55
63template <>
66 static Rot2 project(const Matrix2& matrix) { return Rot2::ClosestTo(matrix); }
67};
68
77template <>
80 static Rot3 project(const Matrix3& matrix) { return Rot3::ClosestTo(matrix); }
81};
82
90template <>
93 static Pose2 project(const Matrix3& matrix) {
94 return Pose2(Rot2::ClosestTo(matrix.topLeftCorner<2, 2>()),
95 matrix.topRightCorner<2, 1>());
96 }
97};
98
106template <>
109 static Pose3 project(const Matrix4& matrix) {
110 return Pose3(Rot3::ClosestTo(matrix.topLeftCorner<3, 3>()),
111 matrix.topRightCorner<3, 1>());
112 }
113};
114
125template <>
128 static Similarity2 project(const Matrix3& matrix) {
129 double inverseScale = matrix(2, 2);
130 if (!std::isfinite(inverseScale)) inverseScale = 1.0;
131 inverseScale = std::abs(inverseScale);
132 if (inverseScale <= std::numeric_limits<double>::epsilon()) {
133 inverseScale = 1.0;
134 }
135 return Similarity2(Rot2::ClosestTo(matrix.topLeftCorner<2, 2>()),
136 matrix.topRightCorner<2, 1>(), 1.0 / inverseScale);
137 }
138};
139
150template <>
153 static Similarity3 project(const Matrix4& matrix) {
154 double inverseScale = matrix(3, 3);
155 if (!std::isfinite(inverseScale)) inverseScale = 1.0;
156 inverseScale = std::abs(inverseScale);
157 if (inverseScale <= std::numeric_limits<double>::epsilon()) {
158 inverseScale = 1.0;
159 }
160 return Similarity3(Rot3::ClosestTo(matrix.topLeftCorner<3, 3>()),
161 matrix.topRightCorner<3, 1>(), 1.0 / inverseScale);
162 }
163};
164
173template <>
176 static SL4 project(const Matrix4& matrix) {
177 const Eigen::JacobiSVD<Matrix4> svd(
178 matrix, Eigen::ComputeFullU | Eigen::ComputeFullV);
179 const auto singularValues = svd.singularValues();
180 const double determinantMagnitude = singularValues.prod();
181 if (!std::isfinite(determinantMagnitude) || determinantMagnitude <= 1e-12) {
182 return SL4::Identity();
183 }
184 return SL4(matrix);
185 }
186};
187
197template <class T>
198struct FastSync {
199 using LieAlgebra = typename T::LieAlgebra;
200 static constexpr int N = LieAlgebra::RowsAtCompileTime;
201 static_assert(N != Eigen::Dynamic && N > 0,
202 "FastSync requires a positive compile-time matrix dimension");
203 static_assert(LieAlgebra::ColsAtCompileTime == N,
204 "FastSync requires a square matrix representation");
205
206 using MatrixN = Eigen::Matrix<double, N, N>;
207 using VectorN = Eigen::Matrix<double, N, 1>;
208
213 explicit FastSync(const NonlinearFactorGraph& graph);
214
225 Values solve(Ordering::OrderingType orderingType = Ordering::METIS) const;
226
235 Values solve(const Ordering& ordering) const;
236
245 Values projectAndAlign(const Values& relaxed) const;
246
247 private:
248 size_t priorCount_ = 0;
249 Key priorKey_ = 0;
250 T priorValue_ = traits<T>::Identity();
251 GaussianFactorGraph reducedGraph_;
252
254 static double isotropicSigma(const SharedNoiseModel& model);
255
257 static void backSubstituteConditional(const GaussianConditional& conditional,
258 const Key& gaugeKey, Values& solution);
259
261 Values solveOrdered(const Ordering& ordering) const;
262};
263
287template <class T>
289 Ordering::OrderingType orderingType = Ordering::METIS);
290
297template <class T>
298Values fastSync(const NonlinearFactorGraph& graph, const Ordering& ordering);
299
300} // namespace gtsam
301
Base class and basic functions for Matrix Lie groups.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
Implementation of Similarity2 transform.
2D rotation
3D Pose manifold SO(3) x R^3 and group SE(3)
2D Pose
Projective Special Linear Group (SL(4, R)) factor.
3D rotation represented as a rotation matrix or quaternion
Implementation of Similarity3 transform.
Linear Factor Graph where all factors are Gaussians.
A non-templated config holding any types of Manifold-group elements.
Factor Graph consisting of non-linear factors.
Template implementation for fixed-size FAST-Sync.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Values fastSync(const NonlinearFactorGraph &graph, Ordering::OrderingType orderingType)
Initialize a synchronization graph using FAST-Sync.
Definition FastSync-inl.h:204
void svd(const Matrix &A, Matrix &U, Vector &S, Matrix &V)
SVD computes economy SVD A=U*S*V'.
Definition Matrix.cpp:557
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 2D pose (Point2,Rot2).
Definition Pose2.h:39
A 3D pose (R,t) : (Rot3,Point3).
Definition Pose3.h:42
Rotation matrix NOTE: the angle theta is in radians unless explicitly stated.
Definition Rot2.h:40
static Rot2 ClosestTo(const Matrix2 &M)
Find closest valid rotation matrix, given a 2x2 matrix.
Definition Rot2.cpp:140
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
static Rot3 ClosestTo(const Matrix3 &M)
Static, named constructor that finds Rot3 element closest to M in Frobenius norm.
Definition Rot3.h:287
2D similarity transform
Definition Similarity2.h:34
3D similarity transform
Definition Similarity3.h:36
Definition SL4.h:31
static SL4 Identity()
identity for group operation
Definition SL4.h:66
Definition Ordering.h:33
OrderingType
Type of ordering to use.
Definition Ordering.h:40
A GaussianConditional functions as the node in a Bayes network.
Definition GaussianConditional.h:43
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
Definition NonlinearFactorGraph.h:57
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
Projection customization point used by fastSync().
Definition FastSync.h:54
static Rot2 project(const Matrix2 &matrix)
Return the closest proper planar rotation to matrix.
Definition FastSync.h:66
static Rot3 project(const Matrix3 &matrix)
Return the closest proper three-dimensional rotation to matrix.
Definition FastSync.h:80
static Pose2 project(const Matrix3 &matrix)
Round the rotation block and retain the recovered planar translation.
Definition FastSync.h:93
static Pose3 project(const Matrix4 &matrix)
Round the rotation block and retain the recovered 3D translation.
Definition FastSync.h:109
static Similarity2 project(const Matrix3 &matrix)
Round rotation and recover translation and positive planar scale.
Definition FastSync.h:128
static Similarity3 project(const Matrix4 &matrix)
Round rotation and recover translation and positive 3D scale.
Definition FastSync.h:153
static SL4 project(const Matrix4 &matrix)
Normalize a nonsingular ambient matrix to SL(4), or return identity.
Definition FastSync.h:176
Values solve(Ordering::OrderingType orderingType=Ordering::METIS) const
Solve the relaxed ambient matrix problem and return one matrix per key.
Definition FastSync-inl.h:130
Values projectAndAlign(const Values &relaxed) const
Project relaxed matrices to T and align them to the optional matching prior stored by the constructor...
Definition FastSync-inl.h:177
FastSync(const NonlinearFactorGraph &graph)
Extract matching factors, validate their noise models, and build the reduced Gaussian graph.
Definition FastSync-inl.h:43