1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![allow(dead_code)]
use super::unit_cell::UnitCell;
use crate::reference_tables;
use crate::transformation::TransformationMatrix;
use std::cmp::Ordering;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
/// A Space group of a crystal
pub struct Symmetry {
    /// The index of this symbol in Int. Crys. Handbook Vol A 2016
    index: usize,
}

impl Symmetry {
    /// Create a new `Symmetry` based on a fully qualified Herman Mauguin or Hall symbol
    #[must_use]
    pub fn new(symbol: impl AsRef<str>) -> Option<Self> {
        reference_tables::get_index_for_symbol(symbol.as_ref().trim()).map(|index| Self { index })
    }

    /// Create a new `Symmetry` based on the index of a symbol in Int. Crys. Handbook Vol A 2016
    #[must_use]
    pub fn from_index(index: usize) -> Option<Self> {
        reference_tables::get_herman_mauguin_symbol_for_index(index).map(|_| Self { index })
    }

    /// Get the fully qualified Herman Mauguin symbol for the space group
    #[must_use]
    pub fn herman_mauguin_symbol(&self) -> &str {
        reference_tables::get_herman_mauguin_symbol_for_index(self.index)
            .expect("An invalid index was present in the definition of this symmetry")
    }

    /// Get the fully qualified Hall symbol for the space group
    #[must_use]
    pub fn hall_symbol(&self) -> &str {
        reference_tables::get_hall_symbol_for_index(self.index)
            .expect("An invalid index was present in the definition of this symmetry")
    }

    /// Get the Z value, the number of polymeric sub units in a unit cell, for this space group
    #[allow(clippy::unwrap_used)]
    #[must_use]
    pub fn z(&self) -> usize {
        reference_tables::get_transformation(self.index)
            .unwrap()
            .len()
            + 1
    }

    /// Get the index of this space group in Int. Crys. Handbook Vol A 2016
    #[must_use]
    pub const fn index(&self) -> usize {
        self.index
    }

    /// Get the transformations for this space group needed to fill the unit cell.
    /// The first transformation is always an identity transformation.
    /// The translation is fractional to the unit cell size.
    #[allow(clippy::unwrap_used)]
    #[must_use]
    pub fn transformations(&self) -> Vec<TransformationMatrix> {
        let matrices = reference_tables::get_transformation(self.index).unwrap();
        let mut output = Vec::with_capacity(matrices.len() + 1);
        output.push(TransformationMatrix::identity());
        for matrix in matrices {
            output.push(TransformationMatrix::from_matrix(*matrix));
        }
        output
    }

    /// Get the transformations for this space group needed to fill the unit cell.
    /// The first transformation is always an identity transformation.
    /// The translation is in Å.
    #[allow(clippy::unwrap_used)]
    #[must_use]
    pub fn transformations_absolute(&self, unit_cell: &UnitCell) -> Vec<TransformationMatrix> {
        let matrices = reference_tables::get_transformation(self.index).unwrap();
        let mut output = Vec::with_capacity(matrices.len() + 1);
        output.push(TransformationMatrix::identity());
        for matrix in matrices {
            let mut ma = TransformationMatrix::from_matrix(*matrix);
            ma.multiply_translation(unit_cell.size());
            output.push(ma);
        }
        output
    }
}

impl PartialEq for Symmetry {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index
    }
}

impl Eq for Symmetry {}

impl Ord for Symmetry {
    fn cmp(&self, other: &Self) -> Ordering {
        self.index.cmp(&other.index)
    }
}

impl PartialOrd for Symmetry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(test)]
mod tests {
    use super::Symmetry;

    #[test]
    #[allow(clippy::unwrap_used)]
    fn both_creations() {
        let a = Symmetry::new("P 21 21 21").unwrap();
        let b = Symmetry::from_index(19).unwrap();
        assert_eq!(a, b);
        assert_eq!(a.z(), a.transformations().len());
        assert_eq!(
            4,
            a.transformations_absolute(&crate::UnitCell::new(1.0, 1.0, 1.0, 90.0, 90.0, 90.0))
                .len()
        );
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn symbol_invariant() {
        let a = Symmetry::new("P 21 21 21").unwrap();
        assert_eq!(a.herman_mauguin_symbol(), "P 21 21 21");
        assert_eq!(a.hall_symbol(), "P 2ac 2ab");
        assert_eq!(a.index(), 19);
    }
}