-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatrix.cs
577 lines (481 loc) · 19.2 KB
/
Matrix.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#if PocketPC
using System;
using System.Drawing;
namespace GeoFramework
{
/// <summary>
/// Indicates the insertion point for new transforms.
/// </summary>
public enum MatrixOrder
{
Append,
Prepend,
}
/// <summary>
/// Represents a mechanism for facilitating translation, scaling and rotation of coordinates.
/// </summary>
public sealed class Matrix : IDisposable, IEquatable<Matrix>, ICloneable<Matrix>
{
// TODO: Can _Elements be combined with the other variables? They appear to have the same value.
private float _m11;
private float _m12;
private float _dx;
private float _m21;
private float _m22;
private float _dy;
private float[] _elements;
/* "Unfortunaately, no one can be told what the matrix is...you have to see it for yourself."
*
* | m11 m12 | = x scale, y shear
* | m21 m22 | = x shear, y scale
* | dx dy | = x translation, y translation
*
*/
#region Constructors
/// <summary>
/// Creates a matrix as an identity matrix
/// </summary>
public Matrix()
: this(1, 0, 0, 1, 0, 0)
{ }
/// <summary>
/// Creates a matrix with the indicated elements
/// </summary>
/// <param name="m11"> Row one, column one </param>
/// <param name="m12"> Row one, column two </param>
/// <param name="m21"> Row two, column one </param>
/// <param name="m22"> Row two, column two </param>
/// <param name="dx"> X translation (Row three, column one) </param>
/// <param name="dy"> Y translation (Row three, column two) </param>
public Matrix(float m11, float m12, float m21, float m22, float dx, float dy)
{
this._m11 = m11;
this._m12 = m12;
this._dx = dx;
this._m21 = m21;
this._m22 = m22;
this._dy = dy;
this._elements = new float[] { m11, m12, m21, m22, dx, dy };
}
#endregion
#region Public Properties
public float OffsetX
{
get { return this._dx; }
}
public float OffsetY
{
get { return this._dy; }
}
public float[] Elements
{
get { return this._elements; }
}
/// <summary>
/// Indicates whether or not this is an identity matrix
/// </summary>
public bool IsIdentity
{
get { return this.Equals(new Matrix()); }
}
/// <summary>
/// Indicates whether or not this matrix is invertable.
/// </summary>
public bool IsInvertable
{
get { return this.Determinant() != 0; }
}
#endregion
#region Public Methods
/// <summary>
/// Inverts this matrix if it is invertable.
/// </summary>
/// <remarks>
/// If the matrix is not invertable, this method throws an exception.
/// </remarks>
public void Invert()
{
float det = this.Determinant();
// A 0 determinant matrix cannot be inverted
if (det != 0)
{
// And her's why! div by zero...
double oodet = 1.0 / det;
// Use doubles for intermediate calcs. Precision goes way up
double m11 = this._m22 * oodet;
double m12 = -this._m12 * oodet;
double m21 = -this._m21 * oodet;
double m22 = this._m11 * oodet;
double dx = -(this._dx * m11 + this._dy * m21);
double dy = -(this._dx * m12 + this._dy * m22);
//double dx = (this._dy * m21) - (this._dx * m11);
//double dy = (this._dx * m12) - (this._dy * m22);
this._m11 = (float)m11;
this._m12 = (float)m12;
this._m21 = (float)m21;
this._m22 = (float)m22;
this._dx = (float)dx;
this._dy = (float)dy;
this._elements = new float[] { this._m11, this._m12, this._m21, this._m22, this._dx, this._dy };
}
else
{
// We should just throw "Parameter not valid", like GDI does.
throw new ArgumentException("The matrix cannot be inverted (Neo would fall on his head). Use IsInvertable to check whether or not a matrix can be inverted.");
}
}
/// <summary>
/// Calculates the determinat of this matrix.
/// </summary>
/// <returns> The signed area of the parallelagram described by this matrix. </returns>
/// <remarks>
/// The determinant is a scalar value typically used to invert a matrix. As a signed area, it can also be used to
/// identify "flipped" orientations, like mirroring. A negative determinant indicates that a matrix is "flipped".
/// </remarks>
private float Determinant()
{
return (this._m11 * this._m22) - (this._m12 * this._m21);
}
/// <summary>
/// Resests the matrix to the identity matrix.
/// </summary>
public void Reset()
{
this._m11 = 1;
this._m12 = 0;
this._m21 = 0;
this._m22 = 1;
this._dx = 0;
this._dy = 0;
this._elements = new float[] { this._m11, this._m12, this._m21, this._m22, this._dx, this._dy };
}
/// <summary>
/// Multiplies this matrix with the supplied matrix, using a prepended matrix order
/// </summary>
/// <param name="matrix"> The matrix to multiply with this matrix. </param>
public void Multiply(Matrix matrix)
{
this.Multiply(matrix, MatrixOrder.Prepend);
}
/// <summary>
/// Multiplies this matrix with the supplied matrix.
/// </summary>
/// <param name="matrix"> The matrix to multiply with this matrix. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void Multiply(Matrix matrix, MatrixOrder matrixOrder)
{
// Matrix placholders
Matrix a;
Matrix b;
// Order the operation based on the enum
if (matrixOrder == MatrixOrder.Append)
{
// Multiply this matrix with the parameter matrix
a = this;
b = matrix;
}
else
{
// Multiply the parameter matrix with this matrix.
a = matrix;
b = this;
}
// Use doubles for intermediate calcs. Precision goes way up.
double m11 = (a._m11 * b._m11) + (a._m12 * b._m21);
double m12 = (a._m11 * b._m12) + (a._m12 * b._m22);
double m21 = (a._m21 * b._m11) + (a._m22 * b._m21);
double m22 = (a._m21 * b._m12) + (a._m22 * b._m22);
double dx = (a._dx * b._m11) + (a._dy * b._m21) + b._dx;
double dy = (a._dx * b._m12) + (a._dy * b._m22) + b._dy;
// Push calc'd values to this matrix
this._m11 = (float)m11;
this._m12 = (float)m12;
this._m21 = (float)m21;
this._m22 = (float)m22;
this._dx = (float)dx;
this._dy = (float)dy;
this._elements = new float[] { this._m11, this._m12, this._m21, this._m22, this._dx, this._dy };
}
/// <summary>
/// Translate this matrix by prepending the supplied values.
/// </summary>
/// <param name="xOffset"> Amount to translate in the X direction. </param>
/// <param name="yOffset"> Amount to translate in the Y direction. </param>
public void Translate(float xOffset, float yOffset)
{
this.Translate(xOffset, yOffset, MatrixOrder.Prepend);
}
/// <summary>
/// Translate this matrix using the supplied values.
/// </summary>
/// <param name="xOffset"> Amount to translate in the X direction. </param>
/// <param name="yOffset"> Amount to translate in the Y direction. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void Translate(float xOffset, float yOffset, MatrixOrder matrixOrder)
{
// All operations are multiplications
this.Multiply(new Matrix(1, 0, 0, 1, xOffset, yOffset), matrixOrder);
}
/// <summary>
/// Scale this matrix by prepending the supplied values.
/// </summary>
/// <param name="xScale"> Amount to scale in the X direction. </param>
/// <param name="yScale"> Amount to scale in the Y direction. </param>
public void Scale(float xScale, float yScale)
{
this.Scale(xScale, yScale, MatrixOrder.Prepend);
}
/// <summary>
/// Scale this matrix using the supplied values.
/// </summary>
/// <param name="xScale"> Amount to scale in the X direction. </param>
/// <param name="yScale"> Amount to scale in the Y direction. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void Scale(float xScale, float yScale, MatrixOrder matrixOrder)
{
// All operations are multiplications
this.Multiply(new Matrix(xScale, 0, 0, yScale, 0, 0), matrixOrder);
}
/// <summary>
/// Shear this matrix by prepending the supplied values.
/// </summary>
/// <param name="xShear"> Amount to shear in the X direction. </param>
/// <param name="yShear"> Amount to shear in the Y direction. </param>
public void Shear(float xShear, float yShear)
{
this.Shear(xShear, yShear, MatrixOrder.Prepend);
}
/// <summary>
/// Shear this matrix using the supplied values.
/// </summary>
/// <param name="xShear"> Amount to shear in the X direction. </param>
/// <param name="yShear"> Amount to shear in the Y direction. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void Shear(float xShear, float yShear, MatrixOrder matrixOrder)
{
// All operations are multiplications
this.Multiply(new Matrix(1, yShear, xShear, 1, 0, 0), matrixOrder);
}
/// <summary>
/// Prepend a rotation to this matrix.
/// </summary>
/// <param name="angle"> Amount, in degrees, to rotate. </param>
public void Rotate(float angle)
{
this.Rotate(angle, MatrixOrder.Prepend);
}
/// <summary>
/// Apply a rotation to this matrix.
/// </summary>
/// <param name="angle"> Amount, in degrees, to rotate. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void Rotate(float angle, MatrixOrder matrixOrder)
{
double a = angle * Radian.RadiansPerDegree;
this.Multiply(new Matrix(
(float)Math.Cos(a), (float)Math.Sin(a),
-(float)Math.Sin(a), (float)Math.Cos(a),
0, 0),
matrixOrder);
}
/// <summary>
/// Prepend a translated rotation to this matrix.
/// </summary>
/// <param name="angle"> Amount, in degrees, to rotate. </param>
/// <param name="point"> The desired center of rotation. </param>
public void RotateAt(float angle, PointF point)
{
this.RotateAt(angle, point, MatrixOrder.Prepend);
}
/// <summary>
/// Apply a translated rotation to this matrix.
/// </summary>
/// <param name="angle"> Amount, in degrees, to rotate. </param>
/// <param name="point"> The desired center of rotation. </param>
/// <param name="matrixOrder"> The order in which to carry out the operation. </param>
public void RotateAt(float angle, PointF point, MatrixOrder matrixOrder)
{
// Create the offset rotstion independant of our current matrix
Matrix rotation = new Matrix();
rotation.Translate(-point.X, -point.Y, MatrixOrder.Append);
rotation.Rotate(angle, MatrixOrder.Append);
rotation.Translate(point.X, point.Y, MatrixOrder.Append);
// Combine the offset rotation matrix with the current matrix
this.Multiply(rotation, matrixOrder);
}
/// <summary>
/// Transform the array of points
/// </summary>
/// <param name="points"></param>
public void TransformPoints(PointF[] points)
{
int limit = points.Length;
for (int i = 0; i < limit; i++)
{
points[i] = this.TransformPoint(points[i]);
}
}
/// <summary>
/// Transform the array of points, without translation
/// </summary>
/// <param name="points"></param>
public void TransformVectors(PointF[] points)
{
int limit = points.Length;
for (int i = 0; i < limit; i++)
{
points[i] = this.TransformVector(points[i]);
}
}
public RectangleF TransformRectangle(RectangleF rectangle)
{
PointF[] points = RectangleFHelper.Corners(rectangle);
TransformPoints(points);
return RectangleFHelper.ComputeBoundingBox(points);
}
public Rectangle TransformRectangleCF(RectangleF rectangle)
{
return Rectangle.Round(TransformRectangle(rectangle)); ;
}
/// <summary>
/// Transform the array of points and round them
/// </summary>
/// <param name="points"></param>
public Point[] TransformPointsCF(PointF[] points)
{
int limit = points.Length;
Point[] result = new Point[limit];
for (int i = 0; i < limit; i++)
{
result[i] = this.TransformPointCF(points[i]);
}
return result;
}
/// <summary>
/// Transform the array of points without translation and round them
/// </summary>
/// <param name="points"></param>
public Point[] TransformVectorsCF(PointF[] points)
{
int limit = points.Length;
Point[] result = new Point[limit];
for (int i = 0; i < limit; i++)
{
result[i] = this.TransformVectorCF(points[i]);
}
return result;
}
private PointF TransformPoint(PointF point)
{
double x = (point.X * this._m11 + point.Y * this._m21);
double y = (point.X * this._m12 + point.Y * this._m22);
point.X = (float)x + this._dx;
point.Y = (float)y + this._dy;
return point;
}
private PointF TransformVector(PointF point)
{
double x = (point.X * this._m11 + point.Y * this._m21);
double y = (point.X * this._m12 + point.Y * this._m22);
point.X = (float)x;
point.Y = (float)y;
return point;
}
private Point TransformPointCF(PointF point)
{
Point result = new Point();
double x = (point.X * this._m11 + point.Y * this._m21);
double y = (point.X * this._m12 + point.Y * this._m22);
result.X = (int)Math.Round(x + this._dx);
result.Y = (int)Math.Round(y + this._dy);
return result;
}
private Point TransformVectorCF(PointF point)
{
Point result = new Point();
double x = (point.X * this._m11 + point.Y * this._m21);
double y = (point.X * this._m12 + point.Y * this._m22);
result.X = (int)Math.Round(x);
result.Y = (int)Math.Round(y);
return result;
}
private void Normalize()
{
float mta = _m11 * _m11 + _m12 * _m12;
if (mta != 1 && mta != 0)
{
float na = 1.0f / (float)Math.Sqrt(mta);
this._m11 *= na;
this._m12 *= na;
}
float mtb = _m21 * _m21 + _m22 * _m22;
if (mtb != 1 && mtb != 0)
{
float nb = 1.0f / (float)Math.Sqrt(mtb);
this._m11 *= nb;
this._m12 *= nb;
}
}
#endregion
#region Overrides
public override string ToString()
{
return string.Format("{0:r}, {1:r}, {2:r}, {3:r}, {4:r}, {5:r}",
this._m11, this._m12, this._m21, this._m22, this._dx, this._dy);
}
public override int GetHashCode()
{
return _m11.GetHashCode() ^ _m12.GetHashCode()
^ _m21.GetHashCode() ^ _m22.GetHashCode()
^ _dx.GetHashCode() ^ _dy.GetHashCode();
}
public override bool Equals(object obj)
{
if(object.ReferenceEquals(obj, null))
return false;
if (obj is Matrix)
return Equals((Matrix)obj);
return false;
}
#endregion
#region Static Properties
// A static readonly field doesn't work here since Matrix is mutable.
/// <summary>
/// Returns an identity (1,0,0,1,0,0) matrix.
/// </summary>
public static Matrix Identity
{
get { return new Matrix(); }
}
#endregion
#region ICloneable<Matrix> Members
/// <summary>
/// Creates an exact copy of this matrix.
/// </summary>
/// <returns> A cloned matrix. </returns>
public Matrix Clone()
{
return new Matrix(this._m11, this._m12, this._m21, this._m22, this._dx, this._dy);
}
#endregion
#region IDisposable Members
public void Dispose()
{
// This is here solely for compatibility with the Matrix class on the desktop.
}
#endregion
#region IEquatable<Matrix> Members
public bool Equals(Matrix other)
{
return
this._dx == other._dx &&
this._dy == other._dy &&
this._m11 == other._m11 &&
this._m12 == other._m12 &&
this._m21 == other._m21 &&
this._m22 == other._m22;
}
#endregion
}
}
#endif