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
use result::*;
use point_3d::Point3D;
use traits::is_3d::Is3D;
use traits::is_buildable_3d::IsBuildable3D;
use functions::cross;
pub struct Matrix4 {
pub data: [[f64; 4]; 4]
}
impl Matrix4 {
pub fn new() -> Matrix4 {
Matrix4{
data: [
[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 zeroes() -> Matrix4 {
Matrix4{
data: [
[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 translation(x: f64, y: f64, z: f64) -> Matrix4 {
Matrix4{
data: [
[1.0, 0.0, 0.0, x],
[0.0, 1.0, 0.0, y],
[0.0, 0.0, 1.0, z],
[0.0, 0.0, 0.0, 1.0]
]
}
}
pub fn scale(x: f64, y: f64, z: f64) -> Matrix4 {
Matrix4{
data: [
[x, 0.0, 0.0, 0.0],
[0.0, y, 0.0, 0.0],
[0.0, 0.0, z, 0.0],
[0.0, 0.0, 0.0, 1.0]
]
}
}
pub fn rotation(rad_x: f64, rad_y: f64, rad_z: f64) -> Matrix4 {
let (mut mx, mut my, mut mz) = (Matrix4::new(), Matrix4::new(), Matrix4::new());
mx.data[0][0] = 1.0; mx.data[0][1] = 0.0; mx.data[0][2] = 0.0; mx.data[0][3] = 0.0;
mx.data[1][0] = 0.0; mx.data[1][1] = rad_x.cos(); mx.data[1][2] = -rad_x.sin(); mx.data[1][3] = 0.0;
mx.data[2][0] = 0.0; mx.data[2][1] = rad_x.sin(); mx.data[2][2] = rad_x.cos(); mx.data[2][3] = 0.0;
mx.data[3][0] = 0.0; mx.data[3][1] = 0.0; mx.data[3][2] = 0.0; mx.data[3][3] = 1.0;
my.data[0][0] = rad_y.cos(); my.data[0][1] = 0.0; my.data[0][2] = rad_y.sin(); my.data[0][3] = 0.0;
my.data[1][0] = 0.0; my.data[1][1] = 1.0; my.data[1][2] = 0.0; my.data[1][3] = 0.0;
my.data[2][0] = -rad_y.sin(); my.data[2][1] = 0.0; my.data[2][2] = rad_y.cos(); my.data[2][3] = 0.0;
my.data[3][0] = 0.0; my.data[3][1] = 0.0; my.data[3][2] = 0.0; my.data[3][3] = 1.0;
mz.data[0][0] = rad_z.cos(); mz.data[0][1] = -rad_z.sin(); mz.data[0][2] = 0.0; mz.data[0][3] = 0.0;
mz.data[1][0] = rad_z.sin(); mz.data[1][1] = rad_z.cos(); mz.data[1][2] = 0.0; mz.data[1][3] = 0.0;
mz.data[2][0] = 0.0; mz.data[2][1] = 0.0; mz.data[2][2] = 1.0; mz.data[2][3] = 0.0;
mz.data[3][0] = 0.0; mz.data[3][1] = 0.0; mz.data[3][2] = 0.0; mz.data[3][3] = 1.0;
mx.multiply_m(&my.multiply_m(&mz))
}
pub fn rotation_axis<P>(axis: &P, rad: f64) -> Result<Matrix4> where
P: IsBuildable3D {
let u = match axis.clone().normalized() {
Err(x) => return Err(x),
Ok(x) => x
};
let mut result = Matrix4::new();
result.data[0][0] = rad.cos() + u.x()*u.x()*(1.0 - rad.cos()); result.data[0][1] = u.x()*u.y()*(1.0 -rad.cos()) - u.z()*rad.sin(); result.data[0][2] = u.x()*u.z()*(1.0 - rad.cos()) + u.y()*rad.sin(); result.data[0][3] = 0.0;
result.data[1][0] = u.y()*u.x()*(1.0 - rad.cos()) + u.z()*rad.sin(); result.data[1][1] = rad.cos() + u.y()*u.y()*(1.0 - rad.cos()); result.data[1][2] = u.y()*u.z()*(1.0 - rad.cos()) - u.x()*rad.sin(); result.data[1][3] = 0.0;
result.data[2][0] = u.z()*u.x()*(1.0 - rad.cos()) - u.y()*rad.sin(); result.data[2][1] = u.z()*u.y()*(1.0 - rad.cos()) + u.x()*rad.sin(); result.data[2][2] = rad.cos() + u.z()*u.z()*(1.0 - rad.cos()); result.data[2][3] = 0.0;
result.data[3][0] = 0.0; result.data[3][1] = 0.0; result.data[3][2] = 0.0; result.data[3][3] = 1.0;
Ok(result)
}
pub fn perspective(close: f64, away: f64, fov_rad: f64) -> Matrix4 {
let range = close - away;
let tan_fov_half = (fov_rad/2.0).tan();
let mut result = Matrix4::new();
result.data[0][0] = 1.0 / (tan_fov_half * away); result.data[0][1] = 0.0; result.data[0][2] = 0.0; result.data[0][3] = 0.0;
result.data[1][0] = 0.0; result.data[1][1] = 1.0 / tan_fov_half; result.data[1][2] = 0.0; result.data[1][3] = 0.0;
result.data[2][0] = 0.0; result.data[2][1] = 0.0; result.data[2][2] = (-close - away) / range; result.data[2][3] = 2.0 * away * close / range;
result.data[3][0] = 0.0; result.data[3][1] = 0.0; result.data[3][2] = 1.0; result.data[3][3] = 1.0;
result
}
pub fn look_at<P>(target: &P, up: &P) -> Result<Matrix4> where
P: IsBuildable3D {
let n = match target.clone().normalized() {
Err(x) => return Err(x),
Ok(x) => x
};
let u: Point3D;
u = match up.clone().normalized() {
Err(x) => return Err(x),
Ok(x) => {
let mut result = *Point3D::new();
result.from(*(cross(&*x, target)));
result
}
};
let v = cross(&*n, &u);
let mut result = Matrix4::new();
result.data[0][0] = u.x(); result.data[0][1] = u.y(); result.data[0][2] = u.z(); result.data[0][3] = 0.0;
result.data[1][0] = v.x(); result.data[1][1] = v.y(); result.data[1][2] = v.z(); result.data[1][3] = 0.0;
result.data[2][0] = n.x(); result.data[2][1] = n.y(); result.data[2][2] = n.z(); result.data[2][3] = 0.0;
result.data[3][0] = 0.0; result.data[3][1] = 0.0; result.data[3][2] = 0.0; result.data[3][3] = 1.0;
Ok(result)
}
pub fn multiply_m(&self, other: &Matrix4) -> Matrix4 {
let mut result = Matrix4::new();
for i in 0..4 {
for j in 0..4 {
result.data[i][j] =
self.data[i][0] * other.data[0][j] +
self.data[i][1] * other.data[1][j] +
self.data[i][2] * other.data[2][j] +
self.data[i][3] * other.data[3][j];
}
}
result
}
pub fn multiply_f(&self, other: f64) -> Matrix4 {
let mut result = Matrix4::new();
for i in 0..4 {
for j in 0..4 {
result.data[i][j] = other * self.data[i][j];
}
}
result
}
}