Skip to main content

cmtool_core/grid/
collections.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3///Represent axis absolution direction in a direct-orientied coordinate system
4pub enum OrientedAxis {
5    I = 0,
6    J = 1,
7    K = 2,
8}
9
10///Represent axis direction in cylindrical coordinates
11pub enum CylindricalAxis {
12    R = OrientedAxis::I as isize,
13    Theta = OrientedAxis::J as isize,
14    Z = OrientedAxis::K as isize,
15}
16
17///Represent axis direction in cartesian coordinates
18pub enum CartesianAxis {
19    X = OrientedAxis::I as isize,
20    Y = OrientedAxis::J as isize,
21    Z = OrientedAxis::K as isize,
22}
23
24impl From<CartesianAxis> for usize {
25    fn from(axis: CartesianAxis) -> usize {
26        axis as usize
27    }
28}
29impl From<CylindricalAxis> for usize {
30    fn from(axis: CylindricalAxis) -> usize {
31        axis as usize
32    }
33}
34impl From<OrientedAxis> for usize {
35    fn from(axis: OrientedAxis) -> usize {
36        axis as usize
37    }
38}
39
40impl From<usize> for OrientedAxis {
41    fn from(axis: usize) -> OrientedAxis {
42        match axis {
43            0 => OrientedAxis::I,
44            1 => OrientedAxis::J,
45            2 => OrientedAxis::K,
46            _ => panic!("Oreitnedaxis conversion from usize is possible for 0,1,2 "),
47        }
48    }
49}
50
51#[inline(always)]
52pub const fn cylindrical_index(axis: CylindricalAxis) -> usize {
53    match axis {
54        CylindricalAxis::R => 0,
55        CylindricalAxis::Theta => 1,
56        CylindricalAxis::Z => 2,
57    }
58}
59
60#[inline(always)]
61#[allow(unused)] //FIXME
62pub const fn index_to_oriented(axis: usize) -> OrientedAxis {
63    match axis {
64        0 => OrientedAxis::I,
65        1 => OrientedAxis::J,
66        2 => OrientedAxis::K,
67        _ => panic!("index_to_oriented needs index between 0 and 3"),
68    }
69}
70
71#[inline(always)]
72pub const fn oriented_to_cylindrical(axis: OrientedAxis) -> CylindricalAxis {
73    match axis {
74        OrientedAxis::I => CylindricalAxis::R,
75        OrientedAxis::J => CylindricalAxis::Theta,
76        OrientedAxis::K => CylindricalAxis::Z,
77    }
78}
79
80#[derive(Default)]
81pub struct AxisDescriptor {
82    pub min_range: f64,
83    pub max_range: f64,
84    pub n_range: usize,
85    pub step: f64,
86}
87
88impl AxisDescriptor {
89    pub fn new(min_range: f64, max_range: f64, n_range: usize) -> Self {
90        Self {
91            min_range,
92            max_range,
93            n_range,
94            step: 0.,
95        }
96    }
97}
98
99pub struct CoordAxis {
100    pub edges: Vec<f64>,
101    pub centers: Vec<f64>,
102    pub descriptor: AxisDescriptor,
103    // pub i_axis: usize,
104}
105
106impl From<AxisDescriptor> for CoordAxis {
107    fn from(mut descriptor: AxisDescriptor) -> Self {
108        const STEP_OFFSET: f64 = 0.5;
109        let mut edges = Vec::with_capacity(descriptor.n_range + 1);
110        let mut centers = Vec::with_capacity(descriptor.n_range);
111
112        descriptor.step =
113            (descriptor.max_range - descriptor.min_range) / (descriptor.n_range as f64);
114
115        let mut i_point = 0;
116        while i_point < descriptor.n_range {
117            let f_i_point = i_point as f64;
118            centers.push(descriptor.min_range + descriptor.step * (f_i_point + STEP_OFFSET));
119
120            edges.push(descriptor.min_range + descriptor.step * f_i_point);
121            i_point += 1;
122        }
123        let f_i_point = i_point as f64;
124        edges.push(descriptor.min_range + descriptor.step * f_i_point);
125
126        Self {
127            edges,
128            centers,
129            descriptor,
130        }
131    }
132}
133
134impl CoordAxis {
135    pub fn index_from_edge_value(&self, axis_value: f64) -> Option<usize> {
136        let mut cell_id = 0;
137        let n_point = self.descriptor.n_range;
138        while (cell_id < n_point) && (self.edges[cell_id + 1] < axis_value) {
139            cell_id += 1;
140        }
141        if cell_id == n_point {
142            return None;
143        }
144
145        Some(cell_id)
146    }
147}