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
//! This module is for collecting tools that are useful in RANSAC
use crate::Result;
use rand::{RngExt, rng};
use std::collections::HashSet;
/// Generate `group_count` groups of `N` indices from a list of `index_count` total possibilities.
/// The indices in a single group will be unique.
///
/// # Arguments
///
/// * `group_count`: the total number of groups to generate
/// * `index_count`: the total number of possible indices
///
/// returns: Result<Vec<[usize; N], Global>, Box<dyn Error, Global>>
///
/// # Examples
///
/// ```
///
/// ```
pub fn ransac_indices<const N: usize>(
group_count: usize,
index_count: usize,
) -> Result<Vec<[usize; N]>> {
if index_count < N {
return Err("Not enough indices".into());
}
// TODO: this method relies on there being a lot more indices than the size of each group.
// TODO: Make a more reasonable implementation that adjusts based on the size
let mut result = Vec::with_capacity(group_count);
let mut working = HashSet::with_capacity(N);
let mut rng = rng();
for _ in 0..group_count {
working.clear();
let mut indices = [0; N];
for slot in indices.iter_mut() {
loop {
let j = rng.random_range(0..index_count);
if !working.contains(&j) {
working.insert(j);
*slot = j;
break;
}
}
}
result.push(indices);
}
Ok(result)
}