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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

use crate::{
    vector::Vector,
    error,
    enums::Axis
};


#[derive(Debug, Clone)]
pub struct Matrix {
    m: Vec<f64>
}

impl Matrix {

    pub fn default() -> Matrix {
        Matrix{
            m:vec![0.0, 0.0, 0.0, 0.0,
                   0.0, 0.0, 0.0, 0.0,
                   0.0, 0.0, 0.0, 0.0,
                   0.0, 0.0, 0.0, 0.0]
        }
    }

    pub fn identity() -> Matrix {
        Matrix{
            m:vec![1.0, 0.0, 0.0, 0.0,
                   0.0, 1.0, 0.0, 0.0,
                   0.0, 0.0, 1.0, 0.0,
                   0.0, 0.0, 0.0, 1.0]
        }
    }

    pub fn new_with_fill(f:f64) -> Matrix {
        Matrix{
            m:vec![f, f, f, f,
                   f, f, f, f,
                   f, f, f, f, 
                   f, f, f, f]
        }
    }

    pub fn new_from_vec(m:&Vec<f64>) -> error::Result<Matrix> {
        if m.len() == 12 {
            Ok(Matrix{
                m:m.clone()
            })
        } else {
            panic!("Array size mismatch");
        }
    }

    pub fn new_from_array(m:&[f64; 12]) -> Matrix {
        Matrix{
            m:m.to_vec()
        }
    }

    pub fn new_with_values(v00:f64, v01:f64, v02:f64,
                           v10:f64, v11:f64, v12:f64,
                           v20:f64, v21:f64, v22:f64) -> Matrix {
        Matrix{
            m:vec![v00, v01, v02,
                   v10, v11, v12,
                   v20, v21, v22]
        }
    }

    fn index(&self, x:usize, y:usize) -> usize {
        y * 4 + x
    }

    pub fn set(&mut self, x:usize, y:usize, v:f64)  {
        let i = self.index(x, y);
        if i < 16 {
            self.m[i] = v;
        } else {
            panic!("Invalid pixel coordinates");
        }
    }

    pub fn get(&self, x:usize, y:usize) -> f64 {
        let i = self.index(x, y);
        if i < 16 {
            self.m[i]
        } else {
            panic!("Invalid pixel coordinates");
        }
    }

    pub fn matmul4(a:&Matrix, b:&Matrix) -> Matrix {

        let mut product = vec![0.0, 0.0, 0.0,
                                0.0, 0.0, 0.0,
                                0.0, 0.0, 0.0];

        for row in 0..3 {
            let ai0 = a.m[(0 << 2) + row];
            let ai1 = a.m[(1 << 2) + row];
            let ai2 = a.m[(2 << 2) + row];

            product[(0 << 2) + row] = ai0 * b.m[(0 << 2) + 0] + ai1 * b.m[(0 << 2) + 1] + ai2 * b.m[(0 << 2) + 2];
            product[(1 << 2) + row] = ai0 * b.m[(1 << 2) + 0] + ai1 * b.m[(1 << 2) + 1] + ai2 * b.m[(1 << 2) + 2];
            product[(2 << 2) + row] = ai0 * b.m[(2 << 2) + 0] + ai1 * b.m[(2 << 2) + 1] + ai2 * b.m[(2 << 2) + 2];
        }

        Matrix::new_from_vec(&product).unwrap()
    }


    pub fn multiply(&self, other:&Matrix) -> Matrix {
        Matrix::matmul4(&self, &other)
    }

    pub fn multiply_vector(&self, other:&Vector) -> Vector {
        let x = other.x * self.m[0 * 4 + 0] + other.y * self.m[1 * 4 + 0] + other.z * self.m[2 * 4 + 0];
		let y = other.x * self.m[0 * 4 + 1] + other.y * self.m[1 * 4 + 1] + other.z * self.m[2 * 4 + 1];
		let z = other.x * self.m[0 * 4 + 2] + other.y * self.m[1 * 4 + 2] + other.z * self.m[2 * 4 + 2];
        Vector::new(x, y, z)
	}

    pub fn scale(&self, x:f64, y:f64, z:f64) -> Matrix {
        Matrix{
            m:vec![self.m[0] * x, self.m[1] * x, self.m[2] * x, 
                   self.m[4] * y, self.m[5] * y, self.m[6] * y,
                   self.m[8] * z, self.m[9] * z, self.m[10] * z]
        }
    }

    pub fn transpose_rotation(&self) -> Matrix {
        let mut t = self.clone();
        t.m[1] = self.m[4];
        t.m[4] = self.m[1];

        t.m[2] = self.m[8];
        t.m[8] = self.m[2];

        t.m[6] = self.m[9];
        t.m[9] = self.m[6];

        t
    }

    pub fn rotate(angle:f64, axis:Axis) -> Matrix {
        let mut m = Matrix::identity();

        let _a = if axis != Axis::YAxis { 
            angle 
        } else { 
            angle * -1.0
        };

        let c = _a.cos();
        let s = _a.sin();

        match axis {
            Axis::XAxis => {
                m.set(1, 1, c);
                m.set(2, 2, c);
                m.set(2, 1, -s);
                m.set(1, 2, s);
            },
            Axis::YAxis => {
                m.set(0, 0, c);
                m.set(2, 2, c);
                m.set(2, 0, s);
                m.set(0, 2, -s);
            },
            Axis::ZAxis => {
                m.set(0, 0, c);
                m.set(1, 1, c);
                m.set(0, 1, s);
                m.set(1, 0, -s);
            }
        }

        m
    }


}