Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: tests/Matrix44Test.cpp

Issue 25484006: Add perspective support to SkMatrix44 initializers. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Switch SkMatrix inputs to SkScalar and be more careful about scalar comparisons Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/utils/SkMatrix44.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 static bool nearly_equal_double(double a, double b) { 11 static bool nearly_equal_double(double a, double b) {
12 const double tolerance = 1e-7; 12 const double tolerance = 1e-7;
13 double diff = a - b; 13 double diff = a - b;
14 if (diff < 0) 14 if (diff < 0)
15 diff = -diff; 15 diff = -diff;
16 return diff <= tolerance; 16 return diff <= tolerance;
17 } 17 }
18 18
19 static bool nearly_equal_scalar(SkMScalar a, SkMScalar b) { 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) {
20 // Note that we get more compounded error for multiple operations when 26 // Note that we get more compounded error for multiple operations when
21 // SK_SCALAR_IS_FIXED. 27 // SK_SCALAR_IS_FIXED.
22 #ifdef SK_SCALAR_IS_FLOAT 28 #ifdef SK_SCALAR_IS_FLOAT
23 const SkScalar tolerance = SK_Scalar1 / 200000; 29 const SkScalar tolerance = SK_Scalar1 / 200000;
24 #else 30 #else
25 const SkScalar tolerance = SK_Scalar1 / 1024; 31 const SkScalar tolerance = SK_Scalar1 / 1024;
26 #endif 32 #endif
27 33
28 return SkTAbs<SkMScalar>(a - b) <= tolerance; 34 return SkScalarAbs(a - b) <= tolerance;
29 } 35 }
30 36
31 template <typename T> void assert16(skiatest::Reporter* reporter, const T data[] , 37 template <typename T> void assert16(skiatest::Reporter* reporter, const T data[] ,
32 T m0, T m1, T m2, T m3, 38 T m0, T m1, T m2, T m3,
33 T m4, T m5, T m6, T m7, 39 T m4, T m5, T m6, T m7,
34 T m8, T m9, T m10, T m11, 40 T m8, T m9, T m10, T m11,
35 T m12, T m13, T m14, T m15) { 41 T m12, T m13, T m14, T m15) {
36 REPORTER_ASSERT(reporter, data[0] == m0); 42 REPORTER_ASSERT(reporter, data[0] == m0);
37 REPORTER_ASSERT(reporter, data[1] == m1); 43 REPORTER_ASSERT(reporter, data[1] == m1);
38 REPORTER_ASSERT(reporter, data[2] == m2); 44 REPORTER_ASSERT(reporter, data[2] == m2);
(...skipping 11 matching lines...) Expand all
50 56
51 REPORTER_ASSERT(reporter, data[12] == m12); 57 REPORTER_ASSERT(reporter, data[12] == m12);
52 REPORTER_ASSERT(reporter, data[13] == m13); 58 REPORTER_ASSERT(reporter, data[13] == m13);
53 REPORTER_ASSERT(reporter, data[14] == m14); 59 REPORTER_ASSERT(reporter, data[14] == m14);
54 REPORTER_ASSERT(reporter, data[15] == m15); 60 REPORTER_ASSERT(reporter, data[15] == m15);
55 } 61 }
56 62
57 static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) { 63 static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) {
58 for (int i = 0; i < 4; ++i) { 64 for (int i = 0; i < 4; ++i) {
59 for (int j = 0; j < 4; ++j) { 65 for (int j = 0; j < 4; ++j) {
60 if (!nearly_equal_scalar(a.get(i, j), b.get(i, j))) { 66 if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) {
61 printf("not equal %g %g\n", a.get(i, j), b.get(i, j)); 67 printf("not equal %g %g\n", a.get(i, j), b.get(i, j));
62 return false; 68 return false;
63 } 69 }
64 } 70 }
65 } 71 }
66 return true; 72 return true;
67 } 73 }
68 74
69 static bool is_identity(const SkMatrix44& m) { 75 static bool is_identity(const SkMatrix44& m) {
70 SkMatrix44 identity; 76 SkMatrix44 identity;
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 b.transpose(); 470 b.transpose();
465 REPORTER_ASSERT(reporter, nearly_equal(a, b)); 471 REPORTER_ASSERT(reporter, nearly_equal(a, b));
466 a.asColMajorf(bufferf); 472 a.asColMajorf(bufferf);
467 b.setColMajorf(bufferf); 473 b.setColMajorf(bufferf);
468 REPORTER_ASSERT(reporter, nearly_equal(a, b)); 474 REPORTER_ASSERT(reporter, nearly_equal(a, b));
469 b.setRowMajorf(bufferf); 475 b.setRowMajorf(bufferf);
470 b.transpose(); 476 b.transpose();
471 REPORTER_ASSERT(reporter, nearly_equal(a, b)); 477 REPORTER_ASSERT(reporter, nearly_equal(a, b));
472 } 478 }
473 479
480 static void test_3x3_conversion(skiatest::Reporter* reporter) {
481 SkMScalar values4x4[16] = { 1, 2, 3, 4,
482 5, 6, 7, 8,
483 9, 10, 11, 12,
484 13, 14, 15, 16 };
485 SkScalar values3x3[9] = { 1, 2, 4,
486 5, 6, 8,
487 13, 14, 16 };
488 SkMScalar values4x4flattened[16] = { 1, 2, 0, 4,
489 5, 6, 0, 8,
490 0, 0, 1, 0,
491 13, 14, 0, 16 };
492 SkMatrix44 a44;
493 a44.setRowMajor(values4x4);
494
495 SkMatrix a33 = a44;
496 SkMatrix expected33;
497 for (int i = 0; i < 9; i++) expected33[i] = values3x3[i];
498 REPORTER_ASSERT(reporter, expected33 == a33);
499
500 SkMatrix44 a44flattened = a33;
501 SkMatrix44 expected44flattened;
502 expected44flattened.setRowMajor(values4x4flattened);
503 REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened));
504
505 // Test that a point with a Z value of 0 is transformed the same way.
506 SkScalar vec4[4] = { 2, 4, 0, 8 };
507 SkScalar vec3[3] = { 2, 4, 8 };
508
509 SkScalar vec4transformed[4];
510 SkScalar vec3transformed[3];
511 SkScalar vec4transformed2[4];
512 a44.mapScalars(vec4, vec4transformed);
513 a33.mapHomogeneousPoints(vec3transformed, vec3, 1);
514 a44flattened.mapScalars(vec4, vec4transformed2);
515 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transf ormed[0]));
516 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transf ormed[1]));
517 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transf ormed[2]));
518 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transf ormed2[0]));
519 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transf ormed2[1]));
520 REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4trans formed2[2]));
521 REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transf ormed2[3]));
522 }
523
474 static void TestMatrix44(skiatest::Reporter* reporter) { 524 static void TestMatrix44(skiatest::Reporter* reporter) {
475 SkMatrix44 mat, inverse, iden1, iden2, rot; 525 SkMatrix44 mat, inverse, iden1, iden2, rot;
476 526
477 mat.reset(); 527 mat.reset();
478 mat.setTranslate(1, 1, 1); 528 mat.setTranslate(1, 1, 1);
479 mat.invert(&inverse); 529 mat.invert(&inverse);
480 iden1.setConcat(mat, inverse); 530 iden1.setConcat(mat, inverse);
481 REPORTER_ASSERT(reporter, is_identity(iden1)); 531 REPORTER_ASSERT(reporter, is_identity(iden1));
482 532
483 mat.setScale(2, 2, 2); 533 mat.setScale(2, 2, 2);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 test_constructor(reporter); 615 test_constructor(reporter);
566 test_gettype(reporter); 616 test_gettype(reporter);
567 test_determinant(reporter); 617 test_determinant(reporter);
568 test_invert(reporter); 618 test_invert(reporter);
569 test_transpose(reporter); 619 test_transpose(reporter);
570 test_get_set_double(reporter); 620 test_get_set_double(reporter);
571 test_set_row_col_major(reporter); 621 test_set_row_col_major(reporter);
572 test_translate(reporter); 622 test_translate(reporter);
573 test_scale(reporter); 623 test_scale(reporter);
574 test_map2(reporter); 624 test_map2(reporter);
625 test_3x3_conversion(reporter);
575 } 626 }
576 627
577 #include "TestClassDef.h" 628 #include "TestClassDef.h"
578 DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44) 629 DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)
OLDNEW
« no previous file with comments | « src/utils/SkMatrix44.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698