Overview
TheLoopClosing class detects loops in the trajectory and performs place recognition. When a loop is detected, it performs pose graph optimization and full bundle adjustment to correct accumulated drift. It also handles map merging in multi-map scenarios.
Constructor
Parameters
pAtlas: Pointer to the Atlas (multi-map manager)pDB: Pointer to the KeyFrame database for place recognitionpVoc: Pointer to ORB vocabulary for bag-of-wordsbFixScale: True if scale should be fixed (false for monocular)bActiveLC: True to enable loop closing, false to disable
Type Definitions
ConsistentGroup
KeyFrameAndPose
Thread Control Methods
Run
RequestFinish
isFinished
Thread Communication
SetTracker
pTracker: Pointer to Tracking object
SetLocalMapper
pLocalMapper: Pointer to LocalMapping object
Keyframe Processing
InsertKeyFrame
pKF: Pointer to the keyframe to process
State Management
RequestReset
RequestResetActiveMap
pMap: Pointer to the map to reset
Global Bundle Adjustment
RunGlobalBundleAdjustment
pActiveMap: Pointer to the map to optimizenLoopKF: ID of the loop closure keyframe
isRunningGBA
isFinishedGBA
Public Member Variables
Viewer
Usage Example
Loop Detection Pipeline
The loop closing thread follows this processing pipeline:- Wait for Keyframes: Check the queue for new keyframes
- Detect Loop Candidates:
- Query KeyFrame database using bag-of-words
- Check temporal and geometric consistency
- Compute Sim3:
- Estimate relative Sim3 transformation
- Verify with RANSAC and feature matching
- Loop Fusion:
- Fuse duplicate map points
- Update covisibility graph
- Pose Graph Optimization:
- Optimize poses to close the loop
- Propagate corrections through covisibility graph
- Global Bundle Adjustment:
- Optional full bundle adjustment for maximum accuracy
- Runs in separate thread to avoid blocking
Place Recognition Methods
The loop detection uses two main approaches:NewDetectCommonRegions (Protected)
- Loop closures: Revisiting a previous location in the same map
- Map merging: Detecting overlap between different maps
Detection Steps
- Bag-of-Words Query: Find keyframe candidates using visual similarity
- Consistency Check: Verify temporal consistency across multiple frames
- Geometric Verification: Compute and verify Sim3 transformation
- Map Point Matching: Match and project map points between regions
Map Merging
When multiple maps exist in the Atlas, loop closing can detect overlaps and merge maps:MergeLocal (Protected)
- Align the two maps using Sim3 transformation
- Fuse duplicate map points and keyframes
- Perform welding bundle adjustment
- Update Atlas with merged map
MergeLocal2 (Protected)
Sim3 Computation
The class uses Sim3 (3D similarity transformation) which includes:- Rotation: 3D rotation matrix
- Translation: 3D translation vector
- Scale: Scale factor (important for monocular SLAM)
DetectAndReffineSim3FromLastKF (Protected)
Optimization Methods
CorrectLoop (Protected)
- Stops local mapping
- Computes corrected poses for all keyframes
- Fuses map points
- Optimizes the pose graph (Essential Graph)
- Launches global bundle adjustment
- Resumes local mapping
SearchAndFuse (Protected)
Thread Safety
The LoopClosing class uses several mutexes for thread-safe operation:mMutexLoopQueue: Protects the keyframe queuemMutexGBA: Protects global BA statemMutexReset: Protects reset requestsmMutexFinish: Protects finish requests
Configuration
Scale Fixing
- Fixed Scale (
bFixScale = true): For stereo/RGB-D where scale is observable - Free Scale (
bFixScale = false): For monocular where scale is estimated
Loop Closing Activation
- No loops are expected
- Computational resources are limited
- Real-time performance is critical
Performance Considerations
Covisibility Consistency
Global Bundle Adjustment
Global BA is computationally expensive:- Runs in a separate thread to avoid blocking
- Can be aborted if new loops are detected
- Optional - system continues working without waiting for completion
Related Classes
- System - Main SLAM system interface
- Tracking - Provides keyframes for loop detection
- LocalMapping - Coordinates with loop closing for map optimization