rust_3d/cluster.rs
1/*
2Copyright 2019 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Spatial clustering of data on the x/y plane
24
25use crate::*;
26
27use std::collections::HashMap;
28
29//------------------------------------------------------------------------------
30
31/// Spatial clustering of data on the x/y plane
32pub struct Cluster<HB>
33where
34 HB: HasBoundingBox3D,
35{
36 data: Vec<HB>,
37 map: HashMap<(u16, u16), Vec<usize>>,
38 start_x: f64,
39 start_y: f64,
40 incr_x: f64,
41 incr_y: f64,
42}
43
44//------------------------------------------------------------------------------
45
46impl<HB> Cluster<HB>
47where
48 HB: HasBoundingBox3D,
49{
50 pub fn new(data: Vec<HB>, count_x: u16, count_y: u16) -> Result<Self> {
51 let mut map: HashMap<(u16, u16), Vec<usize>> = HashMap::new();
52 let mut bb_all: Result<BoundingBox3D> = Err(ErrorKind::BoundingBoxMissing);
53 for x in data.iter() {
54 let bb = x.bounding_box();
55 if let Ok(all) = bb_all {
56 bb_all = Ok(all.combine(&&bb));
57 } else {
58 bb_all = Ok(bb);
59 }
60 }
61
62 let bb = bb_all?;
63 let (start_x, start_y) = (bb.min_p().x(), bb.min_p().y());
64 let (size_x, size_y) = (bb.size_x().get(), bb.size_y().get());
65 let (incr_x, incr_y) = (size_x / count_x as f64, size_y / count_y as f64);
66
67 for (i, x) in data.iter().enumerate() {
68 let xbb = x.bounding_box();
69 let (min_x, min_y) = (xbb.min_p().x(), xbb.min_p().y());
70 let (max_x, max_y) = (xbb.max_p().x(), xbb.max_p().y());
71
72 let c_min_x = ((min_x - start_x) / incr_x) as u16;
73 let c_min_y = ((min_y - start_y) / incr_y) as u16;
74
75 let c_max_x = ((max_x - start_x) / incr_x) as u16;
76 let c_max_y = ((max_y - start_y) / incr_y) as u16;
77
78 for cx in c_min_x..=c_max_x {
79 for cy in c_min_y..=c_max_y {
80 map.entry((cx, cy))
81 .and_modify(|e| e.push(i))
82 .or_insert(vec![i]);
83 }
84 }
85 }
86 Ok(Self {
87 data,
88 map,
89 start_x,
90 start_y,
91 incr_x,
92 incr_y,
93 })
94 }
95
96 pub fn for_each_candidate<'a>(&'a self, x: f64, y: f64, f: &mut dyn FnMut(&HB)) {
97 let c_x = ((x - self.start_x) / self.incr_x) as u16;
98 let c_y = ((y - self.start_y) / self.incr_y) as u16;
99
100 if let Some(is) = self.map.get(&(c_x, c_y)) {
101 for i in is {
102 f(&self.data[*i]);
103 }
104 }
105 }
106}