File: testTranslationRecovery.cpp

package info (click to toggle)
gtsam 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 46,108 kB
  • sloc: cpp: 127,191; python: 14,312; xml: 8,442; makefile: 252; sh: 119; ansic: 101
file content (361 lines) | stat: -rw-r--r-- 13,510 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* ----------------------------------------------------------------------------

 * GTSAM Copyright 2010-2020, Georgia Tech Research Corporation,
 * Atlanta, Georgia 30332-0415
 * All Rights Reserved
 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)

 * See LICENSE for the license information

 * -------------------------------------------------------------------------- */

/**
 * @file testTranslationRecovery.cpp
 * @author Frank Dellaert, Akshay Krishnan
 * @date March 2020
 * @brief test recovering translations when rotations are given.
 */

#include <CppUnitLite/TestHarness.h>
#include <gtsam/sfm/SfmData.h>
#include <gtsam/sfm/TranslationRecovery.h>
#include <gtsam/slam/dataset.h>

using namespace std;
using namespace gtsam;

// Returns the Unit3 direction as measured in the binary measurement, but
// computed from the input poses. Helper function used in the unit tests.
Unit3 GetDirectionFromPoses(const Values& poses,
                            const BinaryMeasurement<Unit3>& unitTranslation) {
  const Pose3 wTa = poses.at<Pose3>(unitTranslation.key1()),
              wTb = poses.at<Pose3>(unitTranslation.key2());
  const Point3 Ta = wTa.translation(), Tb = wTb.translation();
  return Unit3(Tb - Ta);
}

/* ************************************************************************* */
// We read the BAL file, which has 3 cameras in it, with poses. We then assume
// the rotations are correct, but translations have to be estimated from
// translation directions only. Since we have 3 cameras, A, B, and C, we can at
// most create three relative measurements, let's call them w_aZb, w_aZc, and
// bZc. These will be of type Unit3. We then call `recoverTranslations` which
// sets up an optimization problem for the three unknown translations.
TEST(TranslationRecovery, BAL) {
  const string filename = findExampleDataFile("dubrovnik-3-7-pre");
  SfmData db = SfmData::FromBalFile(filename);

  // Get camera poses, as Values
  size_t j = 0;
  Values poses;
  for (auto camera : db.cameras) {
    poses.insert(j++, camera.pose());
  }

  // Simulate measurements
  const auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {0, 2}, {1, 2}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;
  const auto graph = algorithm.buildGraph(relativeTranslations);
  EXPECT_LONGS_EQUAL(3, graph.size());

  // Run translation recovery
  const double scale = 2.0;
  const auto result = algorithm.run(relativeTranslations, scale);

  // Check result for first two translations, determined by prior
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0)));
  EXPECT(assert_equal(
      Point3(2 * GetDirectionFromPoses(poses, relativeTranslations[0])),
      result.at<Point3>(1)));

  // Check that the third translations is correct
  Point3 Ta = poses.at<Pose3>(0).translation();
  Point3 Tb = poses.at<Pose3>(1).translation();
  Point3 Tc = poses.at<Pose3>(2).translation();
  Point3 expected = (Tc - Ta) * (scale / (Tb - Ta).norm());
  EXPECT(assert_equal(expected, result.at<Point3>(2), 1e-4));

  // TODO(frank): how to get stats back?
  // EXPECT_DOUBLES_EQUAL(0.0199833, actualError, 1e-5);
}

TEST(TranslationRecovery, TwoPoseTest) {
  // Create a dataset with 2 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //
  // 0 and 1 face in the same direction but have a translation offset.
  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));

  auto relativeTranslations =
      TranslationRecovery::SimulateMeasurements(poses, {{0, 1}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;
  const auto graph = algorithm.buildGraph(relativeTranslations);
  EXPECT_LONGS_EQUAL(1, graph.size());

  // Run translation recovery
  const auto result = algorithm.run(relativeTranslations, /*scale=*/3.0);

  // Check result for first two translations, determined by prior
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-8));
  EXPECT(assert_equal(Point3(3, 0, 0), result.at<Point3>(1), 1e-8));
}

TEST(TranslationRecovery, ThreePoseTest) {
  // Create a dataset with 3 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //    \ __ /
  //     \\//
  //       3
  //
  // 0 and 1 face in the same direction but have a translation offset. 3 is in
  // the same direction as 0 and 1, in between 0 and 1, with some Y axis offset.

  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(3, Pose3(Rot3(), Point3(1, -1, 0)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {1, 3}, {3, 0}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;
  const auto graph = algorithm.buildGraph(relativeTranslations);
  EXPECT_LONGS_EQUAL(3, graph.size());

  const auto result = algorithm.run(relativeTranslations, /*scale=*/3.0);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-8));
  EXPECT(assert_equal(Point3(3, 0, 0), result.at<Point3>(1), 1e-8));
  EXPECT(assert_equal(Point3(1.5, -1.5, 0), result.at<Point3>(3), 1e-8));
}

TEST(TranslationRecovery, ThreePosesIncludingZeroTranslation) {
  // Create a dataset with 3 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //          2 <|
  //
  // 0 and 1 face in the same direction but have a translation offset. 2 is at
  // the same point as 1 but is rotated, with little FOV overlap.
  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(2, Pose3(Rot3::RzRyRx(-M_PI / 2, 0, 0), Point3(2, 0, 0)));

  auto relativeTranslations =
      TranslationRecovery::SimulateMeasurements(poses, {{0, 1}, {1, 2}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;
  // Run translation recovery
  const auto result = algorithm.run(relativeTranslations, /*scale=*/3.0);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-8));
  EXPECT(assert_equal(Point3(3, 0, 0), result.at<Point3>(1), 1e-8));
  EXPECT(assert_equal(Point3(3, 0, 0), result.at<Point3>(2), 1e-8));
}

TEST(TranslationRecovery, FourPosesIncludingZeroTranslation) {
  // Create a dataset with 4 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //    \ __  2 <|
  //     \\//
  //       3
  //
  // 0 and 1 face in the same direction but have a translation offset. 2 is at
  // the same point as 1 but is rotated, with very little FOV overlap. 3 is in
  // the same direction as 0 and 1, in between 0 and 1, with some Y axis offset.

  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(2, Pose3(Rot3::RzRyRx(-M_PI / 2, 0, 0), Point3(2, 0, 0)));
  poses.insert<Pose3>(3, Pose3(Rot3(), Point3(1, -1, 0)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {1, 2}, {1, 3}, {3, 0}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;

  // Run translation recovery
  const auto result = algorithm.run(relativeTranslations, /*scale=*/4.0);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-8));
  EXPECT(assert_equal(Point3(4, 0, 0), result.at<Point3>(1), 1e-8));
  EXPECT(assert_equal(Point3(4, 0, 0), result.at<Point3>(2), 1e-8));
  EXPECT(assert_equal(Point3(2, -2, 0), result.at<Point3>(3), 1e-8));
}

TEST(TranslationRecovery, ThreePosesWithZeroTranslation) {
  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3::RzRyRx(-M_PI / 6, 0, 0), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(2, Pose3(Rot3::RzRyRx(M_PI / 6, 0, 0), Point3(0, 0, 0)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {1, 2}, {2, 0}});

  // Check simulated measurements.
  for (auto& unitTranslation : relativeTranslations) {
    EXPECT(assert_equal(GetDirectionFromPoses(poses, unitTranslation),
                        unitTranslation.measured()));
  }

  TranslationRecovery algorithm;

  // Run translation recovery
  const auto result = algorithm.run(relativeTranslations, /*scale=*/4.0);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-8));
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(1), 1e-8));
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(2), 1e-8));
}

TEST(TranslationRecovery, ThreePosesWithOneSoftConstraint) {
  // Create a dataset with 3 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //    \ __ /
  //     \\//
  //       3
  //
  // 0 and 1 face in the same direction but have a translation offset. 3 is in
  // the same direction as 0 and 1, in between 0 and 1, with some Y axis offset.

  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(3, Pose3(Rot3(), Point3(1, -1, 0)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {0, 3}, {1, 3}});

  std::vector<BinaryMeasurement<Point3>> betweenTranslations;
  betweenTranslations.emplace_back(0, 3, Point3(1, -1, 0),
                                   noiseModel::Isotropic::Sigma(3, 1e-2));

  TranslationRecovery algorithm;
  auto result =
      algorithm.run(relativeTranslations, /*scale=*/0.0, betweenTranslations);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-4));
  EXPECT(assert_equal(Point3(2, 0, 0), result.at<Point3>(1), 1e-4));
  EXPECT(assert_equal(Point3(1, -1, 0), result.at<Point3>(3), 1e-4));
}

TEST(TranslationRecovery, ThreePosesWithOneHardConstraint) {
  // Create a dataset with 3 poses.
  // __      __
  // \/      \/
  //  0 _____ 1
  //    \ __ /
  //     \\//
  //       3
  //
  // 0 and 1 face in the same direction but have a translation offset. 3 is in
  // the same direction as 0 and 1, in between 0 and 1, with some Y axis offset.

  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(3, Pose3(Rot3(), Point3(1, -1, 0)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {0, 3}, {1, 3}});

  std::vector<BinaryMeasurement<Point3>> betweenTranslations;
  betweenTranslations.emplace_back(0, 1, Point3(2, 0, 0),
                                   noiseModel::Constrained::All(3, 1e2));

  TranslationRecovery algorithm;
  auto result =
      algorithm.run(relativeTranslations, /*scale=*/0.0, betweenTranslations);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-4));
  EXPECT(assert_equal(Point3(2, 0, 0), result.at<Point3>(1), 1e-4));
  EXPECT(assert_equal(Point3(1, -1, 0), result.at<Point3>(3), 1e-4));
}

TEST(TranslationRecovery, NodeWithBetweenFactorAndNoMeasurements) {
  // Checks that valid results are obtained when a between translation edge is
  // provided with a node that does not have any other relative translations.
  Values poses;
  poses.insert<Pose3>(0, Pose3(Rot3(), Point3(0, 0, 0)));
  poses.insert<Pose3>(1, Pose3(Rot3(), Point3(2, 0, 0)));
  poses.insert<Pose3>(3, Pose3(Rot3(), Point3(1, -1, 0)));
  poses.insert<Pose3>(4, Pose3(Rot3(), Point3(1, 2, 1)));

  auto relativeTranslations = TranslationRecovery::SimulateMeasurements(
      poses, {{0, 1}, {0, 3}, {1, 3}});

  std::vector<BinaryMeasurement<Point3>> betweenTranslations;
  betweenTranslations.emplace_back(0, 1, Point3(2, 0, 0),
                                   noiseModel::Constrained::All(3, 1e2));
  // Node 4 only has this between translation prior, no relative translations.
  betweenTranslations.emplace_back(0, 4, Point3(1, 2, 1));

  TranslationRecovery algorithm;
  auto result =
      algorithm.run(relativeTranslations, /*scale=*/0.0, betweenTranslations);

  // Check result
  EXPECT(assert_equal(Point3(0, 0, 0), result.at<Point3>(0), 1e-4));
  EXPECT(assert_equal(Point3(2, 0, 0), result.at<Point3>(1), 1e-4));
  EXPECT(assert_equal(Point3(1, -1, 0), result.at<Point3>(3), 1e-4));
  EXPECT(assert_equal(Point3(1, 2, 1), result.at<Point3>(4), 1e-4));
}

/* ************************************************************************* */
int main() {
  TestResult tr;
  return TestRegistry::runAllTests(tr);
}
/* ************************************************************************* */