visloc-rs 0.5.0

Visual localization foundation library using SfM / visual maps.
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
//! Restart-safe, bounded-memory storage for global image descriptors.
//!
//! The format binds descriptor rows to the model, ordered input manifest, and
//! preprocessing protocol. Rows are appended to a `.partial` sibling and the
//! complete file is installed with one same-directory rename.

use sha2::{Digest, Sha256};
use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

const MAGIC: &[u8; 8] = b"VLVPRD01";
const VERSION: u32 = 1;
const HEADER_LEN: usize = 160;
const COMPLETED_OFFSET: u64 = 40;
const TAG_LEN: usize = 8;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GlobalDescriptorBinding {
    pub row_count: u64,
    pub dimension: u32,
    pub resize_width: u32,
    pub resize_height: u32,
    pub model_sha256: [u8; 32],
    pub manifest_sha256: [u8; 32],
    pub preprocessing_sha256: [u8; 32],
}

impl GlobalDescriptorBinding {
    fn record_len(&self) -> Result<usize, String> {
        (self.dimension as usize)
            .checked_mul(4)
            .and_then(|bytes| bytes.checked_add(TAG_LEN))
            .ok_or_else(|| "global-descriptor record size overflow".to_owned())
    }
}

pub struct GlobalDescriptorWriter {
    final_path: PathBuf,
    partial_path: PathBuf,
    file: File,
    binding: GlobalDescriptorBinding,
    completed: u64,
}

impl GlobalDescriptorWriter {
    pub fn begin_or_resume(
        final_path: impl AsRef<Path>,
        binding: GlobalDescriptorBinding,
    ) -> Result<Self, String> {
        let final_path = final_path.as_ref().to_path_buf();
        if final_path.exists() {
            return Err(format!(
                "refusing to overwrite complete descriptor store {}",
                final_path.display()
            ));
        }
        let parent = final_path.parent().unwrap_or_else(|| Path::new("."));
        std::fs::create_dir_all(parent)
            .map_err(|error| format!("create {}: {error}", parent.display()))?;
        let partial_path = partial_path(&final_path)?;
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(!partial_path.exists())
            .open(&partial_path)
            .map_err(|error| format!("open {}: {error}", partial_path.display()))?;

        let completed = if file.metadata().map_err(|e| e.to_string())?.len() == 0 {
            write_header(&mut file, &binding, 0)?;
            file.sync_all().map_err(|e| e.to_string())?;
            0
        } else {
            let (stored, _) = read_header(&mut file)?;
            if stored != binding {
                return Err(format!(
                    "partial descriptor binding differs: {}",
                    partial_path.display()
                ));
            }
            recover_valid_rows(&mut file, &binding)?
        };
        file.seek(SeekFrom::End(0)).map_err(|e| e.to_string())?;
        Ok(Self {
            final_path,
            partial_path,
            file,
            binding,
            completed,
        })
    }

    pub const fn completed(&self) -> u64 {
        self.completed
    }

    pub fn append(&mut self, descriptor: &[f32]) -> Result<(), String> {
        if self.completed >= self.binding.row_count {
            return Err("descriptor store already contains every declared row".to_owned());
        }
        if descriptor.len() != self.binding.dimension as usize {
            return Err(format!(
                "descriptor dimension {} differs from declared {}",
                descriptor.len(),
                self.binding.dimension
            ));
        }
        let norm2 = descriptor.iter().try_fold(0.0_f64, |sum, value| {
            if value.is_finite() {
                Ok(sum + f64::from(*value) * f64::from(*value))
            } else {
                Err("descriptor contains a non-finite value".to_owned())
            }
        })?;
        if (norm2.sqrt() - 1.0).abs() > 1.0e-3 {
            return Err(format!(
                "descriptor is not L2-normalized (norm={:.9})",
                norm2.sqrt()
            ));
        }
        let mut bytes = Vec::with_capacity(self.binding.dimension as usize * 4);
        for value in descriptor {
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        let digest = Sha256::digest(&bytes);
        self.file.write_all(&bytes).map_err(|e| e.to_string())?;
        self.file
            .write_all(&digest[..TAG_LEN])
            .map_err(|e| e.to_string())?;
        self.completed += 1;
        Ok(())
    }

    pub fn checkpoint(&mut self) -> Result<(), String> {
        self.file.sync_data().map_err(|e| e.to_string())?;
        write_completed(&mut self.file, self.completed)?;
        self.file.sync_data().map_err(|e| e.to_string())?;
        self.file
            .seek(SeekFrom::End(0))
            .map_err(|e| e.to_string())?;
        Ok(())
    }

    pub fn finalize(mut self) -> Result<(), String> {
        if self.completed != self.binding.row_count {
            return Err(format!(
                "cannot finalize {} of {} descriptor rows",
                self.completed, self.binding.row_count
            ));
        }
        self.checkpoint()?;
        self.file.sync_all().map_err(|e| e.to_string())?;
        drop(self.file);
        std::fs::rename(&self.partial_path, &self.final_path).map_err(|error| {
            format!(
                "install {} from {}: {error}",
                self.final_path.display(),
                self.partial_path.display()
            )
        })?;
        if let Some(parent) = self.final_path.parent() {
            File::open(parent)
                .and_then(|directory| directory.sync_all())
                .map_err(|error| format!("sync {}: {error}", parent.display()))?;
        }
        Ok(())
    }
}

pub struct GlobalDescriptorStore {
    binding: GlobalDescriptorBinding,
    file: File,
    record_len: usize,
}

impl GlobalDescriptorStore {
    pub fn open(path: impl AsRef<Path>) -> Result<Self, String> {
        let path = path.as_ref();
        let mut file = File::open(path)
            .map_err(|error| format!("open descriptor store {}: {error}", path.display()))?;
        let (binding, completed) = read_header(&mut file)?;
        if completed != binding.row_count {
            return Err(format!(
                "descriptor store is incomplete: {completed}/{} rows",
                binding.row_count
            ));
        }
        let record_len = binding.record_len()?;
        let expected = expected_len(&binding, binding.row_count)?;
        let actual = file.metadata().map_err(|e| e.to_string())?.len();
        if actual != expected {
            return Err(format!(
                "descriptor store length {actual} != expected {expected}"
            ));
        }
        Ok(Self {
            binding,
            file,
            record_len,
        })
    }

    pub const fn binding(&self) -> &GlobalDescriptorBinding {
        &self.binding
    }

    /// Byte stride between descriptor rows, including the trailing checksum.
    pub const fn record_len_bytes(&self) -> usize {
        self.record_len
    }

    /// Byte range of only the little-endian f32 payload for one row.
    pub fn descriptor_byte_range(&self, row: u64) -> Result<std::ops::Range<usize>, String> {
        if row >= self.binding.row_count {
            return Err(format!("descriptor row {row} is out of range"));
        }
        let start = HEADER_LEN + row as usize * self.record_len;
        Ok(start..start + self.binding.dimension as usize * 4)
    }

    pub fn descriptor(&self, row: u64) -> Result<Vec<f32>, String> {
        let range = self.descriptor_byte_range(row)?;
        let mut file = self.file.try_clone().map_err(|e| e.to_string())?;
        file.seek(SeekFrom::Start(range.start as u64))
            .map_err(|e| e.to_string())?;
        let descriptor_len = self.binding.dimension as usize * 4;
        let mut record = vec![0_u8; self.record_len];
        file.read_exact(&mut record).map_err(|e| e.to_string())?;
        let digest = Sha256::digest(&record[..descriptor_len]);
        if digest[..TAG_LEN] != record[descriptor_len..] {
            return Err(format!("descriptor row {row} checksum mismatch"));
        }
        Ok(record[..descriptor_len]
            .chunks_exact(4)
            .map(|bytes| f32::from_le_bytes(bytes.try_into().expect("four bytes")))
            .collect())
    }
}

fn partial_path(path: &Path) -> Result<PathBuf, String> {
    let name = path
        .file_name()
        .ok_or_else(|| format!("output has no filename: {}", path.display()))?;
    let mut partial = name.to_os_string();
    partial.push(".partial");
    Ok(path.with_file_name(partial))
}

fn expected_len(binding: &GlobalDescriptorBinding, rows: u64) -> Result<u64, String> {
    let record = u64::try_from(binding.record_len()?).map_err(|e| e.to_string())?;
    (HEADER_LEN as u64)
        .checked_add(
            rows.checked_mul(record)
                .ok_or("descriptor length overflow")?,
        )
        .ok_or_else(|| "descriptor length overflow".to_owned())
}

fn write_header(
    file: &mut File,
    binding: &GlobalDescriptorBinding,
    completed: u64,
) -> Result<(), String> {
    let mut bytes = [0_u8; HEADER_LEN];
    bytes[0..8].copy_from_slice(MAGIC);
    bytes[8..12].copy_from_slice(&VERSION.to_le_bytes());
    bytes[12..16].copy_from_slice(&(HEADER_LEN as u32).to_le_bytes());
    bytes[16..24].copy_from_slice(&binding.row_count.to_le_bytes());
    bytes[24..28].copy_from_slice(&binding.dimension.to_le_bytes());
    bytes[28..32].copy_from_slice(&binding.resize_width.to_le_bytes());
    bytes[32..36].copy_from_slice(&binding.resize_height.to_le_bytes());
    bytes[40..48].copy_from_slice(&completed.to_le_bytes());
    bytes[48..80].copy_from_slice(&binding.model_sha256);
    bytes[80..112].copy_from_slice(&binding.manifest_sha256);
    bytes[112..144].copy_from_slice(&binding.preprocessing_sha256);
    bytes[144..152].copy_from_slice(&(binding.record_len()? as u64).to_le_bytes());
    file.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?;
    file.write_all(&bytes).map_err(|e| e.to_string())
}

fn read_header(file: &mut File) -> Result<(GlobalDescriptorBinding, u64), String> {
    let mut bytes = [0_u8; HEADER_LEN];
    file.seek(SeekFrom::Start(0)).map_err(|e| e.to_string())?;
    file.read_exact(&mut bytes).map_err(|e| e.to_string())?;
    if &bytes[0..8] != MAGIC {
        return Err("invalid global-descriptor store magic".to_owned());
    }
    if le_u32(&bytes[8..12]) != VERSION || le_u32(&bytes[12..16]) != HEADER_LEN as u32 {
        return Err("unsupported global-descriptor store version/header".to_owned());
    }
    let mut model = [0_u8; 32];
    let mut manifest = [0_u8; 32];
    let mut preprocessing = [0_u8; 32];
    model.copy_from_slice(&bytes[48..80]);
    manifest.copy_from_slice(&bytes[80..112]);
    preprocessing.copy_from_slice(&bytes[112..144]);
    let binding = GlobalDescriptorBinding {
        row_count: le_u64(&bytes[16..24]),
        dimension: le_u32(&bytes[24..28]),
        resize_width: le_u32(&bytes[28..32]),
        resize_height: le_u32(&bytes[32..36]),
        model_sha256: model,
        manifest_sha256: manifest,
        preprocessing_sha256: preprocessing,
    };
    if binding.row_count == 0 || binding.dimension == 0 {
        return Err("descriptor store declares zero rows or dimension".to_owned());
    }
    if le_u64(&bytes[144..152]) != binding.record_len()? as u64 {
        return Err("descriptor store record length is inconsistent".to_owned());
    }
    Ok((binding, le_u64(&bytes[40..48])))
}

fn recover_valid_rows(file: &mut File, binding: &GlobalDescriptorBinding) -> Result<u64, String> {
    let record_len = binding.record_len()? as u64;
    let len = file.metadata().map_err(|e| e.to_string())?.len();
    if len < HEADER_LEN as u64 {
        return Err("partial descriptor store is shorter than its header".to_owned());
    }
    let candidate_rows = ((len - HEADER_LEN as u64) / record_len).min(binding.row_count);
    let mut record = vec![0_u8; record_len as usize];
    let mut valid = 0;
    file.seek(SeekFrom::Start(HEADER_LEN as u64))
        .map_err(|e| e.to_string())?;
    while valid < candidate_rows {
        file.read_exact(&mut record).map_err(|e| e.to_string())?;
        let split = record.len() - TAG_LEN;
        if Sha256::digest(&record[..split])[..TAG_LEN] != record[split..] {
            break;
        }
        valid += 1;
    }
    file.set_len(expected_len(binding, valid)?)
        .map_err(|e| e.to_string())?;
    write_completed(file, valid)?;
    file.sync_all().map_err(|e| e.to_string())?;
    Ok(valid)
}

fn write_completed(file: &mut File, completed: u64) -> Result<(), String> {
    file.seek(SeekFrom::Start(COMPLETED_OFFSET))
        .map_err(|e| e.to_string())?;
    file.write_all(&completed.to_le_bytes())
        .map_err(|e| e.to_string())
}

fn le_u32(bytes: &[u8]) -> u32 {
    u32::from_le_bytes(bytes.try_into().expect("four bytes"))
}

fn le_u64(bytes: &[u8]) -> u64 {
    u64::from_le_bytes(bytes.try_into().expect("eight bytes"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn path(label: &str) -> PathBuf {
        std::env::temp_dir().join(format!(
            "visloc-vpr-store-{label}-{}-{}",
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ))
    }

    fn binding(rows: u64) -> GlobalDescriptorBinding {
        GlobalDescriptorBinding {
            row_count: rows,
            dimension: 2,
            resize_width: 640,
            resize_height: 480,
            model_sha256: [1; 32],
            manifest_sha256: [2; 32],
            preprocessing_sha256: [3; 32],
        }
    }

    #[test]
    fn final_store_is_mmap_readable_and_bound() {
        let output = path("final");
        let mut writer = GlobalDescriptorWriter::begin_or_resume(&output, binding(2)).unwrap();
        writer.append(&[0.6, 0.8]).unwrap();
        writer.append(&[0.0, 1.0]).unwrap();
        writer.finalize().unwrap();
        assert!(!partial_path(&output).unwrap().exists());
        let store = GlobalDescriptorStore::open(&output).unwrap();
        assert_eq!(store.binding(), &binding(2));
        let first = store.descriptor(0).unwrap();
        let second = store.descriptor(1).unwrap();
        let dot: f32 = first.iter().zip(second).map(|(a, b)| a * b).sum();
        assert!((dot - 0.8).abs() < 1e-6);
        std::fs::remove_file(output).unwrap();
    }

    #[test]
    fn resume_discards_a_torn_tail_and_keeps_valid_rows() {
        let output = path("resume");
        {
            let mut writer = GlobalDescriptorWriter::begin_or_resume(&output, binding(2)).unwrap();
            writer.append(&[0.6, 0.8]).unwrap();
            writer.checkpoint().unwrap();
        }
        let partial = partial_path(&output).unwrap();
        OpenOptions::new()
            .append(true)
            .open(&partial)
            .unwrap()
            .write_all(&[9, 8, 7])
            .unwrap();
        let mut resumed = GlobalDescriptorWriter::begin_or_resume(&output, binding(2)).unwrap();
        assert_eq!(resumed.completed(), 1);
        resumed.append(&[0.0, 1.0]).unwrap();
        resumed.finalize().unwrap();
        assert_eq!(
            GlobalDescriptorStore::open(&output)
                .unwrap()
                .binding()
                .row_count,
            2
        );
        std::fs::remove_file(output).unwrap();
    }

    #[test]
    fn resume_rejects_a_different_binding() {
        let output = path("binding");
        let writer = GlobalDescriptorWriter::begin_or_resume(&output, binding(1)).unwrap();
        drop(writer);
        let mut changed = binding(1);
        changed.model_sha256 = [9; 32];
        assert!(GlobalDescriptorWriter::begin_or_resume(&output, changed).is_err());
        std::fs::remove_file(partial_path(&output).unwrap()).unwrap();
    }
}