gmac/core/
clusters.rs

1use crate::core::transformation::{build_rotation_matrix, rotate_node};
2
3/// Creates a new 3D block of nodes.
4///
5/// # Arguments
6/// * `length`: A `[f64; 3]` specifying the length of the grid in the x, y, and z directions.
7/// * `centre`: A `[f64; 3]` specifying the coordinates of the centre of the grid.
8/// * `theta`: A `[f64; 3]` specifying the angles of rotation of the grid.
9/// * `resolution`: A `[usize; 3]` specifying the number of divisions in the x, y, and z directions.
10///
11/// # Returns
12/// A new `Vec<[f64; 3]>` instance.
13pub fn generate_block_cluster(
14    length: [f64; 3],
15    centre: [f64; 3],
16    theta: [f64; 3],
17    resolution: [usize; 3],
18) -> Vec<[f64; 3]> {
19    for &s in &length {
20        if s <= 0.0 {
21            panic!("Invalid length: dimensions must be positive");
22        }
23    }
24    for &r in &resolution {
25        if r == 0 {
26            panic!("Invalid resolution: dimensions must be non-zero");
27        }
28    }
29
30    let [rx, ry, rz] = resolution;
31
32    let step = [
33        length[0] / (rx as f64),
34        length[1] / (ry as f64),
35        length[2] / (rz as f64),
36    ];
37
38    let start = [
39        centre[0] - length[0] * 0.5,
40        centre[1] - length[1] * 0.5,
41        centre[2] - length[2] * 0.5,
42    ];
43
44    let rotation_matrix = build_rotation_matrix(&theta);
45
46    let mut nodes = vec![[0.0; 3]; (rx + 1) * (ry + 1) * (rz + 1)];
47
48    let mut node_count = 0;
49    for i in 0..=rx {
50        for j in 0..=ry {
51            for k in 0..=rz {
52                nodes[node_count] = [
53                    start[0] + i as f64 * step[0],
54                    start[1] + j as f64 * step[1],
55                    start[2] + k as f64 * step[2],
56                ];
57
58                if theta.iter().any(|angle| *angle != 0.0) {
59                    rotate_node(&mut nodes[node_count], &rotation_matrix, &centre);
60                }
61
62                node_count += 1;
63            }
64        }
65    }
66
67    nodes
68}