testBetweenFactor.cpp
Go to the documentation of this file.
1 
11 #include <gtsam/geometry/Pose3.h>
12 #include <gtsam/geometry/Rot3.h>
13 #include <gtsam/inference/Symbol.h>
16 
17 using namespace std::placeholders;
18 using namespace gtsam;
19 using namespace gtsam::symbol_shorthand;
20 using namespace gtsam::noiseModel;
21 
26  Rot3 R1 = Rot3::Rodrigues(0.1, 0.2, 0.3);
27  Rot3 R2 = Rot3::Rodrigues(0.4, 0.5, 0.6);
28  Rot3 noise = Rot3(); // Rot3::Rodrigues(0.01, 0.01, 0.01); // Uncomment to make unit test fail
29  Rot3 measured = R1.between(R2)*noise ;
30 
31  BetweenFactor<Rot3> factor(R(1), R(2), measured, Isotropic::Sigma(3, 0.05));
32  Matrix actualH1, actualH2;
33  Vector actual = factor.evaluateError(R1, R2, actualH1, actualH2);
34 
35  Vector expected = Rot3::Logmap(measured.inverse() * R1.between(R2));
36  EXPECT(assert_equal(expected,actual/*, 1e-100*/)); // Uncomment to make unit test fail
37 
38  Matrix numericalH1 = numericalDerivative21<Vector3, Rot3, Rot3>(
39  [&factor](const Rot3& r1, const Rot3& r2) {return factor.evaluateError(r1, r2);},
40  R1, R2, 1e-5);
41  EXPECT(assert_equal(numericalH1,actualH1, 1E-5));
42 
43  Matrix numericalH2 = numericalDerivative22<Vector3,Rot3,Rot3>(
44  [&factor](const Rot3& r1, const Rot3& r2) {return factor.evaluateError(r1, r2);},
45  R1, R2, 1e-5);
46  EXPECT(assert_equal(numericalH2,actualH2, 1E-5));
47 }
48 
49 /* ************************************************************************* */
50 // Constructor scalar
51 TEST(BetweenFactor, ConstructorScalar) {
53  double measured = 0.0;
54  BetweenFactor<double> factor(1, 2, measured, model);
55 }
56 
57 /* ************************************************************************* */
58 // Constructor vector3
59 TEST(BetweenFactor, ConstructorVector3) {
60  SharedNoiseModel model = noiseModel::Isotropic::Sigma(3, 1.0);
61  Vector3 measured(1, 2, 3);
62  BetweenFactor<Vector3> factor(1, 2, measured, model);
63 }
64 
65 /* ************************************************************************* */
66 // Constructor dynamic sized vector
67 TEST(BetweenFactor, ConstructorDynamicSizeVector) {
68  SharedNoiseModel model = noiseModel::Isotropic::Sigma(5, 1.0);
69  Vector measured(5); measured << 1, 2, 3, 4, 5;
70  BetweenFactor<Vector> factor(1, 2, measured, model);
71 }
72 
73 /* ************************************************************************* */
74 TEST(BetweenFactor, Point3Jacobians) {
75  SharedNoiseModel model = noiseModel::Isotropic::Sigma(3, 1.0);
76  Point3 measured(1, 2, 3);
77  BetweenFactor<Point3> factor(1, 2, measured, model);
78 
79  Values values;
80  values.insert(1, Point3(0, 0, 0));
81  values.insert(2, Point3(1, 2, 3));
82  Vector3 error = factor.evaluateError(Point3(0, 0, 0), Point3(1, 2, 3));
83  EXPECT(assert_equal<Vector3>(Vector3::Zero(), error, 1e-9));
84  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, 1e-7, 1e-5);
85 }
86 
87 /* ************************************************************************* */
88 TEST(BetweenFactor, Rot3Jacobians) {
89  SharedNoiseModel model = noiseModel::Isotropic::Sigma(3, 1.0);
90  Rot3 measured = Rot3::Ry(M_PI_2);
91  BetweenFactor<Rot3> factor(1, 2, measured, model);
92 
93  Values values;
94  values.insert(1, Rot3());
95  values.insert(2, Rot3::Ry(M_PI_2));
96  Vector3 error = factor.evaluateError(Rot3(), Rot3::Ry(M_PI_2));
97  EXPECT(assert_equal<Vector3>(Vector3::Zero(), error, 1e-9));
98  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, 1e-7, 1e-5);
99 }
100 
101 /* ************************************************************************* */
102 TEST(BetweenFactor, Pose3Jacobians) {
103  SharedNoiseModel model = noiseModel::Isotropic::Sigma(6, 1.0);
104  Pose3 measured(Rot3(), Point3(1, 2, 3));
105  BetweenFactor<Pose3> factor(1, 2, measured, model);
106 
107  Pose3 pose1, pose2(Rot3(), Point3(1, 2, 3));
108  Values values;
109  values.insert(1, pose1);
110  values.insert(2, pose2);
111  Vector6 error = factor.evaluateError(pose1, pose2);
112  EXPECT(assert_equal<Vector6>(Vector6::Zero(), error, 1e-9));
113  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, 1e-7, 1e-5);
114 }
115 
116 /* ************************************************************************* */
117 int main() {
118  TestResult tr;
119  return TestRegistry::runAllTests(tr);
120 }
121 /* ************************************************************************* */
Point2 measured(-17, 30)
Provides additional testing facilities for common data structures.
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
TEST(BetweenFactor, Rot3)
noiseModel::Diagonal::shared_ptr model
Matrix expected
Definition: testMatrix.cpp:971
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Rot2 R(Rot2::fromAngle(0.1))
leaf::MyValues values
Some functions to compute numerical derivatives.
Evaluate derivatives of a nonlinear factor numerically.
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
Rot3 inverse() const
inverse of a rotation
Definition: Rot3.h:308
Eigen::VectorXd Vector
Definition: Vector.h:38
Vector evaluateError(const T &p1, const T &p2, OptionalMatrixType H1, OptionalMatrixType H2) const override
evaluate error, returns vector of errors size of tangent space
#define EXPECT(condition)
Definition: Test.h:150
Array< double, 1, 3 > e(1./3., 0.5, 2.)
static const double r2
static const Pose3 pose1(Rot3(), Point3(0, 1, 0.5))
traits
Definition: chartTesting.h:28
int main()
DiscreteKey E(5, 2)
static const double r1
#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
static Pose3 pose2
void insert(Key j, const Value &val)
Definition: Values.cpp:155
static double error
Definition: testRot3.cpp:37
Vector3 Point3
Definition: Point3.h:38
3D Pose
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741
3D rotation represented as a rotation matrix or quaternion
All noise models live in the noiseModel namespace.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:57