• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkM44.h"
9 #include "include/utils/SkRandom.h"
10 #include "src/core/SkMatrixPriv.h"
11 #include "tests/Test.h"
12 
eq(const SkM44 & a,const SkM44 & b,float tol)13 static bool eq(const SkM44& a, const SkM44& b, float tol) {
14     float fa[16], fb[16];
15     a.getColMajor(fa);
16     b.getColMajor(fb);
17     for (int i = 0; i < 16; ++i) {
18         if (!SkScalarNearlyEqual(fa[i], fb[i], tol)) {
19             return false;
20         }
21     }
22     return true;
23 }
24 
DEF_TEST(M44,reporter)25 DEF_TEST(M44, reporter) {
26     SkM44 m, im;
27 
28     REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 0,
29                                     0, 1, 0, 0,
30                                     0, 0, 1, 0,
31                                     0, 0, 0, 1) == m);
32     REPORTER_ASSERT(reporter, SkM44() == m);
33     REPORTER_ASSERT(reporter, m.invert(&im));
34     REPORTER_ASSERT(reporter, SkM44() == im);
35 
36     m.setTranslate(3, 4, 2);
37     REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 3,
38                                     0, 1, 0, 4,
39                                     0, 0, 1, 2,
40                                     0, 0, 0, 1) == m);
41 
42     const float f[] = { 1, 0, 0, 2, 3, 1, 2, 5, 0, 5, 3, 0, 0, 1, 0, 2 };
43     m = SkM44::ColMajor(f);
44     REPORTER_ASSERT(reporter, SkM44(f[0], f[4], f[ 8], f[12],
45                                     f[1], f[5], f[ 9], f[13],
46                                     f[2], f[6], f[10], f[14],
47                                     f[3], f[7], f[11], f[15]) == m);
48 
49     {
50         SkM44 t = m.transpose();
51         REPORTER_ASSERT(reporter, t != m);
52         REPORTER_ASSERT(reporter, t.rc(1,0) == m.rc(0,1));
53         SkM44 tt = t.transpose();
54         REPORTER_ASSERT(reporter, tt == m);
55     }
56 
57     m = SkM44::RowMajor(f);
58     REPORTER_ASSERT(reporter, SkM44(f[ 0], f[ 1], f[ 2], f[ 3],
59                                     f[ 4], f[ 5], f[ 6], f[ 7],
60                                     f[ 8], f[ 9], f[10], f[14],
61                                     f[12], f[13], f[14], f[15]) == m);
62 
63     REPORTER_ASSERT(reporter, m.invert(&im));
64 
65     m = m * im;
66     // m should be identity now, but our calc is not perfect...
67     REPORTER_ASSERT(reporter, eq(SkM44(), m, 0.0000005f));
68     REPORTER_ASSERT(reporter, SkM44() != m);
69 }
70 
DEF_TEST(M44_v3,reporter)71 DEF_TEST(M44_v3, reporter) {
72     SkV3 a = {1, 2, 3},
73          b = {1, 2, 2};
74 
75     REPORTER_ASSERT(reporter, a.lengthSquared() == 1 + 4 + 9);
76     REPORTER_ASSERT(reporter, b.length() == 3);
77     REPORTER_ASSERT(reporter, a.dot(b) == 1 + 4 + 6);
78     REPORTER_ASSERT(reporter, b.dot(a) == 1 + 4 + 6);
79     REPORTER_ASSERT(reporter, (a.cross(b) == SkV3{-2,  1, 0}));
80     REPORTER_ASSERT(reporter, (b.cross(a) == SkV3{ 2, -1, 0}));
81 
82     SkM44 m = {
83         2, 0, 0, 3,
84         0, 1, 0, 5,
85         0, 0, 3, 1,
86         0, 0, 0, 1
87     };
88 
89     SkV3 c = m * a;
90     REPORTER_ASSERT(reporter, (c == SkV3{2, 2, 9}));
91     SkV4 d = m.map(4, 3, 2, 1);
92     REPORTER_ASSERT(reporter, (d == SkV4{11, 8, 7, 1}));
93 }
94 
DEF_TEST(M44_v4,reporter)95 DEF_TEST(M44_v4, reporter) {
96     SkM44 m( 1,  2,  3,  4,
97              5,  6,  7,  8,
98              9, 10, 11, 12,
99             13, 14, 15, 16);
100 
101     SkV4 r0 = m.row(0),
102          r1 = m.row(1),
103          r2 = m.row(2),
104          r3 = m.row(3);
105 
106     REPORTER_ASSERT(reporter, (r0 == SkV4{ 1,  2,  3,  4}));
107     REPORTER_ASSERT(reporter, (r1 == SkV4{ 5,  6,  7,  8}));
108     REPORTER_ASSERT(reporter, (r2 == SkV4{ 9, 10, 11, 12}));
109     REPORTER_ASSERT(reporter, (r3 == SkV4{13, 14, 15, 16}));
110 
111     REPORTER_ASSERT(reporter, SkM44::Rows(r0, r1, r2, r3) == m);
112 
113     SkV4 c0 = m.col(0),
114          c1 = m.col(1),
115          c2 = m.col(2),
116          c3 = m.col(3);
117 
118     REPORTER_ASSERT(reporter, (c0 == SkV4{1, 5,  9, 13}));
119     REPORTER_ASSERT(reporter, (c1 == SkV4{2, 6, 10, 14}));
120     REPORTER_ASSERT(reporter, (c2 == SkV4{3, 7, 11, 15}));
121     REPORTER_ASSERT(reporter, (c3 == SkV4{4, 8, 12, 16}));
122 
123     REPORTER_ASSERT(reporter, SkM44::Cols(c0, c1, c2, c3) == m);
124 
125     // implement matrix * vector using column vectors
126     SkV4 v = {1, 2, 3, 4};
127     SkV4 v1 = m * v;
128     SkV4 v2 = c0 * v.x + c1 * v.y + c2 * v.z + c3 * v.w;
129     REPORTER_ASSERT(reporter, v1 == v2);
130 
131     REPORTER_ASSERT(reporter, (c0 + r0 == SkV4{c0.x+r0.x, c0.y+r0.y, c0.z+r0.z, c0.w+r0.w}));
132     REPORTER_ASSERT(reporter, (c0 - r0 == SkV4{c0.x-r0.x, c0.y-r0.y, c0.z-r0.z, c0.w-r0.w}));
133     REPORTER_ASSERT(reporter, (c0 * r0 == SkV4{c0.x*r0.x, c0.y*r0.y, c0.z*r0.z, c0.w*r0.w}));
134 }
135 
DEF_TEST(M44_rotate,reporter)136 DEF_TEST(M44_rotate, reporter) {
137     const SkV3 x = {1, 0, 0},
138                y = {0, 1, 0},
139                z = {0, 0, 1};
140 
141     // We have radians version of setRotateAbout methods, but even with our best approx
142     // for PI, sin(SK_ScalarPI) != 0, so to make the comparisons in the unittest clear,
143     // I'm using the variants that explicitly take the sin,cos values.
144 
145     struct {
146         SkScalar sinAngle, cosAngle;
147         SkV3 aboutAxis;
148         SkV3 expectedX, expectedY, expectedZ;
149     } recs[] = {
150         { 0, 1,    x,   x, y, z},    // angle = 0
151         { 0, 1,    y,   x, y, z},    // angle = 0
152         { 0, 1,    z,   x, y, z},    // angle = 0
153 
154         { 0,-1,    x,   x,-y,-z},    // angle = 180
155         { 0,-1,    y,  -x, y,-z},    // angle = 180
156         { 0,-1,    z,  -x,-y, z},    // angle = 180
157 
158         // Skia coordinate system is right-handed
159 
160         { 1, 0,    x,   x, z,-y},    // angle = 90
161         { 1, 0,    y,  -z, y, x},    // angle = 90
162         { 1, 0,    z,   y,-x, z},    // angle = 90
163 
164         {-1, 0,    x,   x,-z, y},    // angle = -90
165         {-1, 0,    y,   z, y,-x},    // angle = -90
166         {-1, 0,    z,  -y, x, z},    // angle = -90
167     };
168 
169     for (const auto& r : recs) {
170         SkM44 m(SkM44::kNaN_Constructor);
171         m.setRotateUnitSinCos(r.aboutAxis, r.sinAngle, r.cosAngle);
172 
173         auto mx = m * x;
174         auto my = m * y;
175         auto mz = m * z;
176         REPORTER_ASSERT(reporter, mx == r.expectedX);
177         REPORTER_ASSERT(reporter, my == r.expectedY);
178         REPORTER_ASSERT(reporter, mz == r.expectedZ);
179 
180         // flipping the axis-of-rotation should flip the results
181         mx = m * -x;
182         my = m * -y;
183         mz = m * -z;
184         REPORTER_ASSERT(reporter, mx == -r.expectedX);
185         REPORTER_ASSERT(reporter, my == -r.expectedY);
186         REPORTER_ASSERT(reporter, mz == -r.expectedZ);
187     }
188 }
189 
DEF_TEST(M44_rectToRect,reporter)190 DEF_TEST(M44_rectToRect, reporter) {
191     SkV2 dstScales[] = {
192         {1.f,   1.f},   // no aspect ratio change, nor up/down scaling
193         {0.25f, 0.5f},  // aspect ratio narrows, downscale x and y
194         {0.5f,  0.25f}, // aspect ratio widens, downscale x and y
195         {0.5f,  0.5f},  // no aspect ratio change, downscale x and y
196         {2.f,   3.f},   // aspect ratio narrows, upscale x and y
197         {3.f,   2.f},   // aspect ratio widens, upscale x and y
198         {2.f,   2.f},   // no aspect ratio change, upscale x and y
199         {0.5f,  2.f},   // aspect ratio narrows, downscale x and upscale y
200         {2.f,   0.5f}   // aspect ratio widens, upscale x and downscale y
201     };
202 
203     auto map2d = [&](const SkM44& m, SkV2 p) {
204         SkV4 mapped = m.map(p.x, p.y, 0.f, 1.f);
205         REPORTER_ASSERT(reporter, mapped.z == 0.f);
206         REPORTER_ASSERT(reporter, mapped.w == 1.f);
207         return SkV2{mapped.x, mapped.y};
208     };
209     auto assertNearlyEqual = [&](float actual, float expected) {
210         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(actual, expected),
211                         "Expected %g == %g", actual, expected);
212     };
213     auto assertEdges = [&](float actualLow, float actualHigh,
214                            float expectedLow, float expectedHigh) {
215         SkASSERT(expectedLow < expectedHigh);
216         REPORTER_ASSERT(reporter, actualLow < actualHigh,
217                         "Expected %g < %g", actualLow, actualHigh);
218 
219         assertNearlyEqual(actualLow, expectedLow);
220         assertNearlyEqual(actualHigh, expectedHigh);
221     };
222 
223     SkRandom rand;
224     for (const auto& r : dstScales) {
225         SkRect src = SkRect::MakeXYWH(rand.nextRangeF(-10.f, 10.f),
226                                       rand.nextRangeF(-10.f, 10.f),
227                                       rand.nextRangeF(1.f, 10.f),
228                                       rand.nextRangeF(1.f, 10.f));
229         SkRect dst = SkRect::MakeXYWH(rand.nextRangeF(-10.f, 10.f),
230                                       rand.nextRangeF(-10.f, 10.f),
231                                       r.x * src.width(),
232                                       r.y * src.height());
233 
234         SkM44 m = SkM44::RectToRect(src, dst);
235 
236         // Regardless of the factory, center of src maps to center of dst
237         SkV2 center = map2d(m, {src.centerX(), src.centerY()});
238         assertNearlyEqual(center.x, dst.centerX());
239         assertNearlyEqual(center.y, dst.centerY());
240 
241         // Map the four corners of src and validate against expected edge mapping
242         SkV2 tl = map2d(m, {src.fLeft, src.fTop});
243         SkV2 tr = map2d(m, {src.fRight, src.fTop});
244         SkV2 br = map2d(m, {src.fRight, src.fBottom});
245         SkV2 bl = map2d(m, {src.fLeft, src.fBottom});
246 
247         assertEdges(tl.x, tr.x, dst.fLeft, dst.fRight);
248         assertEdges(bl.x, br.x, dst.fLeft, dst.fRight);
249         assertEdges(tl.y, bl.y, dst.fTop, dst.fBottom);
250         assertEdges(tr.y, br.y, dst.fTop, dst.fBottom);
251     }
252 }
253 
DEF_TEST(M44_mapRect,reporter)254 DEF_TEST(M44_mapRect, reporter) {
255     auto assertRectsNearlyEqual = [&](const SkRect& actual, const SkRect& expected,
256                                       const SkRect& e) {
257         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(actual.fLeft, expected.fLeft, e.fLeft),
258                         "Expected %g == %g", actual.fLeft, expected.fLeft);
259         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(actual.fTop, expected.fTop, e.fTop),
260                         "Expected %g == %g", actual.fTop, expected.fTop);
261         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(actual.fRight, expected.fRight, e.fRight),
262                         "Expected %g == %g", actual.fRight, expected.fRight);
263         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(actual.fBottom, expected.fBottom, e.fBottom),
264                         "Expected %g == %g", actual.fBottom, expected.fBottom);
265     };
266     auto assertMapRect = [&](const SkM44& m, const SkRect& src, const SkRect* expected) {
267         SkRect epsilon = {1e-5f, 1e-5f, 1e-5f, 1e-5f};
268 
269         SkRect actual = SkMatrixPriv::MapRect(m, src);
270         REPORTER_ASSERT(reporter, !actual.isEmpty());
271 
272         if (expected) {
273             assertRectsNearlyEqual(actual, *expected, epsilon);
274         }
275 
276         SkV4 corners[4] = {{src.fLeft, src.fTop, 0.f, 1.f},
277                            {src.fRight, src.fTop, 0.f, 1.f},
278                            {src.fRight, src.fBottom, 0.f, 1.f},
279                            {src.fLeft, src.fBottom, 0.f, 1.f}};
280         bool leftFound = false;
281         bool topFound = false;
282         bool rightFound = false;
283         bool bottomFound = false;
284         bool clipped = false;
285         for (int i = 0; i < 4; ++i) {
286             SkV4 mapped = m * corners[i];
287             if (mapped.w > 0.f) {
288                 // Should be contained in actual and might be on one or two of actual's edges
289                 float x = mapped.x / mapped.w;
290                 float y = mapped.y / mapped.w;
291 
292                 // Can't use SkRect::contains() since it treats right and bottom edges as exclusive
293                 REPORTER_ASSERT(reporter, actual.fLeft <= x && x <= actual.fRight,
294                                 "Expected %g contained in [%g, %g]",
295                                 x, actual.fLeft, actual.fRight);
296                 REPORTER_ASSERT(reporter, actual.fTop <= y && y <= actual.fBottom,
297                                 "Expected %g contained in [%g, %g]",
298                                 y, actual.fTop, actual.fBottom);
299 
300                 leftFound   |= SkScalarNearlyEqual(x, actual.fLeft);
301                 topFound    |= SkScalarNearlyEqual(y, actual.fTop);
302                 rightFound  |= SkScalarNearlyEqual(x, actual.fRight);
303                 bottomFound |= SkScalarNearlyEqual(y, actual.fBottom);
304             } else {
305                 // The mapped point would be clipped so the clipped mapped bounds don't necessarily
306                 // contain it
307                 clipped = true;
308             }
309         }
310 
311         if (clipped) {
312             // At least one of the mapped corners should have contributed to the rect
313             REPORTER_ASSERT(reporter, leftFound || topFound || rightFound || bottomFound);
314             // For any edge that came from a clipped corner, increase its error tolerance relative
315             // to what SkPath::ApplyPerspectiveClip calculates.
316             // TODO(michaelludwig): skbug.com/12335 required updating the w epsilon distance which
317             // greatly increased noise for coords projecting to infinity. They aren't "wrong", since
318             // the intent was clearly to pick a big number that's definitely offscreen, but
319             // MapRect should have a more robust solution than a fixed w > epsilon and when it does,
320             // these expectations for clipped points should be more accurate.
321             if (!leftFound) {   epsilon.fLeft   = .01f * actual.fLeft; }
322             if (!topFound) {    epsilon.fTop    = .01f * actual.fTop; }
323             if (!rightFound) {  epsilon.fRight  = .01f * actual.fRight; }
324             if (!bottomFound) { epsilon.fBottom = .01f * actual.fBottom; }
325         } else {
326             // The mapped corners should have contributed to all four edges of the returned rect
327             REPORTER_ASSERT(reporter, leftFound && topFound && rightFound && bottomFound);
328         }
329 
330         SkPath path = SkPath::Rect(src);
331         path.transform(m.asM33(), SkApplyPerspectiveClip::kYes);
332         assertRectsNearlyEqual(actual, path.getBounds(), epsilon);
333     };
334 
335     // src chosen arbitrarily
336     const SkRect src = SkRect::MakeLTRB(4.83f, -0.48f, 5.53f, 30.68f);
337 
338     // Identity maps src to src
339     assertMapRect(SkM44(), src, &src);
340     // Scale+Translate just offsets src
341     SkRect st = SkRect::MakeLTRB(10.f + 2.f * src.fLeft,  8.f + 4.f * src.fTop,
342                                  10.f + 2.f * src.fRight, 8.f + 4.f * src.fBottom);
343     assertMapRect(SkM44::Scale(2.f, 4.f).postTranslate(10.f, 8.f), src, &st);
344     // Rotate 45 degrees about center
345     assertMapRect(SkM44::Rotate({0.f, 0.f, 1.f}, SK_ScalarPI / 4.f)
346                         .preTranslate(-src.centerX(), -src.centerY())
347                         .postTranslate(src.centerX(), src.centerY()),
348                   src, nullptr);
349 
350     // Perspective matrix where src does not need to be clipped w > 0
351     SkM44 p = SkM44::Perspective(0.01f, 10.f, SK_ScalarPI / 3.f);
352     p.preTranslate(0.f, 5.f, -0.1f);
353     p.preConcat(SkM44::Rotate({0.f, 1.f, 0.f}, 0.008f /* radians */));
354     assertMapRect(p, src, nullptr);
355 
356     // Perspective matrix where src *does* need to be clipped w > 0
357     p.setIdentity();
358     p.setRow(3, {-.2f, -.6f, 0.f, 8.f});
359     assertMapRect(p, src, nullptr);
360 }
361 
DEF_TEST(M44_mapRect_skbug12335,r)362 DEF_TEST(M44_mapRect_skbug12335, r) {
363     // Stripped down test case from skbug.com/12335. Essentially, the corners of this rect would
364     // map to homogoneous coords with very small w's (below the old value of kW0PlaneDistance) and
365     // so they would be clipped "behind" the plane, resulting in an empty mapped rect. Coordinates
366     // with positive that wouldn't overflow when divided by w should still be included in the mapped
367     // rectangle.
368     SkRect rect = SkRect::MakeLTRB(0, 0, 319, 620);
369     SkM44 m(SkMatrix::MakeAll( 0.000152695269f, 0.00000000f,     -6.53848401e-05f,
370                               -1.75697533e-05f, 0.000157153074f, -1.10847975e-06f,
371                               -6.00415362e-08f, 0.00000000f,      0.000169880834f));
372     SkRect out = SkMatrixPriv::MapRect(m, rect);
373     REPORTER_ASSERT(r, !out.isEmpty());
374 }
375