testTranslationFactor.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2020, 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 
20 #include <gtsam/geometry/Point3.h>
22 
24 
25 using namespace std::placeholders;
26 using namespace std;
27 using namespace gtsam;
28 
29 // Create a noise model for the chordal error
30 static SharedNoiseModel model(noiseModel::Isotropic::Sigma(3, 0.05));
31 
32 // Keys are deliberately *not* in sorted order to test that case.
33 static const Key kKey1(2), kKey2(1), kEdgeKey(3);
34 static const Unit3 kMeasured(1, 0, 0);
35 
36 /* ************************************************************************* */
37 TEST(TranslationFactor, Constructor) {
39 }
40 
41 /* ************************************************************************* */
42 TEST(TranslationFactor, ZeroError) {
43  // Create a factor
45 
46  // Set the linearization
47  Point3 T1(1, 0, 0), T2(2, 0, 0);
48 
49  // Use the factor to calculate the error
50  Vector actualError(factor.evaluateError(T1, T2));
51 
52  // Verify we get the expected error
53  Vector expected = (Vector3() << 0, 0, 0).finished();
54  EXPECT(assert_equal(expected, actualError, 1e-9));
55 }
56 
57 /* ************************************************************************* */
58 TEST(TranslationFactor, NonZeroError) {
59  // create a factor
61 
62  // set the linearization
63  Point3 T1(0, 1, 1), T2(0, 2, 2);
64 
65  // use the factor to calculate the error
66  Vector actualError(factor.evaluateError(T1, T2));
67 
68  // verify we get the expected error
69  Vector expected = (Vector3() << -1, 1 / sqrt(2), 1 / sqrt(2)).finished();
70  EXPECT(assert_equal(expected, actualError, 1e-9));
71 }
72 
73 /* ************************************************************************* */
75  const TranslationFactor &factor) {
76  return factor.evaluateError(T1, T2);
77 }
78 
79 TEST(TranslationFactor, Jacobian) {
80  // Create a factor
82 
83  // Set the linearization
84  Point3 T1(1, 0, 0), T2(2, 0, 0);
85 
86  // Use the factor to calculate the Jacobians
87  Matrix H1Actual, H2Actual;
88  factor.evaluateError(T1, T2, H1Actual, H2Actual);
89 
90  // Use numerical derivatives to calculate the Jacobians
91  Matrix H1Expected, H2Expected;
92  H1Expected = numericalDerivative11<Vector, Point3>(
93  std::bind(&factorError, std::placeholders::_1, T2, factor), T1);
94  H2Expected = numericalDerivative11<Vector, Point3>(
95  std::bind(&factorError, T1, std::placeholders::_1, factor), T2);
96 
97  // Verify the Jacobians are correct
98  EXPECT(assert_equal(H1Expected, H1Actual, 1e-9));
99  EXPECT(assert_equal(H2Expected, H2Actual, 1e-9));
100 }
101 
102 
103 /* ************************************************************************* */
106 }
107 
108 /* ************************************************************************* */
110  // Create a factor
112 
113  // Set the linearization
114  Point3 T1(1, 0, 0), T2(2, 0, 0);
115  Vector1 scale(1.0);
116 
117  // Use the factor to calculate the error
118  Vector actualError(factor.evaluateError(T1, T2, scale));
119 
120  // Verify we get the expected error
121  Vector expected = (Vector3() << 0, 0, 0).finished();
122  EXPECT(assert_equal(expected, actualError, 1e-9));
123 }
124 
125 /* ************************************************************************* */
127  // create a factor
129 
130  // set the linearization
131  Point3 T1(0, 1, 1), T2(0, 2, 2);
132  Vector1 scale(1.0 / sqrt(2));
133 
134  // use the factor to calculate the error
135  Vector actualError(factor.evaluateError(T1, T2, scale));
136 
137  // verify we get the expected error
138  Vector expected = (Vector3() << -1, 1 / sqrt(2), 1 / sqrt(2)).finished();
139  EXPECT(assert_equal(expected, actualError, 1e-9));
140 }
141 
142 /* ************************************************************************* */
144  const BilinearAngleTranslationFactor &factor) {
145  return factor.evaluateError(T1, T2, scale);
146 }
147 
149  // Create a factor
151 
152  // Set the linearization
153  Point3 T1(1, 0, 0), T2(2, 0, 0);
154  Vector1 scale(1.0);
155 
156  // Use the factor to calculate the Jacobians
157  Matrix H1Actual, H2Actual, H3Actual;
158  factor.evaluateError(T1, T2, scale, H1Actual, H2Actual, H3Actual);
159 
160  // Use numerical derivatives to calculate the Jacobians
161  Matrix H1Expected, H2Expected, H3Expected;
162  H1Expected = numericalDerivative11<Vector, Point3>(
163  std::bind(&bilinearAngleFactorError, std::placeholders::_1, T2, scale, factor), T1);
164  H2Expected = numericalDerivative11<Vector, Point3>(
165  std::bind(&bilinearAngleFactorError, T1, std::placeholders::_1, scale, factor), T2);
166  H3Expected = numericalDerivative11<Vector, Vector1>(
167  std::bind(&bilinearAngleFactorError, T1, T2, std::placeholders::_1, factor), scale);
168 
169  // Verify the Jacobians are correct
170  EXPECT(assert_equal(H1Expected, H1Actual, 1e-9));
171  EXPECT(assert_equal(H2Expected, H2Actual, 1e-9));
172  EXPECT(assert_equal(H3Expected, H3Actual, 1e-9));
173 }
174 
175 /* ************************************************************************* */
176 int main() {
177  TestResult tr;
178  return TestRegistry::runAllTests(tr);
179 }
180 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
kMeasured
static const Unit3 kMeasured(1, 0, 0)
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
TranslationFactor.h
Binary factor for a relative translation direction measurement.
bilinearAngleFactorError
Vector bilinearAngleFactorError(const Point3 &T1, const Point3 &T2, const Vector1 &scale, const BilinearAngleTranslationFactor &factor)
Definition: testTranslationFactor.cpp:143
gtsam::TranslationFactor::evaluateError
Vector evaluateError(const Point3 &Ta, const Point3 &Tb, OptionalMatrixType H1, OptionalMatrixType H2) const override
Calculate error: (norm(Tb - Ta) - measurement) where Tb and Ta are Point3 translations and measuremen...
Definition: TranslationFactor.h:68
gtsam::BilinearAngleTranslationFactor::evaluateError
Vector evaluateError(const Point3 &Ta, const Point3 &Tb, const Vector1 &scale, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Calculate error: (scale * (Tb - Ta) - measurement) where Tb and Ta are Point3 translations and measur...
Definition: TranslationFactor.h:133
kKey2
static const Key kKey2(1)
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Point3.h
3D Point
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
TEST
TEST(TranslationFactor, Constructor)
Definition: testTranslationFactor.cpp:37
numericalDerivative.h
Some functions to compute numerical derivatives.
scale
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
Definition: gnuplot_common_settings.hh:54
kKey1
static const Key kKey1(2)
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
main
int main()
Definition: testTranslationFactor.cpp:176
T2
static const Pose3 T2(Rot3::Rodrigues(0.3, 0.2, 0.1), P2)
gtsam::SharedNoiseModel
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:762
TestResult
Definition: TestResult.h:26
gtsam
traits
Definition: SFMdata.h:40
model
static SharedNoiseModel model(noiseModel::Isotropic::Sigma(3, 0.05))
kEdgeKey
static const Key kEdgeKey(3)
std
Definition: BFloat16.h:88
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
gtsam::Unit3
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
T1
static const Similarity3 T1(R, Point3(3.5, -8.2, 4.2), 1)
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::BilinearAngleTranslationFactor
Definition: TranslationFactor.h:104
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
gtsam::TranslationFactor
Definition: TranslationFactor.h:41
factorError
Vector factorError(const Point3 &T1, const Point3 &T2, const TranslationFactor &factor)
Definition: testTranslationFactor.cpp:74


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:41:53