use std::collections::{BTreeSet, HashMap};
use visloc_core::types::VisualMap;
use crate::binary_index::{BinaryLandmarkIndex, PackedDescriptors};
const WORD_BITS: [usize; 32] = [
170, 215, 41, 38, 96, 172, 52, 1, 182, 89, 234, 98, 217, 73, 195, 113, 161, 247, 24, 75, 7,
232, 49, 196, 144, 69, 3, 86, 94, 201, 107, 251,
];
fn word(descriptor: &[u64], bits: u8) -> u32 {
let mut word = 0_u32;
for (index, &bit) in WORD_BITS.iter().take(usize::from(bits.min(32))).enumerate() {
if descriptor
.get(bit / 64)
.is_some_and(|w| w & (1_u64 << (bit % 64)) != 0)
{
word |= 1 << index;
}
}
word
}
fn bag_of_words<'a>(descriptors: impl Iterator<Item = &'a [u64]>, bits: u8) -> HashMap<u32, f32> {
let mut counts = HashMap::<u32, f32>::new();
let mut total = 0.0_f32;
for descriptor in descriptors {
*counts.entry(word(descriptor, bits)).or_default() += 1.0;
total += 1.0;
}
for weight in counts.values_mut() {
*weight /= total;
}
counts
}
#[derive(Debug, Clone)]
pub struct KeyframeRetrieval {
bits: u8,
keyframe_ids: Vec<u64>,
landmarks: Vec<Vec<usize>>,
inverted: HashMap<u32, Vec<(u32, f32)>>,
}
impl KeyframeRetrieval {
pub fn new(map: &VisualMap, index: &BinaryLandmarkIndex, bits: u8) -> Self {
let mut keyframes = map.keyframes.iter().collect::<Vec<_>>();
keyframes.sort_by_key(|(id, _)| **id);
let mut retrieval = Self {
bits,
keyframe_ids: Vec::new(),
landmarks: Vec::new(),
inverted: HashMap::new(),
};
for (&id, keyframe) in keyframes {
let landmarks = keyframe
.observations
.iter()
.filter_map(|observation| index.landmark_index(observation.landmark_id))
.collect::<BTreeSet<_>>()
.into_iter()
.collect::<Vec<_>>();
if landmarks.is_empty() {
continue;
}
let words = bag_of_words(
landmarks
.iter()
.filter_map(|&landmark| index.landmark_descriptor(landmark)),
bits,
);
let keyframe_index = retrieval.keyframe_ids.len() as u32;
for (word, weight) in words {
retrieval
.inverted
.entry(word)
.or_default()
.push((keyframe_index, weight));
}
retrieval.keyframe_ids.push(id);
retrieval.landmarks.push(landmarks);
}
retrieval
}
pub fn len(&self) -> usize {
self.keyframe_ids.len()
}
pub fn is_empty(&self) -> bool {
self.keyframe_ids.is_empty()
}
fn ranked(&self, query: &PackedDescriptors, top_k: usize) -> Vec<(usize, f32)> {
let words = bag_of_words(
(0..query.len()).filter_map(|index| query.get(index)),
self.bits,
);
let mut scores = vec![0.0_f32; self.keyframe_ids.len()];
for (word, query_weight) in words {
for &(keyframe, weight) in self.inverted.get(&word).into_iter().flatten() {
scores[keyframe as usize] += query_weight.min(weight);
}
}
let mut ranked = scores
.into_iter()
.enumerate()
.filter(|(_, score)| *score > 0.0)
.collect::<Vec<_>>();
ranked.sort_by(|a, b| b.1.total_cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
ranked.truncate(top_k);
ranked
}
pub fn query(&self, query: &PackedDescriptors, top_k: usize) -> Vec<(u64, f32)> {
self.ranked(query, top_k)
.into_iter()
.map(|(keyframe, score)| (self.keyframe_ids[keyframe], score))
.collect()
}
pub fn candidate_landmarks(&self, query: &PackedDescriptors, top_k: usize) -> Vec<usize> {
self.ranked(query, top_k)
.into_iter()
.flat_map(|(keyframe, _)| self.landmarks[keyframe].iter().copied())
.collect::<BTreeSet<_>>()
.into_iter()
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::binary_index::BinaryMatchGates;
use nalgebra::{Point2, Point3};
use visloc_core::geometry::Pose;
use visloc_core::types::{Frame, Keyframe, Landmark, LandmarkDescriptorStore, Observation};
fn descriptor(seed: u64) -> Vec<f32> {
let mut state = (seed + 1).wrapping_mul(0x9e37_79b9_7f4a_7c15);
(0..256)
.map(|_| {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
f32::from(u8::from(state & 1 == 1))
})
.collect()
}
fn map() -> (VisualMap, LandmarkDescriptorStore) {
let mut map = VisualMap::new();
let mut store = LandmarkDescriptorStore::new();
for keyframe in 0..3_u64 {
let mut observations = Vec::new();
for point in 0..40_u64 {
let id = keyframe * 100 + point;
map.landmarks.insert(
id,
Landmark {
id,
position: Point3::new(keyframe as f64, point as f64, 5.0),
descriptor: None,
observations: Vec::new(),
},
);
store.insert(id, descriptor(id));
observations.push(Observation {
frame_id: keyframe,
landmark_id: id,
keypoint_index: point as usize,
xy: Point2::new(point as f64, 0.0),
});
}
map.keyframes.insert(
keyframe,
Keyframe {
frame: Frame {
id: keyframe,
camera_id: 1,
keypoints: Vec::new(),
descriptors: Vec::new(),
pose: Some(Pose::identity()),
},
observations,
},
);
}
(map, store)
}
#[test]
fn a_query_retrieves_the_keyframe_it_shares_landmarks_with() {
let (map, store) = map();
let index = BinaryLandmarkIndex::new(&map, &store);
let retrieval = KeyframeRetrieval::new(&map, &index, 8);
assert_eq!(retrieval.len(), 3);
let query = (0..30)
.map(|point| descriptor(100 + point))
.collect::<Vec<_>>();
let packed = index.pack_query(&query);
let ranked = retrieval.query(&packed, 3);
assert_eq!(ranked[0].0, 1, "{ranked:?}");
assert!(ranked.get(1).is_none_or(|second| second.1 < ranked[0].1));
let landmarks = retrieval.candidate_landmarks(&packed, 1);
assert_eq!(landmarks.len(), 40);
let keypoints = (0..30)
.map(|i| Point2::new(i as f64, 0.0))
.collect::<Vec<_>>();
let gates = BinaryMatchGates {
max_distance: 10,
ratio: Some(0.8),
};
let set = index.match_among(&keypoints, &packed, gates, &landmarks);
assert_eq!(set.match_count, 30);
assert_eq!(set.candidate_landmark_count, 40);
assert!(set.landmark_ids.iter().all(|id| (100..140).contains(id)));
let all = index.match_all(&keypoints, &packed, gates, 1);
assert_eq!(all.landmark_ids, set.landmark_ids);
}
#[test]
fn landmarks_outside_the_index_and_empty_keyframes_are_skipped() {
let (mut map, store) = map();
map.keyframes.insert(
9,
Keyframe {
frame: Frame {
id: 9,
camera_id: 1,
keypoints: Vec::new(),
descriptors: Vec::new(),
pose: Some(Pose::identity()),
},
observations: vec![Observation {
frame_id: 9,
landmark_id: 12_345,
keypoint_index: 0,
xy: Point2::new(0.0, 0.0),
}],
},
);
let index = BinaryLandmarkIndex::new(&map, &store);
assert_eq!(index.landmark_index(12_345), None);
assert_eq!(index.landmark_index(100), Some(40));
let retrieval = KeyframeRetrieval::new(&map, &index, 16);
assert_eq!(retrieval.len(), 3);
let empty = index.pack_query::<Vec<f32>>(&[]);
assert!(retrieval.query(&empty, 5).is_empty());
assert!(retrieval.candidate_landmarks(&empty, 5).is_empty());
}
#[test]
fn words_take_the_permuted_bits() {
let mut descriptor = [0_u64; 4];
descriptor[170 / 64] |= 1 << (170 % 64); descriptor[0] |= 1 << 38; assert_eq!(word(&descriptor, 16), 0b1001);
assert_eq!(word(&descriptor, 1), 0b1);
assert_eq!(word(&descriptor, 0), 0);
}
}