rust_3d/
cluster_vertices.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//! Algorithm to cluster nearby vertices within a mesh
24
25use crate::*;
26
27use fnv::FnvHashMap;
28use std::hash::Hash;
29
30//------------------------------------------------------------------------------
31
32/// Algorithm to cluster nearby vertices within a mesh
33pub fn cluster_vertices<P, M>(mesh: &M, cluster_size: f64) -> Result<M>
34where
35    M: IsFaceEditableMesh<P, Face3>
36        + IsVertexEditableMesh<P, Face3>
37        + Default
38        + HasBoundingBox3DMaybe,
39    P: IsBuildable3D + Eq + Hash + Clone,
40{
41    let bb = mesh.bounding_box_maybe()?;
42    let [sx, sy, sz] = bb.sizes();
43    let min = P::new_from(&bb.min_p());
44    let (nx, ny, nz) = (
45        (sx.get() / cluster_size) as usize,
46        (sy.get() / cluster_size) as usize,
47        (sz.get() / cluster_size) as usize,
48    );
49    if nx < 2 || ny < 2 || nz < 2 {
50        return Err(ErrorKind::ClusterTooBig);
51    }
52
53    let cluster_of = |ref p| {
54        let v = conn(&min, p);
55        (
56            (v.x() / cluster_size) as usize,
57            (v.y() / cluster_size) as usize,
58            (v.z() / cluster_size) as usize,
59        )
60    };
61
62    let nv = mesh.num_vertices();
63    let mut cluster_map = FnvHashMap::default();
64    let mut clusters = Vec::with_capacity(nv);
65
66    for i in 0..nv {
67        let p = mesh.vertex(VId { val: i }).unwrap(); // safe, since index in range
68        let cluster = cluster_of(p);
69        cluster_map.insert(cluster, i); //@todo later this must keep the 'best' vertex instead of the last
70        clusters.push(cluster);
71    }
72
73    let mut result = M::default();
74
75    let new_vertex = |old_index| {
76        let cluster = &clusters[old_index];
77        mesh.vertex(VId {
78            val: *cluster_map.get(cluster).unwrap(), // safe since any cluster in clusters also within cluster_map
79        })
80        .unwrap() // safe, since index in range
81    };
82
83    let nf = mesh.num_faces();
84    result.reserve_faces(nf);
85    result.reserve_vertices(3 * nf);
86    for i in 0..nf {
87        let f = mesh.face_vertex_ids(FId { val: i }).unwrap();
88
89        result.add_face(
90            new_vertex(f.a.val),
91            new_vertex(f.b.val),
92            new_vertex(f.c.val),
93        );
94    }
95
96    result = heal_mesh(&result)?;
97
98    Ok(result)
99}