Skip to main content

vtk_pure_rs/data/
traits.rs

1use crate::types::BoundingBox;
2
3use crate::data::{DataSetAttributes, FieldData};
4
5/// Base trait for any data object with field data.
6///
7/// Analogous to VTK's `vtkDataObject`.
8pub trait DataObject {
9    fn field_data(&self) -> &FieldData;
10    fn field_data_mut(&mut self) -> &mut FieldData;
11}
12
13/// Trait for spatial datasets with points and cells.
14///
15/// Analogous to VTK's `vtkDataSet`.
16pub trait DataSet: DataObject {
17    fn num_points(&self) -> usize;
18    fn num_cells(&self) -> usize;
19    fn point(&self, idx: usize) -> [f64; 3];
20    fn bounds(&self) -> BoundingBox;
21    fn point_data(&self) -> &DataSetAttributes;
22    fn point_data_mut(&mut self) -> &mut DataSetAttributes;
23    fn cell_data(&self) -> &DataSetAttributes;
24    fn cell_data_mut(&mut self) -> &mut DataSetAttributes;
25
26    /// Center of the bounding box.
27    fn center(&self) -> [f64; 3] {
28        self.bounds().center()
29    }
30
31    /// Diagonal length of the bounding box.
32    fn diagonal(&self) -> f64 {
33        self.bounds().diagonal_length()
34    }
35
36    /// Whether the dataset has no points.
37    fn is_empty(&self) -> bool {
38        self.num_points() == 0
39    }
40
41    /// Number of point data arrays.
42    fn num_point_arrays(&self) -> usize {
43        self.point_data().num_arrays()
44    }
45
46    /// Number of cell data arrays.
47    fn num_cell_arrays(&self) -> usize {
48        self.cell_data().num_arrays()
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::data::PolyData;
56
57    #[test]
58    fn dataset_default_methods() {
59        let pd = PolyData::from_triangles(
60            vec![[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 2.0, 0.0]],
61            vec![[0, 1, 2]],
62        );
63        let c = pd.center();
64        assert!((c[0] - 1.0).abs() < 1e-10);
65        assert!(pd.diagonal() > 0.0);
66        assert!(!pd.is_empty());
67        assert_eq!(pd.num_point_arrays(), 0);
68    }
69
70    #[test]
71    fn empty_dataset() {
72        let pd = PolyData::new();
73        assert!(pd.is_empty());
74    }
75}