-
Notifications
You must be signed in to change notification settings - Fork 310
/
AffineTransformation.cs
1109 lines (1047 loc) · 44.9 KB
/
AffineTransformation.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
namespace NetTopologySuite.Geometries.Utilities
{
/// <summary>
/// Represents an affine transformation on the 2D Cartesian plane.
/// </summary>
/// <remarks>
/// <para>
/// It can be used to transform a <see cref="Coordinate"/> or <see cref="Geometry"/>.
/// An affine transformation is a mapping of the 2D plane into itself
/// via a series of transformations of the following basic types:
/// <ul>
/// <li>reflection (through a line)</li>
/// <li>rotation (around the origin)</li>
/// <li>scaling (relative to the origin)</li>
/// <li>shearing (in both the X and Y directions)</li>
/// <li>translation</li>
/// </ul>
/// </para>
/// <para>
/// In general, affine transformations preserve straightness and parallel lines,
/// but do not preserve distance or shape.
/// </para>
/// <para>
/// An affine transformation can be represented by a 3x3
/// matrix in the following form:
/// <blockquote><code>
/// T = | m00 m01 m02 |<br/>
/// | m10 m11 m12 |<br/>
/// | 0 0 1 |
/// </code></blockquote>
/// A coordinate P = (x, y) can be transformed to a new coordinate P' = (x', y')
/// by representing it as a 3x1 matrix and using matrix multiplication to compute:
/// <blockquote><code>
/// | x' | = T x | x |<br/>
/// | y' | | y |<br/>
/// | 1 | | 1 |
/// </code></blockquote>
/// </para>
/// <h3>Transformation Composition</h3>
/// <para>
/// Affine transformations can be composed using the <see cref="Compose"/> method.
/// Composition is computed via multiplication of the
/// transformation matrices, and is defined as:
/// <blockquote><pre>
/// A.compose(B) = T<sub>B</sub> x T<sub>A</sub>
/// </pre></blockquote>
/// </para>
/// <para>
/// This produces a transformation whose effect is that of A followed by B.
/// The methods <see cref="Reflect"/>, <see cref="Rotate"/>,
/// <see cref="Scale"/>, <see cref="Shear"/>, and <see cref="Translate"/>
/// have the effect of composing a transformation of that type with
/// the transformation they are invoked on.
/// The composition of transformations is in general <i>not</i> commutative.
/// </para>
/// <h3>Transformation Inversion</h3>
/// <para>
/// Affine transformations may be invertible or non-invertible.
/// If a transformation is invertible, then there exists
/// an inverse transformation which when composed produces
/// the identity transformation.
/// The <see cref="GetInverse"/> method
/// computes the inverse of a transformation, if one exists.
/// </para>
/// <para>
/// @author Martin Davis
/// </para>
/// </remarks>
public class AffineTransformation : ICloneable, ICoordinateSequenceFilter, IEquatable<AffineTransformation>
{
/// <summary>
/// Creates a transformation for a reflection about the
/// line (x0,y0) - (x1,y1).
/// </summary>
/// <param name="x0"> the x-ordinate of a point on the reflection line</param>
/// <param name="y0"> the y-ordinate of a point on the reflection line</param>
/// <param name="x1"> the x-ordinate of a another point on the reflection line</param>
/// <param name="y1"> the y-ordinate of a another point on the reflection line</param>
/// <returns> a transformation for the reflection</returns>
public static AffineTransformation ReflectionInstance(double x0, double y0, double x1, double y1)
{
var trans = new AffineTransformation();
trans.SetToReflection(x0, y0, x1, y1);
return trans;
}
/// <summary>
/// Creates a transformation for a reflection about the
/// line (0,0) - (x,y).
/// </summary>
/// <param name="x"> the x-ordinate of a point on the reflection line</param>
/// <param name="y"> the y-ordinate of a point on the reflection line</param>
/// <returns> a transformation for the reflection</returns>
public static AffineTransformation ReflectionInstance(double x, double y)
{
var trans = new AffineTransformation();
trans.SetToReflection(x, y);
return trans;
}
/// <summary>
/// Creates a transformation for a rotation
/// about the origin
/// by an angle <i>theta</i>.
/// </summary>
/// <remarks>
/// Positive angles correspond to a rotation
/// in the counter-clockwise direction.
/// </remarks>
/// <param name="theta"> the rotation angle, in radians</param>
/// <returns> a transformation for the rotation</returns>
public static AffineTransformation RotationInstance(double theta)
{
return RotationInstance(Math.Sin(theta), Math.Cos(theta));
}
/// <summary>
/// Creates a transformation for a rotation
/// by an angle <i>theta</i>,
/// specified by the sine and cosine of the angle.
/// </summary>
/// <remarks>
/// This allows providing exact values for sin(theta) and cos(theta)
/// for the common case of rotations of multiples of quarter-circles.
/// </remarks>
/// <param name="sinTheta"> the sine of the rotation angle</param>
/// <param name="cosTheta"> the cosine of the rotation angle</param>
/// <returns> a transformation for the rotation</returns>
public static AffineTransformation RotationInstance(double sinTheta, double cosTheta)
{
var trans = new AffineTransformation();
trans.SetToRotation(sinTheta, cosTheta);
return trans;
}
/// <summary>
/// Creates a transformation for a rotation
/// about the point (x,y) by an angle <i>theta</i>.
/// </summary>
/// <remarks>
/// Positive angles correspond to a rotation
/// in the counter-clockwise direction.
/// </remarks>
/// <param name="theta"> the rotation angle, in radians</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> a transformation for the rotation</returns>
public static AffineTransformation RotationInstance(double theta, double x, double y)
{
return RotationInstance(Math.Sin(theta), Math.Cos(theta), x, y);
}
/// <summary>
/// Creates a transformation for a rotation
/// about the point (x,y) by an angle <i>theta</i>,
/// specified by the sine and cosine of the angle.
/// </summary>
/// <remarks>
/// This allows providing exact values for sin(theta) and cos(theta)
/// for the common case of rotations of multiples of quarter-circles.
/// </remarks>
/// <param name="sinTheta"> the sine of the rotation angle</param>
/// <param name="cosTheta"> the cosine of the rotation angle</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> a transformation for the rotation</returns>
public static AffineTransformation RotationInstance(double sinTheta, double cosTheta, double x, double y)
{
var trans = new AffineTransformation();
trans.SetToRotation(sinTheta, cosTheta, x, y);
return trans;
}
/// <summary>
/// Creates a transformation for a scaling relative to the origin.
/// </summary>
/// <param name="xScale"> the value to scale by in the x direction</param>
/// <param name="yScale"> the value to scale by in the y direction</param>
/// <returns> a transformation for the scaling</returns>
public static AffineTransformation ScaleInstance(double xScale, double yScale)
{
var trans = new AffineTransformation();
trans.SetToScale(xScale, yScale);
return trans;
}
/// <summary>
/// Creates a transformation for a scaling relative to the point (x,y).
/// </summary>
/// <param name="xScale">The value to scale by in the x direction</param>
/// <param name="yScale">The value to scale by in the y direction</param>
/// <param name="x">The x-ordinate of the point to scale around</param>
/// <param name="y">The y-ordinate of the point to scale around</param>
/// <returns>A transformation for the scaling</returns>
public static AffineTransformation ScaleInstance(double xScale, double yScale, double x, double y)
{
var trans = new AffineTransformation();
trans.Translate(-x, -y);
trans.Scale(xScale, yScale);
trans.Translate(x, y);
return trans;
}
/// <summary>
/// Creates a transformation for a shear.
/// </summary>
/// <param name="xShear"> the value to shear by in the x direction</param>
/// <param name="yShear"> the value to shear by in the y direction</param>
/// <returns> a transformation for the shear</returns>
public static AffineTransformation ShearInstance(double xShear, double yShear)
{
var trans = new AffineTransformation();
trans.SetToShear(xShear, yShear);
return trans;
}
/// <summary>
/// Creates a transformation for a translation.
/// </summary>
/// <param name="x"> the value to translate by in the x direction</param>
/// <param name="y"> the value to translate by in the y direction</param>
/// <returns> a transformation for the translation</returns>
public static AffineTransformation TranslationInstance(double x, double y)
{
var trans = new AffineTransformation();
trans.SetToTranslation(x, y);
return trans;
}
// affine matrix entries
// (bottom row is always [ 0 0 1 ])
private double _m00;
private double _m01;
private double _m02;
private double _m10;
private double _m11;
private double _m12;
/// <summary>
/// Constructs a new identity transformation
/// </summary>
public AffineTransformation()
{
SetToIdentity();
}
/// <summary>
/// Constructs a new transformation whose
/// matrix has the specified values.
/// </summary>
/// <param name="matrix"> an array containing the 6 values { m00, m01, m02, m10, m11, m12 }</param>
/// <exception cref="NullReferenceException"> if matrix is null</exception>
/// <exception cref="IndexOutOfRangeException"> if matrix is too small</exception>
public AffineTransformation(double[] matrix)
{
_m00 = matrix[0];
_m01 = matrix[1];
_m02 = matrix[2];
_m10 = matrix[3];
_m11 = matrix[4];
_m12 = matrix[5];
}
/// <summary>
/// Constructs a new transformation whose
/// matrix has the specified values.
/// </summary>
/// <param name="m00"> the entry for the [0, 0] element in the transformation matrix</param>
/// <param name="m01"> the entry for the [0, 1] element in the transformation matrix</param>
/// <param name="m02"> the entry for the [0, 2] element in the transformation matrix</param>
/// <param name="m10"> the entry for the [1, 0] element in the transformation matrix</param>
/// <param name="m11"> the entry for the [1, 1] element in the transformation matrix</param>
/// <param name="m12"> the entry for the [1, 2] element in the transformation matrix</param>
public AffineTransformation(double m00,
double m01,
double m02,
double m10,
double m11,
double m12)
{
SetTransformation(m00, m01, m02, m10, m11, m12);
}
/// <summary>
/// Constructs a transformation which is
/// a copy of the given one.
/// </summary>
/// <param name="trans"> the transformation to copy</param>
public AffineTransformation(AffineTransformation trans)
{
SetTransformation(trans);
}
/// <summary>
/// Constructs a transformation
/// which maps the given source
/// points into the given destination points.
/// </summary>
/// <param name="src0"> source point 0</param>
/// <param name="src1"> source point 1</param>
/// <param name="src2"> source point 2</param>
/// <param name="dest0"> the mapped point for source point 0</param>
/// <param name="dest1"> the mapped point for source point 1</param>
/// <param name="dest2"> the mapped point for source point 2</param>
public AffineTransformation(Coordinate src0,
Coordinate src1,
Coordinate src2,
Coordinate dest0,
Coordinate dest1,
Coordinate dest2)
: this(new AffineTransformationBuilder(src0, src1, src2, dest0, dest1, dest2).GetTransformation())
{
}
/// <summary>
/// Sets this transformation to be the identity transformation.
/// </summary>
/// <remarks>
/// The identity transformation has the matrix:
/// <blockquote><code>
/// | 1 0 0 |<br/>
/// | 0 1 0 |<br/>
/// | 0 0 1 |
/// </code></blockquote>
/// </remarks>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToIdentity()
{
_m00 = 1.0; _m01 = 0.0; _m02 = 0.0;
_m10 = 0.0; _m11 = 1.0; _m12 = 0.0;
return this;
}
/// <summary>
/// Sets this transformation's matrix to have the given values.
/// </summary>
/// <param name="m00"> the entry for the [0, 0] element in the transformation matrix</param>
/// <param name="m01"> the entry for the [0, 1] element in the transformation matrix</param>
/// <param name="m02"> the entry for the [0, 2] element in the transformation matrix</param>
/// <param name="m10"> the entry for the [1, 0] element in the transformation matrix</param>
/// <param name="m11"> the entry for the [1, 1] element in the transformation matrix</param>
/// <param name="m12"> the entry for the [1, 2] element in the transformation matrix</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetTransformation(double m00,
double m01,
double m02,
double m10,
double m11,
double m12)
{
_m00 = m00;
_m01 = m01;
_m02 = m02;
_m10 = m10;
_m11 = m11;
_m12 = m12;
return this;
}
/// <summary>
/// Sets this transformation to be a copy of the given one
/// </summary>
/// <param name="trans"> a transformation to copy</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetTransformation(AffineTransformation trans)
{
_m00 = trans._m00; _m01 = trans._m01; _m02 = trans._m02;
_m10 = trans._m10; _m11 = trans._m11; _m12 = trans._m12;
return this;
}
/// <summary>
/// Gets an array containing the entries
/// of the transformation matrix.
/// </summary>
/// <remarks>
/// Only the 6 non-trivial entries are returned,
/// in the sequence:
/// <pre>
/// m00, m01, m02, m10, m11, m12
/// </pre>
/// </remarks>
/// <returns> an array of length 6</returns>
public double[] MatrixEntries => new[] { _m00, _m01, _m02, _m10, _m11, _m12 };
/// <summary>
/// Computes the determinant of the transformation matrix.
/// </summary>
/// <remarks>
/// <para>
/// The determinant is computed as:
/// <blockquote><code>
/// | m00 m01 m02 |<br/>
/// | m10 m11 m12 | = m00 * m11 - m01 * m10<br/>
/// | 0 0 1 |
/// </code></blockquote>
/// </para>
/// <para>
/// If the determinant is zero,
/// the transform is singular (not invertible),
/// and operations which attempt to compute
/// an inverse will throw a <see cref="NoninvertibleTransformationException"/>.
/// </para>
/// </remarks>
/// <returns> the determinant of the transformation</returns>
/// <see cref="GetInverse()" />
///
/// <returns>The determinant of the transformation</returns>
/// <see cref="GetInverse"/>
public double Determinant => _m00 * _m11 - _m01 * _m10;
/// <summary>
/// Computes the inverse of this transformation, if one
/// exists.
/// </summary>
/// <remarks>
/// <para>
/// The inverse is the transformation which when
/// composed with this one produces the identity
/// transformation.
/// A transformation has an inverse if and only if it
/// is not singular (i.e. its
/// determinant is non-zero).
/// Geometrically, an transformation is non-invertible
/// if it maps the plane to a line or a point.
/// If no inverse exists this method
/// will throw a <see cref="NoninvertibleTransformationException"/>.
/// </para>
/// <para>
/// The matrix of the inverse is equal to the
/// inverse of the matrix for the transformation.
/// It is computed as follows:
/// <blockquote><code>
/// 1
/// inverse(A) = --- x adjoint(A)
/// det
///
///
/// = 1 | m11 -m01 m01*m12-m02*m11 |
/// --- x | -m10 m00 -m00*m12+m10*m02 |
/// det | 0 0 m00*m11-m10*m01 |
///
///
///
/// = | m11/det -m01/det m01*m12-m02*m11/det |
/// | -m10/det m00/det -m00*m12+m10*m02/det |
/// | 0 0 1 |
/// </code></blockquote>
/// </para>
/// </remarks>
/// <returns>A new inverse transformation</returns>
/// <see cref="Determinant"/>
/// <exception cref="NoninvertibleTransformationException"></exception>
public AffineTransformation GetInverse()
{
double det = Determinant;
if (det == 0)
throw new NoninvertibleTransformationException("Transformation is non-invertible");
double im00 = _m11 / det;
double im10 = -_m10 / det;
double im01 = -_m01 / det;
double im11 = _m00 / det;
double im02 = (_m01 * _m12 - _m02 * _m11) / det;
double im12 = (-_m00 * _m12 + _m10 * _m02) / det;
return new AffineTransformation(im00, im01, im02, im10, im11, im12);
}
/// <summary>
/// Explicitly computes the math for a reflection. May not work.
/// </summary>
/// <param name="x0">The x-ordinate of one point on the reflection line</param>
/// <param name="y0">The y-ordinate of one point on the reflection line</param>
/// <param name="x1">The x-ordinate of another point on the reflection line</param>
/// <param name="y1">The y-ordinate of another point on the reflection line</param>
/// <returns>This transformation with an updated matrix</returns>
public AffineTransformation SetToReflectionBasic(double x0, double y0, double x1, double y1)
{
if (x0 == x1 && y0 == y1)
{
throw new ArgumentException("Reflection line points must be distinct");
}
double dx = x1 - x0;
double dy = y1 - y0;
double d = Math.Sqrt(dx * dx + dy * dy);
double sin = dy / d;
double cos = dx / d;
double cs2 = 2 * sin * cos;
double c2s2 = cos * cos - sin * sin;
_m00 = c2s2; _m01 = cs2; _m02 = 0.0;
_m10 = cs2; _m11 = -c2s2; _m12 = 0.0;
return this;
}
/// <summary>
/// Sets this transformation to be a reflection about the line defined by a line <tt>(x0,y0) - (x1,y1)</tt>.
/// </summary>
/// <param name="x0">The x-ordinate of one point on the reflection line</param>
/// <param name="y0">The y-ordinate of one point on the reflection line</param>
/// <param name="x1">The x-ordinate of another point on the reflection line</param>
/// <param name="y1">The y-ordinate of another point on the reflection line</param>
/// <returns>This transformation with an updated matrix</returns>
public AffineTransformation SetToReflection(double x0, double y0, double x1, double y1)
{
if (x0 == x1 && y0 == y1)
{
throw new ArgumentException("Reflection line points must be distinct");
}
// translate line vector to origin
SetToTranslation(-x0, -y0);
// rotate vector to positive x axis direction
double dx = x1 - x0;
double dy = y1 - y0;
double d = Math.Sqrt(dx * dx + dy * dy);
double sin = dy / d;
double cos = dx / d;
Rotate(-sin, cos);
// reflect about the x axis
Scale(1, -1);
// rotate back
Rotate(sin, cos);
// translate back
Translate(x0, y0);
return this;
}
/// <summary>
/// Sets this transformation to be a reflection
/// about the line defined by vector (x,y).
/// </summary>
/// <remarks>
/// The transformation for a reflection
/// is computed by:
/// <blockquote><code>
/// d = sqrt(x<sup>2</sup> + y<sup>2</sup>)
/// sin = x / d;
/// cos = x / d;
/// T<sub>ref</sub> = T<sub>rot(sin, cos)</sub> x T<sub>scale(1, -1)</sub> x T<sub>rot(-sin, cos)</sub>
/// </code></blockquote>
/// </remarks>
/// <param name="x"> the x-component of the reflection line vector</param>
/// <param name="y"> the y-component of the reflection line vector</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToReflection(double x, double y)
{
if (x == 0.0 && y == 0.0)
{
throw new ArgumentException("Reflection vector must be non-zero");
}
/*
* Handle special case - x = y.
* This case is specified explicitly to avoid round off error.
*/
if (x == y)
{
_m00 = 0.0;
_m01 = 1.0;
_m02 = 0.0;
_m10 = 1.0;
_m11 = 0.0;
_m12 = 0.0;
return this;
}
// rotate vector to positive x axis direction
double d = Math.Sqrt(x * x + y * y);
double sin = y / d;
double cos = x / d;
Rotate(-sin, cos);
// reflect about the x-axis
Scale(1, -1);
// rotate back
Rotate(sin, cos);
return this;
}
/// <summary>
/// Sets this transformation to be a rotation around the orign.
/// </summary>
/// <remarks>
/// A positive rotation angle corresponds
/// to a counter-clockwise rotation.
/// The transformation matrix for a rotation
/// by an angle <c>theta</c>
/// has the value:
/// <blockquote><pre>
/// | cos(theta) -sin(theta) 0 |
/// | sin(theta) cos(theta) 0 |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="theta"> the rotation angle, in radians</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToRotation(double theta)
{
SetToRotation(Math.Sin(theta), Math.Cos(theta));
return this;
}
/// <summary>
/// Sets this transformation to be a rotation around the origin
/// by specifying the sin and cos of the rotation angle directly.
/// </summary>
/// <remarks>
/// The transformation matrix for the rotation
/// has the value:
/// <blockquote><pre>
/// | cosTheta -sinTheta 0 |
/// | sinTheta cosTheta 0 |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="sinTheta"> the sine of the rotation angle</param>
/// <param name="cosTheta"> the cosine of the rotation angle</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToRotation(double sinTheta, double cosTheta)
{
_m00 = cosTheta; _m01 = -sinTheta; _m02 = 0.0;
_m10 = sinTheta; _m11 = cosTheta; _m12 = 0.0;
return this;
}
/// <summary>
/// Sets this transformation to be a rotation
/// around a given point (x,y).
/// </summary>
/// <remarks>
/// A positive rotation angle corresponds
/// to a counter-clockwise rotation.
/// The transformation matrix for a rotation
/// by an angle <paramref name="theta" />
/// has the value:
/// <blockquote><pre>
/// | cosTheta -sinTheta x-x*cos+y*sin |
/// | sinTheta cosTheta y-x*sin-y*cos |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="theta"> the rotation angle, in radians</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToRotation(double theta, double x, double y)
{
SetToRotation(Math.Sin(theta), Math.Cos(theta), x, y);
return this;
}
/// <summary>
/// Sets this transformation to be a rotation
/// around a given point (x,y)
/// by specifying the sin and cos of the rotation angle directly.
/// </summary>
/// <remarks>
/// The transformation matrix for the rotation
/// has the value:
/// <blockquote><pre>
/// | cosTheta -sinTheta x-x*cos+y*sin |
/// | sinTheta cosTheta y-x*sin-y*cos |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="sinTheta"> the sine of the rotation angle</param>
/// <param name="cosTheta"> the cosine of the rotation angle</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToRotation(double sinTheta, double cosTheta, double x, double y)
{
_m00 = cosTheta; _m01 = -sinTheta; _m02 = x - x * cosTheta + y * sinTheta;
_m10 = sinTheta; _m11 = cosTheta; _m12 = y - x * sinTheta - y * cosTheta;
return this;
}
/// <summary>
/// Sets this transformation to be a scaling.
/// </summary>
/// <remarks>
/// The transformation matrix for a scale
/// has the value:
/// <blockquote><pre>
/// | xScale 0 dx |
/// | 0 yScale dy |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="xScale"> the amount to scale x-ordinates by</param>
/// <param name="yScale"> the amount to scale y-ordinates by</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToScale(double xScale, double yScale)
{
_m00 = xScale; _m01 = 0.0; _m02 = 0.0;
_m10 = 0.0; _m11 = yScale; _m12 = 0.0;
return this;
}
/// <summary>
/// Sets this transformation to be a shear.
/// </summary>
/// <remarks>
/// The transformation matrix for a shear
/// has the value:
/// <blockquote><pre>
/// | 1 xShear 0 |
/// | yShear 1 0 |
/// | 0 0 1 |
/// </pre></blockquote>
/// Note that a shear of (1, 1) is <i>not</i>
/// equal to shear(1, 0) composed with shear(0, 1).
/// Instead, shear(1, 1) corresponds to a mapping onto the
/// line x = y.
/// </remarks>
/// <param name="xShear"> the x component to shear by</param>
/// <param name="yShear"> the y component to shear by</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToShear(double xShear, double yShear)
{
_m00 = 1.0; _m01 = xShear; _m02 = 0.0;
_m10 = yShear; _m11 = 1.0; _m12 = 0.0;
return this;
}
/// <summary>
/// Sets this transformation to be a translation.
/// </summary>
/// <remarks>
/// For a translation by the vector (x, y)
/// the transformation matrix has the value:
/// <blockquote><pre>
/// | 1 0 dx |
/// | 1 0 dy |
/// | 0 0 1 |
/// </pre></blockquote>
/// </remarks>
/// <param name="dx"> the x component to translate by</param>
/// <param name="dy"> the y component to translate by</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation SetToTranslation(double dx, double dy)
{
_m00 = 1.0; _m01 = 0.0; _m02 = dx;
_m10 = 0.0; _m11 = 1.0; _m12 = dy;
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a reflection transformation composed
/// with the current value.
/// </summary>
/// <param name="x0"> the x-ordinate of a point on the line to reflect around</param>
/// <param name="y0"> the y-ordinate of a point on the line to reflect around</param>
/// <param name="x1"> the x-ordinate of a point on the line to reflect around</param>
/// <param name="y1"> the y-ordinate of a point on the line to reflect around</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Reflect(double x0, double y0, double x1, double y1)
{
Compose(ReflectionInstance(x0, y0, x1, y1));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a reflection transformation composed
/// with the current value.
/// </summary>
/// <param name="x"> the x-ordinate of the line to reflect around</param>
/// <param name="y"> the y-ordinate of the line to reflect around</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Reflect(double x, double y)
{
Compose(ReflectionInstance(x, y));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a rotation transformation composed
/// with the current value.
/// </summary>
/// <remarks>
/// Positive angles correspond to a rotation
/// in the counter-clockwise direction.
/// </remarks>
/// <param name="theta"> the angle to rotate by in radians</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Rotate(double theta)
{
Compose(RotationInstance(theta));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a rotation around the origin composed
/// with the current value,
/// with the sin and cos of the rotation angle specified directly.
/// </summary>
/// <param name="sinTheta"> the sine of the angle to rotate by</param>
/// <param name="cosTheta"> the cosine of the angle to rotate by</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Rotate(double sinTheta, double cosTheta)
{
Compose(RotationInstance(sinTheta, cosTheta));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a rotation around a given point composed
/// with the current value.
/// </summary>
/// <remarks>
/// Positive angles correspond to a rotation
/// in the counter-clockwise direction.
/// </remarks>
/// <param name="theta"> the angle to rotate by, in radians</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Rotate(double theta, double x, double y)
{
Compose(RotationInstance(theta, x, y));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a rotation around a given point composed
/// with the current value,
/// with the sin and cos of the rotation angle specified directly.
/// </summary>
/// <param name="sinTheta"> the sine of the angle to rotate by</param>
/// <param name="cosTheta"> the cosine of the angle to rotate by</param>
/// <param name="x"> the x-ordinate of the rotation point</param>
/// <param name="y"> the y-ordinate of the rotation point</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Rotate(double sinTheta, double cosTheta, double x, double y)
{
Compose(RotationInstance(sinTheta, cosTheta, x, y));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a scale transformation composed
/// with the current value.
/// </summary>
/// <param name="xScale"> the value to scale by in the x direction</param>
/// <param name="yScale"> the value to scale by in the y direction</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Scale(double xScale, double yScale)
{
Compose(ScaleInstance(xScale, yScale));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a shear transformation composed
/// with the current value.
/// </summary>
/// <param name="xShear"> the value to shear by in the x direction</param>
/// <param name="yShear"> the value to shear by in the y direction</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Shear(double xShear, double yShear)
{
Compose(ShearInstance(xShear, yShear));
return this;
}
/// <summary>
/// Updates the value of this transformation
/// to that of a translation transformation composed
/// with the current value.
/// </summary>
/// <param name="x"> the value to translate by in the x direction</param>
/// <param name="y"> the value to translate by in the y direction</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Translate(double x, double y)
{
Compose(TranslationInstance(x, y));
return this;
}
/// <summary>
/// Updates this transformation to be
/// the composition of this transformation with the given <see cref="AffineTransformation" />.
/// </summary>
/// <remarks>
/// This produces a transformation whose effect
/// is equal to applying this transformation
/// followed by the argument transformation.
/// Mathematically,
/// <blockquote><pre>
/// A.compose(B) = T<sub>B</sub> x T<sub>A</sub>
/// </pre></blockquote>
/// </remarks>
/// <param name="trans"> an affine transformation</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation Compose(AffineTransformation trans)
{
double mp00 = trans._m00 * _m00 + trans._m01 * _m10;
double mp01 = trans._m00 * _m01 + trans._m01 * _m11;
double mp02 = trans._m00 * _m02 + trans._m01 * _m12 + trans._m02;
double mp10 = trans._m10 * _m00 + trans._m11 * _m10;
double mp11 = trans._m10 * _m01 + trans._m11 * _m11;
double mp12 = trans._m10 * _m02 + trans._m11 * _m12 + trans._m12;
_m00 = mp00;
_m01 = mp01;
_m02 = mp02;
_m10 = mp10;
_m11 = mp11;
_m12 = mp12;
return this;
}
/// <summary>
/// Updates this transformation to be the composition
/// of a given <see cref="AffineTransformation" /> with this transformation.
/// </summary>
/// <remarks>
/// This produces a transformation whose effect
/// is equal to applying the argument transformation
/// followed by this transformation.
/// Mathematically,
/// <blockquote><pre>
/// A.composeBefore(B) = T<sub>A</sub> x T<sub>B</sub>
/// </pre></blockquote>
/// </remarks>
/// <param name="trans"> an affine transformation</param>
/// <returns> this transformation, with an updated matrix</returns>
public AffineTransformation ComposeBefore(AffineTransformation trans)
{
double mp00 = _m00 * trans._m00 + _m01 * trans._m10;
double mp01 = _m00 * trans._m01 + _m01 * trans._m11;
double mp02 = _m00 * trans._m02 + _m01 * trans._m12 + _m02;
double mp10 = _m10 * trans._m00 + _m11 * trans._m10;
double mp11 = _m10 * trans._m01 + _m11 * trans._m11;
double mp12 = _m10 * trans._m02 + _m11 * trans._m12 + _m12;
_m00 = mp00;
_m01 = mp01;
_m02 = mp02;
_m10 = mp10;
_m11 = mp11;
_m12 = mp12;
return this;
}
/// <summary>
/// Applies this transformation to the <paramref name="src" /> coordinate
/// and places the results in the <paramref name="dest" /> coordinate
/// (which may be the same as the source).
/// </summary>
/// <param name="src"> the coordinate to transform</param>
/// <param name="dest"> the coordinate to accept the results</param>
/// <returns> the <c>dest</c> coordinate</returns>
///
public Coordinate Transform(Coordinate src, Coordinate dest)
{
double xp = _m00 * src.X + _m01 * src.Y + _m02;
double yp = _m10 * src.X + _m11 * src.Y + _m12;
dest.X = xp;
dest.Y = yp;
return dest;
}
/// <summary>
/// Creates a new <see cref="Geometry"/> which is the result of this transformation applied to the input Geometry.
/// </summary>
/// <param name="g">A <c>Geometry</c></param>
/// <returns>The transformed Geometry</returns>
public Geometry Transform(Geometry g)
{
var g2 = g.Copy();
g2.Apply(this);
return g2;
}
/// <summary>
/// Applies this transformation to the i'th coordinate
/// in the given CoordinateSequence.
/// </summary>
/// <param name="seq"> a <c>CoordinateSequence</c></param>
/// <param name="i"> the index of the coordinate to transform</param>
public void Transform(CoordinateSequence seq, int i)
{
double xp = _m00 * seq.GetOrdinate(i, 0) + _m01 * seq.GetOrdinate(i, 1) + _m02;
double yp = _m10 * seq.GetOrdinate(i, 0) + _m11 * seq.GetOrdinate(i, 1) + _m12;
seq.SetOrdinate(i, 0, xp);
seq.SetOrdinate(i, 1, yp);
}
/// <summary>
/// Transforms the i'th coordinate in the input sequence
/// </summary>