OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
7 | 7 |
8 #include "ui/gfx/transform.h" | 8 #include "ui/gfx/transform.h" |
9 | 9 |
10 #include <cmath> | 10 #include <cmath> |
11 #include <ostream> | 11 #include <ostream> |
12 #include <limits> | 12 #include <limits> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/logging.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "ui/gfx/point.h" | 17 #include "ui/gfx/point.h" |
17 #include "ui/gfx/point3_f.h" | 18 #include "ui/gfx/point3_f.h" |
18 #include "ui/gfx/transform_util.h" | 19 #include "ui/gfx/transform_util.h" |
19 #include "ui/gfx/vector3d_f.h" | 20 #include "ui/gfx/vector3d_f.h" |
20 | 21 |
21 namespace gfx { | 22 namespace gfx { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 xform.TransformPointReverse(pt); | 648 xform.TransformPointReverse(pt); |
648 EXPECT_EQ(pt.x(), value.x); | 649 EXPECT_EQ(pt.x(), value.x); |
649 EXPECT_EQ(pt.y(), value.y); | 650 EXPECT_EQ(pt.y(), value.y); |
650 } | 651 } |
651 } | 652 } |
652 } | 653 } |
653 } | 654 } |
654 | 655 |
655 TEST(XFormTest, BlendTranslate) { | 656 TEST(XFormTest, BlendTranslate) { |
656 Transform from; | 657 Transform from; |
657 for (int i = 0; i < 10; ++i) { | 658 for (int i = -5; i < 15; ++i) { |
658 Transform to; | 659 Transform to; |
659 to.Translate3d(1, 1, 1); | 660 to.Translate3d(1, 1, 1); |
660 double t = i / 9.0; | 661 double t = i / 9.0; |
661 EXPECT_TRUE(to.Blend(from, t)); | 662 EXPECT_TRUE(to.Blend(from, t)); |
662 EXPECT_FLOAT_EQ(t, to.matrix().get(0, 3)); | 663 EXPECT_FLOAT_EQ(t, to.matrix().get(0, 3)); |
663 EXPECT_FLOAT_EQ(t, to.matrix().get(1, 3)); | 664 EXPECT_FLOAT_EQ(t, to.matrix().get(1, 3)); |
664 EXPECT_FLOAT_EQ(t, to.matrix().get(2, 3)); | 665 EXPECT_FLOAT_EQ(t, to.matrix().get(2, 3)); |
665 } | 666 } |
666 } | 667 } |
667 | 668 |
668 TEST(XFormTest, BlendRotate) { | 669 TEST(XFormTest, BlendRotate) { |
669 Vector3dF axes[] = { | 670 Vector3dF axes[] = { |
670 Vector3dF(1, 0, 0), | 671 Vector3dF(1, 0, 0), |
671 Vector3dF(0, 1, 0), | 672 Vector3dF(0, 1, 0), |
672 Vector3dF(0, 0, 1), | 673 Vector3dF(0, 0, 1), |
673 Vector3dF(1, 1, 1) | 674 Vector3dF(1, 1, 1) |
674 }; | 675 }; |
675 Transform from; | 676 Transform from; |
676 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { | 677 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { |
677 for (int i = 0; i < 10; ++i) { | 678 for (int i = -5; i < 15; ++i) { |
678 Transform to; | 679 Transform to; |
679 to.RotateAbout(axes[index], 90); | 680 to.RotateAbout(axes[index], 90); |
680 double t = i / 9.0; | 681 double t = i / 9.0; |
681 EXPECT_TRUE(to.Blend(from, t)); | 682 EXPECT_TRUE(to.Blend(from, t)); |
682 | 683 |
683 Transform expected; | 684 Transform expected; |
684 expected.RotateAbout(axes[index], 90 * t); | 685 expected.RotateAbout(axes[index], 90 * t); |
685 | 686 |
686 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); | 687 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); |
687 } | 688 } |
688 } | 689 } |
689 } | 690 } |
690 | 691 |
691 TEST(XFormTest, BlendRotateFollowsShortestPath) { | 692 TEST(XFormTest, BlendRotateFollowsShortestPath) { |
692 // Verify that we interpolate along the shortest path regardless of whether | 693 // Verify that we interpolate along the shortest path regardless of whether |
693 // this path crosses the 180-degree point. | 694 // this path crosses the 180-degree point. |
694 Vector3dF axes[] = { | 695 Vector3dF axes[] = { |
695 Vector3dF(1, 0, 0), | 696 Vector3dF(1, 0, 0), |
696 Vector3dF(0, 1, 0), | 697 Vector3dF(0, 1, 0), |
697 Vector3dF(0, 0, 1), | 698 Vector3dF(0, 0, 1), |
698 Vector3dF(1, 1, 1) | 699 Vector3dF(1, 1, 1) |
699 }; | 700 }; |
700 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { | 701 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { |
701 for (int i = 0; i < 10; ++i) { | 702 for (int i = -5; i < 15; ++i) { |
702 Transform from1; | 703 Transform from1; |
703 from1.RotateAbout(axes[index], 130.0); | 704 from1.RotateAbout(axes[index], 130.0); |
704 Transform to1; | 705 Transform to1; |
705 to1.RotateAbout(axes[index], 175.0); | 706 to1.RotateAbout(axes[index], 175.0); |
706 | 707 |
707 Transform from2; | 708 Transform from2; |
708 from2.RotateAbout(axes[index], 140.0); | 709 from2.RotateAbout(axes[index], 140.0); |
709 Transform to2; | 710 Transform to2; |
710 to2.RotateAbout(axes[index], 185.0); | 711 to2.RotateAbout(axes[index], 185.0); |
711 | 712 |
(...skipping 15 matching lines...) Expand all Loading... |
727 | 728 |
728 TEST(XFormTest, CanBlend180DegreeRotation) { | 729 TEST(XFormTest, CanBlend180DegreeRotation) { |
729 Vector3dF axes[] = { | 730 Vector3dF axes[] = { |
730 Vector3dF(1, 0, 0), | 731 Vector3dF(1, 0, 0), |
731 Vector3dF(0, 1, 0), | 732 Vector3dF(0, 1, 0), |
732 Vector3dF(0, 0, 1), | 733 Vector3dF(0, 0, 1), |
733 Vector3dF(1, 1, 1) | 734 Vector3dF(1, 1, 1) |
734 }; | 735 }; |
735 Transform from; | 736 Transform from; |
736 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { | 737 for (size_t index = 0; index < ARRAYSIZE_UNSAFE(axes); ++index) { |
737 for (int i = 0; i < 10; ++i) { | 738 for (int i = -5; i < 15; ++i) { |
738 Transform to; | 739 Transform to; |
739 to.RotateAbout(axes[index], 180); | 740 to.RotateAbout(axes[index], 180); |
740 double t = i / 9.0; | 741 double t = i / 9.0; |
741 EXPECT_TRUE(to.Blend(from, t)); | 742 EXPECT_TRUE(to.Blend(from, t)); |
742 | 743 |
743 Transform expected; | 744 Transform expected; |
744 expected.RotateAbout(axes[index], 180 * t); | 745 expected.RotateAbout(axes[index], 180 * t); |
745 | 746 |
746 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); | 747 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); |
747 } | 748 } |
748 } | 749 } |
749 } | 750 } |
750 | 751 |
751 TEST(XFormTest, BlendScale) { | 752 TEST(XFormTest, BlendScale) { |
752 Transform from; | 753 Transform from; |
753 for (int i = 0; i < 10; ++i) { | 754 for (int i = -5; i < 15; ++i) { |
754 Transform to; | 755 Transform to; |
755 to.Scale3d(5, 4, 3); | 756 to.Scale3d(5, 4, 3); |
756 double t = i / 9.0; | 757 double t = i / 9.0; |
757 EXPECT_TRUE(to.Blend(from, t)); | 758 EXPECT_TRUE(to.Blend(from, t)); |
758 EXPECT_FLOAT_EQ(t * 4 + 1, to.matrix().get(0, 0)); | 759 EXPECT_FLOAT_EQ(t * 4 + 1, to.matrix().get(0, 0)); |
759 EXPECT_FLOAT_EQ(t * 3 + 1, to.matrix().get(1, 1)); | 760 EXPECT_FLOAT_EQ(t * 3 + 1, to.matrix().get(1, 1)); |
760 EXPECT_FLOAT_EQ(t * 2 + 1, to.matrix().get(2, 2)); | 761 EXPECT_FLOAT_EQ(t * 2 + 1, to.matrix().get(2, 2)); |
761 } | 762 } |
762 } | 763 } |
763 | 764 |
764 TEST(XFormTest, BlendSkew) { | 765 TEST(XFormTest, BlendSkew) { |
765 Transform from; | 766 Transform from; |
766 for (int i = 0; i < 2; ++i) { | 767 for (int i = 0; i < 2; ++i) { |
767 Transform to; | 768 Transform to; |
768 to.SkewX(20); | 769 to.SkewX(20); |
769 to.SkewY(10); | 770 to.SkewY(10); |
770 double t = i; | 771 double t = i; |
771 Transform expected; | 772 Transform expected; |
772 expected.SkewX(t * 20); | 773 expected.SkewX(t * 20); |
773 expected.SkewY(t * 10); | 774 expected.SkewY(t * 10); |
774 EXPECT_TRUE(to.Blend(from, t)); | 775 EXPECT_TRUE(to.Blend(from, t)); |
775 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); | 776 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); |
776 } | 777 } |
777 } | 778 } |
778 | 779 |
| 780 TEST(XFormTest, ExtrapolateSkew) { |
| 781 Transform from; |
| 782 for (int i = -1; i < 2; ++i) { |
| 783 Transform to; |
| 784 to.SkewX(20); |
| 785 double t = i; |
| 786 Transform expected; |
| 787 expected.SkewX(t * 20); |
| 788 EXPECT_TRUE(to.Blend(from, t)); |
| 789 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); |
| 790 } |
| 791 } |
| 792 |
779 TEST(XFormTest, BlendPerspective) { | 793 TEST(XFormTest, BlendPerspective) { |
780 Transform from; | 794 Transform from; |
781 from.ApplyPerspectiveDepth(200); | 795 from.ApplyPerspectiveDepth(200); |
782 for (int i = 0; i < 2; ++i) { | 796 for (int i = -1; i < 3; ++i) { |
783 Transform to; | 797 Transform to; |
784 to.ApplyPerspectiveDepth(800); | 798 to.ApplyPerspectiveDepth(800); |
785 double t = i; | 799 double t = i; |
| 800 double depth = 1.0 / ((1.0 / 200) * (1.0 - t) + (1.0 / 800) * t); |
786 Transform expected; | 801 Transform expected; |
787 expected.ApplyPerspectiveDepth(t * 600 + 200); | 802 expected.ApplyPerspectiveDepth(depth); |
788 EXPECT_TRUE(to.Blend(from, t)); | 803 EXPECT_TRUE(to.Blend(from, t)); |
789 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); | 804 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to)); |
790 } | 805 } |
791 } | 806 } |
792 | 807 |
793 TEST(XFormTest, BlendIdentity) { | 808 TEST(XFormTest, BlendIdentity) { |
794 Transform from; | 809 Transform from; |
795 Transform to; | 810 Transform to; |
796 EXPECT_TRUE(to.Blend(from, 0.5)); | 811 EXPECT_TRUE(to.Blend(from, 0.5)); |
797 EXPECT_EQ(to, from); | 812 EXPECT_EQ(to, from); |
(...skipping 1461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2259 A.FlattenTo2d(); | 2274 A.FlattenTo2d(); |
2260 EXPECT_ROW1_EQ(10.0f, 14.0f, 0.0f, 22.0f, A); | 2275 EXPECT_ROW1_EQ(10.0f, 14.0f, 0.0f, 22.0f, A); |
2261 EXPECT_ROW2_EQ(11.0f, 15.0f, 0.0f, 23.0f, A); | 2276 EXPECT_ROW2_EQ(11.0f, 15.0f, 0.0f, 23.0f, A); |
2262 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A); | 2277 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A); |
2263 EXPECT_ROW4_EQ(13.0f, 17.0f, 0.0f, 25.0f, A); | 2278 EXPECT_ROW4_EQ(13.0f, 17.0f, 0.0f, 25.0f, A); |
2264 } | 2279 } |
2265 | 2280 |
2266 } // namespace | 2281 } // namespace |
2267 | 2282 |
2268 } // namespace gfx | 2283 } // namespace gfx |
OLD | NEW |