OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkMatrix44.h" | 9 #include "SkMatrix44.h" |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) { | 66 if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) { |
67 SkDebugf("not equal %g %g\n", a.get(i, j), b.get(i, j)); | 67 SkDebugf("not equal %g %g\n", a.get(i, j), b.get(i, j)); |
68 return false; | 68 return false; |
69 } | 69 } |
70 } | 70 } |
71 } | 71 } |
72 return true; | 72 return true; |
73 } | 73 } |
74 | 74 |
75 static bool is_identity(const SkMatrix44& m) { | 75 static bool is_identity(const SkMatrix44& m) { |
76 SkMatrix44 identity; | 76 SkMatrix44 identity(SkMatrix44::kIdentity_Constructor); |
77 identity.reset(); | |
78 return nearly_equal(m, identity); | 77 return nearly_equal(m, identity); |
79 } | 78 } |
80 | 79 |
81 /////////////////////////////////////////////////////////////////////////////// | 80 /////////////////////////////////////////////////////////////////////////////// |
82 static bool bits_isonly(int value, int mask) { | 81 static bool bits_isonly(int value, int mask) { |
83 return 0 == (value & ~mask); | 82 return 0 == (value & ~mask); |
84 } | 83 } |
85 | 84 |
86 static void test_constructor(skiatest::Reporter* reporter) { | 85 static void test_constructor(skiatest::Reporter* reporter) { |
87 // Allocate a matrix on the heap | 86 // Allocate a matrix on the heap |
88 SkMatrix44* placeholderMatrix = new SkMatrix44(); | 87 SkMatrix44* placeholderMatrix = new SkMatrix44(SkMatrix44::kUninitialized_Co
nstructor); |
89 SkAutoTDelete<SkMatrix44> deleteMe(placeholderMatrix); | 88 SkAutoTDelete<SkMatrix44> deleteMe(placeholderMatrix); |
90 | 89 |
91 for (int row = 0; row < 4; ++row) { | 90 for (int row = 0; row < 4; ++row) { |
92 for (int col = 0; col < 4; ++col) { | 91 for (int col = 0; col < 4; ++col) { |
93 placeholderMatrix->setDouble(row, col, row * col); | 92 placeholderMatrix->setDouble(row, col, row * col); |
94 } | 93 } |
95 } | 94 } |
96 | 95 |
97 // Use placement-new syntax to trigger the constructor on top of the heap | 96 // Use placement-new syntax to trigger the constructor on top of the heap |
98 // address we already initialized. This allows us to check that the | 97 // address we already initialized. This allows us to check that the |
99 // constructor did avoid initializing the matrix contents. | 98 // constructor did avoid initializing the matrix contents. |
100 SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUnin
itialized_Constructor); | 99 SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUnin
itialized_Constructor); |
101 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); | 100 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); |
102 REPORTER_ASSERT(reporter, !testMatrix->isIdentity()); | 101 REPORTER_ASSERT(reporter, !testMatrix->isIdentity()); |
103 for (int row = 0; row < 4; ++row) { | 102 for (int row = 0; row < 4; ++row) { |
104 for (int col = 0; col < 4; ++col) { | 103 for (int col = 0; col < 4; ++col) { |
105 REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix-
>getDouble(row, col))); | 104 REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix-
>getDouble(row, col))); |
106 } | 105 } |
107 } | 106 } |
108 | 107 |
109 // Verify that kIdentity_Constructor really does initialize to an identity m
atrix. | 108 // Verify that kIdentity_Constructor really does initialize to an identity m
atrix. |
110 testMatrix = 0; | 109 testMatrix = 0; |
111 testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constru
ctor); | 110 testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constru
ctor); |
112 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); | 111 REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix); |
113 REPORTER_ASSERT(reporter, testMatrix->isIdentity()); | 112 REPORTER_ASSERT(reporter, testMatrix->isIdentity()); |
114 REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I()); | 113 REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I()); |
115 } | 114 } |
116 | 115 |
117 static void test_translate(skiatest::Reporter* reporter) { | 116 static void test_translate(skiatest::Reporter* reporter) { |
118 SkMatrix44 mat, inverse; | 117 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 118 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
119 | 119 |
120 mat.setTranslate(0, 0, 0); | 120 mat.setTranslate(0, 0, 0); |
121 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_M
ask)); | 121 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_M
ask)); |
122 mat.setTranslate(1, 2, 3); | 122 mat.setTranslate(1, 2, 3); |
123 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_
Mask)); | 123 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_
Mask)); |
124 REPORTER_ASSERT(reporter, mat.invert(&inverse)); | 124 REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
125 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTransl
ate_Mask)); | 125 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTransl
ate_Mask)); |
126 | 126 |
127 SkMatrix44 a, b, c; | 127 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 128 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 129 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
128 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); | 130 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); |
129 b.setTranslate(10, 11, 12); | 131 b.setTranslate(10, 11, 12); |
130 | 132 |
131 c.setConcat(a, b); | 133 c.setConcat(a, b); |
132 mat = a; | 134 mat = a; |
133 mat.preTranslate(10, 11, 12); | 135 mat.preTranslate(10, 11, 12); |
134 REPORTER_ASSERT(reporter, mat == c); | 136 REPORTER_ASSERT(reporter, mat == c); |
135 | 137 |
136 c.setConcat(b, a); | 138 c.setConcat(b, a); |
137 mat = a; | 139 mat = a; |
138 mat.postTranslate(10, 11, 12); | 140 mat.postTranslate(10, 11, 12); |
139 REPORTER_ASSERT(reporter, mat == c); | 141 REPORTER_ASSERT(reporter, mat == c); |
140 } | 142 } |
141 | 143 |
142 static void test_scale(skiatest::Reporter* reporter) { | 144 static void test_scale(skiatest::Reporter* reporter) { |
143 SkMatrix44 mat, inverse; | 145 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 146 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
144 | 147 |
145 mat.setScale(1, 1, 1); | 148 mat.setScale(1, 1, 1); |
146 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_M
ask)); | 149 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_M
ask)); |
147 mat.setScale(1, 2, 3); | 150 mat.setScale(1, 2, 3); |
148 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask
)); | 151 REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask
)); |
149 REPORTER_ASSERT(reporter, mat.invert(&inverse)); | 152 REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
150 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_
Mask)); | 153 REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_
Mask)); |
151 | 154 |
152 SkMatrix44 a, b, c; | 155 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 156 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 157 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
153 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); | 158 a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9); |
154 b.setScale(10, 11, 12); | 159 b.setScale(10, 11, 12); |
155 | 160 |
156 c.setConcat(a, b); | 161 c.setConcat(a, b); |
157 mat = a; | 162 mat = a; |
158 mat.preScale(10, 11, 12); | 163 mat.preScale(10, 11, 12); |
159 REPORTER_ASSERT(reporter, mat == c); | 164 REPORTER_ASSERT(reporter, mat == c); |
160 | 165 |
161 c.setConcat(b, a); | 166 c.setConcat(b, a); |
162 mat = a; | 167 mat = a; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 205 |
201 mat.map2(src2, 1, dstA); | 206 mat.map2(src2, 1, dstA); |
202 mat.mapMScalars(src4, dstB); | 207 mat.mapMScalars(src4, dstB); |
203 | 208 |
204 for (int i = 0; i < 4; ++i) { | 209 for (int i = 0; i < 4; ++i) { |
205 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]); | 210 REPORTER_ASSERT(reporter, dstA[i] == dstB[i]); |
206 } | 211 } |
207 } | 212 } |
208 | 213 |
209 static void test_map2(skiatest::Reporter* reporter) { | 214 static void test_map2(skiatest::Reporter* reporter) { |
210 SkMatrix44 mat; | 215 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
211 | 216 |
212 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) { | 217 for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) { |
213 gMakeProcs[i](&mat); | 218 gMakeProcs[i](&mat); |
214 test_map2(reporter, mat); | 219 test_map2(reporter, mat); |
215 } | 220 } |
216 } | 221 } |
217 | 222 |
218 static void test_gettype(skiatest::Reporter* reporter) { | 223 static void test_gettype(skiatest::Reporter* reporter) { |
219 SkMatrix44 matrix; | 224 SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor); |
220 | 225 |
221 REPORTER_ASSERT(reporter, matrix.isIdentity()); | 226 REPORTER_ASSERT(reporter, matrix.isIdentity()); |
222 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType()); | 227 REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType()); |
223 | 228 |
224 int expectedMask; | 229 int expectedMask; |
225 | 230 |
226 matrix.set(1, 1, 0); | 231 matrix.set(1, 1, 0); |
227 expectedMask = SkMatrix44::kScale_Mask; | 232 expectedMask = SkMatrix44::kScale_Mask; |
228 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask); | 233 REPORTER_ASSERT(reporter, matrix.getType() == expectedMask); |
229 | 234 |
(...skipping 14 matching lines...) Expand all Loading... |
244 SkMScalar dz = 0; | 249 SkMScalar dz = 0; |
245 matrix.setTranslate(-dx, -dy, -dz); | 250 matrix.setTranslate(-dx, -dy, -dz); |
246 REPORTER_ASSERT(reporter, matrix.isIdentity()); | 251 REPORTER_ASSERT(reporter, matrix.isIdentity()); |
247 matrix.preTranslate(-dx, -dy, -dz); | 252 matrix.preTranslate(-dx, -dy, -dz); |
248 REPORTER_ASSERT(reporter, matrix.isIdentity()); | 253 REPORTER_ASSERT(reporter, matrix.isIdentity()); |
249 matrix.postTranslate(-dx, -dy, -dz); | 254 matrix.postTranslate(-dx, -dy, -dz); |
250 REPORTER_ASSERT(reporter, matrix.isIdentity()); | 255 REPORTER_ASSERT(reporter, matrix.isIdentity()); |
251 } | 256 } |
252 | 257 |
253 static void test_common_angles(skiatest::Reporter* reporter) { | 258 static void test_common_angles(skiatest::Reporter* reporter) { |
254 SkMatrix44 rot; | 259 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
255 // Test precision of rotation in common cases | 260 // Test precision of rotation in common cases |
256 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 }; | 261 int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 }; |
257 for (int i = 0; i < 9; ++i) { | 262 for (int i = 0; i < 9; ++i) { |
258 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i])); | 263 rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i])); |
259 | 264 |
260 SkMatrix rot3x3 = rot; | 265 SkMatrix rot3x3 = rot; |
261 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect()); | 266 REPORTER_ASSERT(reporter, rot3x3.rectStaysRect()); |
262 } | 267 } |
263 } | 268 } |
264 | 269 |
265 static void test_concat(skiatest::Reporter* reporter) { | 270 static void test_concat(skiatest::Reporter* reporter) { |
266 int i; | 271 int i; |
267 SkMatrix44 a, b, c, d; | 272 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 273 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 274 SkMatrix44 c(SkMatrix44::kUninitialized_Constructor); |
| 275 SkMatrix44 d(SkMatrix44::kUninitialized_Constructor); |
268 | 276 |
269 a.setTranslate(10, 10, 10); | 277 a.setTranslate(10, 10, 10); |
270 b.setScale(2, 2, 2); | 278 b.setScale(2, 2, 2); |
271 | 279 |
272 SkScalar src[8] = { | 280 SkScalar src[8] = { |
273 0, 0, 0, 1, | 281 0, 0, 0, 1, |
274 1, 1, 1, 1 | 282 1, 1, 1, 1 |
275 }; | 283 }; |
276 SkScalar dst[8]; | 284 SkScalar dst[8]; |
277 | 285 |
(...skipping 16 matching lines...) Expand all Loading... |
294 REPORTER_ASSERT(reporter, d == c); | 302 REPORTER_ASSERT(reporter, d == c); |
295 | 303 |
296 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4); | 304 c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4); |
297 for (i = 0; i < 3; ++i) { | 305 for (i = 0; i < 3; ++i) { |
298 REPORTER_ASSERT(reporter, 20 == dst[i]); | 306 REPORTER_ASSERT(reporter, 20 == dst[i]); |
299 REPORTER_ASSERT(reporter, 22 == dst[i + 4]); | 307 REPORTER_ASSERT(reporter, 22 == dst[i + 4]); |
300 } | 308 } |
301 } | 309 } |
302 | 310 |
303 static void test_determinant(skiatest::Reporter* reporter) { | 311 static void test_determinant(skiatest::Reporter* reporter) { |
304 SkMatrix44 a; | 312 SkMatrix44 a(SkMatrix44::kIdentity_Constructor); |
305 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant())); | 313 REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant())); |
306 a.set(1, 1, 2); | 314 a.set(1, 1, 2); |
307 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant())); | 315 REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant())); |
308 SkMatrix44 b; | 316 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
309 REPORTER_ASSERT(reporter, a.invert(&b)); | 317 REPORTER_ASSERT(reporter, a.invert(&b)); |
310 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant())); | 318 REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant())); |
311 SkMatrix44 c = b = a; | 319 SkMatrix44 c = b = a; |
312 c.set(0, 1, 4); | 320 c.set(0, 1, 4); |
313 b.set(1, 0, 4); | 321 b.set(1, 0, 4); |
314 REPORTER_ASSERT(reporter, | 322 REPORTER_ASSERT(reporter, |
315 nearly_equal_double(a.determinant(), | 323 nearly_equal_double(a.determinant(), |
316 b.determinant())); | 324 b.determinant())); |
317 SkMatrix44 d = a; | 325 SkMatrix44 d = a; |
318 d.set(0, 0, 8); | 326 d.set(0, 0, 8); |
319 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant())); | 327 REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant())); |
320 | 328 |
321 SkMatrix44 e = a; | 329 SkMatrix44 e = a; |
322 e.postConcat(d); | 330 e.postConcat(d); |
323 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant())); | 331 REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant())); |
324 e.set(0, 0, 0); | 332 e.set(0, 0, 0); |
325 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant())); | 333 REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant())); |
326 } | 334 } |
327 | 335 |
328 static void test_invert(skiatest::Reporter* reporter) { | 336 static void test_invert(skiatest::Reporter* reporter) { |
329 SkMatrix44 inverse; | 337 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
330 double inverseData[16]; | 338 double inverseData[16]; |
331 | 339 |
332 SkMatrix44 identity; | 340 SkMatrix44 identity(SkMatrix44::kIdentity_Constructor); |
333 identity.setIdentity(); | |
334 identity.invert(&inverse); | 341 identity.invert(&inverse); |
335 inverse.asRowMajord(inverseData); | 342 inverse.asRowMajord(inverseData); |
336 assert16<double>(reporter, inverseData, | 343 assert16<double>(reporter, inverseData, |
337 1, 0, 0, 0, | 344 1, 0, 0, 0, |
338 0, 1, 0, 0, | 345 0, 1, 0, 0, |
339 0, 0, 1, 0, | 346 0, 0, 1, 0, |
340 0, 0, 0, 1); | 347 0, 0, 0, 1); |
341 | 348 |
342 SkMatrix44 translation; | 349 SkMatrix44 translation(SkMatrix44::kUninitialized_Constructor); |
343 translation.setTranslate(2, 3, 4); | 350 translation.setTranslate(2, 3, 4); |
344 translation.invert(&inverse); | 351 translation.invert(&inverse); |
345 inverse.asRowMajord(inverseData); | 352 inverse.asRowMajord(inverseData); |
346 assert16<double>(reporter, inverseData, | 353 assert16<double>(reporter, inverseData, |
347 1, 0, 0, -2, | 354 1, 0, 0, -2, |
348 0, 1, 0, -3, | 355 0, 1, 0, -3, |
349 0, 0, 1, -4, | 356 0, 0, 1, -4, |
350 0, 0, 0, 1); | 357 0, 0, 0, 1); |
351 | 358 |
352 SkMatrix44 scale; | 359 SkMatrix44 scale(SkMatrix44::kUninitialized_Constructor); |
353 scale.setScale(2, 4, 8); | 360 scale.setScale(2, 4, 8); |
354 scale.invert(&inverse); | 361 scale.invert(&inverse); |
355 inverse.asRowMajord(inverseData); | 362 inverse.asRowMajord(inverseData); |
356 assert16<double>(reporter, inverseData, | 363 assert16<double>(reporter, inverseData, |
357 0.5, 0, 0, 0, | 364 0.5, 0, 0, 0, |
358 0, 0.25, 0, 0, | 365 0, 0.25, 0, 0, |
359 0, 0, 0.125, 0, | 366 0, 0, 0.125, 0, |
360 0, 0, 0, 1); | 367 0, 0, 0, 1); |
361 | 368 |
362 SkMatrix44 scaleTranslation; | 369 SkMatrix44 scaleTranslation(SkMatrix44::kUninitialized_Constructor); |
363 scaleTranslation.setScale(10, 100, 1000); | 370 scaleTranslation.setScale(10, 100, 1000); |
364 scaleTranslation.preTranslate(2, 3, 4); | 371 scaleTranslation.preTranslate(2, 3, 4); |
365 scaleTranslation.invert(&inverse); | 372 scaleTranslation.invert(&inverse); |
366 inverse.asRowMajord(inverseData); | 373 inverse.asRowMajord(inverseData); |
367 assert16<double>(reporter, inverseData, | 374 assert16<double>(reporter, inverseData, |
368 0.1, 0, 0, -2, | 375 0.1, 0, 0, -2, |
369 0, 0.01, 0, -3, | 376 0, 0.01, 0, -3, |
370 0, 0, 0.001, -4, | 377 0, 0, 0.001, -4, |
371 0, 0, 0, 1); | 378 0, 0, 0, 1); |
372 | 379 |
373 SkMatrix44 rotation; | 380 SkMatrix44 rotation(SkMatrix44::kUninitialized_Constructor); |
374 rotation.setRotateDegreesAbout(0, 0, 1, 90); | 381 rotation.setRotateDegreesAbout(0, 0, 1, 90); |
375 rotation.invert(&inverse); | 382 rotation.invert(&inverse); |
376 SkMatrix44 expected; | 383 SkMatrix44 expected(SkMatrix44::kUninitialized_Constructor); |
377 double expectedInverseRotation[16] = | 384 double expectedInverseRotation[16] = |
378 {0, 1, 0, 0, | 385 {0, 1, 0, 0, |
379 -1, 0, 0, 0, | 386 -1, 0, 0, 0, |
380 0, 0, 1, 0, | 387 0, 0, 1, 0, |
381 0, 0, 0, 1}; | 388 0, 0, 0, 1}; |
382 expected.setRowMajord(expectedInverseRotation); | 389 expected.setRowMajord(expectedInverseRotation); |
383 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); | 390 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
384 | 391 |
385 SkMatrix44 affine; | 392 SkMatrix44 affine(SkMatrix44::kUninitialized_Constructor); |
386 affine.setRotateDegreesAbout(0, 0, 1, 90); | 393 affine.setRotateDegreesAbout(0, 0, 1, 90); |
387 affine.preScale(10, 20, 100); | 394 affine.preScale(10, 20, 100); |
388 affine.preTranslate(2, 3, 4); | 395 affine.preTranslate(2, 3, 4); |
389 affine.invert(&inverse); | 396 affine.invert(&inverse); |
390 double expectedInverseAffine[16] = | 397 double expectedInverseAffine[16] = |
391 {0, 0.1, 0, -2, | 398 {0, 0.1, 0, -2, |
392 -0.05, 0, 0, -3, | 399 -0.05, 0, 0, -3, |
393 0, 0, 0.01, -4, | 400 0, 0, 0.01, -4, |
394 0, 0, 0, 1}; | 401 0, 0, 0, 1}; |
395 expected.setRowMajord(expectedInverseAffine); | 402 expected.setRowMajord(expectedInverseAffine); |
396 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); | 403 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
397 | 404 |
398 SkMatrix44 perspective; | 405 SkMatrix44 perspective(SkMatrix44::kIdentity_Constructor); |
399 perspective.setIdentity(); | |
400 perspective.setDouble(3, 2, 1.0); | 406 perspective.setDouble(3, 2, 1.0); |
401 perspective.invert(&inverse); | 407 perspective.invert(&inverse); |
402 double expectedInversePerspective[16] = | 408 double expectedInversePerspective[16] = |
403 {1, 0, 0, 0, | 409 {1, 0, 0, 0, |
404 0, 1, 0, 0, | 410 0, 1, 0, 0, |
405 0, 0, 1, 0, | 411 0, 0, 1, 0, |
406 0, 0, -1, 1}; | 412 0, 0, -1, 1}; |
407 expected.setRowMajord(expectedInversePerspective); | 413 expected.setRowMajord(expectedInversePerspective); |
408 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); | 414 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
409 | 415 |
410 SkMatrix44 affineAndPerspective; | 416 SkMatrix44 affineAndPerspective(SkMatrix44::kIdentity_Constructor); |
411 affineAndPerspective.setIdentity(); | |
412 affineAndPerspective.setDouble(3, 2, 1.0); | 417 affineAndPerspective.setDouble(3, 2, 1.0); |
413 affineAndPerspective.preScale(10, 20, 100); | 418 affineAndPerspective.preScale(10, 20, 100); |
414 affineAndPerspective.preTranslate(2, 3, 4); | 419 affineAndPerspective.preTranslate(2, 3, 4); |
415 affineAndPerspective.invert(&inverse); | 420 affineAndPerspective.invert(&inverse); |
416 double expectedInverseAffineAndPerspective[16] = | 421 double expectedInverseAffineAndPerspective[16] = |
417 {0.1, 0, 2, -2, | 422 {0.1, 0, 2, -2, |
418 0, 0.05, 3, -3, | 423 0, 0.05, 3, -3, |
419 0, 0, 4.01, -4, | 424 0, 0, 4.01, -4, |
420 0, 0, -1, 1}; | 425 0, 0, -1, 1}; |
421 expected.setRowMajord(expectedInverseAffineAndPerspective); | 426 expected.setRowMajord(expectedInverseAffineAndPerspective); |
422 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); | 427 REPORTER_ASSERT(reporter, nearly_equal(expected, inverse)); |
423 } | 428 } |
424 | 429 |
425 static void test_transpose(skiatest::Reporter* reporter) { | 430 static void test_transpose(skiatest::Reporter* reporter) { |
426 SkMatrix44 a; | 431 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
427 SkMatrix44 b; | 432 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
428 | 433 |
429 int i = 0; | 434 int i = 0; |
430 for (int row = 0; row < 4; ++row) { | 435 for (int row = 0; row < 4; ++row) { |
431 for (int col = 0; col < 4; ++col) { | 436 for (int col = 0; col < 4; ++col) { |
432 a.setDouble(row, col, i); | 437 a.setDouble(row, col, i); |
433 b.setDouble(col, row, i++); | 438 b.setDouble(col, row, i++); |
434 } | 439 } |
435 } | 440 } |
436 | 441 |
437 a.transpose(); | 442 a.transpose(); |
438 REPORTER_ASSERT(reporter, nearly_equal(a, b)); | 443 REPORTER_ASSERT(reporter, nearly_equal(a, b)); |
439 } | 444 } |
440 | 445 |
441 static void test_get_set_double(skiatest::Reporter* reporter) { | 446 static void test_get_set_double(skiatest::Reporter* reporter) { |
442 SkMatrix44 a; | 447 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
443 for (int row = 0; row < 4; ++row) { | 448 for (int row = 0; row < 4; ++row) { |
444 for (int col = 0; col < 4; ++col) { | 449 for (int col = 0; col < 4; ++col) { |
445 a.setDouble(row, col, 3.141592653589793); | 450 a.setDouble(row, col, 3.141592653589793); |
446 REPORTER_ASSERT(reporter, | 451 REPORTER_ASSERT(reporter, |
447 nearly_equal_double(3.141592653589793, | 452 nearly_equal_double(3.141592653589793, |
448 a.getDouble(row, col))); | 453 a.getDouble(row, col))); |
449 a.setDouble(row, col, 0); | 454 a.setDouble(row, col, 0); |
450 REPORTER_ASSERT(reporter, | 455 REPORTER_ASSERT(reporter, |
451 nearly_equal_double(0, a.getDouble(row, col))); | 456 nearly_equal_double(0, a.getDouble(row, col))); |
452 } | 457 } |
453 } | 458 } |
454 } | 459 } |
455 | 460 |
456 static void test_set_row_col_major(skiatest::Reporter* reporter) { | 461 static void test_set_row_col_major(skiatest::Reporter* reporter) { |
457 SkMatrix44 a, b, c, d; | 462 SkMatrix44 a(SkMatrix44::kUninitialized_Constructor); |
| 463 SkMatrix44 b(SkMatrix44::kUninitialized_Constructor); |
| 464 |
458 for (int row = 0; row < 4; ++row) { | 465 for (int row = 0; row < 4; ++row) { |
459 for (int col = 0; col < 4; ++col) { | 466 for (int col = 0; col < 4; ++col) { |
460 a.setDouble(row, col, row * 4 + col); | 467 a.setDouble(row, col, row * 4 + col); |
461 } | 468 } |
462 } | 469 } |
463 | 470 |
464 double bufferd[16]; | 471 double bufferd[16]; |
465 float bufferf[16]; | 472 float bufferf[16]; |
466 a.asColMajord(bufferd); | 473 a.asColMajord(bufferd); |
467 b.setColMajord(bufferd); | 474 b.setColMajord(bufferd); |
(...skipping 14 matching lines...) Expand all Loading... |
482 5, 6, 7, 8, | 489 5, 6, 7, 8, |
483 9, 10, 11, 12, | 490 9, 10, 11, 12, |
484 13, 14, 15, 16 }; | 491 13, 14, 15, 16 }; |
485 SkScalar values3x3[9] = { 1, 2, 4, | 492 SkScalar values3x3[9] = { 1, 2, 4, |
486 5, 6, 8, | 493 5, 6, 8, |
487 13, 14, 16 }; | 494 13, 14, 16 }; |
488 SkMScalar values4x4flattened[16] = { 1, 2, 0, 4, | 495 SkMScalar values4x4flattened[16] = { 1, 2, 0, 4, |
489 5, 6, 0, 8, | 496 5, 6, 0, 8, |
490 0, 0, 1, 0, | 497 0, 0, 1, 0, |
491 13, 14, 0, 16 }; | 498 13, 14, 0, 16 }; |
492 SkMatrix44 a44; | 499 SkMatrix44 a44(SkMatrix44::kUninitialized_Constructor); |
493 a44.setRowMajor(values4x4); | 500 a44.setRowMajor(values4x4); |
494 | 501 |
495 SkMatrix a33 = a44; | 502 SkMatrix a33 = a44; |
496 SkMatrix expected33; | 503 SkMatrix expected33; |
497 for (int i = 0; i < 9; i++) expected33[i] = values3x3[i]; | 504 for (int i = 0; i < 9; i++) expected33[i] = values3x3[i]; |
498 REPORTER_ASSERT(reporter, expected33 == a33); | 505 REPORTER_ASSERT(reporter, expected33 == a33); |
499 | 506 |
500 SkMatrix44 a44flattened = a33; | 507 SkMatrix44 a44flattened = a33; |
501 SkMatrix44 expected44flattened; | 508 SkMatrix44 expected44flattened(SkMatrix44::kUninitialized_Constructor); |
502 expected44flattened.setRowMajor(values4x4flattened); | 509 expected44flattened.setRowMajor(values4x4flattened); |
503 REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened)); | 510 REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened)); |
504 | 511 |
505 // Test that a point with a Z value of 0 is transformed the same way. | 512 // Test that a point with a Z value of 0 is transformed the same way. |
506 SkScalar vec4[4] = { 2, 4, 0, 8 }; | 513 SkScalar vec4[4] = { 2, 4, 0, 8 }; |
507 SkScalar vec3[3] = { 2, 4, 8 }; | 514 SkScalar vec3[3] = { 2, 4, 8 }; |
508 | 515 |
509 SkScalar vec4transformed[4]; | 516 SkScalar vec4transformed[4]; |
510 SkScalar vec3transformed[3]; | 517 SkScalar vec3transformed[3]; |
511 SkScalar vec4transformed2[4]; | 518 SkScalar vec4transformed2[4]; |
512 a44.mapScalars(vec4, vec4transformed); | 519 a44.mapScalars(vec4, vec4transformed); |
513 a33.mapHomogeneousPoints(vec3transformed, vec3, 1); | 520 a33.mapHomogeneousPoints(vec3transformed, vec3, 1); |
514 a44flattened.mapScalars(vec4, vec4transformed2); | 521 a44flattened.mapScalars(vec4, vec4transformed2); |
515 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transf
ormed[0])); | 522 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transf
ormed[0])); |
516 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transf
ormed[1])); | 523 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transf
ormed[1])); |
517 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transf
ormed[2])); | 524 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transf
ormed[2])); |
518 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transf
ormed2[0])); | 525 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transf
ormed2[0])); |
519 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transf
ormed2[1])); | 526 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transf
ormed2[1])); |
520 REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4trans
formed2[2])); | 527 REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4trans
formed2[2])); |
521 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transf
ormed2[3])); | 528 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transf
ormed2[3])); |
522 } | 529 } |
523 | 530 |
524 static void TestMatrix44(skiatest::Reporter* reporter) { | 531 static void TestMatrix44(skiatest::Reporter* reporter) { |
525 SkMatrix44 mat, inverse, iden1, iden2, rot; | 532 SkMatrix44 mat(SkMatrix44::kUninitialized_Constructor); |
| 533 SkMatrix44 inverse(SkMatrix44::kUninitialized_Constructor); |
| 534 SkMatrix44 iden1(SkMatrix44::kUninitialized_Constructor); |
| 535 SkMatrix44 iden2(SkMatrix44::kUninitialized_Constructor); |
| 536 SkMatrix44 rot(SkMatrix44::kUninitialized_Constructor); |
526 | 537 |
527 mat.reset(); | |
528 mat.setTranslate(1, 1, 1); | 538 mat.setTranslate(1, 1, 1); |
529 mat.invert(&inverse); | 539 mat.invert(&inverse); |
530 iden1.setConcat(mat, inverse); | 540 iden1.setConcat(mat, inverse); |
531 REPORTER_ASSERT(reporter, is_identity(iden1)); | 541 REPORTER_ASSERT(reporter, is_identity(iden1)); |
532 | 542 |
533 mat.setScale(2, 2, 2); | 543 mat.setScale(2, 2, 2); |
534 mat.invert(&inverse); | 544 mat.invert(&inverse); |
535 iden1.setConcat(mat, inverse); | 545 iden1.setConcat(mat, inverse); |
536 REPORTER_ASSERT(reporter, is_identity(iden1)); | 546 REPORTER_ASSERT(reporter, is_identity(iden1)); |
537 | 547 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 test_get_set_double(reporter); | 630 test_get_set_double(reporter); |
621 test_set_row_col_major(reporter); | 631 test_set_row_col_major(reporter); |
622 test_translate(reporter); | 632 test_translate(reporter); |
623 test_scale(reporter); | 633 test_scale(reporter); |
624 test_map2(reporter); | 634 test_map2(reporter); |
625 test_3x3_conversion(reporter); | 635 test_3x3_conversion(reporter); |
626 } | 636 } |
627 | 637 |
628 #include "TestClassDef.h" | 638 #include "TestClassDef.h" |
629 DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44) | 639 DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44) |
OLD | NEW |