45 const gtsam::NavState& W_currentPropagatedState,
46 const bool centerMeasurementsAtKeyframePositionBeforeAlignmentFlag)
final {
53 std::lock_guard<std::mutex> modifyGraphKeysLock(gtsamDynamicExpressionKeys.get<gtsam::Pose3>().mutex());
57 Eigen::Vector3d measurementOriginPosition = Eigen::Vector3d::Zero();
58 if (centerMeasurementsAtKeyframePositionBeforeAlignmentFlag && this->
measuresPosition()) {
66 bool initialGuessSetExternally =
false;
69 T_W_fixedFrame_initial)) {
70 initialGuessSetExternally =
true;
82 throw std::logic_error(
"GmsfUnaryExpressionAbsolut: a measurement without a position cannot create the keyframe of " +
84 ". Add a measurement with a position of the same frame (e.g. a position measurement) before this one.");
86 bool newGraphKeyAddedFlag =
false;
103 bool introducedNewKeyframeDisplacement =
false;
105 const gtsam::Key oldGtsamKey = graphKey.
key();
113 std::unique_ptr<DynamicFactorGraphStateKey<gtsam::Pose3>> oldKeyPtr =
nullptr;
114 if (!oldVariableWasActive) {
115 REGULAR_COUT <<
" Old keyframe is not active anymore. Hence keeping the belief and uncertainty of previous key." << std::endl;
116 oldKeyPtr = std::make_unique<DynamicFactorGraphStateKey<gtsam::Pose3>>(graphKey);
121 if (!oldVariableWasActive) {
123 if (newGraphKeyAddedFlag) {
124 throw std::logic_error(
"GmsfUnaryExpressionAbsolut: Old keyframe was not active, but new keyframe was added.");
127 gtsamDynamicExpressionKeys.get<gtsam::Pose3>()
131 gtsamDynamicExpressionKeys.get<gtsam::Pose3>()
133 .resetNumberStepsOptimized();
136 <<
" as it was deactivated, but is needed again. Also set number of optimization steps to 0." <<
COLOR_END << std::endl;
140 if (oldKeyPtr ==
nullptr) {
141 throw std::logic_error(
"GmsfUnaryExpressionAbsolut: Old keyframe was not active, but old key ptr is null.");
144 gtsam::Pose3 T_W_fixedFrameOld;
146 gtsam::SharedNoiseModel noiseModelPtr;
148 if (oldKeyPtr->getNumberStepsOptimized() > 0) {
149 T_W_fixedFrameOld = oldKeyPtr->getTransformationAfterOptimization();
150 Eigen::Matrix<double, 6, 6> oldKeyframeCovariance = oldKeyPtr->getCovarianceAfterOptimization();
152 noiseModelPtr = gtsam::noiseModel::Gaussian::Covariance(oldKeyframeCovariance);
154 REGULAR_COUT <<
" Old keyframe has been optimized before. Adding previous optimization outcome again to online graph." << std::endl;
158 T_W_fixedFrameOld = oldKeyPtr->getApproximateTransformationBeforeOptimization();
163 REGULAR_COUT <<
" Old keyframe has not been optimized before. Adding very initial guess that should have been added last time."
173 <<
" to the online graph, as it was not active anymore." <<
COLOR_END << std::endl;
178 gtsam::Pose3 T_fixedFrameOld_fixedFrame;
185 graphKey = gtsamDynamicExpressionKeys.get<gtsam::Pose3>().getTransformationKey<
'r'>(
191 introducedNewKeyframeDisplacement =
true;
192 T_fixedFrameOld_fixedFrame =
198 if (centerMeasurementsAtKeyframePositionBeforeAlignmentFlag && this->
measuresPosition() && !initialGuessSetExternally) {
204 gtsamDynamicExpressionKeys.get<gtsam::Pose3>()
206 .setApproximateTransformationBeforeOptimization(T_W_fixedFrame_initial);
211 if (introducedNewKeyframeDisplacement && !initialGuessSetExternally) {
212 T_W_fixedFrame_initial = T_W_fixedFrameOld_belief * T_fixedFrameOld_fixedFrame;
213 gtsamDynamicExpressionKeys.get<gtsam::Pose3>()
215 .setApproximateTransformationBeforeOptimization(T_W_fixedFrame_initial);
219 gtsam::Pose3_ exp_T_W_fixedFrame(graphKey.
key());
224 if (newGraphKeyAddedFlag) {
226 <<
", RPY (deg): " << T_W_fixedFrame_initial.rotation().rpy().transpose() * (180.0 / M_PI)
227 <<
", t (x, y, z): " << T_W_fixedFrame_initial.translation().transpose() << std::endl;
233 if (!introducedNewKeyframeDisplacement) {
236 graphKey.
key(), T_W_fixedFrame_initial,
240 <<
" to the online graph, as it is an entirely new keyframe." <<
COLOR_END << std::endl;
249 gtsam::SharedDiagonal noiseModelPtr =
254 REGULAR_COUT <<
GREEN_START <<
" Adding relative RANDOM WALK constraint from key " << gtsam::Symbol(oldGtsamKey) <<
" to key "
255 << gtsam::Symbol(graphKey.
key()) <<
": " << T_fixedFrameOld_fixedFrame <<
COLOR_END << std::endl;
260 gtsam::SharedConstrained noiseModelPtr =
265 REGULAR_COUT <<
GREEN_START <<
" Adding relative DETERMINISTIC constraint from key " << gtsam::Symbol(oldGtsamKey) <<
" to key "
266 << gtsam::Symbol(graphKey.
key()) <<
": " << T_fixedFrameOld_fixedFrame <<
COLOR_END << std::endl;