testVisualISAM2.cpp
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 
20 
21 #include <examples/SFMdata.h>
22 #include <gtsam/geometry/Point2.h>
23 #include <gtsam/inference/Symbol.h>
24 #include <gtsam/nonlinear/ISAM2.h>
26 #include <gtsam/nonlinear/Values.h>
28 
29 #include <vector>
30 
31 using namespace std;
32 using namespace gtsam;
33 
34 /* ************************************************************************* */
35 TEST(testVisualISAM2, all)
36 {
37  Cal3_S2::shared_ptr K(new Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0));
38 
39  auto measurementNoise = noiseModel::Isotropic::Sigma(2, 1.0);
40 
41  // Create ground truth data
42  vector<Point3> points = createPoints();
43  vector<Pose3> poses = createPoses();
44 
45  // Set the parameters
47  parameters.relinearizeThreshold = 0.01;
48  parameters.relinearizeSkip = 1;
50 
51  // Create a Factor Graph and Values to hold the new data
53  Values initialEstimate;
54 
55  // Loop over the poses, adding the observations to iSAM incrementally
56  for (size_t i = 0; i < poses.size(); ++i)
57  {
58  // Add factors for each landmark observation
59  for (size_t j = 0; j < points.size(); ++j)
60  {
61  PinholeCamera<Cal3_S2> camera(poses[i], *K);
62  Point2 measurement = camera.project(points[j]);
64  measurement, measurementNoise, Symbol('x', i), Symbol('l', j), K);
65  }
66 
67  // Add an initial guess for the current pose
68  // Intentionally initialize the variables off from the ground truth
69  static Pose3 kDeltaPose(Rot3::Rodrigues(-0.1, 0.2, 0.25),
70  Point3(0.05, -0.10, 0.20));
71  initialEstimate.insert(Symbol('x', i), poses[i] * kDeltaPose);
72 
73  // Treat first iteration as special case
74  if (i == 0)
75  {
76  // Add a prior on pose x0, 30cm std on x,y,z and 0.1 rad on roll,pitch,yaw
77  static auto kPosePrior = noiseModel::Diagonal::Sigmas(
78  (Vector(6) << Vector3::Constant(0.1), Vector3::Constant(0.3))
79  .finished());
80  graph.addPrior(Symbol('x', 0), poses[0], kPosePrior);
81 
82  // Add a prior on landmark l0
83  static auto kPointPrior = noiseModel::Isotropic::Sigma(3, 0.1);
84  graph.addPrior(Symbol('l', 0), points[0], kPointPrior);
85 
86  // Add initial guesses to all observed landmarks
87  // Intentionally initialize the variables off from the ground truth
88  static Point3 kDeltaPoint(-0.25, 0.20, 0.15);
89  for (size_t j = 0; j < points.size(); ++j)
90  initialEstimate.insert<Point3>(Symbol('l', j), points[j] + kDeltaPoint);
91  }
92  else
93  {
94  // Update iSAM with the new factors
95  isam.update(graph, initialEstimate);
96 
97  // Do an extra update to converge withing these 8 iterations
98  isam.update();
99 
100  // Optimize
101  Values currentEstimate = isam.calculateEstimate();
102 
103  // reset for next iteration
104  graph.resize(0);
105  initialEstimate.clear();
106  }
107  } // for loop
108 
109  auto result = isam.calculateEstimate();
111  for (size_t j = 0; j < points.size(); ++j)
112  {
113  Symbol key('l', j);
114  EXPECT(assert_equal(points[j], result.at<Point3>(key), 0.01));
115  }
116 }
117 
118 /* ************************************************************************* */
119 int main()
120 {
121  TestResult tr;
122  return TestRegistry::runAllTests(tr);
123 }
124 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::ISAM2Params
Definition: ISAM2Params.h:136
ProjectionFactor.h
Reprojection of a LANDMARK to a 2D point.
gtsam::ISAM2
Definition: ISAM2.h:45
gtsam::createPoints
std::vector< Point3 > createPoints()
Create a set of ground-truth landmarks.
Definition: SFMdata.h:43
gtsam::Values::size
size_t size() const
Definition: Values.h:178
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::NonlinearISAM::update
void update(const NonlinearFactorGraph &newFactors, const Values &initialValues)
Definition: NonlinearISAM.cpp:35
Point2.h
2D Point
gtsam::Values::at
const ValueType at(Key j) const
Definition: Values-inl.h:261
gtsam::FactorGraph::resize
virtual void resize(size_t size)
Definition: FactorGraph.h:367
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Pose3
Definition: Pose3.h:37
parameters
static ConjugateGradientParameters parameters
Definition: testIterative.cpp:33
Eigen::all
static const Eigen::internal::all_t all
Definition: IndexedViewHelper.h:171
gtsam::PinholeCamera
Definition: PinholeCamera.h:33
isam
NonlinearISAM isam(relinearizeInterval)
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::GenericProjectionFactor
Definition: ProjectionFactor.h:40
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
Symbol.h
gtsam::Point2
Vector2 Point2
Definition: Point2.h:32
gtsam::Cal3_S2::shared_ptr
std::shared_ptr< Cal3_S2 > shared_ptr
Definition: Cal3_S2.h:39
TestResult
Definition: TestResult.h:26
key
const gtsam::Symbol key('X', 0)
main
int main()
Definition: testVisualISAM2.cpp:119
ISAM2.h
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
gtsam::createPoses
std::vector< Pose3 > createPoses(const Pose3 &init=Pose3(Rot3::Ypr(M_PI_2, 0, -M_PI_2), {30, 0, 0}), const Pose3 &delta=Pose3(Rot3::Ypr(0, -M_PI_4, 0), {sin(M_PI_4) *30, 0, 30 *(1 - sin(M_PI_4))}), int steps=8)
Definition: SFMdata.h:62
gtsam
traits
Definition: SFMdata.h:40
TEST
TEST(testVisualISAM2, all)
Definition: testVisualISAM2.cpp:35
K
#define K
Definition: igam.h:8
NonlinearFactorGraph.h
Factor Graph consisting of non-linear factors.
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
gtsam::Values::clear
void clear()
Definition: Values.h:347
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:156
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
gtsam::Cal3_S2
The most common 5DOF 3D->2D calibration.
Definition: Cal3_S2.h:34
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
camera
static const CalibratedCamera camera(kDefaultPose)
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
SFMdata.h
Simple example for the structure-from-motion problems.
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
measurement
static Point2 measurement(323.0, 240.0)
Values.h
A non-templated config holding any types of Manifold-group elements.
gtsam::CalibratedCamera::project
Point2 project(const Point3 &point, OptionalJacobian< 2, 6 > Dcamera={}, OptionalJacobian< 2, 3 > Dpoint={}) const
Definition: CalibratedCamera.cpp:188
gtsam::FactorGraph::emplace_shared
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:153
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:17:41