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 SkScalar ms[9] = {m[0], m[1], m[2], |
| 611 m[3], m[4], m[5], |
| 612 m[6], m[7], m[8]}; |
| 613 res[0] = src[0] * ms[0] + src[1] * ms[1] + src[2] * ms[2]; |
| 614 res[1] = src[0] * ms[3] + src[1] * ms[4] + src[2] * ms[5]; |
| 615 res[2] = src[0] * ms[6] + src[1] * ms[7] + src[2] * ms[8]; |
| 616 return scalar_array_nearly_equal_relative(res, dst, 3); |
| 617 } |
| 618 |
| 619 static void test_matrix_homogeneous(skiatest::Reporter* reporter) { |
| 620 SkMatrix mat; |
| 621 |
| 622 const float kRotation0 = 15.5f; |
| 623 const float kRotation1 = -50.f; |
| 624 const float kScale0 = 5000.f; |
| 625 |
| 626 const int kTripleCount = 1000; |
| 627 const int kMatrixCount = 1000; |
| 628 SkRandom rand; |
| 629 |
| 630 SkScalar randTriples[3*kTripleCount]; |
| 631 for (int i = 0; i < 3*kTripleCount; ++i) { |
| 632 randTriples[i] = rand.nextRangeF(-3000.f, 3000.f); |
| 633 } |
| 634 |
| 635 SkMatrix mats[kMatrixCount]; |
| 636 for (int i = 0; i < kMatrixCount; ++i) { |
| 637 for (int j = 0; j < 9; ++j) { |
| 638 mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f)); |
| 639 } |
| 640 } |
| 641 |
| 642 // identity |
| 643 { |
| 644 mat.reset(); |
| 645 SkScalar dst[3*kTripleCount]; |
| 646 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 647 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(randTriples, ds
t, kTripleCount*3)); |
| 648 } |
| 649 |
| 650 // zero matrix |
| 651 { |
| 652 mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f); |
| 653 SkScalar dst[3*kTripleCount]; |
| 654 mat.mapHomogeneousPoints(dst, randTriples, kTripleCount); |
| 655 SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 656 for (int i = 0; i < kTripleCount; ++i) { |
| 657 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(&dst[i*3],
zeros, 3)); |
| 658 } |
| 659 } |
| 660 |
| 661 // zero point |
| 662 { |
| 663 SkScalar zeros[3] = {0.f, 0.f, 0.f}; |
| 664 for (int i = 0; i < kMatrixCount; ++i) { |
| 665 SkScalar dst[3]; |
| 666 mats[i].mapHomogeneousPoints(dst, zeros, 1); |
| 667 REPORTER_ASSERT(reporter, scalar_array_nearly_equal_relative(dst, zeros,
3)); |
| 668 } |
| 669 } |
| 670 |
| 671 // doesn't crash with null dst, src, count == 0 |
| 672 { |
| 673 mats[0].mapHomogeneousPoints(NULL, NULL, 0); |
| 674 } |
| 675 |
| 676 // uniform scale of point |
| 677 { |
| 678 mat.setScale(kScale0, kScale0); |
| 679 SkScalar dst[3]; |
| 680 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 681 SkPoint pnt; |
| 682 pnt.set(src[0], src[1]); |
| 683 mat.mapHomogeneousPoints(dst, src, 1); |
| 684 mat.mapPoints(&pnt, &pnt, 1); |
| 685 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 686 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 687 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 688 } |
| 689 |
| 690 // rotation of point |
| 691 { |
| 692 mat.setRotate(kRotation0); |
| 693 SkScalar dst[3]; |
| 694 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 695 SkPoint pnt; |
| 696 pnt.set(src[0], src[1]); |
| 697 mat.mapHomogeneousPoints(dst, src, 1); |
| 698 mat.mapPoints(&pnt, &pnt, 1); |
| 699 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 700 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 701 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 702 } |
| 703 |
| 704 // rotation, scale, rotation of point |
| 705 { |
| 706 mat.setRotate(kRotation1); |
| 707 mat.postScale(kScale0, kScale0); |
| 708 mat.postRotate(kRotation0); |
| 709 SkScalar dst[3]; |
| 710 SkScalar src[3] = {randTriples[0], randTriples[1], 1.f}; |
| 711 SkPoint pnt; |
| 712 pnt.set(src[0], src[1]); |
| 713 mat.mapHomogeneousPoints(dst, src, 1); |
| 714 mat.mapPoints(&pnt, &pnt, 1); |
| 715 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[0], pnt.fX)); |
| 716 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[1], pnt.fY)); |
| 717 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst[2], SK_Scalar1)); |
| 718 } |
| 719 |
| 720 // compare with naive approach |
| 721 { |
| 722 for (int i = 0; i < kMatrixCount; ++i) { |
| 723 for (int j = 0; j < kTripleCount; ++j) { |
| 724 SkScalar dst[3]; |
| 725 mats[i].mapHomogeneousPoints(dst, &randTriples[j*3], 1); |
| 726 REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], &randTr
iples[j*3], dst)); |
| 727 } |
| 728 } |
| 729 } |
| 730 |
| 731 } |
| 732 |
595 static void TestMatrix(skiatest::Reporter* reporter) { | 733 static void TestMatrix(skiatest::Reporter* reporter) { |
596 SkMatrix mat, inverse, iden1, iden2; | 734 SkMatrix mat, inverse, iden1, iden2; |
597 | 735 |
598 mat.reset(); | 736 mat.reset(); |
599 mat.setTranslate(SK_Scalar1, SK_Scalar1); | 737 mat.setTranslate(SK_Scalar1, SK_Scalar1); |
600 REPORTER_ASSERT(reporter, mat.invert(&inverse)); | 738 REPORTER_ASSERT(reporter, mat.invert(&inverse)); |
601 iden1.setConcat(mat, inverse); | 739 iden1.setConcat(mat, inverse); |
602 REPORTER_ASSERT(reporter, is_identity(iden1)); | 740 REPORTER_ASSERT(reporter, is_identity(iden1)); |
603 | 741 |
604 mat.setScale(SkIntToScalar(2), SkIntToScalar(4)); | 742 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 | 844 #ifdef SK_SCALAR_IS_FIXED |
707 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); | 845 REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2)); |
708 #else | 846 #else |
709 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); | 847 REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2)); |
710 #endif | 848 #endif |
711 | 849 |
712 test_matrix_max_stretch(reporter); | 850 test_matrix_max_stretch(reporter); |
713 test_matrix_is_similarity(reporter); | 851 test_matrix_is_similarity(reporter); |
714 test_matrix_recttorect(reporter); | 852 test_matrix_recttorect(reporter); |
715 test_matrix_decomposition(reporter); | 853 test_matrix_decomposition(reporter); |
| 854 test_matrix_homogeneous(reporter); |
716 } | 855 } |
717 | 856 |
718 #include "TestClassDef.h" | 857 #include "TestClassDef.h" |
719 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) | 858 DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) |
OLD | NEW |