rust_3d/
is_topology_unit.rs

1/*
2Copyright 2017 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//! IsTopologyUnit trait used for single units of a topology. E.g. size 1 for paths, size 3 for tri meshes, size 4 for quad meshes
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// IsTopologyUnit trait used for single units of a topology. E.g. size 1 for paths, size 3 for tri meshes, size 4 for quad meshes
30pub trait IsTopologyUnit {
31    /// Should return the number of indices a unit is defined with. (e.g. 3 for a tri mesh)
32    fn n_vids() -> usize;
33    /// Should return the vertex id of the nth element of this unit. Failure if index out of bounds
34    fn vid(&self, index: usize) -> Result<VId>;
35
36    /// Applies the provided function to all indices within this unit
37    fn for_each_vid(&self, f: &mut dyn FnMut(VId)) {
38        for i in 0..Self::n_vids() {
39            f(self.vid(i).unwrap()) // safe as long as implementation isn't incorrect
40        }
41    }
42}