testShonanFactor.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2019, 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 
21 #include <gtsam/base/lieProxies.h>
22 #include <gtsam/base/testLie.h>
23 #include <gtsam/geometry/Rot3.h>
24 #include <gtsam/geometry/SO3.h>
25 #include <gtsam/geometry/SO4.h>
29 #include <gtsam/sfm/ShonanFactor.h>
31 
33 
34 using namespace std;
35 using namespace gtsam;
36 
37 //******************************************************************************
38 namespace so3 {
40 Vector3 v1 = (Vector(3) << 0.1, 0, 0).finished();
42 Vector3 v2 = (Vector(3) << 0.01, 0.02, 0.03).finished();
44 SO3 R12 = R1.between(R2);
45 } // namespace so3
46 
47 //******************************************************************************
48 namespace submanifold {
50 Vector6 v1 = (Vector(6) << 0, 0, 0, 0.1, 0, 0).finished();
51 SO3 R1 = SO3::Expmap(v1.tail<3>());
53 Vector6 v2 = (Vector(6) << 0, 0, 0, 0.01, 0.02, 0.03).finished();
54 SO3 R2 = SO3::Expmap(v2.tail<3>());
56 SO3 R12 = R1.between(R2);
57 } // namespace submanifold
58 
59 /* ************************************************************************* */
60 TEST(ShonanFactor3, evaluateError) {
61  auto model = noiseModel::Isotropic::Sigma(3, 1.2);
62  for (const size_t p : {5, 4, 3}) {
63  Matrix M = Matrix::Identity(p, p);
64  M.topLeftCorner(3, 3) = submanifold::R1.matrix();
65  SOn Q1(M);
66  M.topLeftCorner(3, 3) = submanifold::R2.matrix();
67  SOn Q2(M);
68  auto factor = ShonanFactor3(1, 2, Rot3(::so3::R12.matrix()), p, model);
69  Matrix H1, H2;
70  factor.evaluateError(Q1, Q2, H1, H2);
71 
72  // Test derivatives
73  Values values;
74  values.insert(1, Q1);
75  values.insert(2, Q2);
76  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, 1e-7, 1e-5);
77  }
78 }
79 
80 /* ************************************************************************* */
81 TEST(ShonanFactor3, equivalenceToSO3) {
82  using namespace ::submanifold;
83  auto R12 = ::so3::R12.retract(Vector3(0.1, 0.2, -0.1));
84  auto model = noiseModel::Isotropic::Sigma(6, 1.2); // wrong dimension
86  auto factor4 = ShonanFactor3(1, 2, Rot3(R12.matrix()), 4, model);
87  const Matrix3 E3(factor3.evaluateError(R1, R2).data());
88  const Matrix43 E4(
89  factor4.evaluateError(SOn(Q1.matrix()), SOn(Q2.matrix())).data());
90  EXPECT(assert_equal((Matrix)E4.topLeftCorner<3, 3>(), E3, 1e-9));
91  EXPECT(assert_equal((Matrix)E4.row(3), Matrix13::Zero(), 1e-9));
92 }
93 
94 /* ************************************************************************* */
95 TEST(ShonanFactor2, evaluateError) {
96  auto model = noiseModel::Isotropic::Sigma(1, 1.2);
97  const Rot2 R1(0.3), R2(0.5), R12(0.2);
98  for (const size_t p : {5, 4, 3, 2}) {
99  Matrix M = Matrix::Identity(p, p);
100  M.topLeftCorner(2, 2) = R1.matrix();
101  SOn Q1(M);
102  M.topLeftCorner(2, 2) = R2.matrix();
103  SOn Q2(M);
104  auto factor = ShonanFactor2(1, 2, R12, p, model);
105  Matrix H1, H2;
106  factor.evaluateError(Q1, Q2, H1, H2);
107 
108  // Test derivatives
109  Values values;
110  values.insert(1, Q1);
111  values.insert(2, Q2);
112  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, 1e-7, 1e-5);
113  }
114 }
115 
116 /* ************************************************************************* */
117 int main() {
118  TestResult tr;
119  return TestRegistry::runAllTests(tr);
120 }
121 /* ************************************************************************* */
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
int main()
Main factor type in Shonan averaging, on SO(n) pairs.
Provides convenient mappings of common member functions for testing.
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
Factor Graph consisting of non-linear factors.
noiseModel::Diagonal::shared_ptr model
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
leaf::MyValues values
4*4 matrix representation of SO(4)
Pose2_ Expmap(const Vector3_ &xi)
Definition: BFloat16.h:88
Evaluate derivatives of a nonlinear factor numerically.
3*3 matrix representation of SO(3)
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
static const Similarity3 id
ShonanFactor< 3 > ShonanFactor3
Definition: ShonanFactor.h:90
const MatrixNN & matrix() const
Return matrix.
Definition: SOn.h:157
#define EXPECT(condition)
Definition: Test.h:150
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Various factors that minimize some Frobenius norm.
traits
Definition: chartTesting.h:28
TEST(ShonanFactor3, evaluateError)
Class retract(const TangentVector &v) const
retract as required by manifold concept: applies v at *this
Definition: Lie.h:131
#define EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, numerical_derivative_step, tolerance)
Check the Jacobians produced by a factor against finite differences.
Class between(const Class &g) const
Definition: Lie.h:52
Vector evaluateError(const Rot &R1, const Rot &R2, OptionalMatrixType H1, OptionalMatrixType H2) const override
Error is Frobenius norm between R1*R12 and R2.
float * p
void insert(Key j, const Value &val)
Definition: Values.cpp:155
SO< Eigen::Dynamic > SOn
Definition: SOn.h:342
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
3D rotation represented as a rotation matrix or quaternion
ShonanFactor< 2 > ShonanFactor2
Definition: ShonanFactor.h:89


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:31