visloc_io/
two_view_matches.rs1use std::fs;
2use std::num::{ParseFloatError, ParseIntError};
3use std::path::Path;
4
5use nalgebra::Point2;
6use thiserror::Error;
7
8#[derive(Debug, Clone, PartialEq)]
9pub struct TwoViewFeatureMatch {
10 pub previous_index: usize,
11 pub current_index: usize,
12 pub previous_xy: Point2<f64>,
13 pub current_xy: Point2<f64>,
14 pub score: Option<f64>,
15}
16
17#[derive(Debug, Clone, Default, PartialEq)]
18pub struct TwoViewMatchSet {
19 matches: Vec<TwoViewFeatureMatch>,
20}
21
22impl TwoViewMatchSet {
23 pub const fn new(matches: Vec<TwoViewFeatureMatch>) -> Self {
24 Self { matches }
25 }
26
27 pub fn matches(&self) -> &[TwoViewFeatureMatch] {
28 &self.matches
29 }
30
31 pub fn into_matches(self) -> Vec<TwoViewFeatureMatch> {
32 self.matches
33 }
34
35 pub fn len(&self) -> usize {
36 self.matches.len()
37 }
38
39 pub fn is_empty(&self) -> bool {
40 self.matches.is_empty()
41 }
42
43 pub fn matched_previous_keypoints(&self) -> Vec<Point2<f64>> {
44 self.matches
45 .iter()
46 .map(|feature_match| feature_match.previous_xy)
47 .collect()
48 }
49
50 pub fn matched_current_keypoints(&self) -> Vec<Point2<f64>> {
51 self.matches
52 .iter()
53 .map(|feature_match| feature_match.current_xy)
54 .collect()
55 }
56}
57
58#[derive(Debug, Error)]
59pub enum TwoViewMatchError {
60 #[error("I/O error: {0}")]
61 Io(#[from] std::io::Error),
62 #[error("integer parse error: {0}")]
63 ParseInt(#[from] ParseIntError),
64 #[error("float parse error: {0}")]
65 ParseFloat(#[from] ParseFloatError),
66 #[error("invalid two-view match line: {line}")]
67 InvalidLine { line: String },
68}
69
70pub fn read_two_view_matches_txt(
71 path: impl AsRef<Path>,
72) -> Result<TwoViewMatchSet, TwoViewMatchError> {
73 parse_two_view_matches_txt(&fs::read_to_string(path)?)
74}
75
76pub fn parse_two_view_matches_txt(contents: &str) -> Result<TwoViewMatchSet, TwoViewMatchError> {
77 let mut matches = Vec::new();
78
79 for line in contents.lines().map(str::trim) {
80 if line.is_empty() || line.starts_with('#') {
81 continue;
82 }
83
84 let tokens = line.split_whitespace().collect::<Vec<_>>();
85 if tokens.len() != 6 && tokens.len() != 7 {
86 return Err(TwoViewMatchError::InvalidLine {
87 line: line.to_owned(),
88 });
89 }
90
91 matches.push(TwoViewFeatureMatch {
92 previous_index: tokens[0].parse()?,
93 current_index: tokens[1].parse()?,
94 previous_xy: Point2::new(tokens[2].parse()?, tokens[3].parse()?),
95 current_xy: Point2::new(tokens[4].parse()?, tokens[5].parse()?),
96 score: tokens.get(6).map(|value| value.parse()).transpose()?,
97 });
98 }
99
100 Ok(TwoViewMatchSet::new(matches))
101}