Falco Engine 3.9.0.1 (beta)
Matrix3.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6
7namespace FalcoEngine
8{
9 public struct Matrix3
10 {
11 public float m00;
12 public float m10;
13 public float m20;
14 public float m01;
15 public float m11;
16 public float m21;
17 public float m02;
18 public float m12;
19 public float m22;
20
24 private static readonly Matrix3 zeroMatrix = new Matrix3(new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f));
25
29 private static readonly Matrix3 identityMatrix = new Matrix3(new Vector3(1f, 0f, 0f), new Vector3(0f, 1f, 0f), new Vector3(0f, 0f, 1f));
30
31 public Matrix3(Vector3 column0, Vector3 column1, Vector3 column2)
32 {
33 m00 = column0.x;
34 m01 = column1.x;
35 m02 = column2.x;
36 m10 = column0.y;
37 m11 = column1.y;
38 m12 = column2.y;
39 m20 = column0.z;
40 m21 = column1.z;
41 m22 = column2.z;
42 }
43
44 public Matrix3(float d00, float d01, float d02,
45 float d10, float d11, float d12,
46 float d20, float d21, float d22)
47 {
48 m00 = d00;
49 m01 = d01;
50 m02 = d02;
51 m10 = d10;
52 m11 = d11;
53 m12 = d12;
54 m20 = d20;
55 m21 = d21;
56 m22 = d22;
57 }
58
59 public float this[int row, int column]
60 {
61 get
62 {
63 return this[row + column * 3];
64 }
65 set
66 {
67 this[row + column * 3] = value;
68 }
69 }
70
71 public float this[int index]
72 {
73 get
74 {
75 switch (index)
76 {
77 case 0:
78 return m00;
79 case 1:
80 return m10;
81 case 2:
82 return m20;
83 case 3:
84 return m01;
85 case 4:
86 return m11;
87 case 5:
88 return m21;
89 case 6:
90 return m02;
91 case 7:
92 return m12;
93 case 8:
94 return m22;
95 default:
96 throw new IndexOutOfRangeException("Invalid matrix index!");
97 }
98 }
99 set
100 {
101 switch (index)
102 {
103 case 0:
104 m00 = value;
105 break;
106 case 1:
107 m10 = value;
108 break;
109 case 2:
110 m20 = value;
111 break;
112 case 3:
113 m01 = value;
114 break;
115 case 4:
116 m11 = value;
117 break;
118 case 5:
119 m21 = value;
120 break;
121 case 6:
122 m02 = value;
123 break;
124 case 7:
125 m12 = value;
126 break;
127 case 8:
128 m22 = value;
129 break;
130 default:
131 throw new IndexOutOfRangeException("Invalid matrix index!");
132 }
133 }
134 }
135
136 public override bool Equals(object other)
137 {
138 if (!(other is Matrix3))
139 {
140 return false;
141 }
142 return Equals((Matrix3)other);
143 }
144
145 public bool Equals(Matrix3 other)
146 {
147 return GetColumn(0).Equals(other.GetColumn(0)) && GetColumn(1).Equals(other.GetColumn(1)) && GetColumn(2).Equals(other.GetColumn(2)) && GetColumn(3).Equals(other.GetColumn(3));
148 }
149
150 public static Matrix3 operator *(Matrix3 lhs, Matrix3 rhs)
151 {
152 Matrix3 result = default(Matrix3);
153 result.m00 = lhs.m00 * rhs.m00 + lhs.m01 * rhs.m10 + lhs.m02 * rhs.m20;
154 result.m01 = lhs.m00 * rhs.m01 + lhs.m01 * rhs.m11 + lhs.m02 * rhs.m21;
155 result.m02 = lhs.m00 * rhs.m02 + lhs.m01 * rhs.m12 + lhs.m02 * rhs.m22;
156 result.m10 = lhs.m10 * rhs.m00 + lhs.m11 * rhs.m10 + lhs.m12 * rhs.m20;
157 result.m11 = lhs.m10 * rhs.m01 + lhs.m11 * rhs.m11 + lhs.m12 * rhs.m21;
158 result.m12 = lhs.m10 * rhs.m02 + lhs.m11 * rhs.m12 + lhs.m12 * rhs.m22;
159 result.m20 = lhs.m20 * rhs.m00 + lhs.m21 * rhs.m10 + lhs.m22 * rhs.m20;
160 result.m21 = lhs.m20 * rhs.m01 + lhs.m21 * rhs.m11 + lhs.m22 * rhs.m21;
161 result.m22 = lhs.m20 * rhs.m02 + lhs.m21 * rhs.m12 + lhs.m22 * rhs.m22;
162 return result;
163 }
164
165 public static Vector3 operator *(Matrix3 lhs, Vector3 vector)
166 {
167 Vector3 result = default(Vector3);
168 result.x = lhs.m00 * vector.x + lhs.m01 * vector.y + lhs.m02 * vector.z;
169 result.y = lhs.m10 * vector.x + lhs.m11 * vector.y + lhs.m12 * vector.z;
170 result.z = lhs.m20 * vector.x + lhs.m21 * vector.y + lhs.m22 * vector.z;
171 return result;
172 }
173
174 public static bool operator ==(Matrix3 lhs, Matrix3 rhs)
175 {
176 return lhs.GetColumn(0) == rhs.GetColumn(0) && lhs.GetColumn(1) == rhs.GetColumn(1) && lhs.GetColumn(2) == rhs.GetColumn(2);
177 }
178
179 public static bool operator !=(Matrix3 lhs, Matrix3 rhs)
180 {
181 return !(lhs == rhs);
182 }
183
188 public Vector3 GetColumn(int index)
189 {
190 switch (index)
191 {
192 case 0:
193 return new Vector3(m00, m10, m20);
194 case 1:
195 return new Vector3(m01, m11, m21);
196 case 2:
197 return new Vector3(m02, m12, m22);
198 default:
199 throw new IndexOutOfRangeException("Invalid column index!");
200 }
201 }
202
207 public Vector3 GetRow(int index)
208 {
209 switch (index)
210 {
211 case 0:
212 return new Vector3(m00, m01, m02);
213 case 1:
214 return new Vector3(m10, m11, m12);
215 case 2:
216 return new Vector3(m20, m21, m22);
217 default:
218 throw new IndexOutOfRangeException("Invalid row index!");
219 }
220 }
221
227 public void SetColumn(int index, Vector3 column)
228 {
229 this[0, index] = column.x;
230 this[1, index] = column.y;
231 this[2, index] = column.z;
232 }
233
239 public void SetRow(int index, Vector3 row)
240 {
241 this[index, 0] = row.x;
242 this[index, 1] = row.y;
243 this[index, 2] = row.z;
244 }
245
251 {
252 Vector3 result = default(Vector3);
253 result.x = m00 * vector.x + m01 * vector.y + m02 * vector.z;
254 result.y = m10 * vector.x + m11 * vector.y + m12 * vector.z;
255 result.z = m20 * vector.x + m21 * vector.y + m22 * vector.z;
256 return result;
257 }
258
263 public static Matrix3 Scale(Vector3 vector)
264 {
265 Matrix3 result = default(Matrix3);
266 result.m00 = vector.x;
267 result.m01 = 0f;
268 result.m02 = 0f;
269 result.m10 = 0f;
270 result.m11 = vector.y;
271 result.m12 = 0f;
272 result.m20 = 0f;
273 result.m21 = 0f;
274 result.m22 = vector.z;
275 return result;
276 }
277
282 public static Matrix3 Rotate(Quaternion q)
283 {
284 float num = q.x * 2f;
285 float num2 = q.y * 2f;
286 float num3 = q.z * 2f;
287 float num4 = q.x * num;
288 float num5 = q.y * num2;
289 float num6 = q.z * num3;
290 float num7 = q.x * num2;
291 float num8 = q.x * num3;
292 float num9 = q.y * num3;
293 float num10 = q.w * num;
294 float num11 = q.w * num2;
295 float num12 = q.w * num3;
296 Matrix3 result = default(Matrix3);
297 result.m00 = 1f - (num5 + num6);
298 result.m10 = num7 + num12;
299 result.m20 = num8 - num11;
300 result.m01 = num7 - num12;
301 result.m11 = 1f - (num4 + num6);
302 result.m21 = num9 + num10;
303 result.m02 = num8 + num11;
304 result.m12 = num9 - num10;
305 result.m22 = 1f - (num4 + num5);
306 return result;
307 }
308
313 public override string ToString()
314 {
315 return String.Format("{0:F5}\t{1:F5}\t{2:F5}\t{3:F5}\n{4:F5}\t{5:F5}\t{6:F5}\t{7:F5}\n{8:F5}\n", m00, m01, m02, m10, m11, m12, m20, m21, m22);
316 }
317
322 public string ToString(string format)
323 {
324 return String.Format("{0}\t{1}\t{2}\t{3}\n{4}\t{5}\t{6}\t{7}\n{8}\n", ToInvariantString(format, m00), ToInvariantString(format, m01), ToInvariantString(format, m02), ToInvariantString(format, m10), ToInvariantString(format, m11), ToInvariantString(format, m12), ToInvariantString(format, m20), ToInvariantString(format, m21), ToInvariantString(format, m22));
325 }
326
327 private string ToInvariantString(string format, float val)
328 {
329 return val.ToString(format, CultureInfo.InvariantCulture.NumberFormat);
330 }
331
332 public override int GetHashCode()
333 {
334 return base.GetHashCode();
335 }
336 }
337}
bool Equals(Matrix3 other)
Definition: Matrix3.cs:145
Vector3 GetRow(int index)
Definition: Matrix3.cs:207
void SetRow(int index, Vector3 row)
Definition: Matrix3.cs:239
Vector3 GetColumn(int index)
Definition: Matrix3.cs:188
static Matrix3 Rotate(Quaternion q)
Definition: Matrix3.cs:282
static Matrix3 operator*(Matrix3 lhs, Matrix3 rhs)
Definition: Matrix3.cs:150
Matrix3(Vector3 column0, Vector3 column1, Vector3 column2)
Definition: Matrix3.cs:31
static bool operator==(Matrix3 lhs, Matrix3 rhs)
Definition: Matrix3.cs:174
override string ToString()
Definition: Matrix3.cs:313
override bool Equals(object other)
Definition: Matrix3.cs:136
static Matrix3 Scale(Vector3 vector)
Definition: Matrix3.cs:263
override int GetHashCode()
Definition: Matrix3.cs:332
void SetColumn(int index, Vector3 column)
Definition: Matrix3.cs:227
Vector3 MultiplyVector(Vector3 vector)
Definition: Matrix3.cs:250
static bool operator!=(Matrix3 lhs, Matrix3 rhs)
Definition: Matrix3.cs:179
string ToString(string format)
Definition: Matrix3.cs:322
Matrix3(float d00, float d01, float d02, float d10, float d11, float d12, float d20, float d21, float d22)
Definition: Matrix3.cs:44
override bool Equals(object other)
Definition: Vector3.cs:169