blob: 7c6e0de5da772d07ff4970904632530d6762600e [file] [log] [blame]
Mike Reed56aa7102020-04-16 06:25:54 -04001/*
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 "tests/Test.h"
10
11static bool eq(const SkM44& a, const SkM44& b, float tol) {
12 float fa[16], fb[16];
13 a.getColMajor(fa);
14 b.getColMajor(fb);
15 for (int i = 0; i < 16; ++i) {
16 if (!SkScalarNearlyEqual(fa[i], fb[i], tol)) {
17 return false;
18 }
19 }
20 return true;
21}
22
23DEF_TEST(M44, reporter) {
24 SkM44 m, im;
25
26 REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 0,
27 0, 1, 0, 0,
28 0, 0, 1, 0,
29 0, 0, 0, 1) == m);
30 REPORTER_ASSERT(reporter, SkM44() == m);
31 REPORTER_ASSERT(reporter, m.invert(&im));
32 REPORTER_ASSERT(reporter, SkM44() == im);
33
34 m.setTranslate(3, 4, 2);
35 REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 3,
36 0, 1, 0, 4,
37 0, 0, 1, 2,
38 0, 0, 0, 1) == m);
39
40 const float f[] = { 1, 0, 0, 2, 3, 1, 2, 5, 0, 5, 3, 0, 0, 1, 0, 2 };
41 m = SkM44::ColMajor(f);
42 REPORTER_ASSERT(reporter, SkM44(f[0], f[4], f[ 8], f[12],
43 f[1], f[5], f[ 9], f[13],
44 f[2], f[6], f[10], f[14],
45 f[3], f[7], f[11], f[15]) == m);
46
47 {
48 SkM44 t = m.transpose();
49 REPORTER_ASSERT(reporter, t != m);
50 REPORTER_ASSERT(reporter, t.rc(1,0) == m.rc(0,1));
51 SkM44 tt = t.transpose();
52 REPORTER_ASSERT(reporter, tt == m);
53 }
54
55 m = SkM44::RowMajor(f);
56 REPORTER_ASSERT(reporter, SkM44(f[ 0], f[ 1], f[ 2], f[ 3],
57 f[ 4], f[ 5], f[ 6], f[ 7],
58 f[ 8], f[ 9], f[10], f[14],
59 f[12], f[13], f[14], f[15]) == m);
60
61 REPORTER_ASSERT(reporter, m.invert(&im));
62
63 m = m * im;
64 // m should be identity now, but our calc is not perfect...
65 REPORTER_ASSERT(reporter, eq(SkM44(), m, 0.0000005f));
66 REPORTER_ASSERT(reporter, SkM44() != m);
67}
68
69DEF_TEST(M44_v3, reporter) {
70 SkV3 a = {1, 2, 3},
71 b = {1, 2, 2};
72
73 REPORTER_ASSERT(reporter, a.lengthSquared() == 1 + 4 + 9);
74 REPORTER_ASSERT(reporter, b.length() == 3);
75 REPORTER_ASSERT(reporter, a.dot(b) == 1 + 4 + 6);
76 REPORTER_ASSERT(reporter, b.dot(a) == 1 + 4 + 6);
77 REPORTER_ASSERT(reporter, (a.cross(b) == SkV3{-2, 1, 0}));
78 REPORTER_ASSERT(reporter, (b.cross(a) == SkV3{ 2, -1, 0}));
79
80 SkM44 m = {
81 2, 0, 0, 3,
82 0, 1, 0, 5,
83 0, 0, 3, 1,
84 0, 0, 0, 1
85 };
86
87 SkV3 c = m * a;
88 REPORTER_ASSERT(reporter, (c == SkV3{2, 2, 9}));
89 SkV4 d = m.map(4, 3, 2, 1);
90 REPORTER_ASSERT(reporter, (d == SkV4{11, 8, 7, 1}));
91}
92
93DEF_TEST(M44_v4, reporter) {
94 SkM44 m( 1, 2, 3, 4,
95 5, 6, 7, 8,
96 9, 10, 11, 12,
97 13, 14, 15, 16);
98
99 SkV4 r0 = m.row(0),
100 r1 = m.row(1),
101 r2 = m.row(2),
102 r3 = m.row(3);
103
104 REPORTER_ASSERT(reporter, (r0 == SkV4{ 1, 2, 3, 4}));
105 REPORTER_ASSERT(reporter, (r1 == SkV4{ 5, 6, 7, 8}));
106 REPORTER_ASSERT(reporter, (r2 == SkV4{ 9, 10, 11, 12}));
107 REPORTER_ASSERT(reporter, (r3 == SkV4{13, 14, 15, 16}));
108
109 REPORTER_ASSERT(reporter, SkM44::Rows(r0, r1, r2, r3) == m);
110
111 SkV4 c0 = m.col(0),
112 c1 = m.col(1),
113 c2 = m.col(2),
114 c3 = m.col(3);
115
116 REPORTER_ASSERT(reporter, (c0 == SkV4{1, 5, 9, 13}));
117 REPORTER_ASSERT(reporter, (c1 == SkV4{2, 6, 10, 14}));
118 REPORTER_ASSERT(reporter, (c2 == SkV4{3, 7, 11, 15}));
119 REPORTER_ASSERT(reporter, (c3 == SkV4{4, 8, 12, 16}));
120
121 REPORTER_ASSERT(reporter, SkM44::Cols(c0, c1, c2, c3) == m);
122
123 // implement matrix * vector using column vectors
124 SkV4 v = {1, 2, 3, 4};
125 SkV4 v1 = m * v;
126 SkV4 v2 = c0 * v.x + c1 * v.y + c2 * v.z + c3 * v.w;
127 REPORTER_ASSERT(reporter, v1 == v2);
128
129 REPORTER_ASSERT(reporter, (c0 + r0 == SkV4{c0.x+r0.x, c0.y+r0.y, c0.z+r0.z, c0.w+r0.w}));
130 REPORTER_ASSERT(reporter, (c0 - r0 == SkV4{c0.x-r0.x, c0.y-r0.y, c0.z-r0.z, c0.w-r0.w}));
131 REPORTER_ASSERT(reporter, (c0 * r0 == SkV4{c0.x*r0.x, c0.y*r0.y, c0.z*r0.z, c0.w*r0.w}));
132}
133
134DEF_TEST(M44_rotate, reporter) {
135 const SkV3 x = {1, 0, 0},
136 y = {0, 1, 0},
137 z = {0, 0, 1};
138
139 // We have radians version of setRotateAbout methods, but even with our best approx
140 // for PI, sin(SK_ScalarPI) != 0, so to make the comparisons in the unittest clear,
141 // I'm using the variants that explicitly take the sin,cos values.
142
143 struct {
144 SkScalar sinAngle, cosAngle;
145 SkV3 aboutAxis;
146 SkV3 expectedX, expectedY, expectedZ;
147 } recs[] = {
148 { 0, 1, x, x, y, z}, // angle = 0
149 { 0, 1, y, x, y, z}, // angle = 0
150 { 0, 1, z, x, y, z}, // angle = 0
151
152 { 0,-1, x, x,-y,-z}, // angle = 180
153 { 0,-1, y, -x, y,-z}, // angle = 180
154 { 0,-1, z, -x,-y, z}, // angle = 180
155
156 // Skia coordinate system is right-handed
157
158 { 1, 0, x, x, z,-y}, // angle = 90
159 { 1, 0, y, -z, y, x}, // angle = 90
160 { 1, 0, z, y,-x, z}, // angle = 90
161
162 {-1, 0, x, x,-z, y}, // angle = -90
163 {-1, 0, y, z, y,-x}, // angle = -90
164 {-1, 0, z, -y, x, z}, // angle = -90
165 };
166
167 for (const auto& r : recs) {
168 SkM44 m(SkM44::kNaN_Constructor);
169 m.setRotateUnitSinCos(r.aboutAxis, r.sinAngle, r.cosAngle);
170
171 auto mx = m * x;
172 auto my = m * y;
173 auto mz = m * z;
174 REPORTER_ASSERT(reporter, mx == r.expectedX);
175 REPORTER_ASSERT(reporter, my == r.expectedY);
176 REPORTER_ASSERT(reporter, mz == r.expectedZ);
177
178 // flipping the axis-of-rotation should flip the results
179 mx = m * -x;
180 my = m * -y;
181 mz = m * -z;
182 REPORTER_ASSERT(reporter, mx == -r.expectedX);
183 REPORTER_ASSERT(reporter, my == -r.expectedY);
184 REPORTER_ASSERT(reporter, mz == -r.expectedZ);
185 }
186}