Skip to main content

use_voronoi/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use use_point::Point2;
5
6/// A Voronoi cell represented by its site index and boundary vertices.
7#[derive(Debug, Clone, PartialEq)]
8pub struct VoronoiCell {
9    site_index: usize,
10    boundary_vertices: Vec<Point2>,
11}
12
13impl VoronoiCell {
14    /// Creates a Voronoi cell record.
15    #[must_use]
16    pub const fn new(site_index: usize, boundary_vertices: Vec<Point2>) -> Self {
17        Self {
18            site_index,
19            boundary_vertices,
20        }
21    }
22
23    /// Returns the site index.
24    #[must_use]
25    pub const fn site_index(&self) -> usize {
26        self.site_index
27    }
28
29    /// Returns the boundary vertices.
30    #[must_use]
31    pub fn boundary_vertices(&self) -> &[Point2] {
32        &self.boundary_vertices
33    }
34}
35
36/// A Voronoi diagram record.
37#[derive(Debug, Clone, PartialEq)]
38pub struct VoronoiDiagram {
39    cells: Vec<VoronoiCell>,
40}
41
42impl VoronoiDiagram {
43    /// Creates a Voronoi diagram from cells.
44    #[must_use]
45    pub const fn new(cells: Vec<VoronoiCell>) -> Self {
46        Self { cells }
47    }
48
49    /// Returns the cells.
50    #[must_use]
51    pub fn cells(&self) -> &[VoronoiCell] {
52        &self.cells
53    }
54
55    /// Returns the number of cells.
56    #[must_use]
57    pub fn cell_count(&self) -> usize {
58        self.cells.len()
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::{VoronoiCell, VoronoiDiagram};
65    use use_point::Point2;
66
67    #[test]
68    fn stores_voronoi_cells() {
69        let cell = VoronoiCell::new(0, vec![Point2::origin()]);
70        let diagram = VoronoiDiagram::new(vec![cell]);
71
72        assert_eq!(diagram.cell_count(), 1);
73        assert_eq!(diagram.cells()[0].site_index(), 0);
74        assert_eq!(diagram.cells()[0].boundary_vertices(), &[Point2::origin()]);
75    }
76}