Skip to content

Commit 3ef86b4

Browse files
authored
Merge pull request #139 from MOLAorg/fix/monothonic-kf-ids
Fix: ensure monothonic KF ids
2 parents 5e2a96c + b7be648 commit 3ef86b4

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

mola_metric_maps/include/mola_metric_maps/KeyframePointCloudMap.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ class KeyframePointCloudMap : public mrpt::maps::CMetricMap,
486486
/// KF ids evicted since the last call to drainEvictedKeyFrameIDs().
487487
std::vector<KeyFrameID> evicted_kf_ids_;
488488

489+
/// Monotonically increasing KF id allocator. Incremented on every
490+
/// insertion, never decremented on eviction, so ids are never reused.
491+
/// Reset to 0 on internal_clear(); restored to max-loaded-id+1 in
492+
/// serializeFrom().
493+
KeyFrameID next_free_kf_id_ = 0;
494+
489495
/// for cached_ and _keyframes
490496
mutable mrpt::containers::NonCopiableData<std::recursive_mutex> state_mtx_;
491497

@@ -546,15 +552,10 @@ class KeyframePointCloudMap : public mrpt::maps::CMetricMap,
546552
return {kf_idx, local_pt_idx};
547553
}
548554

549-
/// Return the next key-frame ID, which is the size of the map:
550-
[[nodiscard]] KeyFrameID nextFreeKeyFrameID() const
551-
{
552-
if (keyframes_.empty())
553-
{
554-
return 0; // First key-frame
555-
}
556-
return keyframes_.rbegin()->first + 1; // Next ID
557-
}
555+
/// Returns the next monotonically-allocated key-frame ID. The id is NOT
556+
/// consumed by this call; callers must use it as the key for the new KF
557+
/// and then bump `next_free_kf_id_`.
558+
[[nodiscard]] KeyFrameID nextFreeKeyFrameID() const { return next_free_kf_id_; }
558559
};
559560

560561
} // namespace mola

mola_metric_maps/src/KeyframePointCloudMap.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ void KeyframePointCloudMap::serializeFrom(mrpt::serialization::CArchive& in, uin
195195
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version);
196196
};
197197

198+
// Restore the monotonic id counter so future insertions don't collide
199+
// with already-loaded ids.
200+
next_free_kf_id_ = keyframes_.empty() ? 0 : (keyframes_.rbegin()->first + 1);
201+
198202
// cache reset:
199203
cached_.reset();
200204
}
@@ -545,7 +549,7 @@ void KeyframePointCloudMap::merge_with(
545549
continue;
546550
}
547551
auto [it, isNew] =
548-
keyframes_.emplace(nextFreeKeyFrameID(), creationOptions.k_correspondences_for_cov);
552+
keyframes_.emplace(next_free_kf_id_++, creationOptions.k_correspondences_for_cov);
549553
auto& new_kf = it->second;
550554

551555
// copy
@@ -1165,6 +1169,7 @@ void KeyframePointCloudMap::internal_clear()
11651169
keyframes_.clear();
11661170
last_inserted_kf_id_.reset();
11671171
evicted_kf_ids_.clear();
1172+
next_free_kf_id_ = 0;
11681173
cached_.reset();
11691174
}
11701175

@@ -1244,9 +1249,9 @@ bool KeyframePointCloudMap::internal_insertObservation(
12441249
{
12451250
ASSERT_(obsPC->pointcloud);
12461251

1247-
// Add KF:
1252+
// Add KF: allocate a fresh monotonic id (never reused, even after eviction).
12481253
auto [it, isNew] =
1249-
keyframes_.emplace(nextFreeKeyFrameID(), creationOptions.k_correspondences_for_cov);
1254+
keyframes_.emplace(next_free_kf_id_++, creationOptions.k_correspondences_for_cov);
12501255
auto& new_kf = it->second;
12511256

12521257
new_kf.timestamp = obs.timestamp;

0 commit comments

Comments
 (0)