epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 7 | |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 8 | #include "SkMatrix44.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 10 | |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 11 | static bool nearly_equal_double(double a, double b) { |
| 12 | const double tolerance = 1e-7; |
| 13 | double diff = a - b; |
| 14 | if (diff < 0) |
| 15 | diff = -diff; |
| 16 | return diff <= tolerance; |
| 17 | } |
| 18 | |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 19 | static bool nearly_equal_mscalar(SkMScalar a, SkMScalar b) { |
| 20 | const SkMScalar tolerance = SK_MScalar1 / 200000; |
| 21 | |
| 22 | return SkTAbs<SkMScalar>(a - b) <= tolerance; |
| 23 | } |
| 24 | |
| 25 | static bool nearly_equal_scalar(SkScalar a, SkScalar b) { |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 26 | const SkScalar tolerance = SK_Scalar1 / 200000; |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 27 | return SkScalarAbs(a - b) <= tolerance; |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 28 | } |
| 29 | |
reed@google.com | da9fac0 | 2011-06-13 14:46:52 +0000 | [diff] [blame] | 30 | template <typename T> void assert16(skiatest::Reporter* reporter, const T data[], |
| 31 | T m0, T m1, T m2, T m3, |
| 32 | T m4, T m5, T m6, T m7, |
| 33 | T m8, T m9, T m10, T m11, |
| 34 | T m12, T m13, T m14, T m15) { |
| 35 | REPORTER_ASSERT(reporter, data[0] == m0); |
| 36 | REPORTER_ASSERT(reporter, data[1] == m1); |
| 37 | REPORTER_ASSERT(reporter, data[2] == m2); |
| 38 | REPORTER_ASSERT(reporter, data[3] == m3); |
| 39 | |
| 40 | REPORTER_ASSERT(reporter, data[4] == m4); |
| 41 | REPORTER_ASSERT(reporter, data[5] == m5); |
| 42 | REPORTER_ASSERT(reporter, data[6] == m6); |
| 43 | REPORTER_ASSERT(reporter, data[7] == m7); |
| 44 | |
| 45 | REPORTER_ASSERT(reporter, data[8] == m8); |
| 46 | REPORTER_ASSERT(reporter, data[9] == m9); |
| 47 | REPORTER_ASSERT(reporter, data[10] == m10); |
| 48 | REPORTER_ASSERT(reporter, data[11] == m11); |
| 49 | |
| 50 | REPORTER_ASSERT(reporter, data[12] == m12); |
| 51 | REPORTER_ASSERT(reporter, data[13] == m13); |
| 52 | REPORTER_ASSERT(reporter, data[14] == m14); |
| 53 | REPORTER_ASSERT(reporter, data[15] == m15); |
| 54 | } |
| 55 | |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 56 | static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) { |
| 57 | for (int i = 0; i < 4; ++i) { |
| 58 | for (int j = 0; j < 4; ++j) { |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 59 | if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 60 | SkDebugf("not equal %g %g\n", a.get(i, j), b.get(i, j)); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | static bool is_identity(const SkMatrix44& m) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 69 | SkMatrix44 identity(SkMatrix44::kIdentity_Constructor); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 70 | return nearly_equal(m, identity); |
| 71 | } |
| 72 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | static bool bits_isonly(int value, int mask) { |
| 75 | return 0 == (value & ~mask); |
| 76 | } |
| 77 | |
vollick@chromium.org | 57a54e3 | 2012-12-10 20:16:10 +0000 | [diff] [blame] | 78 | static void test_constructor(skiatest::Reporter* reporter) { |
| 79 | // Allocate a matrix on the heap |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 80 | SkMatrix44* placeholderMatrix = new SkMatrix44(SkMatrix44::kUninitialized_Constructor); |
robertphillips@google.com | 35300c4 | 2013-03-21 17:38:49 +0000 | [diff] [blame] | 81 | SkAutoTDelete<SkMatrix44> deleteMe(placeholderMatrix); |
| 82 | |
vollick@chromium.org | 57a54e3 | 2012-12-10 20:16:10 +0000 | [diff] [blame] | 83 | for (int row = 0; row < 4; ++row) { |
| 84 | for (int col = 0; col < 4; ++col) { |
| 85 | placeholderMatrix->setDouble(row, col, row * col); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Use placement-new syntax to trigger the constructor on top of the heap |
| 90 | // address we already initialized. This allows us to check that the |
| 91 | // constructor did avoid initializing the matrix contents. |
| 92 | SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUninitialized_Constructor); |
| 93 | REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); |
| 94 | REPORTER_ASSERT(reporter, !testMatrix->isIdentity()); |
| 95 | for (int row = 0; row < 4; ++row) { |
| 96 | for (int col = 0; col < 4; ++col) { |
| 97 | REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix->getDouble(row, col))); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Verify that kIdentity_Constructor really does initialize to an identity matrix. |
| 102 | testMatrix = 0; |
skia.committer@gmail.com | c7b4be7 | 2012-12-11 02:01:20 +0000 | [diff] [blame] | 103 | testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constructor); |
vollick@chromium.org | 57a54e3 | 2012-12-10 20:16:10 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); |
| 105 | REPORTER_ASSERT(reporter, testMatrix->isIdentity()); |
| 106 | REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I()); |
fs | 88640cf | 2014-12-16 08:36:11 -0800 | [diff] [blame] | 107 | |
| 108 | // Verify that that constructing from an SkMatrix initializes everything. |
| 109 | SkMatrix44 scaleMatrix(SkMatrix44::kUninitialized_Constructor); |
| 110 | scaleMatrix.setScale(3, 4, 5); |
| 111 | REPORTER_ASSERT(reporter, scaleMatrix.isScale()); |
| 112 | testMatrix = new(&scaleMatrix) SkMatrix44(SkMatrix::I()); |
| 113 | REPORTER_ASSERT(reporter, testMatrix->isIdentity()); |
| 114 | REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I()); |
vollick@chromium.org | 57a54e3 | 2012-12-10 20:16:10 +0000 | [diff] [blame] | 115 | } |
| 116 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 117 | static void test_translate(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 118 | SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 119 | SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 120 | |
| 121 | mat.setTranslate(0, 0, 0); |
| 122 | REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask)); |
| 123 | mat.setTranslate(1, 2, 3); |
| 124 | REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_Mask)); |
| 125 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
| 126 | REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTranslate_Mask)); |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 127 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 128 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 129 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 130 | SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 131 | a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 132 | b.setTranslate(10, 11, 12); |
| 133 | |
| 134 | c.setConcat(a, b); |
| 135 | mat = a; |
| 136 | mat.preTranslate(10, 11, 12); |
| 137 | REPORTER_ASSERT(reporter, mat == c); |
| 138 | |
| 139 | c.setConcat(b, a); |
| 140 | mat = a; |
| 141 | mat.postTranslate(10, 11, 12); |
| 142 | REPORTER_ASSERT(reporter, mat == c); |
| 143 | } |
| 144 | |
| 145 | static void test_scale(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 146 | SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 147 | SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 148 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 149 | mat.setScale(1, 1, 1); |
| 150 | REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask)); |
| 151 | mat.setScale(1, 2, 3); |
| 152 | REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask)); |
| 153 | REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
| 154 | REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask)); |
| 155 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 156 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 157 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 158 | SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 159 | a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 160 | b.setScale(10, 11, 12); |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 161 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 162 | c.setConcat(a, b); |
| 163 | mat = a; |
| 164 | mat.preScale(10, 11, 12); |
| 165 | REPORTER_ASSERT(reporter, mat == c); |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 166 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 167 | c.setConcat(b, a); |
| 168 | mat = a; |
| 169 | mat.postScale(10, 11, 12); |
| 170 | REPORTER_ASSERT(reporter, mat == c); |
| 171 | } |
| 172 | |
| 173 | static void make_i(SkMatrix44* mat) { mat->setIdentity(); } |
| 174 | static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); } |
| 175 | static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); } |
| 176 | static void make_st(SkMatrix44* mat) { |
| 177 | mat->setScale(1, 2, 3); |
| 178 | mat->postTranslate(1, 2, 3); |
| 179 | } |
| 180 | static void make_a(SkMatrix44* mat) { |
| 181 | mat->setRotateDegreesAbout(1, 2, 3, 45); |
| 182 | } |
| 183 | static void make_p(SkMatrix44* mat) { |
| 184 | SkMScalar data[] = { |
| 185 | 1, 2, 3, 4, 5, 6, 7, 8, |
| 186 | 1, 2, 3, 4, 5, 6, 7, 8, |
| 187 | }; |
| 188 | mat->setRowMajor(data); |
| 189 | } |
| 190 | |
| 191 | typedef void (*Make44Proc)(SkMatrix44*); |
| 192 | |
| 193 | static const Make44Proc gMakeProcs[] = { |
| 194 | make_i, make_t, make_s, make_st, make_a, make_p |
| 195 | }; |
| 196 | |
| 197 | static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) { |
| 198 | SkMScalar src2[] = { 1, 2 }; |
| 199 | SkMScalar src4[] = { src2[0], src2[1], 0, 1 }; |
| 200 | SkMScalar dstA[4], dstB[4]; |
| 201 | |
| 202 | for (int i = 0; i < 4; ++i) { |
| 203 | dstA[i] = 123456789; |
| 204 | dstB[i] = 987654321; |
| 205 | } |
| 206 | |
| 207 | mat.map2(src2, 1, dstA); |
| 208 | mat.mapMScalars(src4, dstB); |
skia.committer@gmail.com | 0264fb4 | 2012-12-06 02:01:25 +0000 | [diff] [blame] | 209 | |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 210 | for (int i = 0; i < 4; ++i) { |
| 211 | REPORTER_ASSERT(reporter, dstA[i] == dstB[i]); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void test_map2(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 216 | SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 217 | |
| 218 | for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) { |
| 219 | gMakeProcs[i](&mat); |
| 220 | test_map2(reporter, mat); |
| 221 | } |
| 222 | } |
| 223 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 224 | static void test_gettype(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 225 | SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 226 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 227 | REPORTER_ASSERT(reporter, matrix.isIdentity()); |
| 228 | REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType()); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 229 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 230 | int expectedMask; |
| 231 | |
| 232 | matrix.set(1, 1, 0); |
| 233 | expectedMask = SkMatrix44::kScale_Mask; |
| 234 | REPORTER_ASSERT(reporter, matrix.getType() == expectedMask); |
| 235 | |
| 236 | matrix.set(0, 3, 1); // translate-x |
| 237 | expectedMask |= SkMatrix44::kTranslate_Mask; |
| 238 | REPORTER_ASSERT(reporter, matrix.getType() == expectedMask); |
| 239 | |
| 240 | matrix.set(2, 0, 1); |
| 241 | expectedMask |= SkMatrix44::kAffine_Mask; |
| 242 | REPORTER_ASSERT(reporter, matrix.getType() == expectedMask); |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 243 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 244 | matrix.set(3, 2, 1); |
| 245 | REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask); |
reed@google.com | 87f99cb | 2013-04-19 12:25:00 +0000 | [diff] [blame] | 246 | |
| 247 | // ensure that negative zero is treated as zero |
| 248 | SkMScalar dx = 0; |
| 249 | SkMScalar dy = 0; |
| 250 | SkMScalar dz = 0; |
| 251 | matrix.setTranslate(-dx, -dy, -dz); |
| 252 | REPORTER_ASSERT(reporter, matrix.isIdentity()); |
| 253 | matrix.preTranslate(-dx, -dy, -dz); |
| 254 | REPORTER_ASSERT(reporter, matrix.isIdentity()); |
| 255 | matrix.postTranslate(-dx, -dy, -dz); |
| 256 | REPORTER_ASSERT(reporter, matrix.isIdentity()); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
reed@google.com | 6f2b44d | 2011-06-24 18:13:39 +0000 | [diff] [blame] | 259 | static void test_common_angles(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 260 | SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 6f2b44d | 2011-06-24 18:13:39 +0000 | [diff] [blame] | 261 | // Test precision of rotation in common cases |
| 262 | int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 }; |
| 263 | for (int i = 0; i < 9; ++i) { |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 264 | rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i])); |
reed@google.com | 6f2b44d | 2011-06-24 18:13:39 +0000 | [diff] [blame] | 265 | |
| 266 | SkMatrix rot3x3 = rot; |
| 267 | REPORTER_ASSERT(reporter, rot3x3.rectStaysRect()); |
| 268 | } |
| 269 | } |
| 270 | |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 271 | static void test_concat(skiatest::Reporter* reporter) { |
| 272 | int i; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 273 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 274 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 275 | SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
| 276 | SkMatrix44 d(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 277 | |
| 278 | a.setTranslate(10, 10, 10); |
| 279 | b.setScale(2, 2, 2); |
| 280 | |
| 281 | SkScalar src[8] = { |
| 282 | 0, 0, 0, 1, |
| 283 | 1, 1, 1, 1 |
| 284 | }; |
| 285 | SkScalar dst[8]; |
| 286 | |
| 287 | c.setConcat(a, b); |
| 288 | |
| 289 | d = a; |
| 290 | d.preConcat(b); |
| 291 | REPORTER_ASSERT(reporter, d == c); |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 292 | |
reed@google.com | 1ea95be | 2012-11-09 21:39:48 +0000 | [diff] [blame] | 293 | c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4); |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 294 | for (i = 0; i < 3; ++i) { |
| 295 | REPORTER_ASSERT(reporter, 10 == dst[i]); |
| 296 | REPORTER_ASSERT(reporter, 12 == dst[i + 4]); |
| 297 | } |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 298 | |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 299 | c.setConcat(b, a); |
| 300 | |
| 301 | d = a; |
| 302 | d.postConcat(b); |
| 303 | REPORTER_ASSERT(reporter, d == c); |
| 304 | |
reed@google.com | 1ea95be | 2012-11-09 21:39:48 +0000 | [diff] [blame] | 305 | c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4); |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 306 | for (i = 0; i < 3; ++i) { |
| 307 | REPORTER_ASSERT(reporter, 20 == dst[i]); |
| 308 | REPORTER_ASSERT(reporter, 22 == dst[i + 4]); |
| 309 | } |
| 310 | } |
| 311 | |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 312 | static void test_determinant(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 313 | SkMatrix44 a(SkMatrix44::kIdentity_Constructor); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 314 | REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant())); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 315 | a.set(1, 1, 2); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant())); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 317 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 318 | REPORTER_ASSERT(reporter, a.invert(&b)); |
| 319 | REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant())); |
| 320 | SkMatrix44 c = b = a; |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 321 | c.set(0, 1, 4); |
| 322 | b.set(1, 0, 4); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, |
| 324 | nearly_equal_double(a.determinant(), |
| 325 | b.determinant())); |
| 326 | SkMatrix44 d = a; |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 327 | d.set(0, 0, 8); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 328 | REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant())); |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 329 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 330 | SkMatrix44 e = a; |
| 331 | e.postConcat(d); |
| 332 | REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant())); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 333 | e.set(0, 0, 0); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 334 | REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant())); |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 335 | } |
| 336 | |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 337 | static void test_invert(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 338 | SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 339 | double inverseData[16]; |
| 340 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 341 | SkMatrix44 identity(SkMatrix44::kIdentity_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 342 | identity.invert(&inverse); |
| 343 | inverse.asRowMajord(inverseData); |
| 344 | assert16<double>(reporter, inverseData, |
| 345 | 1, 0, 0, 0, |
| 346 | 0, 1, 0, 0, |
| 347 | 0, 0, 1, 0, |
| 348 | 0, 0, 0, 1); |
| 349 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 350 | SkMatrix44 translation(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 351 | translation.setTranslate(2, 3, 4); |
| 352 | translation.invert(&inverse); |
| 353 | inverse.asRowMajord(inverseData); |
| 354 | assert16<double>(reporter, inverseData, |
| 355 | 1, 0, 0, -2, |
| 356 | 0, 1, 0, -3, |
| 357 | 0, 0, 1, -4, |
| 358 | 0, 0, 0, 1); |
| 359 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 360 | SkMatrix44 scale(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 361 | scale.setScale(2, 4, 8); |
| 362 | scale.invert(&inverse); |
| 363 | inverse.asRowMajord(inverseData); |
| 364 | assert16<double>(reporter, inverseData, |
| 365 | 0.5, 0, 0, 0, |
| 366 | 0, 0.25, 0, 0, |
| 367 | 0, 0, 0.125, 0, |
| 368 | 0, 0, 0, 1); |
| 369 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 370 | SkMatrix44 scaleTranslation(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 371 | scaleTranslation.setScale(10, 100, 1000); |
| 372 | scaleTranslation.preTranslate(2, 3, 4); |
| 373 | scaleTranslation.invert(&inverse); |
| 374 | inverse.asRowMajord(inverseData); |
| 375 | assert16<double>(reporter, inverseData, |
| 376 | 0.1, 0, 0, -2, |
| 377 | 0, 0.01, 0, -3, |
| 378 | 0, 0, 0.001, -4, |
| 379 | 0, 0, 0, 1); |
| 380 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 381 | SkMatrix44 rotation(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 382 | rotation.setRotateDegreesAbout(0, 0, 1, 90); |
| 383 | rotation.invert(&inverse); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 384 | SkMatrix44 expected(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 385 | double expectedInverseRotation[16] = |
| 386 | {0, 1, 0, 0, |
| 387 | -1, 0, 0, 0, |
| 388 | 0, 0, 1, 0, |
| 389 | 0, 0, 0, 1}; |
| 390 | expected.setRowMajord(expectedInverseRotation); |
| 391 | REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 392 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 393 | SkMatrix44 affine(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 394 | affine.setRotateDegreesAbout(0, 0, 1, 90); |
| 395 | affine.preScale(10, 20, 100); |
| 396 | affine.preTranslate(2, 3, 4); |
| 397 | affine.invert(&inverse); |
| 398 | double expectedInverseAffine[16] = |
| 399 | {0, 0.1, 0, -2, |
| 400 | -0.05, 0, 0, -3, |
| 401 | 0, 0, 0.01, -4, |
| 402 | 0, 0, 0, 1}; |
| 403 | expected.setRowMajord(expectedInverseAffine); |
| 404 | REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
| 405 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 406 | SkMatrix44 perspective(SkMatrix44::kIdentity_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 407 | perspective.setDouble(3, 2, 1.0); |
| 408 | perspective.invert(&inverse); |
| 409 | double expectedInversePerspective[16] = |
| 410 | {1, 0, 0, 0, |
| 411 | 0, 1, 0, 0, |
| 412 | 0, 0, 1, 0, |
| 413 | 0, 0, -1, 1}; |
| 414 | expected.setRowMajord(expectedInversePerspective); |
| 415 | REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
| 416 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 417 | SkMatrix44 affineAndPerspective(SkMatrix44::kIdentity_Constructor); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 418 | affineAndPerspective.setDouble(3, 2, 1.0); |
| 419 | affineAndPerspective.preScale(10, 20, 100); |
| 420 | affineAndPerspective.preTranslate(2, 3, 4); |
| 421 | affineAndPerspective.invert(&inverse); |
| 422 | double expectedInverseAffineAndPerspective[16] = |
| 423 | {0.1, 0, 2, -2, |
| 424 | 0, 0.05, 3, -3, |
| 425 | 0, 0, 4.01, -4, |
| 426 | 0, 0, -1, 1}; |
| 427 | expected.setRowMajord(expectedInverseAffineAndPerspective); |
| 428 | REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
| 429 | } |
| 430 | |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 431 | static void test_transpose(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 432 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 433 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 434 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 435 | int i = 0; |
| 436 | for (int row = 0; row < 4; ++row) { |
| 437 | for (int col = 0; col < 4; ++col) { |
| 438 | a.setDouble(row, col, i); |
| 439 | b.setDouble(col, row, i++); |
| 440 | } |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 441 | } |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 442 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 443 | a.transpose(); |
| 444 | REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static void test_get_set_double(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 448 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 449 | for (int row = 0; row < 4; ++row) { |
| 450 | for (int col = 0; col < 4; ++col) { |
| 451 | a.setDouble(row, col, 3.141592653589793); |
| 452 | REPORTER_ASSERT(reporter, |
| 453 | nearly_equal_double(3.141592653589793, |
| 454 | a.getDouble(row, col))); |
| 455 | a.setDouble(row, col, 0); |
| 456 | REPORTER_ASSERT(reporter, |
| 457 | nearly_equal_double(0, a.getDouble(row, col))); |
| 458 | } |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 459 | } |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void test_set_row_col_major(skiatest::Reporter* reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 463 | SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 464 | SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 465 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 466 | for (int row = 0; row < 4; ++row) { |
| 467 | for (int col = 0; col < 4; ++col) { |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 468 | a.setDouble(row, col, row * 4 + col); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
skia.committer@gmail.com | e659c2e | 2012-12-04 02:01:25 +0000 | [diff] [blame] | 471 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 472 | double bufferd[16]; |
| 473 | float bufferf[16]; |
| 474 | a.asColMajord(bufferd); |
| 475 | b.setColMajord(bufferd); |
| 476 | REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
| 477 | b.setRowMajord(bufferd); |
| 478 | b.transpose(); |
| 479 | REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
| 480 | a.asColMajorf(bufferf); |
| 481 | b.setColMajorf(bufferf); |
| 482 | REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
| 483 | b.setRowMajorf(bufferf); |
| 484 | b.transpose(); |
| 485 | REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 486 | } |
| 487 | |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 488 | static void test_3x3_conversion(skiatest::Reporter* reporter) { |
| 489 | SkMScalar values4x4[16] = { 1, 2, 3, 4, |
| 490 | 5, 6, 7, 8, |
| 491 | 9, 10, 11, 12, |
| 492 | 13, 14, 15, 16 }; |
| 493 | SkScalar values3x3[9] = { 1, 2, 4, |
| 494 | 5, 6, 8, |
| 495 | 13, 14, 16 }; |
| 496 | SkMScalar values4x4flattened[16] = { 1, 2, 0, 4, |
| 497 | 5, 6, 0, 8, |
| 498 | 0, 0, 1, 0, |
| 499 | 13, 14, 0, 16 }; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 500 | SkMatrix44 a44(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 501 | a44.setRowMajor(values4x4); |
| 502 | |
| 503 | SkMatrix a33 = a44; |
| 504 | SkMatrix expected33; |
| 505 | for (int i = 0; i < 9; i++) expected33[i] = values3x3[i]; |
| 506 | REPORTER_ASSERT(reporter, expected33 == a33); |
| 507 | |
| 508 | SkMatrix44 a44flattened = a33; |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 509 | SkMatrix44 expected44flattened(SkMatrix44::kUninitialized_Constructor); |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 510 | expected44flattened.setRowMajor(values4x4flattened); |
| 511 | REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened)); |
| 512 | |
| 513 | // Test that a point with a Z value of 0 is transformed the same way. |
| 514 | SkScalar vec4[4] = { 2, 4, 0, 8 }; |
| 515 | SkScalar vec3[3] = { 2, 4, 8 }; |
| 516 | |
| 517 | SkScalar vec4transformed[4]; |
| 518 | SkScalar vec3transformed[3]; |
| 519 | SkScalar vec4transformed2[4]; |
| 520 | a44.mapScalars(vec4, vec4transformed); |
| 521 | a33.mapHomogeneousPoints(vec3transformed, vec3, 1); |
| 522 | a44flattened.mapScalars(vec4, vec4transformed2); |
| 523 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transformed[0])); |
| 524 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transformed[1])); |
| 525 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transformed[2])); |
| 526 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transformed2[0])); |
| 527 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transformed2[1])); |
| 528 | REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4transformed2[2])); |
| 529 | REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transformed2[3])); |
| 530 | } |
| 531 | |
tomhudson | a32f175 | 2014-09-16 08:29:29 -0700 | [diff] [blame] | 532 | static void test_has_perspective(skiatest::Reporter* reporter) { |
| 533 | SkMatrix44 transform(SkMatrix44::kIdentity_Constructor); |
| 534 | |
| 535 | transform.set(3, 2, -0.1); |
| 536 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 537 | |
| 538 | transform.reset(); |
| 539 | REPORTER_ASSERT(reporter, !transform.hasPerspective()); |
| 540 | |
| 541 | transform.set(3, 0, -1.0); |
| 542 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 543 | |
| 544 | transform.reset(); |
| 545 | transform.set(3, 1, -1.0); |
| 546 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 547 | |
| 548 | transform.reset(); |
| 549 | transform.set(3, 2, -0.3); |
| 550 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 551 | |
| 552 | transform.reset(); |
| 553 | transform.set(3, 3, 0.5); |
| 554 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 555 | |
| 556 | transform.reset(); |
| 557 | transform.set(3, 3, 0.0); |
| 558 | REPORTER_ASSERT(reporter, transform.hasPerspective()); |
| 559 | } |
| 560 | |
tomhudson | faccb8e | 2014-09-26 11:45:48 -0700 | [diff] [blame] | 561 | static bool is_rectilinear (SkVector4& p1, SkVector4& p2, SkVector4& p3, SkVector4& p4) { |
| 562 | return (SkScalarNearlyEqual(p1.fData[0], p2.fData[0]) && |
| 563 | SkScalarNearlyEqual(p2.fData[1], p3.fData[1]) && |
| 564 | SkScalarNearlyEqual(p3.fData[0], p4.fData[0]) && |
| 565 | SkScalarNearlyEqual(p4.fData[1], p1.fData[1])) || |
| 566 | (SkScalarNearlyEqual(p1.fData[1], p2.fData[1]) && |
| 567 | SkScalarNearlyEqual(p2.fData[0], p3.fData[0]) && |
| 568 | SkScalarNearlyEqual(p3.fData[1], p4.fData[1]) && |
| 569 | SkScalarNearlyEqual(p4.fData[0], p1.fData[0])); |
| 570 | } |
| 571 | |
| 572 | static SkVector4 mul_with_persp_divide(const SkMatrix44& transform, const SkVector4& target) { |
| 573 | SkVector4 result = transform * target; |
| 574 | if (result.fData[3] != 0.0f && result.fData[3] != SK_Scalar1) { |
| 575 | float wInverse = SK_Scalar1 / result.fData[3]; |
| 576 | result.set(result.fData[0] * wInverse, |
| 577 | result.fData[1] * wInverse, |
| 578 | result.fData[2] * wInverse, |
| 579 | SK_Scalar1); |
| 580 | } |
| 581 | return result; |
| 582 | } |
| 583 | |
| 584 | static bool empirically_preserves_2d_axis_alignment(skiatest::Reporter* reporter, |
| 585 | const SkMatrix44& transform) { |
| 586 | SkVector4 p1(5.0f, 5.0f, 0.0f); |
| 587 | SkVector4 p2(10.0f, 5.0f, 0.0f); |
| 588 | SkVector4 p3(10.0f, 20.0f, 0.0f); |
| 589 | SkVector4 p4(5.0f, 20.0f, 0.0f); |
| 590 | |
| 591 | REPORTER_ASSERT(reporter, is_rectilinear(p1, p2, p3, p4)); |
| 592 | |
| 593 | p1 = mul_with_persp_divide(transform, p1); |
| 594 | p2 = mul_with_persp_divide(transform, p2); |
| 595 | p3 = mul_with_persp_divide(transform, p3); |
| 596 | p4 = mul_with_persp_divide(transform, p4); |
| 597 | |
| 598 | return is_rectilinear(p1, p2, p3, p4); |
| 599 | } |
| 600 | |
| 601 | static void test(bool expected, skiatest::Reporter* reporter, const SkMatrix44& transform) { |
| 602 | if (expected) { |
| 603 | REPORTER_ASSERT(reporter, empirically_preserves_2d_axis_alignment(reporter, transform)); |
| 604 | REPORTER_ASSERT(reporter, transform.preserves2dAxisAlignment()); |
| 605 | } else { |
| 606 | REPORTER_ASSERT(reporter, !empirically_preserves_2d_axis_alignment(reporter, transform)); |
| 607 | REPORTER_ASSERT(reporter, !transform.preserves2dAxisAlignment()); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | static void test_preserves_2d_axis_alignment(skiatest::Reporter* reporter) { |
| 612 | SkMatrix44 transform(SkMatrix44::kUninitialized_Constructor); |
| 613 | SkMatrix44 transform2(SkMatrix44::kUninitialized_Constructor); |
| 614 | |
| 615 | static const struct TestCase { |
| 616 | SkMScalar a; // row 1, column 1 |
| 617 | SkMScalar b; // row 1, column 2 |
| 618 | SkMScalar c; // row 2, column 1 |
| 619 | SkMScalar d; // row 2, column 2 |
| 620 | bool expected; |
| 621 | } test_cases[] = { |
| 622 | { 3.f, 0.f, |
| 623 | 0.f, 4.f, true }, // basic case |
| 624 | { 0.f, 4.f, |
| 625 | 3.f, 0.f, true }, // rotate by 90 |
| 626 | { 0.f, 0.f, |
| 627 | 0.f, 4.f, true }, // degenerate x |
| 628 | { 3.f, 0.f, |
| 629 | 0.f, 0.f, true }, // degenerate y |
| 630 | { 0.f, 0.f, |
| 631 | 3.f, 0.f, true }, // degenerate x + rotate by 90 |
| 632 | { 0.f, 4.f, |
| 633 | 0.f, 0.f, true }, // degenerate y + rotate by 90 |
| 634 | { 3.f, 4.f, |
| 635 | 0.f, 0.f, false }, |
| 636 | { 0.f, 0.f, |
| 637 | 3.f, 4.f, false }, |
| 638 | { 0.f, 3.f, |
| 639 | 0.f, 4.f, false }, |
| 640 | { 3.f, 0.f, |
| 641 | 4.f, 0.f, false }, |
| 642 | { 3.f, 4.f, |
| 643 | 5.f, 0.f, false }, |
| 644 | { 3.f, 4.f, |
| 645 | 0.f, 5.f, false }, |
| 646 | { 3.f, 0.f, |
| 647 | 4.f, 5.f, false }, |
| 648 | { 0.f, 3.f, |
| 649 | 4.f, 5.f, false }, |
| 650 | { 2.f, 3.f, |
| 651 | 4.f, 5.f, false }, |
| 652 | }; |
| 653 | |
| 654 | for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) { |
| 655 | const TestCase& value = test_cases[i]; |
| 656 | transform.setIdentity(); |
| 657 | transform.set(0, 0, value.a); |
| 658 | transform.set(0, 1, value.b); |
| 659 | transform.set(1, 0, value.c); |
| 660 | transform.set(1, 1, value.d); |
| 661 | |
| 662 | test(value.expected, reporter, transform); |
| 663 | } |
| 664 | |
| 665 | // Try the same test cases again, but this time make sure that other matrix |
| 666 | // elements (except perspective) have entries, to test that they are ignored. |
| 667 | for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) { |
| 668 | const TestCase& value = test_cases[i]; |
| 669 | transform.setIdentity(); |
| 670 | transform.set(0, 0, value.a); |
| 671 | transform.set(0, 1, value.b); |
| 672 | transform.set(1, 0, value.c); |
| 673 | transform.set(1, 1, value.d); |
| 674 | |
| 675 | transform.set(0, 2, 1.f); |
| 676 | transform.set(0, 3, 2.f); |
| 677 | transform.set(1, 2, 3.f); |
| 678 | transform.set(1, 3, 4.f); |
| 679 | transform.set(2, 0, 5.f); |
| 680 | transform.set(2, 1, 6.f); |
| 681 | transform.set(2, 2, 7.f); |
| 682 | transform.set(2, 3, 8.f); |
| 683 | |
| 684 | test(value.expected, reporter, transform); |
| 685 | } |
| 686 | |
| 687 | // Try the same test cases again, but this time add perspective which is |
| 688 | // always assumed to not-preserve axis alignment. |
| 689 | for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) { |
| 690 | const TestCase& value = test_cases[i]; |
| 691 | transform.setIdentity(); |
| 692 | transform.set(0, 0, value.a); |
| 693 | transform.set(0, 1, value.b); |
| 694 | transform.set(1, 0, value.c); |
| 695 | transform.set(1, 1, value.d); |
| 696 | |
| 697 | transform.set(0, 2, 1.f); |
| 698 | transform.set(0, 3, 2.f); |
| 699 | transform.set(1, 2, 3.f); |
| 700 | transform.set(1, 3, 4.f); |
| 701 | transform.set(2, 0, 5.f); |
| 702 | transform.set(2, 1, 6.f); |
| 703 | transform.set(2, 2, 7.f); |
| 704 | transform.set(2, 3, 8.f); |
| 705 | transform.set(3, 0, 9.f); |
| 706 | transform.set(3, 1, 10.f); |
| 707 | transform.set(3, 2, 11.f); |
| 708 | transform.set(3, 3, 12.f); |
| 709 | |
| 710 | test(false, reporter, transform); |
| 711 | } |
| 712 | |
| 713 | // Try a few more practical situations to check precision |
| 714 | // Reuse TestCase (a, b, c, d) as (x, y, z, degrees) axis to rotate about. |
| 715 | TestCase rotation_tests[] = { |
| 716 | { 0.0, 0.0, 1.0, 90.0, true }, |
| 717 | { 0.0, 0.0, 1.0, 180.0, true }, |
| 718 | { 0.0, 0.0, 1.0, 270.0, true }, |
| 719 | { 0.0, 1.0, 0.0, 90.0, true }, |
| 720 | { 1.0, 0.0, 0.0, 90.0, true }, |
| 721 | { 0.0, 0.0, 1.0, 45.0, false }, |
| 722 | // In 3d these next two are non-preserving, but we're testing in 2d after |
| 723 | // orthographic projection, where they are. |
| 724 | { 0.0, 1.0, 0.0, 45.0, true }, |
| 725 | { 1.0, 0.0, 0.0, 45.0, true }, |
| 726 | }; |
| 727 | |
| 728 | for (size_t i = 0; i < sizeof(rotation_tests)/sizeof(TestCase); ++i) { |
| 729 | const TestCase& value = rotation_tests[i]; |
| 730 | transform.setRotateDegreesAbout(value.a, value.b, value.c, value.d); |
| 731 | test(value.expected, reporter, transform); |
| 732 | } |
| 733 | |
| 734 | static const struct DoubleRotationCase { |
| 735 | SkMScalar x1; |
| 736 | SkMScalar y1; |
| 737 | SkMScalar z1; |
| 738 | SkMScalar degrees1; |
| 739 | SkMScalar x2; |
| 740 | SkMScalar y2; |
| 741 | SkMScalar z2; |
| 742 | SkMScalar degrees2; |
| 743 | bool expected; |
| 744 | } double_rotation_tests[] = { |
| 745 | { 0.0, 0.0, 1.0, 90.0, 0.0, 1.0, 0.0, 90.0, true }, |
| 746 | { 0.0, 0.0, 1.0, 90.0, 1.0, 0.0, 0.0, 90.0, true }, |
| 747 | { 0.0, 1.0, 0.0, 90.0, 0.0, 0.0, 1.0, 90.0, true }, |
| 748 | }; |
| 749 | |
| 750 | for (size_t i = 0; i < sizeof(double_rotation_tests)/sizeof(DoubleRotationCase); ++i) { |
| 751 | const DoubleRotationCase& value = double_rotation_tests[i]; |
| 752 | transform.setRotateDegreesAbout(value.x1, value.y1, value.z1, value.degrees1); |
| 753 | transform2.setRotateDegreesAbout(value.x2, value.y2, value.z2, value.degrees2); |
| 754 | transform.postConcat(transform2); |
| 755 | test(value.expected, reporter, transform); |
| 756 | } |
| 757 | |
| 758 | // Perspective cases. |
| 759 | transform.setIdentity(); |
| 760 | transform.set(3, 2, -0.1); // Perspective depth 10 |
| 761 | transform2.setRotateDegreesAbout(0.0, 1.0, 0.0, 45.0); |
| 762 | transform.preConcat(transform2); |
| 763 | test(false, reporter, transform); |
| 764 | |
| 765 | transform.setIdentity(); |
| 766 | transform.set(3, 2, -0.1); // Perspective depth 10 |
| 767 | transform2.setRotateDegreesAbout(0.0, 0.0, 1.0, 90.0); |
| 768 | transform.preConcat(transform2); |
| 769 | test(true, reporter, transform); |
| 770 | } |
| 771 | |
reed | 39393e3 | 2014-10-21 12:33:21 -0700 | [diff] [blame] | 772 | // just want to exercise the various converters for MScalar |
| 773 | static void test_toint(skiatest::Reporter* reporter) { |
| 774 | SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 775 | mat.setScale(3, 3, 3); |
| 776 | |
| 777 | SkMScalar sum = SkMScalarFloor(mat.get(0, 0)) + |
| 778 | SkMScalarRound(mat.get(1, 0)) + |
| 779 | SkMScalarCeil(mat.get(2, 0)); |
| 780 | int isum = SkMScalarFloorToInt(mat.get(0, 1)) + |
| 781 | SkMScalarRoundToInt(mat.get(1, 2)) + |
| 782 | SkMScalarCeilToInt(mat.get(2, 3)); |
| 783 | REPORTER_ASSERT(reporter, sum >= 0); |
| 784 | REPORTER_ASSERT(reporter, isum >= 0); |
| 785 | REPORTER_ASSERT(reporter, static_cast<SkMScalar>(isum) == SkIntToMScalar(isum)); |
| 786 | } |
tomhudson | faccb8e | 2014-09-26 11:45:48 -0700 | [diff] [blame] | 787 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 788 | DEF_TEST(Matrix44, reporter) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 789 | SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 790 | SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
| 791 | SkMatrix44 iden1(SkMatrix44::kUninitialized_Constructor); |
| 792 | SkMatrix44 iden2(SkMatrix44::kUninitialized_Constructor); |
| 793 | SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 794 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 795 | mat.setTranslate(1, 1, 1); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 796 | mat.invert(&inverse); |
| 797 | iden1.setConcat(mat, inverse); |
| 798 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 799 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 800 | mat.setScale(2, 2, 2); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 801 | mat.invert(&inverse); |
| 802 | iden1.setConcat(mat, inverse); |
| 803 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 804 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 805 | mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 806 | mat.invert(&inverse); |
| 807 | iden1.setConcat(mat, inverse); |
| 808 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 809 | |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 810 | mat.setScale(3, 3, 3); |
| 811 | rot.setRotateDegreesAbout(0, 0, -1, 90); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 812 | mat.postConcat(rot); |
| 813 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
| 814 | mat.invert(&inverse); |
| 815 | iden1.setConcat(mat, inverse); |
| 816 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 817 | iden2.setConcat(inverse, mat); |
| 818 | REPORTER_ASSERT(reporter, is_identity(iden2)); |
reed@google.com | da9fac0 | 2011-06-13 14:46:52 +0000 | [diff] [blame] | 819 | |
commit-bot@chromium.org | f02f078 | 2013-08-20 15:25:04 +0000 | [diff] [blame] | 820 | // test tiny-valued matrix inverse |
| 821 | mat.reset(); |
| 822 | mat.setScale(1.0e-12, 1.0e-12, 1.0e-12); |
| 823 | rot.setRotateDegreesAbout(0, 0, -1, 90); |
| 824 | mat.postConcat(rot); |
| 825 | mat.postTranslate(1.0e-12, 1.0e-12, 1.0e-12); |
| 826 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
| 827 | mat.invert(&inverse); |
| 828 | iden1.setConcat(mat, inverse); |
| 829 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 830 | |
commit-bot@chromium.org | f02f078 | 2013-08-20 15:25:04 +0000 | [diff] [blame] | 831 | // test mixed-valued matrix inverse |
| 832 | mat.reset(); |
jvanverth@google.com | 25f72ed | 2013-09-03 19:46:16 +0000 | [diff] [blame] | 833 | mat.setScale(1.0e-10, 3.0, 1.0e+10); |
commit-bot@chromium.org | f02f078 | 2013-08-20 15:25:04 +0000 | [diff] [blame] | 834 | rot.setRotateDegreesAbout(0, 0, -1, 90); |
| 835 | mat.postConcat(rot); |
jvanverth@google.com | 25f72ed | 2013-09-03 19:46:16 +0000 | [diff] [blame] | 836 | mat.postTranslate(1.0e+10, 3.0, 1.0e-10); |
commit-bot@chromium.org | f02f078 | 2013-08-20 15:25:04 +0000 | [diff] [blame] | 837 | REPORTER_ASSERT(reporter, mat.invert(NULL)); |
| 838 | mat.invert(&inverse); |
| 839 | iden1.setConcat(mat, inverse); |
| 840 | REPORTER_ASSERT(reporter, is_identity(iden1)); |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 841 | |
commit-bot@chromium.org | f02f078 | 2013-08-20 15:25:04 +0000 | [diff] [blame] | 842 | // test degenerate matrix |
| 843 | mat.reset(); |
| 844 | mat.set3x3(1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
| 845 | REPORTER_ASSERT(reporter, !mat.invert(NULL)); |
skia.committer@gmail.com | b74bdf0 | 2013-08-21 07:01:29 +0000 | [diff] [blame] | 846 | |
reed@google.com | da9fac0 | 2011-06-13 14:46:52 +0000 | [diff] [blame] | 847 | // test rol/col Major getters |
| 848 | { |
| 849 | mat.setTranslate(2, 3, 4); |
| 850 | float dataf[16]; |
| 851 | double datad[16]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 852 | |
reed@google.com | da9fac0 | 2011-06-13 14:46:52 +0000 | [diff] [blame] | 853 | mat.asColMajorf(dataf); |
| 854 | assert16<float>(reporter, dataf, |
| 855 | 1, 0, 0, 0, |
| 856 | 0, 1, 0, 0, |
| 857 | 0, 0, 1, 0, |
| 858 | 2, 3, 4, 1); |
| 859 | mat.asColMajord(datad); |
| 860 | assert16<double>(reporter, datad, 1, 0, 0, 0, |
| 861 | 0, 1, 0, 0, |
| 862 | 0, 0, 1, 0, |
| 863 | 2, 3, 4, 1); |
| 864 | mat.asRowMajorf(dataf); |
| 865 | assert16<float>(reporter, dataf, 1, 0, 0, 2, |
| 866 | 0, 1, 0, 3, |
| 867 | 0, 0, 1, 4, |
| 868 | 0, 0, 0, 1); |
| 869 | mat.asRowMajord(datad); |
| 870 | assert16<double>(reporter, datad, 1, 0, 0, 2, |
| 871 | 0, 1, 0, 3, |
| 872 | 0, 0, 1, 4, |
| 873 | 0, 0, 0, 1); |
| 874 | } |
reed@google.com | 6f2b44d | 2011-06-24 18:13:39 +0000 | [diff] [blame] | 875 | |
reed@google.com | 80b577e | 2012-11-09 21:25:06 +0000 | [diff] [blame] | 876 | test_concat(reporter); |
| 877 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 878 | if (false) { // avoid bit rot, suppress warning (working on making this pass) |
| 879 | test_common_angles(reporter); |
| 880 | } |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 881 | |
vollick@chromium.org | 57a54e3 | 2012-12-10 20:16:10 +0000 | [diff] [blame] | 882 | test_constructor(reporter); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 883 | test_gettype(reporter); |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 884 | test_determinant(reporter); |
commit-bot@chromium.org | 9504575 | 2013-08-20 20:15:24 +0000 | [diff] [blame] | 885 | test_invert(reporter); |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 886 | test_transpose(reporter); |
| 887 | test_get_set_double(reporter); |
reed@google.com | 7d68335 | 2012-12-03 21:19:52 +0000 | [diff] [blame] | 888 | test_set_row_col_major(reporter); |
reed@google.com | 99b5c7f | 2012-12-05 22:13:59 +0000 | [diff] [blame] | 889 | test_translate(reporter); |
| 890 | test_scale(reporter); |
| 891 | test_map2(reporter); |
commit-bot@chromium.org | 722555b | 2013-10-05 01:16:30 +0000 | [diff] [blame] | 892 | test_3x3_conversion(reporter); |
tomhudson | a32f175 | 2014-09-16 08:29:29 -0700 | [diff] [blame] | 893 | test_has_perspective(reporter); |
tomhudson | faccb8e | 2014-09-26 11:45:48 -0700 | [diff] [blame] | 894 | test_preserves_2d_axis_alignment(reporter); |
reed | 39393e3 | 2014-10-21 12:33:21 -0700 | [diff] [blame] | 895 | test_toint(reporter); |
reed@google.com | 125002a | 2011-06-09 19:13:41 +0000 | [diff] [blame] | 896 | } |