Skip to main content

use_face/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// A face boundary represented by vertex indices.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Face {
7    vertices: Vec<usize>,
8}
9
10impl Face {
11    /// Creates a face with at least three vertex indices.
12    #[must_use]
13    pub fn new(vertices: Vec<usize>) -> Option<Self> {
14        if vertices.len() >= 3 {
15            Some(Self { vertices })
16        } else {
17            None
18        }
19    }
20
21    /// Returns the vertex indices.
22    #[must_use]
23    pub fn vertices(&self) -> &[usize] {
24        &self.vertices
25    }
26
27    /// Returns the edge count.
28    #[must_use]
29    pub fn edge_count(&self) -> usize {
30        self.vertices.len()
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::Face;
37
38    #[test]
39    fn validates_face_boundaries() {
40        let face = Face::new(vec![0, 1, 2]).expect("valid face");
41
42        assert_eq!(face.vertices(), &[0, 1, 2]);
43        assert_eq!(face.edge_count(), 3);
44        assert_eq!(Face::new(vec![0, 1]), None);
45    }
46}