TestMatrix.cpp (6408B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 #include "mozilla/gfx/Matrix.h" 9 10 using namespace mozilla; 11 using namespace mozilla::gfx; 12 13 TEST(Matrix, TransformAndClipRect) 14 { 15 Rect c(100, 100, 100, 100); 16 Matrix4x4 m; 17 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 20, 20), c).IsEmpty()); 18 EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 50, 20, 20), c).IsEmpty()); 19 EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 250, 20, 20), c).IsEmpty()); 20 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 250, 20, 20), c).IsEmpty()); 21 22 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 20), c).IsEmpty()); 23 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 20), c).IsEmpty()); 24 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 250, 100, 20), c).IsEmpty()); 25 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 250, 100, 20), c).IsEmpty()); 26 27 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 20, 100), c).IsEmpty()); 28 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 20, 100), c).IsEmpty()); 29 EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 50, 20, 100), c).IsEmpty()); 30 EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 150, 20, 100), c).IsEmpty()); 31 32 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 100), c) 33 .IsEqualInterior(Rect(100, 100, 50, 50))); 34 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 100), c) 35 .IsEqualInterior(Rect(150, 100, 50, 50))); 36 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 150, 100, 100), c) 37 .IsEqualInterior(Rect(150, 150, 50, 50))); 38 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 100, 100), c) 39 .IsEqualInterior(Rect(100, 150, 50, 50))); 40 41 EXPECT_TRUE(m.TransformAndClipBounds(Rect(110, 110, 80, 80), c) 42 .IsEqualInterior(Rect(110, 110, 80, 80))); 43 44 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 200, 200), c) 45 .IsEqualInterior(Rect(100, 100, 100, 100))); 46 47 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 200, 100), c) 48 .IsEqualInterior(Rect(100, 100, 100, 50))); 49 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 200, 100), c) 50 .IsEqualInterior(Rect(100, 150, 100, 50))); 51 EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 200), c) 52 .IsEqualInterior(Rect(100, 100, 50, 100))); 53 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 200), c) 54 .IsEqualInterior(Rect(150, 100, 50, 100))); 55 56 Matrix4x4 m2 = Matrix4x4::From2D(Matrix(22.68, 0, 0, 12, 16, 164)); 57 EXPECT_TRUE( 58 m2.TransformAndClipBounds(Rect(0, 0, 100, 100), Rect(1024, 1024, 0, 0)) 59 .IsEmpty()); 60 61 // Empty rectangles should still have meaningful corners. 62 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 0, 200), c) 63 .IsEqualEdges(Rect(150, 100, 0, 100))); 64 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 150, 0, 0), c) 65 .IsEqualEdges(Rect(150, 150, 0, 0))); 66 EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 100, 300, 0), c) 67 .IsEqualEdges(Rect(150, 100, 50, 0))); 68 } 69 70 TEST(Matrix, RotateTransform) 71 { 72 gfx::Matrix4x4 transformMatrix; 73 gfx::Point3D trans, scale; 74 gfx::Quaternion orient; 75 76 auto floor = [&](float aValue, int aDecimal) { 77 const int digit = pow(10, aDecimal); 78 const float result = (int)(aValue * digit); 79 return result / digit; 80 }; 81 82 // Test rotate 45 degree on x-axis. 83 gfx::Quaternion expectedOrient(0.382f, 0.0f, 0.0f, 0.923f); 84 transformMatrix.RotateX(0.785f); 85 // the orient would be (x:0.3825, y:0, z:0, w: 0.9239) 86 transformMatrix.Decompose(trans, orient, scale); 87 EXPECT_EQ(floor(orient.x, 3), expectedOrient.x); 88 EXPECT_EQ(floor(orient.y, 3), expectedOrient.y); 89 EXPECT_EQ(floor(orient.z, 3), expectedOrient.z); 90 EXPECT_EQ(floor(orient.w, 3), expectedOrient.w); 91 92 // Test set rotate matrix from a quaternion and 93 // compare it with the result from decompose. 94 transformMatrix = gfx::Matrix4x4(); 95 transformMatrix.SetRotationFromQuaternion(orient); 96 transformMatrix.Decompose(trans, orient, scale); 97 EXPECT_EQ(floor(orient.x, 3), expectedOrient.x); 98 EXPECT_EQ(floor(orient.y, 3), expectedOrient.y); 99 EXPECT_EQ(floor(orient.z, 3), expectedOrient.z); 100 EXPECT_EQ(floor(orient.w, 3), expectedOrient.w); 101 102 // Test rotate -45 degree on axis: (0.577f, 0.577f, 0.577f). 103 transformMatrix = gfx::Matrix4x4(); 104 transformMatrix.SetRotateAxisAngle(0.577f, 0.577f, 0.577f, -0.785f); 105 // the orient would be (x:-0.2208, y:-0.2208, z:-0.2208, w: 0.9239) 106 transformMatrix.Decompose(trans, orient, scale); 107 108 expectedOrient.Set(-0.220f, -0.220f, -0.220f, 0.923f); 109 EXPECT_EQ(floor(orient.x, 3), expectedOrient.x); 110 EXPECT_EQ(floor(orient.y, 3), expectedOrient.y); 111 EXPECT_EQ(floor(orient.z, 3), expectedOrient.z); 112 EXPECT_EQ(floor(orient.w, 3), expectedOrient.w); 113 114 // Test set rotate matrix from a quaternion and 115 // compare it with the result from decompose. 116 transformMatrix = gfx::Matrix4x4(); 117 transformMatrix.SetRotationFromQuaternion(orient); 118 transformMatrix.Decompose(trans, orient, scale); 119 EXPECT_EQ(floor(orient.x, 3), expectedOrient.x); 120 EXPECT_EQ(floor(orient.y, 3), expectedOrient.y); 121 EXPECT_EQ(floor(orient.z, 3), expectedOrient.z); 122 EXPECT_EQ(floor(orient.w, 3), expectedOrient.w); 123 } 124 125 TEST(Matrix4x4Flagged, Mult) 126 { 127 Matrix4x4Flagged simple = 128 Matrix4x4::Translation(Point(42, 42)) * Matrix4x4::Scaling(3, 3, 1); 129 // For the general matrix, put a value in every field to make sure 130 // nothing gets dropped. 131 Matrix4x4 general(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2); 132 133 // Use Matrix4x4::operator*(Matrix4x4). 134 // For the purposes of this test, assume that's correct. 135 Matrix4x4Flagged realResult = Matrix4x4Flagged(simple.GetMatrix() * general); 136 137 // Check that Matrix4x4Flagged::operator*(Matrix4x4Flagged) produces the same 138 // result. 139 Matrix4x4Flagged flaggedResult = simple * Matrix4x4Flagged(general); 140 EXPECT_EQ(realResult, flaggedResult); 141 142 // Check that Matrix4x4Flagged::operator*(Matrix4x4) produces the same result. 143 Matrix4x4Flagged mixedResult = simple * general; 144 EXPECT_EQ(realResult, mixedResult); 145 }