visloc-localization 0.4.0

Stateless map-based visual-localization pipeline for visloc-rs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::collections::{HashMap, HashSet};

use nalgebra::Point3;
use visloc_core::geometry::Pose;
use visloc_core::types::{LandmarkDescriptorStore, LandmarkId, QueryImage, VisualMap};
use visloc_vision::matching::{DescriptorMatch, Matcher};
use visloc_vision::pnp::Correspondence2D3D;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CorrespondenceBuildError {
    QueryFeatureShapeMismatch {
        keypoint_count: usize,
        descriptor_count: usize,
    },
    NoCandidateLandmarks,
    NoMapDescriptors {
        candidate_landmark_count: usize,
    },
    NoDescriptorMatches {
        candidate_landmark_count: usize,
    },
    InvalidQueryLandmarkRatio,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CorrespondenceSet {
    pub correspondences: Vec<Correspondence2D3D>,
    pub query_indices: Vec<usize>,
    pub landmark_ids: Vec<LandmarkId>,
    pub candidate_landmark_count: usize,
    pub match_count: usize,
    pub descriptor_matches: Vec<DescriptorMatch>,
}

pub trait CandidateSelector {
    fn select_landmark_ids(&self, query: &QueryImage, map: &VisualMap) -> Vec<LandmarkId>;
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AllLandmarksSelector;

impl CandidateSelector for AllLandmarksSelector {
    fn select_landmark_ids(&self, _query: &QueryImage, map: &VisualMap) -> Vec<LandmarkId> {
        let mut landmark_ids = map.landmarks.keys().copied().collect::<Vec<_>>();
        landmark_ids.sort_unstable();
        landmark_ids
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FixedLandmarkSelector {
    landmark_ids: Vec<LandmarkId>,
}

impl FixedLandmarkSelector {
    pub fn new(mut landmark_ids: Vec<LandmarkId>) -> Self {
        landmark_ids.sort_unstable();
        landmark_ids.dedup();
        Self { landmark_ids }
    }
}

impl CandidateSelector for FixedLandmarkSelector {
    fn select_landmark_ids(&self, _query: &QueryImage, _map: &VisualMap) -> Vec<LandmarkId> {
        self.landmark_ids.clone()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct IntersectCandidateSelector<A, B> {
    pub first: A,
    pub second: B,
}

impl<A, B> IntersectCandidateSelector<A, B> {
    pub const fn new(first: A, second: B) -> Self {
        Self { first, second }
    }
}

impl<A, B> CandidateSelector for IntersectCandidateSelector<A, B>
where
    A: CandidateSelector,
    B: CandidateSelector,
{
    fn select_landmark_ids(&self, query: &QueryImage, map: &VisualMap) -> Vec<LandmarkId> {
        let first_ids = self
            .first
            .select_landmark_ids(query, map)
            .into_iter()
            .collect::<HashSet<_>>();
        let mut landmark_ids = self
            .second
            .select_landmark_ids(query, map)
            .into_iter()
            .filter(|landmark_id| first_ids.contains(landmark_id))
            .collect::<Vec<_>>();
        landmark_ids.sort_unstable();
        landmark_ids.dedup();
        landmark_ids
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct RadiusLandmarkSelector {
    pub center_world: Point3<f64>,
    pub radius: f64,
}

impl RadiusLandmarkSelector {
    pub const fn new(center_world: Point3<f64>, radius: f64) -> Self {
        Self {
            center_world,
            radius,
        }
    }
}

impl CandidateSelector for RadiusLandmarkSelector {
    fn select_landmark_ids(&self, _query: &QueryImage, map: &VisualMap) -> Vec<LandmarkId> {
        if self.radius < 0.0 {
            return Vec::new();
        }

        let radius_squared = self.radius * self.radius;
        let mut landmark_ids = map
            .landmarks
            .values()
            .filter(|landmark| {
                (landmark.position - self.center_world).norm_squared() <= radius_squared
            })
            .map(|landmark| landmark.id)
            .collect::<Vec<_>>();
        landmark_ids.sort_unstable();
        landmark_ids
    }
}

#[derive(Debug, Clone)]
pub struct CorrespondenceBuilder<M, S = AllLandmarksSelector> {
    matcher: M,
    candidate_selector: S,
}

impl<M> CorrespondenceBuilder<M, AllLandmarksSelector>
where
    M: Matcher,
{
    pub const fn new(matcher: M) -> Self {
        Self {
            matcher,
            candidate_selector: AllLandmarksSelector,
        }
    }
}

impl<M, S> CorrespondenceBuilder<M, S>
where
    M: Matcher,
    S: CandidateSelector,
{
    pub const fn with_candidate_selector(matcher: M, candidate_selector: S) -> Self {
        Self {
            matcher,
            candidate_selector,
        }
    }

    pub fn build(
        &self,
        query: &QueryImage,
        map: &VisualMap,
        descriptor_store: &LandmarkDescriptorStore,
    ) -> Result<CorrespondenceSet, CorrespondenceBuildError> {
        if query.keypoints.len() != query.descriptors.len() {
            return Err(CorrespondenceBuildError::QueryFeatureShapeMismatch {
                keypoint_count: query.keypoints.len(),
                descriptor_count: query.descriptors.len(),
            });
        }

        let candidate_landmark_ids = self.candidate_selector.select_landmark_ids(query, map);
        if candidate_landmark_ids.is_empty() {
            return Err(CorrespondenceBuildError::NoCandidateLandmarks);
        }

        let candidate_landmark_count = candidate_landmark_ids.len();
        let candidate_landmark_ids = candidate_landmark_ids.into_iter().collect::<HashSet<_>>();
        let paired = descriptor_store
            .ordered_landmark_descriptors(map)
            .into_iter()
            .filter(|(landmark, _)| candidate_landmark_ids.contains(&landmark.id))
            .collect::<Vec<_>>();
        if paired.is_empty() {
            return Err(CorrespondenceBuildError::NoMapDescriptors {
                candidate_landmark_count,
            });
        }

        let (landmarks, descriptors): (Vec<_>, Vec<_>) = paired
            .into_iter()
            .map(|(landmark, descriptor)| (landmark, descriptor.to_vec()))
            .unzip();

        let matches = self
            .matcher
            .match_descriptors(&query.descriptors, &descriptors);
        if matches.is_empty() {
            return Err(CorrespondenceBuildError::NoDescriptorMatches {
                candidate_landmark_count,
            });
        }

        let mut correspondences = Vec::new();
        let mut query_indices = Vec::new();
        let mut landmark_ids = Vec::new();
        for descriptor_match in &matches {
            let Some(point2d) = query.keypoints.get(descriptor_match.query_index).copied() else {
                continue;
            };
            let Some(landmark) = landmarks.get(descriptor_match.train_index) else {
                continue;
            };
            correspondences.push(Correspondence2D3D {
                point2d,
                point3d: landmark.position,
                confidence: descriptor_match.confidence,
            });
            query_indices.push(descriptor_match.query_index);
            landmark_ids.push(landmark.id);
        }

        Ok(CorrespondenceSet {
            correspondences,
            query_indices,
            landmark_ids,
            candidate_landmark_count,
            match_count: matches.len(),
            descriptor_matches: matches,
        })
    }
}

/// ORB-SLAM3-style projection-guided correspondence building: instead of
/// matching every candidate landmark descriptor against every query
/// descriptor (`CorrespondenceBuilder`'s all-vs-all search), each candidate
/// landmark is projected into the query image with a pose prior and matched
/// only against the query keypoints that fall within `search_radius_px` of
/// the projection. This trades the appearance-only search for a much
/// smaller, geometrically-gated candidate window per landmark, which both
/// disambiguates repeated/near-duplicate descriptors (only one is ever in
/// the window) and is materially cheaper when the map is large.
#[derive(Debug, Clone)]
pub struct ProjectionCorrespondenceBuilder<M, S = AllLandmarksSelector> {
    matcher: M,
    candidate_selector: S,
}

impl<M> ProjectionCorrespondenceBuilder<M, AllLandmarksSelector>
where
    M: Matcher,
{
    pub const fn new(matcher: M) -> Self {
        Self {
            matcher,
            candidate_selector: AllLandmarksSelector,
        }
    }
}

impl<M, S> ProjectionCorrespondenceBuilder<M, S>
where
    M: Matcher,
    S: CandidateSelector,
{
    pub const fn with_candidate_selector(matcher: M, candidate_selector: S) -> Self {
        Self {
            matcher,
            candidate_selector,
        }
    }

    /// Build correspondences by projecting each candidate landmark with
    /// `pose_prior` and matching its descriptor only against query keypoints
    /// within `search_radius_px` (Euclidean, in pixels) of the projection.
    /// Landmarks that project behind the camera, outside the image bounds,
    /// or land in a keypoint-free window contribute no correspondence and
    /// are silently skipped (mirroring how `CorrespondenceBuilder` silently
    /// drops descriptor pairs the matcher rejects). Reuses the caller's
    /// `Matcher` for the actual per-window nearest-neighbour decision, so
    /// ratio-test and cross-check semantics are unchanged from the
    /// appearance-global path.
    pub fn build_with_pose_prior(
        &self,
        query: &QueryImage,
        map: &VisualMap,
        descriptor_store: &LandmarkDescriptorStore,
        pose_prior: &Pose,
        search_radius_px: f64,
    ) -> Result<CorrespondenceSet, CorrespondenceBuildError> {
        self.build_with_pose_prior_and_query_landmark_ratio(
            query,
            map,
            descriptor_store,
            pose_prior,
            search_radius_px,
            None,
        )
    }

    /// Projection matching with an optional reverse ambiguity gate. The
    /// matcher's normal ratio test operates landmark -> keypoints in one
    /// projection window. `max_query_landmark_distance_ratio` additionally
    /// requires the winning landmark for a query keypoint to be strictly
    /// better than the second-best landmark whose projection window contains
    /// that keypoint. This rejects repeated-texture collisions between
    /// overlapping windows before PnP.
    pub fn build_with_pose_prior_and_query_landmark_ratio(
        &self,
        query: &QueryImage,
        map: &VisualMap,
        descriptor_store: &LandmarkDescriptorStore,
        pose_prior: &Pose,
        search_radius_px: f64,
        max_query_landmark_distance_ratio: Option<f32>,
    ) -> Result<CorrespondenceSet, CorrespondenceBuildError> {
        if let Some(ratio) = max_query_landmark_distance_ratio {
            if !ratio.is_finite() || ratio <= 0.0 || ratio >= 1.0 {
                return Err(CorrespondenceBuildError::InvalidQueryLandmarkRatio);
            }
        }
        if query.keypoints.len() != query.descriptors.len() {
            return Err(CorrespondenceBuildError::QueryFeatureShapeMismatch {
                keypoint_count: query.keypoints.len(),
                descriptor_count: query.descriptors.len(),
            });
        }

        let candidate_landmark_ids = self.candidate_selector.select_landmark_ids(query, map);
        if candidate_landmark_ids.is_empty() {
            return Err(CorrespondenceBuildError::NoCandidateLandmarks);
        }

        let candidate_landmark_count = candidate_landmark_ids.len();
        let candidate_landmark_ids = candidate_landmark_ids.into_iter().collect::<HashSet<_>>();
        let paired = descriptor_store
            .ordered_landmark_descriptors(map)
            .into_iter()
            .filter(|(landmark, _)| candidate_landmark_ids.contains(&landmark.id))
            .collect::<Vec<_>>();
        if paired.is_empty() {
            return Err(CorrespondenceBuildError::NoMapDescriptors {
                candidate_landmark_count,
            });
        }

        let radius_squared = search_radius_px * search_radius_px;
        let mut proposals = Vec::new();

        for (landmark_index, (landmark, descriptor)) in paired.iter().enumerate() {
            let point_camera = pose_prior.transform_world_point(&landmark.position);
            let Some(projected) = query.camera.project(&point_camera) else {
                continue;
            };
            if projected.x < 0.0
                || projected.y < 0.0
                || projected.x >= query.camera.width as f64
                || projected.y >= query.camera.height as f64
            {
                continue;
            }

            let mut window_indices = Vec::new();
            let mut window_descriptors = Vec::new();
            for (keypoint_index, keypoint) in query.keypoints.iter().enumerate() {
                let dx = keypoint.x - projected.x;
                let dy = keypoint.y - projected.y;
                if dx * dx + dy * dy <= radius_squared {
                    window_indices.push(keypoint_index);
                    window_descriptors.push(query.descriptors[keypoint_index].clone());
                }
            }
            if window_descriptors.is_empty() {
                continue;
            }

            // Match in landmark -> window direction. With one landmark
            // descriptor as the query, the matcher's best/second-best (and
            // ratio test) select exactly one image keypoint from this
            // projection window. The opposite direction would allow every
            // keypoint in the window to select the same landmark and makes a
            // one-element train set bypass the ratio test.
            let landmark_query = [descriptor.to_vec()];
            let window_matches = self
                .matcher
                .match_descriptors(&landmark_query, &window_descriptors);
            for window_match in window_matches {
                let Some(&global_query_index) = window_indices.get(window_match.train_index) else {
                    continue;
                };
                proposals.push((
                    landmark_index,
                    landmark.id,
                    landmark.position,
                    global_query_index,
                    DescriptorMatch {
                        query_index: global_query_index,
                        train_index: landmark_index,
                        ..window_match
                    },
                ));
            }
        }

        if proposals.is_empty() {
            return Err(CorrespondenceBuildError::NoDescriptorMatches {
                candidate_landmark_count,
            });
        }

        if let Some(ratio) = max_query_landmark_distance_ratio {
            let mut distances_by_query: HashMap<usize, Vec<f32>> = HashMap::new();
            for proposal in &proposals {
                if proposal.4.distance.is_finite() {
                    distances_by_query
                        .entry(proposal.3)
                        .or_default()
                        .push(proposal.4.distance);
                }
            }
            for distances in distances_by_query.values_mut() {
                distances.sort_by(f32::total_cmp);
            }
            proposals.retain(|proposal| {
                let Some(distances) = distances_by_query.get(&proposal.3) else {
                    return false;
                };
                let Some(&best) = distances.first() else {
                    return false;
                };
                if proposal.4.distance != best {
                    return false;
                }
                match distances.get(1) {
                    Some(&second) => best < ratio * second,
                    None => true,
                }
            });
            if proposals.is_empty() {
                return Err(CorrespondenceBuildError::NoDescriptorMatches {
                    candidate_landmark_count,
                });
            }
        }

        // Different projected landmarks can still compete for the same image
        // keypoint when windows overlap. Keep the globally lowest descriptor
        // distance first, with deterministic landmark/query tie breaks, so
        // the final 2D-3D set is one-to-one in both directions.
        proposals.sort_by(|left, right| {
            left.4
                .distance
                .total_cmp(&right.4.distance)
                .then_with(|| left.1.cmp(&right.1))
                .then_with(|| left.3.cmp(&right.3))
        });
        let mut used_query_indices = HashSet::new();
        let mut used_landmark_ids = HashSet::new();
        let mut correspondences = Vec::new();
        let mut query_indices = Vec::new();
        let mut landmark_ids = Vec::new();
        let mut descriptor_matches = Vec::new();
        for (_, landmark_id, point3d, query_index, descriptor_match) in proposals {
            if !used_query_indices.insert(query_index) || !used_landmark_ids.insert(landmark_id) {
                continue;
            }
            let Some(point2d) = query.keypoints.get(query_index).copied() else {
                continue;
            };
            correspondences.push(Correspondence2D3D {
                point2d,
                point3d,
                confidence: descriptor_match.confidence,
            });
            query_indices.push(query_index);
            landmark_ids.push(landmark_id);
            descriptor_matches.push(descriptor_match);
        }

        Ok(CorrespondenceSet {
            correspondences,
            query_indices,
            landmark_ids,
            candidate_landmark_count,
            match_count: descriptor_matches.len(),
            descriptor_matches,
        })
    }
}