Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkMath.h" | 9 #include "SkMath.h" |
| 10 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 585 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &sc aleY, &rotation1)); | 585 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &sc aleY, &rotation1)); |
| 586 mat.reset(); | 586 mat.reset(); |
| 587 // linearly dependent entries | 587 // linearly dependent entries |
| 588 mat[SkMatrix::kMScaleX] = 1.f; | 588 mat[SkMatrix::kMScaleX] = 1.f; |
| 589 mat[SkMatrix::kMSkewX] = 2.f; | 589 mat[SkMatrix::kMSkewX] = 2.f; |
| 590 mat[SkMatrix::kMSkewY] = 4.f; | 590 mat[SkMatrix::kMSkewY] = 4.f; |
| 591 mat[SkMatrix::kMScaleY] = 8.f; | 591 mat[SkMatrix::kMScaleY] = 8.f; |
| 592 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &sc aleY, &rotation1)); | 592 REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &sc aleY, &rotation1)); |
| 593 } | 593 } |
| 594 | 594 |
| 595 // For test_matrix_homogeneous, below. | |
| 596 static bool scalar_array_nearly_equal_relative(const SkScalar a[], const SkScala r b[], int count) { | |
| 597 for (int i = 0; i < count; ++i) { | |
| 598 if (!scalar_nearly_equal_relative(a[i], b[i])) { | |
| 599 return false; | |
| 600 } | |
| 601 } | |
| 602 return true; | |
| 603 } | |
| 604 | |
| 605 // For test_matrix_homogeneous, below. | |
| 606 // Maps a single triple in src using m and compares results to those in dst | |
| 607 static bool naive_homogeneous_mapping(const SkMatrix& m, const SkScalar src[3], | |
| 608 const SkScalar dst[3]) { | |
| 609 SkScalar res[3]; | |
| 610 res[0] = src[0] * m[0] + src[1] * m[1] + src[2] * m[2]; | |
| 611 res[1] = src[0] * m[3] + src[1] * m[4] + src[2] * m[5]; | |
| 612 res[2] = src[0] * m[6] + src[1] * m[7] + src[2] * m[8]; | |
| 613 return scalar_array_nearly_equal_relative(res, dst, 3); | |
| 614 } | |
| 615 | |
| 616 static void test_matrix_homogeneous(skiatest::Reporter* reporter) { | |
| 617 SkMatrix mat; | |
| 618 | |
| 619 const float kRotation0 = 15.5f; | |
| 620 const float kRotation1 = -50.f; | |
| 621 const float kScale0 = 5000.f; | |
| 622 | |
| 623 const int kTripleCount = 1000; | |
| 624 const int kMatrixCount = 1000; | |
| 625 SkRandom rand; | |
| 626 | |
| 627 SkScalar randTriples[3*kTripleCount]; | |
| 628 for (int i = 0; i < 3*kTripleCount; ++i) { | |
| 629 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f); | |
| 630 } | |
| 631 | |
| 632 SkMatrix mats[kMatrixCount]; | |
| 633 for (int i = 0; i < kMatrixCount; ++i) { | |
| 634 for (int j = 0; j < 9; ++j) { | |
| 635 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); | |
| 636 } | |
| 637 } | |
| 638 | |
| 639 // identity | |
| 640 { | |
| 641 mat.reset(); | |
| 642 SkScalar dst[3*kTripleCount]; | |
| 643 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); | |
| 644 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, ds t, kTripleCount*3)); | |
| 645 } | |
| 646 | |
| 647 // zero matrix | |
| 648 { | |
| 649 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); | |
| 650 SkScalar dst[3*kTripleCount]; | |
| 651 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); | |
| 652 SkScalar zeros[3] = {0.f, 0.f, 0.f}; | |
| 653 for (int i = 0; i < kTripleCount; ++i) { | |
| 654 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3], zeros, 3)); | |
| 655 } | |
| 656 } | |
| 657 | |
| 658 // zero point | |
| 659 { | |
| 660 SkScalar zeros[3] = {0.f, 0.f, 0.f}; | |
| 661 for (int i = 0; i < kMatrixCount; ++i) { | |
| 662 SkScalar dst[3]; | |
| 663 mats[i].mapHomogeneousPoints(dst, zeros, 1); | |
| 664 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros, 3)); | |
| 665 } | |
| 666 } | |
| 667 | |
| 668 // doesn't crash with null dst, src, count <= 0 | |
| 669 { | |
| 670 mats[0].mapHomogeneousPoints(NULL, NULL, 0); | |
| 671 mats[0].mapHomogeneousPoints(NULL, NULL, -1); | |
|
bsalomon
2013/08/09 13:53:25
This doesn't trigger an assert?
egdaniel
2013/08/09 13:58:24
So from what Mike was telling me, the tests are no
bsalomon
2013/08/09 14:04:59
The tests *are* run in debug builds :) So if -1 is
| |
| 672 } | |
| 673 | |
| 674 // uniform scale of point | |
| 675 { | |
| 676 mat.setScale(kScale0, kScale0); | |
| 677 SkScalar dst[3]; | |
| 678 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; | |
| 679 SkPoint pnt; | |
| 680 pnt.set(src[0], src[1]); | |
| 681 mat.mapHomogeneousPoints(dst, src, 1); | |
| 682 mat.mapPoints(&pnt, &pnt, 1); | |
| 683 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); | |
| 684 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); | |
| 685 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); | |
| 686 } | |
| 687 | |
| 688 // rotation of point | |
| 689 { | |
| 690 mat.setRotate(kRotation0); | |
| 691 SkScalar dst[3]; | |
| 692 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; | |
| 693 SkPoint pnt; | |
| 694 pnt.set(src[0], src[1]); | |
| 695 mat.mapHomogeneousPoints(dst, src, 1); | |
| 696 mat.mapPoints(&pnt, &pnt, 1); | |
| 697 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); | |
| 698 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); | |
| 699 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); | |
| 700 } | |
| 701 | |
| 702 // rotation, scale, rotation of point | |
| 703 { | |
| 704 mat.setRotate(kRotation1); | |
| 705 mat.postScale(kScale0, kScale0); | |
| 706 mat.postRotate(kRotation0); | |
| 707 SkScalar dst[3]; | |
| 708 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; | |
| 709 SkPoint pnt; | |
| 710 pnt.set(src[0], src[1]); | |
| 711 mat.mapHomogeneousPoints(dst, src, 1); | |
| 712 mat.mapPoints(&pnt, &pnt, 1); | |
| 713 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); | |
| 714 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); | |
| 715 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); | |
| 716 } | |
| 717 | |
| 718 // compare with naive approach | |
| 719 { | |
| 720 for (int i = 0; i < kMatrixCount; ++i) { | |
| 721 for (int j = 0; j < kTripleCount; ++j) { | |
| 722 SkScalar dst[3]; | |
| 723 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1); | |
| 724 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTr iples[j*3], dst)); | |
| 725 } | |
| 726 } | |
| 727 } | |
| 728 | |
| 729 } | |
| 730 | |
| 595 static void TestMatrix(skiatest::Reporter* reporter) { | 731 static void TestMatrix(skiatest::Reporter* reporter) { |
| 596 SkMatrix mat, inverse, iden1, iden2; | 732 SkMatrix mat, inverse, iden1, iden2; |
| 597 | 733 |
| 598 mat.reset(); | 734 mat.reset(); |
| 599 mat.setTranslate(SK_Scalar1, SK_Scalar1); | 735 mat.setTranslate(SK_Scalar1, SK_Scalar1); |
| 600 REPORTER_ASSERT(reporter, mat.invert(&inverse)); | 736 REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
| 601 iden1.setConcat(mat, inverse); | 737 iden1.setConcat(mat, inverse); |
| 602 REPORTER_ASSERT(reporter, is_identity(iden1)); | 738 REPORTER_ASSERT(reporter, is_identity(iden1)); |
| 603 | 739 |
| 604 mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); | 740 mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 706 #ifdef SK_SCALAR_IS_FIXED | 842 #ifdef SK_SCALAR_IS_FIXED |
| 707 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); | 843 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
| 708 #else | 844 #else |
| 709 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); | 845 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
| 710 #endif | 846 #endif |
| 711 | 847 |
| 712 test_matrix_max_stretch(reporter); | 848 test_matrix_max_stretch(reporter); |
| 713 test_matrix_is_similarity(reporter); | 849 test_matrix_is_similarity(reporter); |
| 714 test_matrix_recttorect(reporter); | 850 test_matrix_recttorect(reporter); |
| 715 test_matrix_decomposition(reporter); | 851 test_matrix_decomposition(reporter); |
| 852 test_matrix_homogeneous(reporter); | |
| 716 } | 853 } |
| 717 | 854 |
| 718 #include "TestClassDef.h" | 855 #include "TestClassDef.h" |
| 719 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) | 856 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) |
| OLD | NEW |