use std::collections::HashSet;
use nalgebra::{Point2, Point3};
use visloc_core::geometry::Pose;
use visloc_core::types::{Camera, LandmarkDescriptorStore, LandmarkId, VisualMap};
use visloc_vision::matching::DescriptorMatch;
use visloc_vision::pnp::Correspondence2D3D;
use crate::CorrespondenceSet;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BinaryMatchGates {
pub max_distance: u32,
pub ratio: Option<f32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackedDescriptors {
dimension: usize,
words: usize,
bits: Vec<u64>,
valid: Vec<bool>,
}
impl PackedDescriptors {
pub fn pack<D: AsRef<[f32]>>(descriptors: &[D], dimension: usize) -> Self {
let words = dimension.div_ceil(64);
let mut bits = vec![0_u64; words * descriptors.len()];
let mut valid = Vec::with_capacity(descriptors.len());
for (index, descriptor) in descriptors.iter().enumerate() {
let descriptor = descriptor.as_ref();
let ok = descriptor.len() == dimension;
valid.push(ok);
if !ok {
continue;
}
let packed = &mut bits[index * words..(index + 1) * words];
for (bit, value) in descriptor.iter().enumerate() {
if *value >= 0.5 {
packed[bit / 64] |= 1 << (bit % 64);
}
}
}
Self {
dimension,
words,
bits,
valid,
}
}
pub const fn dimension(&self) -> usize {
self.dimension
}
pub fn len(&self) -> usize {
self.valid.len()
}
pub fn is_empty(&self) -> bool {
self.valid.is_empty()
}
pub(crate) fn get(&self, index: usize) -> Option<&[u64]> {
self.valid
.get(index)
.copied()
.unwrap_or(false)
.then(|| &self.bits[index * self.words..(index + 1) * self.words])
}
fn iter(&self) -> impl Iterator<Item = (usize, &[u64])> {
self.bits
.chunks_exact(self.words.max(1))
.zip(&self.valid)
.enumerate()
.filter_map(|(index, (bits, valid))| valid.then_some((index, bits)))
}
}
fn hamming(a: &[u64], b: &[u64]) -> u32 {
if let (Ok(a), Ok(b)) = (<&[u64; 4]>::try_from(a), <&[u64; 4]>::try_from(b)) {
return (a[0] ^ b[0]).count_ones()
+ (a[1] ^ b[1]).count_ones()
+ (a[2] ^ b[2]).count_ones()
+ (a[3] ^ b[3]).count_ones();
}
a.iter().zip(b).map(|(a, b)| (a ^ b).count_ones()).sum()
}
#[derive(Clone, Copy)]
struct Nearest {
best: (u32, usize),
second: u32,
}
impl Nearest {
const fn new() -> Self {
Self {
best: (u32::MAX, 0),
second: u32::MAX,
}
}
fn offer(&mut self, distance: u32, index: usize) {
if distance < self.best.0 {
if index != self.best.1 {
self.second = self.best.0;
}
self.best = (distance, index);
} else if distance < self.second && index != self.best.1 {
self.second = distance;
}
}
fn accept(self, gates: BinaryMatchGates) -> Option<(u32, usize, Option<f32>)> {
let (distance, index) = self.best;
if distance > gates.max_distance {
return None;
}
let second = (self.second != u32::MAX).then_some(self.second as f32);
if let (Some(ratio), Some(second)) = (gates.ratio, second) {
if distance as f32 >= ratio * second {
return None;
}
}
Some((distance, index, second))
}
}
const DISTINCT_CANDIDATES: usize = 8;
#[derive(Clone, Copy)]
struct NearestDistinct {
candidates: [(u32, usize); DISTINCT_CANDIDATES],
len: usize,
}
impl NearestDistinct {
const fn new() -> Self {
Self {
candidates: [(u32::MAX, 0); DISTINCT_CANDIDATES],
len: 0,
}
}
fn offer(&mut self, distance: u32, index: usize) {
if self.len == DISTINCT_CANDIDATES && distance >= self.candidates[self.len - 1].0 {
return;
}
let at = self.candidates[..self.len].partition_point(|(kept, _)| *kept <= distance);
let end = self.len.min(DISTINCT_CANDIDATES - 1);
self.candidates.copy_within(at..end, at + 1);
self.candidates[at] = (distance, index);
self.len = (self.len + 1).min(DISTINCT_CANDIDATES);
}
fn collapse(self, positions: &[Point3<f64>], radius: f64) -> Nearest {
if self.len == 0 {
return Nearest::new();
}
let (distance, index) = self.candidates[0];
let best = positions[index];
let second = self.candidates[1..self.len]
.iter()
.find(|(_, other)| (positions[*other] - best).norm() > radius)
.map(|(distance, _)| *distance)
.or_else(|| (self.len == DISTINCT_CANDIDATES).then(|| self.candidates[self.len - 1].0))
.unwrap_or(u32::MAX);
Nearest {
best: (distance, index),
second,
}
}
}
fn descriptor_match(
query_index: usize,
train_index: usize,
distance: u32,
second: Option<f32>,
) -> DescriptorMatch {
DescriptorMatch {
query_index,
train_index,
distance: distance as f32,
second_best_distance: second,
ratio: second.map(|second| distance as f32 / second.max(1.0)),
confidence: None,
}
}
#[derive(Debug, Clone)]
pub struct BinaryLandmarkIndex {
landmark_ids: Vec<LandmarkId>,
positions: Vec<Point3<f64>>,
descriptors: PackedDescriptors,
extra: PackedDescriptors,
extra_offsets: Vec<usize>,
extra_owner: Vec<usize>,
duplicate_radius_m: Option<f64>,
}
impl BinaryLandmarkIndex {
pub fn new(map: &VisualMap, store: &LandmarkDescriptorStore) -> Self {
let paired = store.ordered_landmark_descriptors(map);
let dimension = paired.first().map_or(0, |(_, descriptor)| descriptor.len());
let descriptors = paired
.iter()
.map(|(_, descriptor)| *descriptor)
.collect::<Vec<_>>();
Self {
landmark_ids: paired.iter().map(|(landmark, _)| landmark.id).collect(),
positions: paired
.iter()
.map(|(landmark, _)| landmark.position)
.collect(),
descriptors: PackedDescriptors::pack(&descriptors, dimension),
extra: PackedDescriptors::pack::<&[f32]>(&[], dimension),
extra_offsets: vec![0; paired.len() + 1],
extra_owner: Vec::new(),
duplicate_radius_m: None,
}
}
#[must_use]
pub fn with_extra_descriptors<D: AsRef<[f32]>>(
mut self,
rows: &[(LandmarkId, D)],
max_per_landmark: usize,
) -> Self {
let mut grouped: Vec<Vec<&[f32]>> = vec![Vec::new(); self.len()];
for (id, descriptor) in rows {
if let Some(index) = self.landmark_index(*id) {
if grouped[index].len() < max_per_landmark {
grouped[index].push(descriptor.as_ref());
}
}
}
let mut offsets = Vec::with_capacity(self.len() + 1);
let mut owner = Vec::new();
let mut flat = Vec::new();
offsets.push(0);
for (index, descriptors) in grouped.into_iter().enumerate() {
owner.extend(std::iter::repeat_n(index, descriptors.len()));
flat.extend(descriptors);
offsets.push(flat.len());
}
self.extra = PackedDescriptors::pack(&flat, self.dimension());
self.extra_offsets = offsets;
self.extra_owner = owner;
self
}
pub fn extra_descriptor_count(&self) -> usize {
self.extra.len()
}
fn rows(&self) -> impl Iterator<Item = (usize, &[u64])> {
self.descriptors.iter().chain(
self.extra
.iter()
.map(|(row, bits)| (self.extra_owner[row], bits)),
)
}
fn landmark_rows(&self, index: usize) -> impl Iterator<Item = &[u64]> {
let range = self.extra_offsets.get(index).copied().unwrap_or(0)
..self.extra_offsets.get(index + 1).copied().unwrap_or(0);
self.descriptors
.get(index)
.into_iter()
.chain(range.filter_map(|row| self.extra.get(row)))
}
#[must_use]
pub fn with_duplicate_radius(mut self, radius_m: f64) -> Self {
self.duplicate_radius_m = (radius_m > 0.0).then_some(radius_m);
self
}
pub fn len(&self) -> usize {
self.landmark_ids.len()
}
pub fn is_empty(&self) -> bool {
self.landmark_ids.is_empty()
}
pub const fn dimension(&self) -> usize {
self.descriptors.dimension
}
pub fn pack_query<D: AsRef<[f32]>>(&self, descriptors: &[D]) -> PackedDescriptors {
PackedDescriptors::pack(descriptors, self.dimension())
}
pub fn match_all(
&self,
keypoints: &[Point2<f64>],
query: &PackedDescriptors,
gates: BinaryMatchGates,
threads: usize,
) -> CorrespondenceSet {
let count = keypoints.len().min(query.len());
let match_range = |range: std::ops::Range<usize>| {
range
.filter_map(|query_index| {
let bits = query.get(query_index)?;
let nearest = self.search(bits, self.rows());
let (distance, landmark, second) = nearest.accept(gates)?;
Some(descriptor_match(query_index, landmark, distance, second))
})
.collect::<Vec<_>>()
};
let threads = threads.clamp(1, count.max(1));
let matches = if threads == 1 {
match_range(0..count)
} else {
let chunk = count.div_ceil(threads);
std::thread::scope(|scope| {
let handles = (0..count)
.step_by(chunk)
.map(|start| {
let match_range = &match_range;
scope.spawn(move || match_range(start..(start + chunk).min(count)))
})
.collect::<Vec<_>>();
handles
.into_iter()
.flat_map(|handle| handle.join().expect("matcher thread panicked"))
.collect()
})
};
self.correspondence_set(keypoints, matches, self.len())
}
pub fn match_among(
&self,
keypoints: &[Point2<f64>],
query: &PackedDescriptors,
gates: BinaryMatchGates,
landmarks: &[usize],
) -> CorrespondenceSet {
let count = keypoints.len().min(query.len());
let matches = (0..count)
.filter_map(|query_index| {
let bits = query.get(query_index)?;
let candidates = landmarks.iter().flat_map(|&landmark| {
self.landmark_rows(landmark)
.map(move |train| (landmark, train))
});
let (distance, landmark, second) = self.search(bits, candidates).accept(gates)?;
Some(descriptor_match(query_index, landmark, distance, second))
})
.collect();
self.correspondence_set(keypoints, matches, landmarks.len())
}
fn search<'a>(
&self,
bits: &[u64],
candidates: impl Iterator<Item = (usize, &'a [u64])>,
) -> Nearest {
match self.duplicate_radius_m {
None => {
let mut nearest = Nearest::new();
for (landmark, train) in candidates {
nearest.offer(hamming(bits, train), landmark);
}
nearest
}
Some(radius) => {
let mut nearest = NearestDistinct::new();
for (landmark, train) in candidates {
nearest.offer(hamming(bits, train), landmark);
}
nearest.collapse(&self.positions, radius)
}
}
}
pub fn landmark_index(&self, id: LandmarkId) -> Option<usize> {
self.landmark_ids.binary_search(&id).ok()
}
pub(crate) fn landmark_descriptor(&self, index: usize) -> Option<&[u64]> {
self.descriptors.get(index)
}
pub fn match_projected(
&self,
camera: &Camera,
pose_prior: &Pose,
keypoints: &[Point2<f64>],
query: &PackedDescriptors,
radius_px: f64,
gates: BinaryMatchGates,
) -> CorrespondenceSet {
let count = keypoints.len().min(query.len());
let grid = KeypointGrid::new(&keypoints[..count], radius_px);
let (width, height) = (f64::from(camera.width), f64::from(camera.height));
let radius_squared = radius_px * radius_px;
let mut candidate_count = 0;
let mut proposals = Vec::new();
for (landmark, position) in self.positions.iter().enumerate() {
if self.landmark_rows(landmark).next().is_none() {
continue;
}
let point_camera = pose_prior.transform_world_point(position);
if point_camera.z <= 0.0 {
continue;
}
let Some(projected) = camera.project(&point_camera) else {
continue;
};
if projected.x < 0.0
|| projected.y < 0.0
|| projected.x >= width
|| projected.y >= height
{
continue;
}
candidate_count += 1;
let mut nearest = Nearest::new();
for keypoint in grid.within(keypoints, projected, radius_squared) {
if let Some(bits) = query.get(keypoint) {
let distance = self
.landmark_rows(landmark)
.map(|train| hamming(train, bits))
.min()
.unwrap_or(u32::MAX);
nearest.offer(distance, keypoint);
}
}
if let Some((distance, keypoint, second)) = nearest.accept(gates) {
proposals.push(descriptor_match(keypoint, landmark, distance, second));
}
}
proposals.sort_by(|a, b| {
a.distance
.total_cmp(&b.distance)
.then_with(|| a.train_index.cmp(&b.train_index))
.then_with(|| a.query_index.cmp(&b.query_index))
});
let mut used_keypoints = HashSet::new();
let mut used_landmarks = HashSet::new();
proposals.retain(|m| {
used_keypoints.insert(m.query_index) && used_landmarks.insert(m.train_index)
});
self.correspondence_set(keypoints, proposals, candidate_count)
}
fn correspondence_set(
&self,
keypoints: &[Point2<f64>],
matches: Vec<DescriptorMatch>,
candidate_landmark_count: usize,
) -> CorrespondenceSet {
CorrespondenceSet {
correspondences: matches
.iter()
.map(|m| Correspondence2D3D {
point2d: keypoints[m.query_index],
point3d: self.positions[m.train_index],
confidence: None,
})
.collect(),
query_indices: matches.iter().map(|m| m.query_index).collect(),
landmark_ids: matches
.iter()
.map(|m| self.landmark_ids[m.train_index])
.collect(),
candidate_landmark_count,
match_count: matches.len(),
descriptor_matches: matches,
}
}
}
struct KeypointGrid {
origin: Point2<f64>,
cell: f64,
columns: isize,
rows: isize,
cells: Vec<Vec<usize>>,
}
impl KeypointGrid {
fn new(keypoints: &[Point2<f64>], radius_px: f64) -> Self {
let finite = || {
keypoints
.iter()
.filter(|p| p.x.is_finite() && p.y.is_finite())
};
let fold = |init: f64, pick: fn(&Point2<f64>) -> f64, f: fn(f64, f64) -> f64| {
finite().map(pick).fold(init, f)
};
let origin = Point2::new(
fold(f64::INFINITY, |p| p.x, f64::min).min(0.0),
fold(f64::INFINITY, |p| p.y, f64::min).min(0.0),
);
let extent = Point2::new(
fold(0.0, |p| p.x, f64::max) - origin.x,
fold(0.0, |p| p.y, f64::max) - origin.y,
);
let cell = radius_px.max(1.0).max(extent.x.max(extent.y) / 1024.0);
let columns = (extent.x / cell) as isize + 1;
let rows = (extent.y / cell) as isize + 1;
let mut grid = Self {
origin,
cell,
columns,
rows,
cells: vec![Vec::new(); (columns * rows) as usize],
};
for (index, keypoint) in keypoints.iter().enumerate() {
if keypoint.x.is_finite() && keypoint.y.is_finite() {
let (column, row) = grid.cell_of(keypoint);
grid.cells[(row * columns + column) as usize].push(index);
}
}
grid
}
fn cell_of(&self, point: &Point2<f64>) -> (isize, isize) {
(
((point.x - self.origin.x) / self.cell).floor() as isize,
((point.y - self.origin.y) / self.cell).floor() as isize,
)
}
fn within(
&self,
keypoints: &[Point2<f64>],
center: Point2<f64>,
radius_squared: f64,
) -> Vec<usize> {
let (column, row) = self.cell_of(¢er);
let mut found = Vec::new();
for r in (row - 1).max(0)..=(row + 1).min(self.rows - 1) {
for c in (column - 1).max(0)..=(column + 1).min(self.columns - 1) {
for &index in &self.cells[(r * self.columns + c) as usize] {
let d = keypoints[index] - center;
if d.x * d.x + d.y * d.y <= radius_squared {
found.push(index);
}
}
}
}
found.sort_unstable();
found
}
}
#[cfg(test)]
mod tests {
use nalgebra::{UnitQuaternion, Vector3};
use visloc_core::types::Landmark;
use visloc_vision::matching::HammingMatcher;
use super::*;
use crate::{CorrespondenceBuilder, ProjectionCorrespondenceBuilder};
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn unit(&mut self) -> f64 {
(self.next() % 1_000_000) as f64 / 1_000_000.0
}
fn bits(&mut self) -> Vec<f32> {
(0..256).map(|_| (self.next() & 1) as f32).collect()
}
}
fn flip(descriptor: &[f32], count: usize, rng: &mut Rng) -> Vec<f32> {
let mut flipped = descriptor.to_vec();
for _ in 0..count {
let bit = (rng.next() % 256) as usize;
flipped[bit] = 1.0 - flipped[bit];
}
flipped
}
struct Scene {
map: VisualMap,
store: LandmarkDescriptorStore,
camera: Camera,
prior: Pose,
keypoints: Vec<Point2<f64>>,
descriptors: Vec<Vec<f32>>,
}
fn scene() -> Scene {
let mut rng = Rng(0x2545_f491_4f6c_dd1d);
let camera = Camera::pinhole(1, 640, 480, 400.0, 400.0, 320.0, 240.0);
let truth = Pose::identity();
let prior = Pose::from_world_to_camera(
UnitQuaternion::from_euler_angles(0.004, -0.003, 0.002),
Vector3::new(0.01, -0.02, 0.0),
);
let mut map = VisualMap::new();
let mut store = LandmarkDescriptorStore::new();
let mut keypoints = Vec::new();
let mut descriptors = Vec::new();
let mut shared = rng.bits();
for id in 0..400_u64 {
let position = Point3::new(
rng.unit() * 8.0 - 4.0,
rng.unit() * 6.0 - 3.0,
if id % 10 == 0 {
-2.0
} else {
1.5 + rng.unit() * 6.0
},
);
if id % 37 == 0 {
shared = rng.bits();
}
let descriptor = if id % 7 == 0 {
shared.clone()
} else {
rng.bits()
};
map.landmarks.insert(id, Landmark::new(id, position));
store.insert(id, descriptor.clone());
if let Some(pixel) = camera.project(&truth.transform_world_point(&position)) {
if position.z > 0.0 && id % 5 != 0 {
keypoints.push(pixel + nalgebra::Vector2::new(rng.unit(), rng.unit()));
descriptors.push(flip(&descriptor, (rng.next() % 30) as usize, &mut rng));
}
}
}
for _ in 0..150 {
keypoints.push(Point2::new(rng.unit() * 640.0, rng.unit() * 480.0));
descriptors.push(rng.bits());
}
let mut order = (0..keypoints.len()).collect::<Vec<_>>();
order.sort_by_key(|i| (i * 7919) % keypoints.len());
Scene {
map,
store,
camera,
prior,
keypoints: order.iter().map(|&i| keypoints[i]).collect(),
descriptors: order.iter().map(|&i| descriptors[i].clone()).collect(),
}
}
fn query(scene: &Scene) -> visloc_core::types::QueryImage {
visloc_core::types::QueryImage {
camera: scene.camera.clone(),
keypoints: scene.keypoints.clone(),
descriptors: scene.descriptors.clone(),
}
}
const GATES: BinaryMatchGates = BinaryMatchGates {
max_distance: 70,
ratio: Some(0.8),
};
fn hamming_matcher() -> HammingMatcher {
HammingMatcher {
max_distance: GATES.max_distance,
ratio: GATES.ratio,
}
}
#[test]
fn global_matching_equals_the_correspondence_builder() {
let scene = scene();
let index = BinaryLandmarkIndex::new(&scene.map, &scene.store);
assert_eq!(index.len(), 400);
let expected = CorrespondenceBuilder::new(hamming_matcher())
.build(&query(&scene), &scene.map, &scene.store)
.unwrap();
assert!(expected.match_count > 100, "{}", expected.match_count);
let packed = index.pack_query(&scene.descriptors);
for threads in [1, 3] {
let actual = index.match_all(&scene.keypoints, &packed, GATES, threads);
assert_eq!(actual, expected, "threads={threads}");
}
}
#[test]
fn projection_matching_equals_the_projection_builder() {
let scene = scene();
let index = BinaryLandmarkIndex::new(&scene.map, &scene.store);
let packed = index.pack_query(&scene.descriptors);
for radius in [4.0, 12.0, 40.0] {
let expected = ProjectionCorrespondenceBuilder::new(hamming_matcher())
.build_with_pose_prior(
&query(&scene),
&scene.map,
&scene.store,
&scene.prior,
radius,
)
.unwrap();
let actual = index.match_projected(
&scene.camera,
&scene.prior,
&scene.keypoints,
&packed,
radius,
GATES,
);
assert!(
actual.match_count > 50,
"radius {radius}: {}",
actual.match_count
);
assert_eq!(expected.candidate_landmark_count, 400);
assert!(actual.candidate_landmark_count < 400);
assert_eq!(
CorrespondenceSet {
candidate_landmark_count: 0,
..actual
},
CorrespondenceSet {
candidate_landmark_count: 0,
..expected
},
"radius {radius}"
);
}
}
#[test]
fn mismatched_lengths_never_match() {
let mut scene = scene();
scene.descriptors[0] = vec![1.0; 8];
let index = BinaryLandmarkIndex::new(&scene.map, &scene.store);
let packed = index.pack_query(&scene.descriptors);
let all = index.match_all(&scene.keypoints, &packed, GATES, 1);
assert!(all.query_indices.iter().all(|&q| q != 0));
let empty = index.match_all(&[], &index.pack_query::<Vec<f32>>(&[]), GATES, 4);
assert_eq!(empty.match_count, 0);
}
#[test]
fn a_radius_below_the_landmark_spacing_changes_nothing() {
let scene = scene();
let index = BinaryLandmarkIndex::new(&scene.map, &scene.store);
let packed = index.pack_query(&scene.descriptors);
let plain = index.match_all(&scene.keypoints, &packed, GATES, 1);
let index = index.with_duplicate_radius(1e-9);
for threads in [1, 3] {
assert_eq!(
index.match_all(&scene.keypoints, &packed, GATES, threads),
plain
);
}
}
fn duplicates(copies: usize) -> (BinaryLandmarkIndex, Vec<Point2<f64>>, PackedDescriptors) {
let mut rng = Rng(7);
let mut map = VisualMap::new();
let mut store = LandmarkDescriptorStore::new();
let point = |x: f64| Point3::new(x, 0.0, 3.0);
let original = rng.bits();
let twin = rng.bits();
let lone = rng.bits();
let mut add = |id: u64, position: Point3<f64>, descriptor: Vec<f32>| {
map.landmarks.insert(id, Landmark::new(id, position));
store.insert(id, descriptor);
};
add(0, point(0.0), original.clone());
add(1, point(5.0), twin.clone());
add(2, point(-5.0), twin.clone());
add(3, point(2.0), lone.clone());
for copy in 0..copies {
let jitter = 0.002 * copy as f64;
add(100 + copy as u64, point(jitter), original.clone());
}
let index = BinaryLandmarkIndex::new(&map, &store);
let query = [original, twin, lone]
.iter()
.map(|descriptor| flip(descriptor, 8, &mut rng))
.collect::<Vec<_>>();
let packed = index.pack_query(&query);
let keypoints = vec![Point2::new(10.0, 10.0); 3];
(index, keypoints, packed)
}
fn matched_queries(set: &CorrespondenceSet) -> Vec<usize> {
set.query_indices.clone()
}
#[test]
fn duplicates_of_a_point_pass_the_ratio_test_within_the_radius() {
let (index, keypoints, packed) = duplicates(1);
let plain = index.match_all(&keypoints, &packed, GATES, 1);
assert_eq!(matched_queries(&plain), [2]);
let index = index.with_duplicate_radius(0.05);
let tolerant = index.match_all(&keypoints, &packed, GATES, 1);
assert_eq!(matched_queries(&tolerant), [0, 2]);
assert_eq!(tolerant.landmark_ids, [0, 3]);
}
#[test]
fn a_crowd_of_duplicates_falls_back_to_a_lower_bound() {
let (index, keypoints, packed) = duplicates(DISTINCT_CANDIDATES + 2);
let index = index.with_duplicate_radius(0.05);
let tolerant = index.match_all(&keypoints, &packed, GATES, 1);
assert_eq!(matched_queries(&tolerant), [2]);
}
fn flip_first(descriptor: &[f32], count: usize) -> Vec<f32> {
let mut flipped = descriptor.to_vec();
for value in flipped.iter_mut().take(count) {
*value = 1.0 - *value;
}
flipped
}
#[test]
fn extra_descriptors_match_their_landmark_and_never_compete_with_it() {
let mut rng = Rng(0x9e37_79b9_7f4a_7c15);
let camera = Camera::pinhole(1, 640, 480, 400.0, 400.0, 320.0, 240.0);
let mut map = VisualMap::new();
let mut store = LandmarkDescriptorStore::new();
let (a, b, c) = (rng.bits(), rng.bits(), rng.bits());
let near_a = flip_first(&a, 2);
for (id, position, descriptor) in [
(1_u64, Point3::new(0.0, 0.0, 4.0), &a),
(2, Point3::new(0.5, 0.0, 4.0), &c),
] {
map.landmarks.insert(id, Landmark::new(id, position));
store.insert(id, descriptor.clone());
}
let extra = vec![(1_u64, b.clone()), (1, near_a), (9, rng.bits())];
let plain = BinaryLandmarkIndex::new(&map, &store);
let index = plain.clone().with_extra_descriptors(&extra, 4);
assert_eq!(index.extra_descriptor_count(), 2);
assert_eq!(
plain
.clone()
.with_extra_descriptors(&extra, 1)
.extra_descriptor_count(),
1
);
let keypoints = vec![Point2::new(320.0, 240.0), Point2::new(370.0, 240.0)];
let descriptors = vec![flip_first(&b, 5), flip_first(&a, 1)];
let query = index.pack_query(&descriptors);
let ids = |set: &CorrespondenceSet| set.landmark_ids.clone();
assert_eq!(
ids(&plain.match_all(&keypoints[..1], &query, GATES, 1)),
Vec::<u64>::new()
);
let matched = index.match_all(&keypoints, &query, GATES, 1);
assert_eq!(ids(&matched), vec![1, 1]);
assert_eq!(matched.descriptor_matches[0].distance, 5.0);
assert_eq!(matched.descriptor_matches[1].distance, 1.0);
let second = matched.descriptor_matches[1].second_best_distance.unwrap();
assert!(second > 70.0, "{second}");
assert_eq!(
ids(&index.match_among(&keypoints, &query, GATES, &[0, 1])),
vec![1, 1]
);
let pose = Pose::identity();
let projected = index.match_projected(&camera, &pose, &keypoints, &query, 30.0, GATES);
assert_eq!(ids(&projected), vec![1]);
assert_eq!(projected.query_indices, vec![0]);
assert!(plain
.match_projected(
&camera,
&pose,
&keypoints,
&plain.pack_query(&descriptors),
30.0,
GATES
)
.landmark_ids
.is_empty());
}
}