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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.graphics;
import com.android.layoutlib.bridge.impl.DelegateManager;
import android.graphics.Matrix.ScaleToFit;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
/**
* Delegate implementing the native methods of android.graphics.Matrix
*
* Through the layoutlib_create tool, the original native methods of Matrix have been replaced
* by calls to methods of the same name in this delegate class.
*
* This class behaves like the original native implementation, but in Java, keeping previously
* native data into its own objects and mapping them to int that are sent back and forth between
* it and the original Matrix class.
*
* @see DelegateManager
*
*/
public final class Matrix_Delegate {
private final static int MATRIX_SIZE = 9;
// ---- delegate manager ----
private static final DelegateManager<Matrix_Delegate> sManager =
new DelegateManager<Matrix_Delegate>();
// ---- delegate data ----
private float mValues[] = new float[MATRIX_SIZE];
// ---- Public Helper methods ----
public static Matrix_Delegate getDelegate(int native_instance) {
return sManager.getDelegate(native_instance);
}
/**
* Returns an {@link AffineTransform} matching the given Matrix.
*/
public static AffineTransform getAffineTransform(Matrix m) {
Matrix_Delegate delegate = sManager.getDelegate(m.native_instance);
if (delegate == null) {
assert false;
return null;
}
return delegate.getAffineTransform();
}
public static boolean hasPerspective(Matrix m) {
Matrix_Delegate delegate = sManager.getDelegate(m.native_instance);
if (delegate == null) {
assert false;
return false;
}
return delegate.hasPerspective();
}
/**
* Sets the content of the matrix with the content of another matrix.
*/
public void set(Matrix_Delegate matrix) {
System.arraycopy(matrix.mValues, 0, mValues, 0, MATRIX_SIZE);
}
/**
* Resets the matrix to be the identity matrix.
*/
public void reset() {
reset(mValues);
}
/**
* Returns whether or not the matrix is identity.
*/
public boolean isIdentity() {
for (int i = 0, k = 0; i < 3; i++) {
for (int j = 0; j < 3; j++, k++) {
if (mValues[k] != ((i==j) ? 1 : 0)) {
return false;
}
}
}
return true;
}
public static Matrix_Delegate make(AffineTransform matrix) {
float[] values = new float[MATRIX_SIZE];
values[0] = (float) matrix.getScaleX();
values[1] = (float) matrix.getShearX();
values[2] = (float) matrix.getTranslateX();
values[3] = (float) matrix.getShearY();
values[4] = (float) matrix.getScaleY();
values[5] = (float) matrix.getTranslateY();
values[6] = 0.f;
values[7] = 0.f;
values[8] = 1.f;
return new Matrix_Delegate(values);
}
public boolean mapRect(RectF dst, RectF src) {
// array with 4 corners
float[] corners = new float[] {
src.left, src.top,
src.right, src.top,
src.right, src.bottom,
src.left, src.bottom,
};
// apply the transform to them.
mapPoints(corners);
// now put the result in the rect. We take the min/max of Xs and min/max of Ys
dst.left = Math.min(Math.min(corners[0], corners[2]), Math.min(corners[4], corners[6]));
dst.right = Math.max(Math.max(corners[0], corners[2]), Math.max(corners[4], corners[6]));
dst.top = Math.min(Math.min(corners[1], corners[3]), Math.min(corners[5], corners[7]));
dst.bottom = Math.max(Math.max(corners[1], corners[3]), Math.max(corners[5], corners[7]));
return (computeTypeMask() & kRectStaysRect_Mask) != 0;
}
/**
* Returns an {@link AffineTransform} matching the matrix.
*/
public AffineTransform getAffineTransform() {
return getAffineTransform(mValues);
}
public boolean hasPerspective() {
return (mValues[6] != 0 || mValues[7] != 0 || mValues[8] != 1);
}
// ---- native methods ----
/*package*/ static int native_create(int native_src_or_zero) {
// create the delegate
Matrix_Delegate newDelegate = new Matrix_Delegate();
// copy from values if needed.
if (native_src_or_zero > 0) {
Matrix_Delegate oldDelegate = sManager.getDelegate(native_src_or_zero);
if (oldDelegate != null) {
System.arraycopy(
oldDelegate.mValues, 0,
newDelegate.mValues, 0,
MATRIX_SIZE);
}
}
return sManager.addDelegate(newDelegate);
}
/*package*/ static boolean native_isIdentity(int native_object) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
return d.isIdentity();
}
/*package*/ static boolean native_rectStaysRect(int native_object) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return true;
}
return (d.computeTypeMask() & kRectStaysRect_Mask) != 0;
}
/*package*/ static void native_reset(int native_object) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
reset(d.mValues);
}
/*package*/ static void native_set(int native_object, int other) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
Matrix_Delegate src = sManager.getDelegate(other);
if (src == null) {
assert false;
return;
}
System.arraycopy(src.mValues, 0, d.mValues, 0, MATRIX_SIZE);
}
/*package*/ static void native_setTranslate(int native_object, float dx, float dy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
setTranslate(d.mValues, dx, dy);
}
/*package*/ static void native_setScale(int native_object, float sx, float sy,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
d.mValues = getScale(sx, sy, px, py);
}
/*package*/ static void native_setScale(int native_object, float sx, float sy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
d.mValues[0] = sx;
d.mValues[1] = 0;
d.mValues[2] = 0;
d.mValues[3] = 0;
d.mValues[4] = sy;
d.mValues[5] = 0;
d.mValues[6] = 0;
d.mValues[7] = 0;
d.mValues[8] = 1;
}
/*package*/ static void native_setRotate(int native_object, float degrees, float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
d.mValues = getRotate(degrees, px, py);
}
/*package*/ static void native_setRotate(int native_object, float degrees) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
setRotate(d.mValues, degrees);
}
/*package*/ static void native_setSinCos(int native_object, float sinValue, float cosValue,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
// TODO: do it in one pass
// translate so that the pivot is in 0,0
setTranslate(d.mValues, -px, -py);
// scale
d.postTransform(getRotate(sinValue, cosValue));
// translate back the pivot
d.postTransform(getTranslate(px, py));
}
/*package*/ static void native_setSinCos(int native_object, float sinValue, float cosValue) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
setRotate(d.mValues, sinValue, cosValue);
}
/*package*/ static void native_setSkew(int native_object, float kx, float ky,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
d.mValues = getSkew(kx, ky, px, py);
}
/*package*/ static void native_setSkew(int native_object, float kx, float ky) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
d.mValues[0] = 1;
d.mValues[1] = kx;
d.mValues[2] = -0;
d.mValues[3] = ky;
d.mValues[4] = 1;
d.mValues[5] = 0;
d.mValues[6] = 0;
d.mValues[7] = 0;
d.mValues[8] = 1;
}
/*package*/ static boolean native_setConcat(int native_object, int a, int b) {
if (a == native_object) {
return native_preConcat(native_object, b);
} else if (b == native_object) {
return native_postConcat(native_object, a);
}
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
Matrix_Delegate a_mtx = sManager.getDelegate(a);
if (a_mtx == null) {
assert false;
return false;
}
Matrix_Delegate b_mtx = sManager.getDelegate(b);
if (b_mtx == null) {
assert false;
return false;
}
multiply(d.mValues, a_mtx.mValues, b_mtx.mValues);
return true;
}
/*package*/ static boolean native_preTranslate(int native_object, float dx, float dy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getTranslate(dx, dy));
return true;
}
/*package*/ static boolean native_preScale(int native_object, float sx, float sy,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getScale(sx, sy, px, py));
return true;
}
/*package*/ static boolean native_preScale(int native_object, float sx, float sy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getScale(sx, sy));
return true;
}
/*package*/ static boolean native_preRotate(int native_object, float degrees,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getRotate(degrees, px, py));
return true;
}
/*package*/ static boolean native_preRotate(int native_object, float degrees) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
double rad = Math.toRadians(degrees);
float sin = (float)Math.sin(rad);
float cos = (float)Math.cos(rad);
d.preTransform(getRotate(sin, cos));
return true;
}
/*package*/ static boolean native_preSkew(int native_object, float kx, float ky,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getSkew(kx, ky, px, py));
return true;
}
/*package*/ static boolean native_preSkew(int native_object, float kx, float ky) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.preTransform(getSkew(kx, ky));
return true;
}
/*package*/ static boolean native_preConcat(int native_object, int other_matrix) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
Matrix_Delegate other = sManager.getDelegate(other_matrix);
if (d == null) {
assert false;
return false;
}
d.preTransform(other.mValues);
return true;
}
/*package*/ static boolean native_postTranslate(int native_object, float dx, float dy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getTranslate(dx, dy));
return true;
}
/*package*/ static boolean native_postScale(int native_object, float sx, float sy,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getScale(sx, sy, px, py));
return true;
}
/*package*/ static boolean native_postScale(int native_object, float sx, float sy) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getScale(sx, sy));
return true;
}
/*package*/ static boolean native_postRotate(int native_object, float degrees,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getRotate(degrees, px, py));
return true;
}
/*package*/ static boolean native_postRotate(int native_object, float degrees) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getRotate(degrees));
return true;
}
/*package*/ static boolean native_postSkew(int native_object, float kx, float ky,
float px, float py) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getSkew(kx, ky, px, py));
return true;
}
/*package*/ static boolean native_postSkew(int native_object, float kx, float ky) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
d.postTransform(getSkew(kx, ky));
return true;
}
/*package*/ static boolean native_postConcat(int native_object, int other_matrix) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
Matrix_Delegate other = sManager.getDelegate(other_matrix);
if (d == null) {
assert false;
return false;
}
d.postTransform(other.mValues);
return true;
}
/*package*/ static boolean native_setRectToRect(int native_object, RectF src,
RectF dst, int stf) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
if (src.isEmpty()) {
reset(d.mValues);
return false;
}
if (dst.isEmpty()) {
d.mValues[0] = d.mValues[1] = d.mValues[2] = d.mValues[3] = d.mValues[4] = d.mValues[5]
= d.mValues[6] = d.mValues[7] = 0;
d.mValues[8] = 1;
} else {
float tx, sx = dst.width() / src.width();
float ty, sy = dst.height() / src.height();
boolean xLarger = false;
if (stf != ScaleToFit.FILL.nativeInt) {
if (sx > sy) {
xLarger = true;
sx = sy;
} else {
sy = sx;
}
}
tx = dst.left - src.left * sx;
ty = dst.top - src.top * sy;
if (stf == ScaleToFit.CENTER.nativeInt || stf == ScaleToFit.END.nativeInt) {
float diff;
if (xLarger) {
diff = dst.width() - src.width() * sy;
} else {
diff = dst.height() - src.height() * sy;
}
if (stf == ScaleToFit.CENTER.nativeInt) {
diff = diff / 2;
}
if (xLarger) {
tx += diff;
} else {
ty += diff;
}
}
d.mValues[0] = sx;
d.mValues[4] = sy;
d.mValues[2] = tx;
d.mValues[5] = ty;
d.mValues[1] = d.mValues[3] = d.mValues[6] = d.mValues[7] = 0;
}
// shared cleanup
d.mValues[8] = 1;
return true;
}
/*package*/ static boolean native_setPolyToPoly(int native_object, float[] src, int srcIndex,
float[] dst, int dstIndex, int pointCount) {
// FIXME
throw new UnsupportedOperationException("Native delegate needed: Matrix_Delegate.native_setPolyToPoly");
}
/*package*/ static boolean native_invert(int native_object, int inverse) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
Matrix_Delegate inv_mtx = sManager.getDelegate(inverse);
if (inv_mtx == null) {
assert false;
return false;
}
try {
AffineTransform affineTransform = d.getAffineTransform();
AffineTransform inverseTransform = affineTransform.createInverse();
inv_mtx.mValues[0] = (float)inverseTransform.getScaleX();
inv_mtx.mValues[1] = (float)inverseTransform.getShearX();
inv_mtx.mValues[2] = (float)inverseTransform.getTranslateX();
inv_mtx.mValues[3] = (float)inverseTransform.getScaleX();
inv_mtx.mValues[4] = (float)inverseTransform.getShearY();
inv_mtx.mValues[5] = (float)inverseTransform.getTranslateY();
return true;
} catch (NoninvertibleTransformException e) {
return false;
}
}
/*package*/ static void native_mapPoints(int native_object, float[] dst, int dstIndex,
float[] src, int srcIndex, int ptCount, boolean isPts) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
if (isPts) {
d.mapPoints(dst, dstIndex, src, srcIndex, ptCount);
} else {
// src is vectors
// FIXME
throw new UnsupportedOperationException("Native delegate needed: Matrix_Delegate.native_mapPoints");
}
}
/*package*/ static boolean native_mapRect(int native_object, RectF dst, RectF src) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return false;
}
return d.mapRect(dst, src);
}
/*package*/ static float native_mapRadius(int native_object, float radius) {
// FIXME
throw new UnsupportedOperationException("Native delegate needed: Matrix_Delegate.native_mapRadius");
}
/*package*/ static void native_getValues(int native_object, float[] values) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
System.arraycopy(d.mValues, 0, d.mValues, 0, MATRIX_SIZE);
}
/*package*/ static void native_setValues(int native_object, float[] values) {
Matrix_Delegate d = sManager.getDelegate(native_object);
if (d == null) {
assert false;
return;
}
System.arraycopy(values, 0, d.mValues, 0, MATRIX_SIZE);
}
/*package*/ static boolean native_equals(int native_a, int native_b) {
Matrix_Delegate a = sManager.getDelegate(native_a);
if (a == null) {
assert false;
return false;
}
Matrix_Delegate b = sManager.getDelegate(native_b);
if (b == null) {
assert false;
return false;
}
for (int i = 0 ; i < MATRIX_SIZE ; i++) {
if (a.mValues[i] != b.mValues[i]) {
return false;
}
}
return true;
}
/*package*/ static void finalizer(int native_instance) {
sManager.removeDelegate(native_instance);
}
// ---- Private helper methods ----
/*package*/ static AffineTransform getAffineTransform(float[] matrix) {
// the AffineTransform constructor takes the value in a different order
// for a matrix [ 0 1 2 ]
// [ 3 4 5 ]
// the order is 0, 3, 1, 4, 2, 5...
return new AffineTransform(
matrix[0], matrix[3], matrix[1],
matrix[4], matrix[2], matrix[5]);
}
/**
* Reset a matrix to the identity
*/
private static void reset(float[] mtx) {
for (int i = 0, k = 0; i < 3; i++) {
for (int j = 0; j < 3; j++, k++) {
mtx[k] = ((i==j) ? 1 : 0);
}
}
}
@SuppressWarnings("unused")
private final static int kIdentity_Mask = 0;
private final static int kTranslate_Mask = 0x01; //!< set if the matrix has translation
private final static int kScale_Mask = 0x02; //!< set if the matrix has X or Y scale
private final static int kAffine_Mask = 0x04; //!< set if the matrix skews or rotates
private final static int kPerspective_Mask = 0x08; //!< set if the matrix is in perspective
private final static int kRectStaysRect_Mask = 0x10;
@SuppressWarnings("unused")
private final static int kUnknown_Mask = 0x80;
@SuppressWarnings("unused")
private final static int kAllMasks = kTranslate_Mask |
kScale_Mask |
kAffine_Mask |
kPerspective_Mask |
kRectStaysRect_Mask;
// these guys align with the masks, so we can compute a mask from a variable 0/1
@SuppressWarnings("unused")
private final static int kTranslate_Shift = 0;
@SuppressWarnings("unused")
private final static int kScale_Shift = 1;
@SuppressWarnings("unused")
private final static int kAffine_Shift = 2;
@SuppressWarnings("unused")
private final static int kPerspective_Shift = 3;
private final static int kRectStaysRect_Shift = 4;
private int computeTypeMask() {
int mask = 0;
if (mValues[6] != 0. || mValues[7] != 0. || mValues[8] != 1.) {
mask |= kPerspective_Mask;
}
if (mValues[2] != 0. || mValues[5] != 0.) {
mask |= kTranslate_Mask;
}
float m00 = mValues[0];
float m01 = mValues[1];
float m10 = mValues[3];
float m11 = mValues[4];
if (m01 != 0. || m10 != 0.) {
mask |= kAffine_Mask;
}
if (m00 != 1. || m11 != 1.) {
mask |= kScale_Mask;
}
if ((mask & kPerspective_Mask) == 0) {
// map non-zero to 1
int im00 = m00 != 0 ? 1 : 0;
int im01 = m01 != 0 ? 1 : 0;
int im10 = m10 != 0 ? 1 : 0;
int im11 = m11 != 0 ? 1 : 0;
// record if the (p)rimary and (s)econdary diagonals are all 0 or
// all non-zero (answer is 0 or 1)
int dp0 = (im00 | im11) ^ 1; // true if both are 0
int dp1 = im00 & im11; // true if both are 1
int ds0 = (im01 | im10) ^ 1; // true if both are 0
int ds1 = im01 & im10; // true if both are 1
// return 1 if primary is 1 and secondary is 0 or
// primary is 0 and secondary is 1
mask |= ((dp0 & ds1) | (dp1 & ds0)) << kRectStaysRect_Shift;
}
return mask;
}
private Matrix_Delegate() {
reset();
}
private Matrix_Delegate(float[] values) {
System.arraycopy(values, 0, mValues, 0, MATRIX_SIZE);
}
/**
* Adds the given transformation to the current Matrix
* <p/>This in effect does this = this*matrix
* @param matrix
*/
private void postTransform(float[] matrix) {
float[] tmp = new float[9];
multiply(tmp, mValues, matrix);
mValues = tmp;
}
/**
* Adds the given transformation to the current Matrix
* <p/>This in effect does this = matrix*this
* @param matrix
*/
private void preTransform(float[] matrix) {
float[] tmp = new float[9];
multiply(tmp, matrix, mValues);
mValues = tmp;
}
/**
* Apply this matrix to the array of 2D points specified by src, and write
* the transformed points into the array of points specified by dst. The
* two arrays represent their "points" as pairs of floats [x, y].
*
* @param dst The array of dst points (x,y pairs)
* @param dstIndex The index of the first [x,y] pair of dst floats
* @param src The array of src points (x,y pairs)
* @param srcIndex The index of the first [x,y] pair of src floats
* @param pointCount The number of points (x,y pairs) to transform
*/
private void mapPoints(float[] dst, int dstIndex, float[] src, int srcIndex,
int pointCount) {
//checkPointArrays(src, srcIndex, dst, dstIndex, pointCount);
float[] tmpDest = dst;
boolean inPlace = dst == src;
if (inPlace) {
tmpDest = new float[dstIndex + pointCount * 2];
}
for (int i = 0 ; i < pointCount * 2 ; i += 2) {
// just in case we are doing in place, we better put this in temp vars
float x = mValues[0] * src[i + srcIndex] +
mValues[1] * src[i + srcIndex + 1] +
mValues[2];
float y = mValues[3] * src[i + srcIndex] +
mValues[4] * src[i + srcIndex + 1] +
mValues[5];
tmpDest[i + dstIndex] = x;
tmpDest[i + dstIndex + 1] = y;
}
if (inPlace) {
System.arraycopy(tmpDest, dstIndex, dst, dstIndex, pointCount * 2);
}
}
/**
* Apply this matrix to the array of 2D points, and write the transformed
* points back into the array
*
* @param pts The array [x0, y0, x1, y1, ...] of points to transform.
*/
private void mapPoints(float[] pts) {
mapPoints(pts, 0, pts, 0, pts.length >> 1);
}
/**
* multiply two matrices and store them in a 3rd.
* <p/>This in effect does dest = a*b
* dest cannot be the same as a or b.
*/
/*package*/ static void multiply(float dest[], float[] a, float[] b) {
// first row
dest[0] = b[0] * a[0] + b[1] * a[3] + b[2] * a[6];
dest[1] = b[0] * a[1] + b[1] * a[4] + b[2] * a[7];
dest[2] = b[0] * a[2] + b[1] * a[5] + b[2] * a[8];
// 2nd row
dest[3] = b[3] * a[0] + b[4] * a[3] + b[5] * a[6];
dest[4] = b[3] * a[1] + b[4] * a[4] + b[5] * a[7];
dest[5] = b[3] * a[2] + b[4] * a[5] + b[5] * a[8];
// 3rd row
dest[6] = b[6] * a[0] + b[7] * a[3] + b[8] * a[6];
dest[7] = b[6] * a[1] + b[7] * a[4] + b[8] * a[7];
dest[8] = b[6] * a[2] + b[7] * a[5] + b[8] * a[8];
}
/**
* Returns a matrix that represents a given translate
* @param dx
* @param dy
* @return
*/
/*package*/ static float[] getTranslate(float dx, float dy) {
return setTranslate(new float[9], dx, dy);
}
/*package*/ static float[] setTranslate(float[] dest, float dx, float dy) {
dest[0] = 1;
dest[1] = 0;
dest[2] = dx;
dest[3] = 0;
dest[4] = 1;
dest[5] = dy;
dest[6] = 0;
dest[7] = 0;
dest[8] = 1;
return dest;
}
/*package*/ static float[] getScale(float sx, float sy) {
return new float[] { sx, 0, 0, 0, sy, 0, 0, 0, 1 };
}
/**
* Returns a matrix that represents the given scale info.
* @param sx
* @param sy
* @param px
* @param py
*/
/*package*/ static float[] getScale(float sx, float sy, float px, float py) {
float[] tmp = new float[9];
float[] tmp2 = new float[9];
// TODO: do it in one pass
// translate tmp so that the pivot is in 0,0
setTranslate(tmp, -px, -py);
// scale into tmp2
multiply(tmp2, tmp, getScale(sx, sy));
// translate back the pivot back into tmp
multiply(tmp, tmp2, getTranslate(px, py));
return tmp;
}
/*package*/ static float[] getRotate(float degrees) {
double rad = Math.toRadians(degrees);
float sin = (float)Math.sin(rad);
float cos = (float)Math.cos(rad);
return getRotate(sin, cos);
}
/*package*/ static float[] getRotate(float sin, float cos) {
return setRotate(new float[9], sin, cos);
}
/*package*/ static float[] setRotate(float[] dest, float degrees) {
double rad = Math.toRadians(degrees);
float sin = (float)Math.sin(rad);
float cos = (float)Math.cos(rad);
return setRotate(dest, sin, cos);
}
/*package*/ static float[] setRotate(float[] dest, float sin, float cos) {
dest[0] = cos;
dest[1] = -sin;
dest[2] = 0;
dest[3] = sin;
dest[4] = cos;
dest[5] = 0;
dest[6] = 0;
dest[7] = 0;
dest[8] = 1;
return dest;
}
/*package*/ static float[] getRotate(float degrees, float px, float py) {
float[] tmp = new float[9];
float[] tmp2 = new float[9];
// TODO: do it in one pass
// translate so that the pivot is in 0,0
setTranslate(tmp, -px, -py);
// rotate into tmp2
double rad = Math.toRadians(degrees);
float cos = (float)Math.cos(rad);
float sin = (float)Math.sin(rad);
multiply(tmp2, tmp, getRotate(sin, cos));
// translate back the pivot back into tmp
multiply(tmp, tmp2, getTranslate(px, py));
return tmp;
}
/*package*/ static float[] getSkew(float kx, float ky) {
return new float[] { 1, kx, 0, ky, 1, 0, 0, 0, 1 };
}
/*package*/ static float[] getSkew(float kx, float ky, float px, float py) {
float[] tmp = new float[9];
float[] tmp2 = new float[9];
// TODO: do it in one pass
// translate so that the pivot is in 0,0
setTranslate(tmp, -px, -py);
// skew into tmp2
multiply(tmp2, tmp, new float[] { 1, kx, 0, ky, 1, 0, 0, 0, 1 });
// translate back the pivot back into tmp
multiply(tmp, tmp2, getTranslate(px, py));
return tmp;
}
}
|