blob: 6ee70eb40f5b65da373df80ca834ac7900b99ca2 [file] [log] [blame]
reed@google.com125002a2011-06-09 19:13:41 +00001#include "Test.h"
2#include "SkMatrix44.h"
3
reed@google.comda9fac02011-06-13 14:46:52 +00004static bool nearly_equal_scalar(SkMScalar a, SkMScalar b) {
reed@google.com125002a2011-06-09 19:13:41 +00005 // Note that we get more compounded error for multiple operations when
6 // SK_SCALAR_IS_FIXED.
7#ifdef SK_SCALAR_IS_FLOAT
8 const SkScalar tolerance = SK_Scalar1 / 200000;
9#else
10 const SkScalar tolerance = SK_Scalar1 / 1024;
11#endif
12
13 return SkScalarAbs(a - b) <= tolerance;
14}
15
reed@google.comda9fac02011-06-13 14:46:52 +000016template <typename T> void assert16(skiatest::Reporter* reporter, const T data[],
17 T m0, T m1, T m2, T m3,
18 T m4, T m5, T m6, T m7,
19 T m8, T m9, T m10, T m11,
20 T m12, T m13, T m14, T m15) {
21 REPORTER_ASSERT(reporter, data[0] == m0);
22 REPORTER_ASSERT(reporter, data[1] == m1);
23 REPORTER_ASSERT(reporter, data[2] == m2);
24 REPORTER_ASSERT(reporter, data[3] == m3);
25
26 REPORTER_ASSERT(reporter, data[4] == m4);
27 REPORTER_ASSERT(reporter, data[5] == m5);
28 REPORTER_ASSERT(reporter, data[6] == m6);
29 REPORTER_ASSERT(reporter, data[7] == m7);
30
31 REPORTER_ASSERT(reporter, data[8] == m8);
32 REPORTER_ASSERT(reporter, data[9] == m9);
33 REPORTER_ASSERT(reporter, data[10] == m10);
34 REPORTER_ASSERT(reporter, data[11] == m11);
35
36 REPORTER_ASSERT(reporter, data[12] == m12);
37 REPORTER_ASSERT(reporter, data[13] == m13);
38 REPORTER_ASSERT(reporter, data[14] == m14);
39 REPORTER_ASSERT(reporter, data[15] == m15);
40}
41
reed@google.com125002a2011-06-09 19:13:41 +000042static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) {
43 for (int i = 0; i < 4; ++i) {
44 for (int j = 0; j < 4; ++j) {
45 if (!nearly_equal_scalar(a.get(i, j), b.get(i, j))) {
reed@google.comda9fac02011-06-13 14:46:52 +000046 printf("not equal %g %g\n", a.get(i, j), b.get(i, j));
reed@google.com125002a2011-06-09 19:13:41 +000047 return false;
48 }
49 }
50 }
51 return true;
52}
53
54static bool is_identity(const SkMatrix44& m) {
55 SkMatrix44 identity;
56 identity.reset();
57 return nearly_equal(m, identity);
58}
59
reed@google.com125002a2011-06-09 19:13:41 +000060void TestMatrix44(skiatest::Reporter* reporter) {
reed@google.comda9fac02011-06-13 14:46:52 +000061#ifdef SK_SCALAR_IS_FLOAT
reed@google.com125002a2011-06-09 19:13:41 +000062 SkMatrix44 mat, inverse, iden1, iden2, rot;
63
64 mat.reset();
65 mat.setTranslate(SK_Scalar1, SK_Scalar1, SK_Scalar1);
66 mat.invert(&inverse);
67 iden1.setConcat(mat, inverse);
68 REPORTER_ASSERT(reporter, is_identity(iden1));
69
70 mat.setScale(SkIntToScalar(2), SkIntToScalar(2), SkIntToScalar(2));
71 mat.invert(&inverse);
72 iden1.setConcat(mat, inverse);
73 REPORTER_ASSERT(reporter, is_identity(iden1));
74
75 mat.setScale(SK_Scalar1/2, SK_Scalar1/2, SK_Scalar1/2);
76 mat.invert(&inverse);
77 iden1.setConcat(mat, inverse);
78 REPORTER_ASSERT(reporter, is_identity(iden1));
79
80 mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20));
81 rot.setRotateDegreesAbout(
82 SkIntToScalar(0),
83 SkIntToScalar(0),
84 SkIntToScalar(-1),
85 SkIntToScalar(90));
86 mat.postConcat(rot);
87 REPORTER_ASSERT(reporter, mat.invert(NULL));
88 mat.invert(&inverse);
89 iden1.setConcat(mat, inverse);
90 REPORTER_ASSERT(reporter, is_identity(iden1));
91 iden2.setConcat(inverse, mat);
92 REPORTER_ASSERT(reporter, is_identity(iden2));
reed@google.comda9fac02011-06-13 14:46:52 +000093
94 // test rol/col Major getters
95 {
96 mat.setTranslate(2, 3, 4);
97 float dataf[16];
98 double datad[16];
99
100 mat.asColMajorf(dataf);
101 assert16<float>(reporter, dataf,
102 1, 0, 0, 0,
103 0, 1, 0, 0,
104 0, 0, 1, 0,
105 2, 3, 4, 1);
106 mat.asColMajord(datad);
107 assert16<double>(reporter, datad, 1, 0, 0, 0,
108 0, 1, 0, 0,
109 0, 0, 1, 0,
110 2, 3, 4, 1);
111 mat.asRowMajorf(dataf);
112 assert16<float>(reporter, dataf, 1, 0, 0, 2,
113 0, 1, 0, 3,
114 0, 0, 1, 4,
115 0, 0, 0, 1);
116 mat.asRowMajord(datad);
117 assert16<double>(reporter, datad, 1, 0, 0, 2,
118 0, 1, 0, 3,
119 0, 0, 1, 4,
120 0, 0, 0, 1);
121 }
122#endif
reed@google.com125002a2011-06-09 19:13:41 +0000123}
124
125#include "TestClassDef.h"
126DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)