gtsam
Loading...
Searching...
No Matches
InitializePose.h
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
18
19#pragma once
20
26
27namespace gtsam {
28namespace initialize {
29
30static constexpr Key kAnchorKey = 99999999;
31
36template <class Pose>
37static NonlinearFactorGraph buildPoseGraph(const NonlinearFactorGraph& graph) {
38 NonlinearFactorGraph poseGraph;
39
40 for (const auto& factor : graph) {
41 // recast to a between on Pose
42 if (auto between =
43 std::dynamic_pointer_cast<BetweenFactor<Pose> >(factor))
44 poseGraph.add(between);
45
46 // recast PriorFactor<Pose> to BetweenFactor<Pose>
47 if (auto prior = std::dynamic_pointer_cast<PriorFactor<Pose> >(factor))
48 poseGraph.emplace_shared<BetweenFactor<Pose> >(
49 kAnchorKey, prior->keys()[0], prior->prior(), prior->noiseModel());
50 }
51 return poseGraph;
52}
53
57template <class Pose>
58static Values computePoses(const Values& initialRot,
59 NonlinearFactorGraph* posegraph,
60 bool singleIter = true) {
61 const auto origin = Pose().translation();
62
63 // Upgrade rotations to full poses
64 Values initialPose;
65 for (const auto& key_rot : initialRot.extract<typename Pose::Rotation>()) {
66 const Key& key = key_rot.first;
67 const auto& rot = key_rot.second;
68 const Pose initializedPose(rot, origin);
69 initialPose.insert(key, initializedPose);
70 }
71
72 // add prior on dummy node
73 auto priorModel =
74 noiseModel::Unit::Create(static_cast<size_t>(Pose::dimension));
75 initialPose.insert(kAnchorKey, Pose());
76 posegraph->emplace_shared<PriorFactor<Pose> >(kAnchorKey, Pose(), priorModel);
77
78 // Create optimizer
79 GaussNewtonParams params;
80 if (singleIter) {
81 params.maxIterations = 1;
82 } else {
83 params.setVerbosity("TERMINATION");
84 }
85 GaussNewtonOptimizer optimizer(*posegraph, initialPose, params);
86 const Values GNresult = optimizer.optimize();
87
88 // put into Values structure
89 Values estimate;
90 for (const auto& key_pose : GNresult.extract<Pose>()) {
91 const Key& key = key_pose.first;
92 if (key != kAnchorKey) {
93 const Pose& pose = key_pose.second;
94 estimate.insert(key, pose);
95 }
96 }
97 return estimate;
98}
99} // namespace initialize
100} // namespace gtsam
Factor Graph consisting of non-linear factors.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
static shared_ptr Create(size_t dim)
Create a unit covariance noise model.
Definition NoiseModel.h:673