My Project 1.0
Holistic Fusion: A Generic Framework for Multimodal Sensor Fusion
Loading...
Searching...
No Matches
OptimizerIsam2Batch.hpp
Go to the documentation of this file.
1/*
2Copyright 2024 by Julian Nubert, Robotic Systems Lab, ETH Zurich.
3All rights reserved.
4This file is released under the "BSD-3-Clause License".
5Please see the LICENSE file that has been included as part of this package.
6 */
7
8#ifndef OPTIMIZER_ISAM2_BATCH_HPP
9#define OPTIMIZER_ISAM2_BATCH_HPP
10
11// Workspace
13
14namespace holistic_fusion {
15
17 public:
18 explicit OptimizerIsam2Batch(const std::shared_ptr<GraphConfig> graphConfigPtr) : OptimizerIsam2(graphConfigPtr) {
19 // Initialize Slow Bundle Adjustement Smoother (if desired) -----------------------------------------------
20 if (graphConfigPtr_->useAdditionalSlowBatchSmootherFlag_) {
21 // Batch --> slowBatchSmoother: Define whether to use Cholesky factorization
22 if (graphConfigPtr_->slowBatchSmootherUseCholeskyFactorizationFlag_) {
23 isam2Params_.factorization = gtsam::ISAM2Params::CHOLESKY;
24 std::cout << "Using Cholesky factorization for slow batch smoother (ISAM2)." << std::endl;
25 } else {
26 isam2Params_.factorization = gtsam::ISAM2Params::QR;
27 std::cout << "Using QR factorization for slow batch smoother (ISAM2)." << std::endl;
28 }
29 // Initialize Slow Bundle Adjustement Smoother
30 batchSmootherPtr_ = std::make_shared<gtsam::ISAM2>(isam2Params_);
31 batchSmootherPtr_->params().print("HolisticFusion: Factor Graph Parameters of Slow Batch Optimization Graph.");
32 }
33 }
35
36 // Empty update call --> do nothing
37 bool update() override { return true; }
38
39 // Add to graph without running optimization
40 bool update(const gtsam::NonlinearFactorGraph& newGraphFactors, const gtsam::Values& newGraphValues,
41 const std::map<gtsam::Key, double>& newGraphKeysTimeStampMap, const int depth = 0) override {
42 // Add Bundle Adjustement Smoother factors to batch without running optimization
43 containerBatchSmootherFactors_.add(newGraphFactors);
44 containerBatchSmootherValues_.insert(newGraphValues);
45 // Add to keyTimestampMap
46 batchSmootherKeyTimestampMap_.insert(newGraphKeysTimeStampMap.begin(), newGraphKeysTimeStampMap.end());
47 return true;
48 }
49
50 // Optimize
51 void optimize(int maxIterations) override {
52 // Print
53 std::cout << YELLOW_START << "HolisticFusion: OptimizerIsam2Batch" << GREEN_START << " Optimizing slow batch smoother." << COLOR_END
54 << std::endl;
55
56 // Optimize
58 // Reset containers
61
62 // Get optimized result
64
65 // Flag
67 }
68
69 // Optimize bundle adjustement smoother (if desired)
70 const gtsam::Values& getAllOptimizedStates() override {
71 // Check
73 throw std::runtime_error("HolisticFusion: OptimizerIsam2Batch: No optimization has been performed yet.");
74 }
75
76 // Return result
78 }
79
80 // Get all keys of optimized states
81 gtsam::KeyVector getAllOptimizedKeys() override { return batchSmootherOptimizedResult_.keys(); }
82
83 // Get nonlinear factor graph
84 const gtsam::NonlinearFactorGraph& getNonlinearFactorGraph() const override { return batchSmootherPtr_->getFactorsUnsafe(); }
85
86 // Get keyTimestampMap
87 const std::map<gtsam::Key, double>& getFullKeyTimestampMap() override { return batchSmootherKeyTimestampMap_; }
88
89 // Calculate States / Covariance --------------------------------------------------------------------------------------------
90 // Pose3
91 gtsam::Pose3 calculateEstimatedPose3(const gtsam::Key& key) override { return batchSmootherPtr_->calculateEstimate<gtsam::Pose3>(key); }
92 // Velocity3
93 gtsam::Vector3 calculateEstimatedVelocity3(const gtsam::Key& key) override {
94 return batchSmootherPtr_->calculateEstimate<gtsam::Vector3>(key);
95 }
96 // Bias
97 gtsam::imuBias::ConstantBias calculateEstimatedBias(const gtsam::Key& key) override {
98 return batchSmootherPtr_->calculateEstimate<gtsam::imuBias::ConstantBias>(key);
99 }
100 // Point3
101 gtsam::Point3 calculateEstimatedPoint3(const gtsam::Key& key) override {
102 return batchSmootherPtr_->calculateEstimate<gtsam::Point3>(key);
103 }
104 // Vector
105 gtsam::Vector calculateEstimatedVector(const gtsam::Key& key) override {
106 return batchSmootherPtr_->calculateEstimate<gtsam::Vector>(key);
107 }
108 // Marginal Covariance
109 gtsam::Matrix calculateMarginalCovarianceMatrixAtKey(const gtsam::Key& key) override {
111 throw std::runtime_error("HolisticFusion: OptimizerIsam2Batch: No optimization has been performed yet.");
112 }
113 return batchSmootherPtr_->marginalCovariance(key);
114 }
115
116 private:
117 // Optimizer itself
118 std::shared_ptr<gtsam::ISAM2> batchSmootherPtr_;
119 // Containers for bundle adjustment smoother
120 gtsam::NonlinearFactorGraph containerBatchSmootherFactors_;
122 // Result and keyTimestampMap
123 gtsam::ISAM2Result batchSmootherIsam2Result_;
125 std::map<gtsam::Key, double> batchSmootherKeyTimestampMap_;
126};
127
128} // namespace holistic_fusion
129
130#endif // OPTIMIZER_ISAM2_BATCH_HPP
#define COLOR_END
#define GREEN_START
#define YELLOW_START
std::shared_ptr< GraphConfig > graphConfigPtr_
gtsam::NonlinearFactorGraph containerBatchSmootherFactors_
gtsam::KeyVector getAllOptimizedKeys() override
std::map< gtsam::Key, double > batchSmootherKeyTimestampMap_
gtsam::Vector calculateEstimatedVector(const gtsam::Key &key) override
gtsam::Pose3 calculateEstimatedPose3(const gtsam::Key &key) override
gtsam::Matrix calculateMarginalCovarianceMatrixAtKey(const gtsam::Key &key) override
gtsam::Point3 calculateEstimatedPoint3(const gtsam::Key &key) override
OptimizerIsam2Batch(const std::shared_ptr< GraphConfig > graphConfigPtr)
void optimize(int maxIterations) override
bool update(const gtsam::NonlinearFactorGraph &newGraphFactors, const gtsam::Values &newGraphValues, const std::map< gtsam::Key, double > &newGraphKeysTimeStampMap, const int depth=0) override
std::shared_ptr< gtsam::ISAM2 > batchSmootherPtr_
const gtsam::Values & getAllOptimizedStates() override
gtsam::imuBias::ConstantBias calculateEstimatedBias(const gtsam::Key &key) override
const gtsam::NonlinearFactorGraph & getNonlinearFactorGraph() const override
const std::map< gtsam::Key, double > & getFullKeyTimestampMap() override
gtsam::Vector3 calculateEstimatedVelocity3(const gtsam::Key &key) override
OptimizerIsam2(const std::shared_ptr< GraphConfig > graphConfigPtr)