Skip to main content

visloc_io/
query_features.rs

1use std::fs;
2use std::num::ParseFloatError;
3use std::path::Path;
4use thiserror::Error;
5use visloc_vision::features::{FeatureSet, FeatureSetError};
6
7use nalgebra::Point2;
8
9#[derive(Debug, Error)]
10pub enum QueryFeatureError {
11    #[error("I/O error: {0}")]
12    Io(#[from] std::io::Error),
13    #[error("float parse error: {0}")]
14    ParseFloat(#[from] ParseFloatError),
15    #[error("feature set error: {0}")]
16    FeatureSet(#[from] FeatureSetError),
17    #[error("invalid query feature line: {line}")]
18    InvalidLine { line: String },
19}
20
21pub fn read_query_features_txt(path: impl AsRef<Path>) -> Result<FeatureSet, QueryFeatureError> {
22    parse_query_features_txt(&fs::read_to_string(path)?)
23}
24
25pub fn parse_query_features_txt(contents: &str) -> Result<FeatureSet, QueryFeatureError> {
26    let mut keypoints = Vec::new();
27    let mut descriptors = Vec::new();
28
29    for line in contents.lines().map(str::trim) {
30        if line.is_empty() || line.starts_with('#') {
31            continue;
32        }
33
34        let tokens = line.split_whitespace().collect::<Vec<_>>();
35        if tokens.len() < 3 {
36            return Err(QueryFeatureError::InvalidLine {
37                line: line.to_owned(),
38            });
39        }
40
41        keypoints.push(Point2::new(tokens[0].parse()?, tokens[1].parse()?));
42        descriptors.push(
43            tokens[2..]
44                .iter()
45                .map(|value| value.parse::<f32>())
46                .collect::<Result<Vec<_>, _>>()?,
47        );
48    }
49
50    Ok(FeatureSet::new(keypoints, descriptors)?)
51}