vox_geometry_rust/
bcc_lattice_point_generator.rs

1/*
2 * // Copyright (c) 2021 Feng Yang
3 * //
4 * // I am making my contributions/submissions to this project solely in my
5 * // personal capacity and am not conveying any rights to any intellectual
6 * // property of any third parties.
7 */
8
9use crate::point_generator3::PointGenerator3;
10use crate::vector3::Vector3D;
11use crate::bounding_box3::BoundingBox3D;
12use std::sync::{RwLock, Arc};
13
14///
15/// # Body-centered lattice points generator.
16///
17/// \see http://en.wikipedia.org/wiki/Cubic_crystal_system
18///      http://mathworld.wolfram.com/CubicClosePacking.html
19///
20pub struct BccLatticePointGenerator {}
21
22impl PointGenerator3 for BccLatticePointGenerator {
23    ///
24    /// # Invokes \p callback function for each BCC-lattice points inside
25    /// \p bounding_box.
26    ///
27    /// This function iterates every BCC-lattice points inside \p bounding_box
28    /// where \p spacing is the size of the unit cell of BCC structure.
29    ///
30    fn for_each_point<Callback>(&self, bounding_box: &BoundingBox3D, spacing: f64, callback: &mut Callback)
31        where Callback: FnMut(&Vector3D) -> bool {
32        let half_spacing = spacing / 2.0;
33        let box_width = bounding_box.width();
34        let box_height = bounding_box.height();
35        let box_depth = bounding_box.depth();
36
37        let mut position = Vector3D::new_default();
38        let mut has_offset = false;
39        let mut should_quit = false;
40        let mut k = 0;
41        while k as f64 * half_spacing <= box_depth && !should_quit {
42            position.z = k as f64 * half_spacing + bounding_box.lower_corner.z;
43
44            let offset = match has_offset {
45                true => half_spacing,
46                false => 0.0
47            };
48
49            let mut j = 0;
50            while j as f64 * spacing + offset <= box_height && !should_quit {
51                position.y = j as f64 * spacing + offset + bounding_box.lower_corner.y;
52
53                let mut i = 0;
54                while i as f64 * spacing + offset <= box_width {
55                    position.x = i as f64 * spacing + offset + bounding_box.lower_corner.x;
56                    if !callback(&position) {
57                        should_quit = true;
58                        break;
59                    }
60                    i += 1;
61                }
62                j += 1;
63            }
64            has_offset = !has_offset;
65            k += 1;
66        }
67    }
68}
69
70/// Shared pointer type for the BccLatticePointGenerator.
71pub type BccLatticePointGeneratorPtr = Arc<RwLock<BccLatticePointGenerator>>;