Skip to main content

cmtool_core/ensight_gold/
vectors.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3use crate::{
4    ensight_gold::{
5        reader::EnsightGoldReader,
6        variable::{PerElementVariable, VarTypeReader},
7    },
8    utils,
9};
10
11pub(crate) struct VectorReader;
12
13impl VarTypeReader for VectorReader {
14    type VarType = Vec<f32>;
15    fn read_elements(
16        reader: &mut EnsightGoldReader,
17        element: &super::geo::MeshElementType,
18    ) -> std::io::Result<Self::VarType> {
19        let mut flat_data = vec![0.; element.n_elements * 3];
20
21        for i_xyz in 0..3 {
22            for i_vertex in 0..element.n_elements {
23                flat_data[utils::linear_index_coordinates_matrix(i_vertex, i_xyz)] =
24                    reader.read_f32()?;
25            }
26        }
27
28        Ok(flat_data)
29    }
30}
31pub(crate) type VectorField = PerElementVariable<VectorReader>;
32
33impl VectorField {
34    pub fn get_xyz(&self, i_part: usize, i_mesh_element_type: usize, mesh_cell: usize) -> [f64; 3] {
35        let flat = &self.data[i_part][i_mesh_element_type];
36        let cols = flat.len() / 3;
37
38        if cols * 3 != flat.len() {
39            panic!("Error: vector data is not correctly sized (not divisible by 3)");
40        }
41
42        if mesh_cell >= cols {
43            panic!("Error: mesh_cell index out of bounds");
44        }
45
46        let x = flat[utils::linear_index_coordinates_matrix(mesh_cell, 0)] as f64;
47        let y = flat[utils::linear_index_coordinates_matrix(mesh_cell, 1)] as f64;
48        let z = flat[utils::linear_index_coordinates_matrix(mesh_cell, 2)] as f64;
49
50        [x, y, z]
51    }
52}