-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix4.cs
276 lines (259 loc) · 11 KB
/
Matrix4.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Decompiled with JetBrains decompiler
// Type: DK64Viewer.Matrix4
// Assembly: DK64Viewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 34C2999C-2061-412A-B2F3-E6D2C8F1D38B
// Assembly location: F:\DKViewer_0.04a\DK64Viewer.exe
using System;
namespace DK64Viewer
{
public class Matrix4 : Matrix
{
public static Matrix4 I = Matrix4.NewI();
public Matrix4()
: base(4, 4)
{
}
public Matrix4(float[,] matrix)
: base(matrix)
{
if (this.rows != 4 || this.cols != 4)
throw new ArgumentException();
}
public static Matrix4 NewI() => new Matrix4(new float[4, 4]
{
{
1f,
0.0f,
0.0f,
0.0f
},
{
0.0f,
1f,
0.0f,
0.0f
},
{
0.0f,
0.0f,
1f,
0.0f
},
{
0.0f,
0.0f,
0.0f,
1f
}
});
public static bool IsEqual(Matrix4 m1, Matrix4 m2) => (double) m1.matrix[0, 0] == (double) m2.matrix[0, 0] && (double) m1.matrix[0, 1] == (double) m2.matrix[0, 1] && (double) m1.matrix[0, 2] == (double) m2.matrix[0, 2] && (double) m1.matrix[0, 3] == (double) m2.matrix[0, 3] && (double) m1.matrix[1, 0] == (double) m2.matrix[1, 0] && (double) m1.matrix[1, 1] == (double) m2.matrix[1, 1] && (double) m1.matrix[1, 2] == (double) m2.matrix[1, 2] && (double) m1.matrix[1, 3] == (double) m2.matrix[1, 3] && (double) m1.matrix[2, 0] == (double) m2.matrix[2, 0] && (double) m1.matrix[2, 1] == (double) m2.matrix[2, 1] && (double) m1.matrix[2, 2] == (double) m2.matrix[2, 2] && (double) m1.matrix[2, 3] == (double) m2.matrix[2, 3] && (double) m1.matrix[3, 0] == (double) m2.matrix[3, 0] && (double) m1.matrix[3, 1] == (double) m2.matrix[3, 1] && (double) m1.matrix[3, 2] == (double) m2.matrix[3, 2] && (double) m1.matrix[3, 3] == (double) m2.matrix[3, 3];
public static Vector3 operator *(Matrix4 matrix4, Vector3 v)
{
float[,] matrix = matrix4.matrix;
float num = (float) ((double) matrix[3, 0] * (double) v.x + (double) matrix[3, 1] * (double) v.y + (double) matrix[3, 2] * (double) v.z) + matrix[3, 3];
return new Vector3(((float) ((double) matrix[0, 0] * (double) v.x + (double) matrix[0, 1] * (double) v.y + (double) matrix[0, 2] * (double) v.z) + matrix[0, 3]) / num, ((float) ((double) matrix[1, 0] * (double) v.x + (double) matrix[1, 1] * (double) v.y + (double) matrix[1, 2] * (double) v.z) + matrix[1, 3]) / num, ((float) ((double) matrix[2, 0] * (double) v.x + (double) matrix[2, 1] * (double) v.y + (double) matrix[2, 2] * (double) v.z) + matrix[2, 3]) / num);
}
public static Matrix4 operator *(Matrix4 mat1, Matrix4 mat2)
{
float[,] matrix1 = mat1.matrix;
float[,] matrix2 = mat2.matrix;
return new Matrix4(new float[4, 4]
{
{
(float) ((double) matrix1[0, 0] * (double) matrix2[0, 0] + (double) matrix1[0, 1] * (double) matrix2[1, 0] + (double) matrix1[0, 2] * (double) matrix2[2, 0] + (double) matrix1[0, 3] * (double) matrix2[3, 0]),
(float) ((double) matrix1[0, 0] * (double) matrix2[0, 1] + (double) matrix1[0, 1] * (double) matrix2[1, 1] + (double) matrix1[0, 2] * (double) matrix2[2, 1] + (double) matrix1[0, 3] * (double) matrix2[3, 1]),
(float) ((double) matrix1[0, 0] * (double) matrix2[0, 2] + (double) matrix1[0, 1] * (double) matrix2[1, 2] + (double) matrix1[0, 2] * (double) matrix2[2, 2] + (double) matrix1[0, 3] * (double) matrix2[3, 2]),
(float) ((double) matrix1[0, 0] * (double) matrix2[0, 3] + (double) matrix1[0, 1] * (double) matrix2[1, 3] + (double) matrix1[0, 2] * (double) matrix2[2, 3] + (double) matrix1[0, 3] * (double) matrix2[3, 3])
},
{
(float) ((double) matrix1[1, 0] * (double) matrix2[0, 0] + (double) matrix1[1, 1] * (double) matrix2[1, 0] + (double) matrix1[1, 2] * (double) matrix2[2, 0] + (double) matrix1[1, 3] * (double) matrix2[3, 0]),
(float) ((double) matrix1[1, 0] * (double) matrix2[0, 1] + (double) matrix1[1, 1] * (double) matrix2[1, 1] + (double) matrix1[1, 2] * (double) matrix2[2, 1] + (double) matrix1[1, 3] * (double) matrix2[3, 1]),
(float) ((double) matrix1[1, 0] * (double) matrix2[0, 2] + (double) matrix1[1, 1] * (double) matrix2[1, 2] + (double) matrix1[1, 2] * (double) matrix2[2, 2] + (double) matrix1[1, 3] * (double) matrix2[3, 2]),
(float) ((double) matrix1[1, 0] * (double) matrix2[0, 3] + (double) matrix1[1, 1] * (double) matrix2[1, 3] + (double) matrix1[1, 2] * (double) matrix2[2, 3] + (double) matrix1[1, 3] * (double) matrix2[3, 3])
},
{
(float) ((double) matrix1[2, 0] * (double) matrix2[0, 0] + (double) matrix1[2, 1] * (double) matrix2[1, 0] + (double) matrix1[2, 2] * (double) matrix2[2, 0] + (double) matrix1[2, 3] * (double) matrix2[3, 0]),
(float) ((double) matrix1[2, 0] * (double) matrix2[0, 1] + (double) matrix1[2, 1] * (double) matrix2[1, 1] + (double) matrix1[2, 2] * (double) matrix2[2, 1] + (double) matrix1[2, 3] * (double) matrix2[3, 1]),
(float) ((double) matrix1[2, 0] * (double) matrix2[0, 2] + (double) matrix1[2, 1] * (double) matrix2[1, 2] + (double) matrix1[2, 2] * (double) matrix2[2, 2] + (double) matrix1[2, 3] * (double) matrix2[3, 2]),
(float) ((double) matrix1[2, 0] * (double) matrix2[0, 3] + (double) matrix1[2, 1] * (double) matrix2[1, 3] + (double) matrix1[2, 2] * (double) matrix2[2, 3] + (double) matrix1[2, 3] * (double) matrix2[3, 3])
},
{
(float) ((double) matrix1[3, 0] * (double) matrix2[0, 0] + (double) matrix1[3, 1] * (double) matrix2[1, 0] + (double) matrix1[3, 2] * (double) matrix2[2, 0] + (double) matrix1[3, 3] * (double) matrix2[3, 0]),
(float) ((double) matrix1[3, 0] * (double) matrix2[0, 1] + (double) matrix1[3, 1] * (double) matrix2[1, 1] + (double) matrix1[3, 2] * (double) matrix2[2, 1] + (double) matrix1[3, 3] * (double) matrix2[3, 1]),
(float) ((double) matrix1[3, 0] * (double) matrix2[0, 2] + (double) matrix1[3, 1] * (double) matrix2[1, 2] + (double) matrix1[3, 2] * (double) matrix2[2, 2] + (double) matrix1[3, 3] * (double) matrix2[3, 2]),
(float) ((double) matrix1[3, 0] * (double) matrix2[0, 3] + (double) matrix1[3, 1] * (double) matrix2[1, 3] + (double) matrix1[3, 2] * (double) matrix2[2, 3] + (double) matrix1[3, 3] * (double) matrix2[3, 3])
}
});
}
public static Matrix4 operator *(Matrix4 m, float scalar) => new Matrix4(Matrix.Multiply((Matrix) m, scalar));
public static Matrix4 GetRotationMatrixX(double angle)
{
if (angle == 0.0)
return Matrix4.I;
float num1 = (float) Math.Sin(angle);
float num2 = (float) Math.Cos(angle);
float[,] matrix = new float[4, 4];
matrix[0, 0] = 1f;
matrix[1, 1] = num2;
matrix[1, 2] = -num1;
matrix[2, 1] = num1;
matrix[2, 2] = num2;
matrix[3, 3] = 1f;
return new Matrix4(matrix);
}
public static Matrix4 GetRotationMatrixY(double angle)
{
if (angle == 0.0)
return Matrix4.I;
float num1 = (float) Math.Sin(angle);
float num2 = (float) Math.Cos(angle);
float[,] matrix = new float[4, 4];
matrix[0, 0] = num2;
matrix[0, 2] = num1;
matrix[1, 1] = 1f;
matrix[2, 0] = -num1;
matrix[2, 2] = num2;
matrix[3, 3] = 1f;
return new Matrix4(matrix);
}
public static Matrix4 GetRotationMatrixZ(double angle)
{
if (angle == 0.0)
return Matrix4.I;
float num1 = (float) Math.Sin(angle);
float num2 = (float) Math.Cos(angle);
float[,] matrix = new float[4, 4];
matrix[0, 0] = num2;
matrix[0, 1] = -num1;
matrix[1, 0] = num1;
matrix[1, 1] = num2;
matrix[2, 2] = 1f;
matrix[3, 3] = 1f;
return new Matrix4(matrix);
}
public static Matrix4 GetRotationMatrix(double ax, double ay, double az)
{
Matrix4 matrix4_1 = (Matrix4) null;
Matrix4 matrix4_2 = (Matrix4) null;
Matrix4 matrix4_3 = (Matrix4) null;
if (ax != 0.0)
matrix4_3 = Matrix4.GetRotationMatrixX(ax);
if (ay != 0.0)
matrix4_1 = Matrix4.GetRotationMatrixY(ay);
if (az != 0.0)
matrix4_2 = Matrix4.GetRotationMatrixZ(az);
if (matrix4_1 != null)
{
if (matrix4_3 != null)
matrix4_3 *= matrix4_1;
else
matrix4_3 = matrix4_1;
}
if (matrix4_2 != null)
{
if (matrix4_3 != null)
matrix4_3 *= matrix4_2;
else
matrix4_3 = matrix4_2;
}
return matrix4_3 ?? Matrix4.I;
}
public static Matrix4 GetRotationMatrix(Vector3 axis, double angle)
{
if (angle == 0.0)
return Matrix4.I;
float x = axis.x;
float y = axis.y;
float z = axis.z;
float num1 = (float) Math.Sin(angle);
float num2 = (float) Math.Cos(angle);
float num3 = x * x;
float num4 = y * y;
float num5 = z * z;
float num6 = x * y;
float num7 = x * z;
float num8 = y * z;
float[,] matrix = new float[4, 4];
matrix[0, 0] = num3 + (1f - num3) * num2;
matrix[1, 0] = (float) ((double) num6 * (1.0 - (double) num2) + (double) z * (double) num1);
matrix[2, 0] = (float) ((double) num7 * (1.0 - (double) num2) - (double) y * (double) num1);
matrix[3, 0] = 0.0f;
matrix[0, 1] = (float) ((double) num6 * (1.0 - (double) num2) - (double) z * (double) num1);
matrix[1, 1] = num4 + (1f - num4) * num2;
matrix[2, 1] = (float) ((double) num8 * (1.0 - (double) num2) + (double) x * (double) num1);
matrix[3, 1] = 0.0f;
matrix[0, 2] = (float) ((double) num7 * (1.0 - (double) num2) + (double) y * (double) num1);
matrix[1, 2] = (float) ((double) num8 * (1.0 - (double) num2) - (double) x * (double) num1);
matrix[2, 2] = num5 + (1f - num5) * num2;
matrix[3, 2] = 0.0f;
matrix[3, 0] = 0.0f;
matrix[3, 1] = 0.0f;
matrix[3, 2] = 0.0f;
matrix[3, 3] = 1f;
return new Matrix4(matrix);
}
public static Matrix4 GetRotationMatrix(Vector3 source, Vector3 destination)
{
Vector3 axis = Vector3.CrossProduct(source, destination);
if (!(axis != Vector3.Zero))
return Matrix4.I;
axis.Normalize();
double angle = Math.Acos((double) source.DotProduct(destination));
return Matrix4.GetRotationMatrix(axis, angle);
}
public static Matrix4 GetTranslationMatrix(float tx, float ty, float tz) => new Matrix4(new float[4, 4]
{
{
1f,
0.0f,
0.0f,
tx
},
{
0.0f,
1f,
0.0f,
ty
},
{
0.0f,
0.0f,
1f,
tz
},
{
0.0f,
0.0f,
0.0f,
1f
}
});
public static Matrix4 GetScaleMatrix(float sx, float sy, float sz) => new Matrix4(new float[4, 4]
{
{
sx,
0.0f,
0.0f,
0.0f
},
{
0.0f,
sy,
0.0f,
0.0f
},
{
0.0f,
0.0f,
sz,
0.0f
},
{
0.0f,
0.0f,
0.0f,
1f
}
});
}
}