Removed unused icon resources from app_list.
[chromium-blink-merge.git] / ui / gfx / transform_unittest.cc
blob126c87884afc6c08df532c2b771d6574bde399ff
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
3 // found in the LICENSE file.
5 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
8 #include "ui/gfx/transform.h"
10 #include <cmath>
11 #include <ostream>
12 #include <limits>
14 #include "base/basictypes.h"
15 #include "base/logging.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/box_f.h"
18 #include "ui/gfx/geometry/point.h"
19 #include "ui/gfx/geometry/point3_f.h"
20 #include "ui/gfx/geometry/quad_f.h"
21 #include "ui/gfx/geometry/vector3d_f.h"
22 #include "ui/gfx/transform_util.h"
24 namespace gfx {
26 namespace {
28 #define EXPECT_ROW1_EQ(a, b, c, d, transform) \
29 EXPECT_FLOAT_EQ((a), (transform).matrix().get(0, 0)); \
30 EXPECT_FLOAT_EQ((b), (transform).matrix().get(0, 1)); \
31 EXPECT_FLOAT_EQ((c), (transform).matrix().get(0, 2)); \
32 EXPECT_FLOAT_EQ((d), (transform).matrix().get(0, 3));
34 #define EXPECT_ROW2_EQ(a, b, c, d, transform) \
35 EXPECT_FLOAT_EQ((a), (transform).matrix().get(1, 0)); \
36 EXPECT_FLOAT_EQ((b), (transform).matrix().get(1, 1)); \
37 EXPECT_FLOAT_EQ((c), (transform).matrix().get(1, 2)); \
38 EXPECT_FLOAT_EQ((d), (transform).matrix().get(1, 3));
40 #define EXPECT_ROW3_EQ(a, b, c, d, transform) \
41 EXPECT_FLOAT_EQ((a), (transform).matrix().get(2, 0)); \
42 EXPECT_FLOAT_EQ((b), (transform).matrix().get(2, 1)); \
43 EXPECT_FLOAT_EQ((c), (transform).matrix().get(2, 2)); \
44 EXPECT_FLOAT_EQ((d), (transform).matrix().get(2, 3));
46 #define EXPECT_ROW4_EQ(a, b, c, d, transform) \
47 EXPECT_FLOAT_EQ((a), (transform).matrix().get(3, 0)); \
48 EXPECT_FLOAT_EQ((b), (transform).matrix().get(3, 1)); \
49 EXPECT_FLOAT_EQ((c), (transform).matrix().get(3, 2)); \
50 EXPECT_FLOAT_EQ((d), (transform).matrix().get(3, 3)); \
52 // Checking float values for equality close to zero is not robust using
53 // EXPECT_FLOAT_EQ (see gtest documentation). So, to verify rotation matrices,
54 // we must use a looser absolute error threshold in some places.
55 #define EXPECT_ROW1_NEAR(a, b, c, d, transform, errorThreshold) \
56 EXPECT_NEAR((a), (transform).matrix().get(0, 0), (errorThreshold)); \
57 EXPECT_NEAR((b), (transform).matrix().get(0, 1), (errorThreshold)); \
58 EXPECT_NEAR((c), (transform).matrix().get(0, 2), (errorThreshold)); \
59 EXPECT_NEAR((d), (transform).matrix().get(0, 3), (errorThreshold));
61 #define EXPECT_ROW2_NEAR(a, b, c, d, transform, errorThreshold) \
62 EXPECT_NEAR((a), (transform).matrix().get(1, 0), (errorThreshold)); \
63 EXPECT_NEAR((b), (transform).matrix().get(1, 1), (errorThreshold)); \
64 EXPECT_NEAR((c), (transform).matrix().get(1, 2), (errorThreshold)); \
65 EXPECT_NEAR((d), (transform).matrix().get(1, 3), (errorThreshold));
67 #define EXPECT_ROW3_NEAR(a, b, c, d, transform, errorThreshold) \
68 EXPECT_NEAR((a), (transform).matrix().get(2, 0), (errorThreshold)); \
69 EXPECT_NEAR((b), (transform).matrix().get(2, 1), (errorThreshold)); \
70 EXPECT_NEAR((c), (transform).matrix().get(2, 2), (errorThreshold)); \
71 EXPECT_NEAR((d), (transform).matrix().get(2, 3), (errorThreshold));
73 bool PointsAreNearlyEqual(const Point3F& lhs,
74 const Point3F& rhs) {
75 float epsilon = 0.0001f;
76 return lhs.SquaredDistanceTo(rhs) < epsilon;
79 bool MatricesAreNearlyEqual(const Transform& lhs,
80 const Transform& rhs) {
81 float epsilon = 0.0001f;
82 for (int row = 0; row < 4; ++row) {
83 for (int col = 0; col < 4; ++col) {
84 if (std::abs(lhs.matrix().get(row, col) -
85 rhs.matrix().get(row, col)) > epsilon)
86 return false;
89 return true;
92 void InitializeTestMatrix(Transform* transform) {
93 SkMatrix44& matrix = transform->matrix();
94 matrix.set(0, 0, 10.f);
95 matrix.set(1, 0, 11.f);
96 matrix.set(2, 0, 12.f);
97 matrix.set(3, 0, 13.f);
98 matrix.set(0, 1, 14.f);
99 matrix.set(1, 1, 15.f);
100 matrix.set(2, 1, 16.f);
101 matrix.set(3, 1, 17.f);
102 matrix.set(0, 2, 18.f);
103 matrix.set(1, 2, 19.f);
104 matrix.set(2, 2, 20.f);
105 matrix.set(3, 2, 21.f);
106 matrix.set(0, 3, 22.f);
107 matrix.set(1, 3, 23.f);
108 matrix.set(2, 3, 24.f);
109 matrix.set(3, 3, 25.f);
111 // Sanity check
112 EXPECT_ROW1_EQ(10.0f, 14.0f, 18.0f, 22.0f, (*transform));
113 EXPECT_ROW2_EQ(11.0f, 15.0f, 19.0f, 23.0f, (*transform));
114 EXPECT_ROW3_EQ(12.0f, 16.0f, 20.0f, 24.0f, (*transform));
115 EXPECT_ROW4_EQ(13.0f, 17.0f, 21.0f, 25.0f, (*transform));
118 void InitializeTestMatrix2(Transform* transform) {
119 SkMatrix44& matrix = transform->matrix();
120 matrix.set(0, 0, 30.f);
121 matrix.set(1, 0, 31.f);
122 matrix.set(2, 0, 32.f);
123 matrix.set(3, 0, 33.f);
124 matrix.set(0, 1, 34.f);
125 matrix.set(1, 1, 35.f);
126 matrix.set(2, 1, 36.f);
127 matrix.set(3, 1, 37.f);
128 matrix.set(0, 2, 38.f);
129 matrix.set(1, 2, 39.f);
130 matrix.set(2, 2, 40.f);
131 matrix.set(3, 2, 41.f);
132 matrix.set(0, 3, 42.f);
133 matrix.set(1, 3, 43.f);
134 matrix.set(2, 3, 44.f);
135 matrix.set(3, 3, 45.f);
137 // Sanity check
138 EXPECT_ROW1_EQ(30.0f, 34.0f, 38.0f, 42.0f, (*transform));
139 EXPECT_ROW2_EQ(31.0f, 35.0f, 39.0f, 43.0f, (*transform));
140 EXPECT_ROW3_EQ(32.0f, 36.0f, 40.0f, 44.0f, (*transform));
141 EXPECT_ROW4_EQ(33.0f, 37.0f, 41.0f, 45.0f, (*transform));
144 const SkMScalar kApproxZero =
145 SkFloatToMScalar(std::numeric_limits<float>::epsilon());
146 const SkMScalar kApproxOne = 1 - kApproxZero;
148 void InitializeApproxIdentityMatrix(Transform* transform) {
149 SkMatrix44& matrix = transform->matrix();
150 matrix.set(0, 0, kApproxOne);
151 matrix.set(0, 1, kApproxZero);
152 matrix.set(0, 2, kApproxZero);
153 matrix.set(0, 3, kApproxZero);
155 matrix.set(1, 0, kApproxZero);
156 matrix.set(1, 1, kApproxOne);
157 matrix.set(1, 2, kApproxZero);
158 matrix.set(1, 3, kApproxZero);
160 matrix.set(2, 0, kApproxZero);
161 matrix.set(2, 1, kApproxZero);
162 matrix.set(2, 2, kApproxOne);
163 matrix.set(2, 3, kApproxZero);
165 matrix.set(3, 0, kApproxZero);
166 matrix.set(3, 1, kApproxZero);
167 matrix.set(3, 2, kApproxZero);
168 matrix.set(3, 3, kApproxOne);
171 #ifdef SK_MSCALAR_IS_DOUBLE
172 #define ERROR_THRESHOLD 1e-14
173 #else
174 #define ERROR_THRESHOLD 1e-7
175 #endif
176 #define LOOSE_ERROR_THRESHOLD 1e-7
178 TEST(XFormTest, Equality) {
179 Transform lhs, rhs, interpolated;
180 rhs.matrix().set3x3(1, 2, 3,
181 4, 5, 6,
182 7, 8, 9);
183 interpolated = lhs;
184 for (int i = 0; i <= 100; ++i) {
185 for (int row = 0; row < 4; ++row) {
186 for (int col = 0; col < 4; ++col) {
187 float a = lhs.matrix().get(row, col);
188 float b = rhs.matrix().get(row, col);
189 float t = i / 100.0f;
190 interpolated.matrix().set(row, col, a + (b - a) * t);
193 if (i == 100) {
194 EXPECT_TRUE(rhs == interpolated);
195 } else {
196 EXPECT_TRUE(rhs != interpolated);
199 lhs = Transform();
200 rhs = Transform();
201 for (int i = 1; i < 100; ++i) {
202 lhs.MakeIdentity();
203 rhs.MakeIdentity();
204 lhs.Translate(i, i);
205 rhs.Translate(-i, -i);
206 EXPECT_TRUE(lhs != rhs);
207 rhs.Translate(2*i, 2*i);
208 EXPECT_TRUE(lhs == rhs);
212 TEST(XFormTest, ConcatTranslate) {
213 static const struct TestCase {
214 int x1;
215 int y1;
216 float tx;
217 float ty;
218 int x2;
219 int y2;
220 } test_cases[] = {
221 { 0, 0, 10.0f, 20.0f, 10, 20 },
222 { 0, 0, -10.0f, -20.0f, 0, 0 },
223 { 0, 0, -10.0f, -20.0f, -10, -20 },
224 { 0, 0,
225 std::numeric_limits<float>::quiet_NaN(),
226 std::numeric_limits<float>::quiet_NaN(),
227 10, 20 },
230 Transform xform;
231 for (size_t i = 0; i < arraysize(test_cases); ++i) {
232 const TestCase& value = test_cases[i];
233 Transform translation;
234 translation.Translate(value.tx, value.ty);
235 xform = translation * xform;
236 Point3F p1(value.x1, value.y1, 0);
237 Point3F p2(value.x2, value.y2, 0);
238 xform.TransformPoint(&p1);
239 if (value.tx == value.tx &&
240 value.ty == value.ty) {
241 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
246 TEST(XFormTest, ConcatScale) {
247 static const struct TestCase {
248 int before;
249 float scale;
250 int after;
251 } test_cases[] = {
252 { 1, 10.0f, 10 },
253 { 1, .1f, 1 },
254 { 1, 100.0f, 100 },
255 { 1, -1.0f, -100 },
256 { 1, std::numeric_limits<float>::quiet_NaN(), 1 }
259 Transform xform;
260 for (size_t i = 0; i < arraysize(test_cases); ++i) {
261 const TestCase& value = test_cases[i];
262 Transform scale;
263 scale.Scale(value.scale, value.scale);
264 xform = scale * xform;
265 Point3F p1(value.before, value.before, 0);
266 Point3F p2(value.after, value.after, 0);
267 xform.TransformPoint(&p1);
268 if (value.scale == value.scale) {
269 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
274 TEST(XFormTest, ConcatRotate) {
275 static const struct TestCase {
276 int x1;
277 int y1;
278 float degrees;
279 int x2;
280 int y2;
281 } test_cases[] = {
282 { 1, 0, 90.0f, 0, 1 },
283 { 1, 0, -90.0f, 1, 0 },
284 { 1, 0, 90.0f, 0, 1 },
285 { 1, 0, 360.0f, 0, 1 },
286 { 1, 0, 0.0f, 0, 1 },
287 { 1, 0, std::numeric_limits<float>::quiet_NaN(), 1, 0 }
290 Transform xform;
291 for (size_t i = 0; i < arraysize(test_cases); ++i) {
292 const TestCase& value = test_cases[i];
293 Transform rotation;
294 rotation.Rotate(value.degrees);
295 xform = rotation * xform;
296 Point3F p1(value.x1, value.y1, 0);
297 Point3F p2(value.x2, value.y2, 0);
298 xform.TransformPoint(&p1);
299 if (value.degrees == value.degrees) {
300 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
305 TEST(XFormTest, SetTranslate) {
306 static const struct TestCase {
307 int x1; int y1;
308 float tx; float ty;
309 int x2; int y2;
310 } test_cases[] = {
311 { 0, 0, 10.0f, 20.0f, 10, 20 },
312 { 10, 20, 10.0f, 20.0f, 20, 40 },
313 { 10, 20, 0.0f, 0.0f, 10, 20 },
314 { 0, 0,
315 std::numeric_limits<float>::quiet_NaN(),
316 std::numeric_limits<float>::quiet_NaN(),
317 0, 0 }
320 for (size_t i = 0; i < arraysize(test_cases); ++i) {
321 const TestCase& value = test_cases[i];
322 for (int k = 0; k < 3; ++k) {
323 Point3F p0, p1, p2;
324 Transform xform;
325 switch (k) {
326 case 0:
327 p1.SetPoint(value.x1, 0, 0);
328 p2.SetPoint(value.x2, 0, 0);
329 xform.Translate(value.tx, 0.0);
330 break;
331 case 1:
332 p1.SetPoint(0, value.y1, 0);
333 p2.SetPoint(0, value.y2, 0);
334 xform.Translate(0.0, value.ty);
335 break;
336 case 2:
337 p1.SetPoint(value.x1, value.y1, 0);
338 p2.SetPoint(value.x2, value.y2, 0);
339 xform.Translate(value.tx, value.ty);
340 break;
342 p0 = p1;
343 xform.TransformPoint(&p1);
344 if (value.tx == value.tx &&
345 value.ty == value.ty) {
346 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
347 xform.TransformPointReverse(&p1);
348 EXPECT_TRUE(PointsAreNearlyEqual(p1, p0));
354 TEST(XFormTest, SetScale) {
355 static const struct TestCase {
356 int before;
357 float s;
358 int after;
359 } test_cases[] = {
360 { 1, 10.0f, 10 },
361 { 1, 1.0f, 1 },
362 { 1, 0.0f, 0 },
363 { 0, 10.0f, 0 },
364 { 1, std::numeric_limits<float>::quiet_NaN(), 0 },
367 for (size_t i = 0; i < arraysize(test_cases); ++i) {
368 const TestCase& value = test_cases[i];
369 for (int k = 0; k < 3; ++k) {
370 Point3F p0, p1, p2;
371 Transform xform;
372 switch (k) {
373 case 0:
374 p1.SetPoint(value.before, 0, 0);
375 p2.SetPoint(value.after, 0, 0);
376 xform.Scale(value.s, 1.0);
377 break;
378 case 1:
379 p1.SetPoint(0, value.before, 0);
380 p2.SetPoint(0, value.after, 0);
381 xform.Scale(1.0, value.s);
382 break;
383 case 2:
384 p1.SetPoint(value.before, value.before, 0);
385 p2.SetPoint(value.after, value.after, 0);
386 xform.Scale(value.s, value.s);
387 break;
389 p0 = p1;
390 xform.TransformPoint(&p1);
391 if (value.s == value.s) {
392 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
393 if (value.s != 0.0f) {
394 xform.TransformPointReverse(&p1);
395 EXPECT_TRUE(PointsAreNearlyEqual(p1, p0));
402 TEST(XFormTest, SetRotate) {
403 static const struct SetRotateCase {
404 int x;
405 int y;
406 float degree;
407 int xprime;
408 int yprime;
409 } set_rotate_cases[] = {
410 { 100, 0, 90.0f, 0, 100 },
411 { 0, 0, 90.0f, 0, 0 },
412 { 0, 100, 90.0f, -100, 0 },
413 { 0, 1, -90.0f, 1, 0 },
414 { 100, 0, 0.0f, 100, 0 },
415 { 0, 0, 0.0f, 0, 0 },
416 { 0, 0, std::numeric_limits<float>::quiet_NaN(), 0, 0 },
417 { 100, 0, 360.0f, 100, 0 }
420 for (size_t i = 0; i < arraysize(set_rotate_cases); ++i) {
421 const SetRotateCase& value = set_rotate_cases[i];
422 Point3F p0;
423 Point3F p1(value.x, value.y, 0);
424 Point3F p2(value.xprime, value.yprime, 0);
425 p0 = p1;
426 Transform xform;
427 xform.Rotate(value.degree);
428 // just want to make sure that we don't crash in the case of NaN.
429 if (value.degree == value.degree) {
430 xform.TransformPoint(&p1);
431 EXPECT_TRUE(PointsAreNearlyEqual(p1, p2));
432 xform.TransformPointReverse(&p1);
433 EXPECT_TRUE(PointsAreNearlyEqual(p1, p0));
438 // 2D tests
439 TEST(XFormTest, ConcatTranslate2D) {
440 static const struct TestCase {
441 int x1;
442 int y1;
443 float tx;
444 float ty;
445 int x2;
446 int y2;
447 } test_cases[] = {
448 { 0, 0, 10.0f, 20.0f, 10, 20},
449 { 0, 0, -10.0f, -20.0f, 0, 0},
450 { 0, 0, -10.0f, -20.0f, -10, -20},
451 { 0, 0,
452 std::numeric_limits<float>::quiet_NaN(),
453 std::numeric_limits<float>::quiet_NaN(),
454 10, 20},
457 Transform xform;
458 for (size_t i = 0; i < arraysize(test_cases); ++i) {
459 const TestCase& value = test_cases[i];
460 Transform translation;
461 translation.Translate(value.tx, value.ty);
462 xform = translation * xform;
463 Point p1(value.x1, value.y1);
464 Point p2(value.x2, value.y2);
465 xform.TransformPoint(&p1);
466 if (value.tx == value.tx &&
467 value.ty == value.ty) {
468 EXPECT_EQ(p1.x(), p2.x());
469 EXPECT_EQ(p1.y(), p2.y());
474 TEST(XFormTest, ConcatScale2D) {
475 static const struct TestCase {
476 int before;
477 float scale;
478 int after;
479 } test_cases[] = {
480 { 1, 10.0f, 10},
481 { 1, .1f, 1},
482 { 1, 100.0f, 100},
483 { 1, -1.0f, -100},
484 { 1, std::numeric_limits<float>::quiet_NaN(), 1}
487 Transform xform;
488 for (size_t i = 0; i < arraysize(test_cases); ++i) {
489 const TestCase& value = test_cases[i];
490 Transform scale;
491 scale.Scale(value.scale, value.scale);
492 xform = scale * xform;
493 Point p1(value.before, value.before);
494 Point p2(value.after, value.after);
495 xform.TransformPoint(&p1);
496 if (value.scale == value.scale) {
497 EXPECT_EQ(p1.x(), p2.x());
498 EXPECT_EQ(p1.y(), p2.y());
503 TEST(XFormTest, ConcatRotate2D) {
504 static const struct TestCase {
505 int x1;
506 int y1;
507 float degrees;
508 int x2;
509 int y2;
510 } test_cases[] = {
511 { 1, 0, 90.0f, 0, 1},
512 { 1, 0, -90.0f, 1, 0},
513 { 1, 0, 90.0f, 0, 1},
514 { 1, 0, 360.0f, 0, 1},
515 { 1, 0, 0.0f, 0, 1},
516 { 1, 0, std::numeric_limits<float>::quiet_NaN(), 1, 0}
519 Transform xform;
520 for (size_t i = 0; i < arraysize(test_cases); ++i) {
521 const TestCase& value = test_cases[i];
522 Transform rotation;
523 rotation.Rotate(value.degrees);
524 xform = rotation * xform;
525 Point p1(value.x1, value.y1);
526 Point p2(value.x2, value.y2);
527 xform.TransformPoint(&p1);
528 if (value.degrees == value.degrees) {
529 EXPECT_EQ(p1.x(), p2.x());
530 EXPECT_EQ(p1.y(), p2.y());
535 TEST(XFormTest, SetTranslate2D) {
536 static const struct TestCase {
537 int x1; int y1;
538 float tx; float ty;
539 int x2; int y2;
540 } test_cases[] = {
541 { 0, 0, 10.0f, 20.0f, 10, 20},
542 { 10, 20, 10.0f, 20.0f, 20, 40},
543 { 10, 20, 0.0f, 0.0f, 10, 20},
544 { 0, 0,
545 std::numeric_limits<float>::quiet_NaN(),
546 std::numeric_limits<float>::quiet_NaN(),
547 0, 0}
550 for (size_t i = 0; i < arraysize(test_cases); ++i) {
551 const TestCase& value = test_cases[i];
552 for (int j = -1; j < 2; ++j) {
553 for (int k = 0; k < 3; ++k) {
554 float epsilon = 0.0001f;
555 Point p0, p1, p2;
556 Transform xform;
557 switch (k) {
558 case 0:
559 p1.SetPoint(value.x1, 0);
560 p2.SetPoint(value.x2, 0);
561 xform.Translate(value.tx + j * epsilon, 0.0);
562 break;
563 case 1:
564 p1.SetPoint(0, value.y1);
565 p2.SetPoint(0, value.y2);
566 xform.Translate(0.0, value.ty + j * epsilon);
567 break;
568 case 2:
569 p1.SetPoint(value.x1, value.y1);
570 p2.SetPoint(value.x2, value.y2);
571 xform.Translate(value.tx + j * epsilon,
572 value.ty + j * epsilon);
573 break;
575 p0 = p1;
576 xform.TransformPoint(&p1);
577 if (value.tx == value.tx &&
578 value.ty == value.ty) {
579 EXPECT_EQ(p1.x(), p2.x());
580 EXPECT_EQ(p1.y(), p2.y());
581 xform.TransformPointReverse(&p1);
582 EXPECT_EQ(p1.x(), p0.x());
583 EXPECT_EQ(p1.y(), p0.y());
590 TEST(XFormTest, SetScale2D) {
591 static const struct TestCase {
592 int before;
593 float s;
594 int after;
595 } test_cases[] = {
596 { 1, 10.0f, 10},
597 { 1, 1.0f, 1},
598 { 1, 0.0f, 0},
599 { 0, 10.0f, 0},
600 { 1, std::numeric_limits<float>::quiet_NaN(), 0},
603 for (size_t i = 0; i < arraysize(test_cases); ++i) {
604 const TestCase& value = test_cases[i];
605 for (int j = -1; j < 2; ++j) {
606 for (int k = 0; k < 3; ++k) {
607 float epsilon = 0.0001f;
608 Point p0, p1, p2;
609 Transform xform;
610 switch (k) {
611 case 0:
612 p1.SetPoint(value.before, 0);
613 p2.SetPoint(value.after, 0);
614 xform.Scale(value.s + j * epsilon, 1.0);
615 break;
616 case 1:
617 p1.SetPoint(0, value.before);
618 p2.SetPoint(0, value.after);
619 xform.Scale(1.0, value.s + j * epsilon);
620 break;
621 case 2:
622 p1.SetPoint(value.before,
623 value.before);
624 p2.SetPoint(value.after,
625 value.after);
626 xform.Scale(value.s + j * epsilon,
627 value.s + j * epsilon);
628 break;
630 p0 = p1;
631 xform.TransformPoint(&p1);
632 if (value.s == value.s) {
633 EXPECT_EQ(p1.x(), p2.x());
634 EXPECT_EQ(p1.y(), p2.y());
635 if (value.s != 0.0f) {
636 xform.TransformPointReverse(&p1);
637 EXPECT_EQ(p1.x(), p0.x());
638 EXPECT_EQ(p1.y(), p0.y());
646 TEST(XFormTest, SetRotate2D) {
647 static const struct SetRotateCase {
648 int x;
649 int y;
650 float degree;
651 int xprime;
652 int yprime;
653 } set_rotate_cases[] = {
654 { 100, 0, 90.0f, 0, 100},
655 { 0, 0, 90.0f, 0, 0},
656 { 0, 100, 90.0f, -100, 0},
657 { 0, 1, -90.0f, 1, 0},
658 { 100, 0, 0.0f, 100, 0},
659 { 0, 0, 0.0f, 0, 0},
660 { 0, 0, std::numeric_limits<float>::quiet_NaN(), 0, 0},
661 { 100, 0, 360.0f, 100, 0}
664 for (size_t i = 0; i < arraysize(set_rotate_cases); ++i) {
665 const SetRotateCase& value = set_rotate_cases[i];
666 for (int j = 1; j >= -1; --j) {
667 float epsilon = 0.1f;
668 Point pt(value.x, value.y);
669 Transform xform;
670 // should be invariant to small floating point errors.
671 xform.Rotate(value.degree + j * epsilon);
672 // just want to make sure that we don't crash in the case of NaN.
673 if (value.degree == value.degree) {
674 xform.TransformPoint(&pt);
675 EXPECT_EQ(value.xprime, pt.x());
676 EXPECT_EQ(value.yprime, pt.y());
677 xform.TransformPointReverse(&pt);
678 EXPECT_EQ(pt.x(), value.x);
679 EXPECT_EQ(pt.y(), value.y);
685 TEST(XFormTest, TransformPointWithExtremePerspective) {
686 Point3F point(1.f, 1.f, 1.f);
687 Transform perspective;
688 perspective.ApplyPerspectiveDepth(1.f);
689 Point3F transformed = point;
690 perspective.TransformPoint(&transformed);
691 EXPECT_EQ(point.ToString(), transformed.ToString());
693 transformed = point;
694 perspective.MakeIdentity();
695 perspective.ApplyPerspectiveDepth(1.1f);
696 perspective.TransformPoint(&transformed);
697 EXPECT_FLOAT_EQ(11.f, transformed.x());
698 EXPECT_FLOAT_EQ(11.f, transformed.y());
699 EXPECT_FLOAT_EQ(11.f, transformed.z());
702 TEST(XFormTest, BlendTranslate) {
703 Transform from;
704 for (int i = -5; i < 15; ++i) {
705 Transform to;
706 to.Translate3d(1, 1, 1);
707 double t = i / 9.0;
708 EXPECT_TRUE(to.Blend(from, t));
709 EXPECT_FLOAT_EQ(t, to.matrix().get(0, 3));
710 EXPECT_FLOAT_EQ(t, to.matrix().get(1, 3));
711 EXPECT_FLOAT_EQ(t, to.matrix().get(2, 3));
715 TEST(XFormTest, BlendRotate) {
716 Vector3dF axes[] = {
717 Vector3dF(1, 0, 0),
718 Vector3dF(0, 1, 0),
719 Vector3dF(0, 0, 1),
720 Vector3dF(1, 1, 1)
722 Transform from;
723 for (size_t index = 0; index < arraysize(axes); ++index) {
724 for (int i = -5; i < 15; ++i) {
725 Transform to;
726 to.RotateAbout(axes[index], 90);
727 double t = i / 9.0;
728 EXPECT_TRUE(to.Blend(from, t));
730 Transform expected;
731 expected.RotateAbout(axes[index], 90 * t);
733 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to));
738 #if defined(_WIN64)
739 // http://crbug.com/406574
740 #define MAYBE_BlendRotateFollowsShortestPath DISABLED_BlendRotateFollowsShortestPath
741 #else
742 #define MAYBE_BlendRotateFollowsShortestPath BlendRotateFollowsShortestPath
743 #endif
744 TEST(XFormTest, MAYBE_BlendRotateFollowsShortestPath) {
745 // Verify that we interpolate along the shortest path regardless of whether
746 // this path crosses the 180-degree point.
747 Vector3dF axes[] = {
748 Vector3dF(1, 0, 0),
749 Vector3dF(0, 1, 0),
750 Vector3dF(0, 0, 1),
751 Vector3dF(1, 1, 1)
753 for (size_t index = 0; index < arraysize(axes); ++index) {
754 for (int i = -5; i < 15; ++i) {
755 Transform from1;
756 from1.RotateAbout(axes[index], 130.0);
757 Transform to1;
758 to1.RotateAbout(axes[index], 175.0);
760 Transform from2;
761 from2.RotateAbout(axes[index], 140.0);
762 Transform to2;
763 to2.RotateAbout(axes[index], 185.0);
765 double t = i / 9.0;
766 EXPECT_TRUE(to1.Blend(from1, t));
767 EXPECT_TRUE(to2.Blend(from2, t));
769 Transform expected1;
770 expected1.RotateAbout(axes[index], 130.0 + 45.0 * t);
772 Transform expected2;
773 expected2.RotateAbout(axes[index], 140.0 + 45.0 * t);
775 EXPECT_TRUE(MatricesAreNearlyEqual(expected1, to1));
776 EXPECT_TRUE(MatricesAreNearlyEqual(expected2, to2));
781 TEST(XFormTest, CanBlend180DegreeRotation) {
782 Vector3dF axes[] = {
783 Vector3dF(1, 0, 0),
784 Vector3dF(0, 1, 0),
785 Vector3dF(0, 0, 1),
786 Vector3dF(1, 1, 1)
788 Transform from;
789 for (size_t index = 0; index < arraysize(axes); ++index) {
790 for (int i = -5; i < 15; ++i) {
791 Transform to;
792 to.RotateAbout(axes[index], 180.0);
793 double t = i / 9.0;
794 EXPECT_TRUE(to.Blend(from, t));
796 // A 180 degree rotation is exactly opposite on the sphere, therefore
797 // either great circle arc to it is equivalent (and numerical precision
798 // will determine which is closer). Test both directions.
799 Transform expected1;
800 expected1.RotateAbout(axes[index], 180.0 * t);
801 Transform expected2;
802 expected2.RotateAbout(axes[index], -180.0 * t);
804 EXPECT_TRUE(MatricesAreNearlyEqual(expected1, to) ||
805 MatricesAreNearlyEqual(expected2, to))
806 << "axis: " << index << ", i: " << i;
811 #if defined(_WIN64)
812 // http://crbug.com/406574
813 #define MAYBE_BlendScale DISABLED_BlendScale
814 #else
815 #define MAYBE_BlendScale BlendScale
816 #endif
817 TEST(XFormTest, MAYBE_BlendScale) {
818 Transform from;
819 for (int i = -5; i < 15; ++i) {
820 Transform to;
821 to.Scale3d(5, 4, 3);
822 double t = i / 9.0;
823 EXPECT_TRUE(to.Blend(from, t));
824 EXPECT_FLOAT_EQ(t * 4 + 1, to.matrix().get(0, 0)) << "i: " << i;
825 EXPECT_FLOAT_EQ(t * 3 + 1, to.matrix().get(1, 1)) << "i: " << i;
826 EXPECT_FLOAT_EQ(t * 2 + 1, to.matrix().get(2, 2)) << "i: " << i;
830 TEST(XFormTest, BlendSkew) {
831 Transform from;
832 for (int i = 0; i < 2; ++i) {
833 Transform to;
834 to.SkewX(10);
835 to.SkewY(5);
836 double t = i;
837 Transform expected;
838 expected.SkewX(t * 10);
839 expected.SkewY(t * 5);
840 EXPECT_TRUE(to.Blend(from, t));
841 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to));
845 TEST(XFormTest, ExtrapolateSkew) {
846 Transform from;
847 for (int i = -1; i < 2; ++i) {
848 Transform to;
849 to.SkewX(20);
850 double t = i;
851 Transform expected;
852 expected.SkewX(t * 20);
853 EXPECT_TRUE(to.Blend(from, t));
854 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to));
858 #if defined(_WIN64)
859 // http://crbug.com/406574
860 #define MAYBE_BlendPerspective DISABLED_BlendPerspective
861 #else
862 #define MAYBE_BlendPerspective BlendPerspective
863 #endif
864 TEST(XFormTest, MAYBE_BlendPerspective) {
865 Transform from;
866 from.ApplyPerspectiveDepth(200);
867 for (int i = -1; i < 3; ++i) {
868 Transform to;
869 to.ApplyPerspectiveDepth(800);
870 double t = i;
871 double depth = 1.0 / ((1.0 / 200) * (1.0 - t) + (1.0 / 800) * t);
872 Transform expected;
873 expected.ApplyPerspectiveDepth(depth);
874 EXPECT_TRUE(to.Blend(from, t));
875 EXPECT_TRUE(MatricesAreNearlyEqual(expected, to));
879 TEST(XFormTest, BlendIdentity) {
880 Transform from;
881 Transform to;
882 EXPECT_TRUE(to.Blend(from, 0.5));
883 EXPECT_EQ(to, from);
886 TEST(XFormTest, CannotBlendSingularMatrix) {
887 Transform from;
888 Transform to;
889 to.matrix().set(1, 1, SkDoubleToMScalar(0));
890 EXPECT_FALSE(to.Blend(from, 0.5));
893 TEST(XFormTest, VerifyBlendForTranslation) {
894 Transform from;
895 from.Translate3d(100.0, 200.0, 100.0);
897 Transform to;
899 to.Translate3d(200.0, 100.0, 300.0);
900 to.Blend(from, 0.0);
901 EXPECT_EQ(from, to);
903 to = Transform();
904 to.Translate3d(200.0, 100.0, 300.0);
905 to.Blend(from, 0.25);
906 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 125.0f, to);
907 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 175.0f, to);
908 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 150.0f, to);
909 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
911 to = Transform();
912 to.Translate3d(200.0, 100.0, 300.0);
913 to.Blend(from, 0.5);
914 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 150.0f, to);
915 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 150.0f, to);
916 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 200.0f, to);
917 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
919 to = Transform();
920 to.Translate3d(200.0, 100.0, 300.0);
921 to.Blend(from, 1.0);
922 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 200.0f, to);
923 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 100.0f, to);
924 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 300.0f, to);
925 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
928 TEST(XFormTest, VerifyBlendForScale) {
929 Transform from;
930 from.Scale3d(100.0, 200.0, 100.0);
932 Transform to;
934 to.Scale3d(200.0, 100.0, 300.0);
935 to.Blend(from, 0.0);
936 EXPECT_EQ(from, to);
938 to = Transform();
939 to.Scale3d(200.0, 100.0, 300.0);
940 to.Blend(from, 0.25);
941 EXPECT_ROW1_EQ(125.0f, 0.0f, 0.0f, 0.0f, to);
942 EXPECT_ROW2_EQ(0.0f, 175.0f, 0.0f, 0.0f, to);
943 EXPECT_ROW3_EQ(0.0f, 0.0f, 150.0f, 0.0f, to);
944 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
946 to = Transform();
947 to.Scale3d(200.0, 100.0, 300.0);
948 to.Blend(from, 0.5);
949 EXPECT_ROW1_EQ(150.0f, 0.0f, 0.0f, 0.0f, to);
950 EXPECT_ROW2_EQ(0.0f, 150.0f, 0.0f, 0.0f, to);
951 EXPECT_ROW3_EQ(0.0f, 0.0f, 200.0f, 0.0f, to);
952 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
954 to = Transform();
955 to.Scale3d(200.0, 100.0, 300.0);
956 to.Blend(from, 1.0);
957 EXPECT_ROW1_EQ(200.0f, 0.0f, 0.0f, 0.0f, to);
958 EXPECT_ROW2_EQ(0.0f, 100.0f, 0.0f, 0.0f, to);
959 EXPECT_ROW3_EQ(0.0f, 0.0f, 300.0f, 0.0f, to);
960 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
963 TEST(XFormTest, VerifyBlendForSkewX) {
964 Transform from;
965 from.SkewX(0.0);
967 Transform to;
969 to.SkewX(45.0);
970 to.Blend(from, 0.0);
971 EXPECT_EQ(from, to);
973 to = Transform();
974 to.SkewX(45.0);
975 to.Blend(from, 0.5);
976 EXPECT_ROW1_EQ(1.0f, 0.5f, 0.0f, 0.0f, to);
977 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, to);
978 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
979 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
981 to = Transform();
982 to.SkewX(45.0);
983 to.Blend(from, 0.25);
984 EXPECT_ROW1_EQ(1.0f, 0.25f, 0.0f, 0.0f, to);
985 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, to);
986 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
987 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
989 to = Transform();
990 to.SkewX(45.0);
991 to.Blend(from, 1.0);
992 EXPECT_ROW1_EQ(1.0f, 1.0f, 0.0f, 0.0f, to);
993 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, to);
994 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
995 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
998 TEST(XFormTest, VerifyBlendForSkewY) {
999 // NOTE CAREFULLY: Decomposition of skew and rotation terms of the matrix
1000 // is inherently underconstrained, and so it does not always compute the
1001 // originally intended skew parameters. The current implementation uses QR
1002 // decomposition, which decomposes the shear into a rotation + non-uniform
1003 // scale.
1005 // It is unlikely that the decomposition implementation will need to change
1006 // very often, so to get any test coverage, the compromise is to verify the
1007 // exact matrix that the.Blend() operation produces.
1009 // This problem also potentially exists for skewX, but the current QR
1010 // decomposition implementation just happens to decompose those test
1011 // matrices intuitively.
1013 // Unfortunately, this case suffers from uncomfortably large precision
1014 // error.
1016 Transform from;
1017 from.SkewY(0.0);
1019 Transform to;
1021 to.SkewY(45.0);
1022 to.Blend(from, 0.0);
1023 EXPECT_EQ(from, to);
1025 to = Transform();
1026 to.SkewY(45.0);
1027 to.Blend(from, 0.25);
1028 EXPECT_ROW1_NEAR(1.0823489449280947471976333,
1029 0.0464370719145053845178239,
1030 0.0,
1031 0.0,
1033 LOOSE_ERROR_THRESHOLD);
1034 EXPECT_ROW2_NEAR(0.2152925909665224513123150,
1035 0.9541702441750861130032035,
1036 0.0,
1037 0.0,
1039 LOOSE_ERROR_THRESHOLD);
1040 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
1041 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1043 to = Transform();
1044 to.SkewY(45.0);
1045 to.Blend(from, 0.5);
1046 EXPECT_ROW1_NEAR(1.1152212925809066312865525,
1047 0.0676495144007326631996335,
1048 0.0,
1049 0.0,
1051 LOOSE_ERROR_THRESHOLD);
1052 EXPECT_ROW2_NEAR(0.4619397844342648662419037,
1053 0.9519009045724774464858342,
1054 0.0,
1055 0.0,
1057 LOOSE_ERROR_THRESHOLD);
1058 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
1059 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1061 to = Transform();
1062 to.SkewY(45.0);
1063 to.Blend(from, 1.0);
1064 EXPECT_ROW1_NEAR(1.0, 0.0, 0.0, 0.0, to, LOOSE_ERROR_THRESHOLD);
1065 EXPECT_ROW2_NEAR(1.0, 1.0, 0.0, 0.0, to, LOOSE_ERROR_THRESHOLD);
1066 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, to);
1067 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1070 #if defined(_WIN64)
1071 // http://crbug.com/406574
1072 #define MAYBE_VerifyBlendForRotationAboutX DISABLED_VerifyBlendForRotationAboutX
1073 #else
1074 #define MAYBE_VerifyBlendForRotationAboutX VerifyBlendForRotationAboutX
1075 #endif
1076 TEST(XFormTest, MAYBE_VerifyBlendForRotationAboutX) {
1077 // Even though.Blending uses quaternions, axis-aligned rotations should.
1078 // Blend the same with quaternions or Euler angles. So we can test
1079 // rotation.Blending by comparing against manually specified matrices from
1080 // Euler angles.
1082 Transform from;
1083 from.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 0.0);
1085 Transform to;
1087 to.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 90.0);
1088 to.Blend(from, 0.0);
1089 EXPECT_EQ(from, to);
1091 double expectedRotationAngle = 22.5 * M_PI / 180.0;
1092 to = Transform();
1093 to.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 90.0);
1094 to.Blend(from, 0.25);
1095 EXPECT_ROW1_NEAR(1.0, 0.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1096 EXPECT_ROW2_NEAR(0.0,
1097 std::cos(expectedRotationAngle),
1098 -std::sin(expectedRotationAngle),
1099 0.0,
1101 ERROR_THRESHOLD);
1102 EXPECT_ROW3_NEAR(0.0,
1103 std::sin(expectedRotationAngle),
1104 std::cos(expectedRotationAngle),
1105 0.0,
1107 ERROR_THRESHOLD);
1108 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1110 expectedRotationAngle = 45.0 * M_PI / 180.0;
1111 to = Transform();
1112 to.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 90.0);
1113 to.Blend(from, 0.5);
1114 EXPECT_ROW1_NEAR(1.0, 0.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1115 EXPECT_ROW2_NEAR(0.0,
1116 std::cos(expectedRotationAngle),
1117 -std::sin(expectedRotationAngle),
1118 0.0,
1120 ERROR_THRESHOLD);
1121 EXPECT_ROW3_NEAR(0.0,
1122 std::sin(expectedRotationAngle),
1123 std::cos(expectedRotationAngle),
1124 0.0,
1126 ERROR_THRESHOLD);
1127 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1129 to = Transform();
1130 to.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 90.0);
1131 to.Blend(from, 1.0);
1132 EXPECT_ROW1_NEAR(1.0, 0.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1133 EXPECT_ROW2_NEAR(0.0, 0.0, -1.0, 0.0, to, ERROR_THRESHOLD);
1134 EXPECT_ROW3_NEAR(0.0, 1.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1135 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1138 #if defined(_WIN64)
1139 // http://crbug.com/406574
1140 #define MAYBE_VerifyBlendForRotationAboutY DISABLED_VerifyBlendForRotationAboutY
1141 #else
1142 #define MAYBE_VerifyBlendForRotationAboutY VerifyBlendForRotationAboutY
1143 #endif
1144 TEST(XFormTest, MAYBE_VerifyBlendForRotationAboutY) {
1145 Transform from;
1146 from.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 0.0);
1148 Transform to;
1150 to.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 90.0);
1151 to.Blend(from, 0.0);
1152 EXPECT_EQ(from, to);
1154 double expectedRotationAngle = 22.5 * M_PI / 180.0;
1155 to = Transform();
1156 to.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 90.0);
1157 to.Blend(from, 0.25);
1158 EXPECT_ROW1_NEAR(std::cos(expectedRotationAngle),
1159 0.0,
1160 std::sin(expectedRotationAngle),
1161 0.0,
1163 ERROR_THRESHOLD);
1164 EXPECT_ROW2_NEAR(0.0, 1.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1165 EXPECT_ROW3_NEAR(-std::sin(expectedRotationAngle),
1166 0.0,
1167 std::cos(expectedRotationAngle),
1168 0.0,
1170 ERROR_THRESHOLD);
1171 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1173 expectedRotationAngle = 45.0 * M_PI / 180.0;
1174 to = Transform();
1175 to.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 90.0);
1176 to.Blend(from, 0.5);
1177 EXPECT_ROW1_NEAR(std::cos(expectedRotationAngle),
1178 0.0,
1179 std::sin(expectedRotationAngle),
1180 0.0,
1182 ERROR_THRESHOLD);
1183 EXPECT_ROW2_NEAR(0.0, 1.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1184 EXPECT_ROW3_NEAR(-std::sin(expectedRotationAngle),
1185 0.0,
1186 std::cos(expectedRotationAngle),
1187 0.0,
1189 ERROR_THRESHOLD);
1190 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1192 to = Transform();
1193 to.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 90.0);
1194 to.Blend(from, 1.0);
1195 EXPECT_ROW1_NEAR(0.0, 0.0, 1.0, 0.0, to, ERROR_THRESHOLD);
1196 EXPECT_ROW2_NEAR(0.0, 1.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1197 EXPECT_ROW3_NEAR(-1.0, 0.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1198 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1201 #if defined(_WIN64)
1202 // http://crbug.com/406574
1203 #define MAYBE_VerifyBlendForRotationAboutZ DISABLED_VerifyBlendForRotationAboutZ
1204 #else
1205 #define MAYBE_VerifyBlendForRotationAboutZ VerifyBlendForRotationAboutZ
1206 #endif
1207 TEST(XFormTest, MAYBE_VerifyBlendForRotationAboutZ) {
1208 Transform from;
1209 from.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 0.0);
1211 Transform to;
1213 to.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 90.0);
1214 to.Blend(from, 0.0);
1215 EXPECT_EQ(from, to);
1217 double expectedRotationAngle = 22.5 * M_PI / 180.0;
1218 to = Transform();
1219 to.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 90.0);
1220 to.Blend(from, 0.25);
1221 EXPECT_ROW1_NEAR(std::cos(expectedRotationAngle),
1222 -std::sin(expectedRotationAngle),
1223 0.0,
1224 0.0,
1226 ERROR_THRESHOLD);
1227 EXPECT_ROW2_NEAR(std::sin(expectedRotationAngle),
1228 std::cos(expectedRotationAngle),
1229 0.0,
1230 0.0,
1232 ERROR_THRESHOLD);
1233 EXPECT_ROW3_NEAR(0.0, 0.0, 1.0, 0.0, to, ERROR_THRESHOLD);
1234 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1236 expectedRotationAngle = 45.0 * M_PI / 180.0;
1237 to = Transform();
1238 to.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 90.0);
1239 to.Blend(from, 0.5);
1240 EXPECT_ROW1_NEAR(std::cos(expectedRotationAngle),
1241 -std::sin(expectedRotationAngle),
1242 0.0,
1243 0.0,
1245 ERROR_THRESHOLD);
1246 EXPECT_ROW2_NEAR(std::sin(expectedRotationAngle),
1247 std::cos(expectedRotationAngle),
1248 0.0,
1249 0.0,
1251 ERROR_THRESHOLD);
1252 EXPECT_ROW3_NEAR(0.0, 0.0, 1.0, 0.0, to, ERROR_THRESHOLD);
1253 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1255 to = Transform();
1256 to.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 90.0);
1257 to.Blend(from, 1.0);
1258 EXPECT_ROW1_NEAR(0.0, -1.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1259 EXPECT_ROW2_NEAR(1.0, 0.0, 0.0, 0.0, to, ERROR_THRESHOLD);
1260 EXPECT_ROW3_NEAR(0.0, 0.0, 1.0, 0.0, to, ERROR_THRESHOLD);
1261 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, to);
1264 TEST(XFormTest, VerifyBlendForCompositeTransform) {
1265 // Verify that the.Blending was done with a decomposition in correct order
1266 // by blending a composite transform. Using matrix x vector notation
1267 // (Ax = b, where x is column vector), the ordering should be:
1268 // perspective * translation * rotation * skew * scale
1270 // It is not as important (or meaningful) to check intermediate
1271 // interpolations; order of operations will be tested well enough by the
1272 // end cases that are easier to specify.
1274 Transform from;
1275 Transform to;
1277 Transform expectedEndOfAnimation;
1278 expectedEndOfAnimation.ApplyPerspectiveDepth(1.0);
1279 expectedEndOfAnimation.Translate3d(10.0, 20.0, 30.0);
1280 expectedEndOfAnimation.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 25.0);
1281 expectedEndOfAnimation.SkewY(45.0);
1282 expectedEndOfAnimation.Scale3d(6.0, 7.0, 8.0);
1284 to = expectedEndOfAnimation;
1285 to.Blend(from, 0.0);
1286 EXPECT_EQ(from, to);
1288 to = expectedEndOfAnimation;
1289 // We short circuit if blend is >= 1, so to check the numerics, we will
1290 // check that we get close to what we expect when we're nearly done
1291 // interpolating.
1292 to.Blend(from, .99999f);
1294 // Recomposing the matrix results in a normalized matrix, so to verify we
1295 // need to normalize the expectedEndOfAnimation before comparing elements.
1296 // Normalizing means dividing everything by expectedEndOfAnimation.m44().
1297 Transform normalizedExpectedEndOfAnimation = expectedEndOfAnimation;
1298 Transform normalizationMatrix;
1299 normalizationMatrix.matrix().set(
1300 0.0,
1301 0.0,
1302 SkDoubleToMScalar(1 / expectedEndOfAnimation.matrix().get(3.0, 3.0)));
1303 normalizationMatrix.matrix().set(
1304 1.0,
1305 1.0,
1306 SkDoubleToMScalar(1 / expectedEndOfAnimation.matrix().get(3.0, 3.0)));
1307 normalizationMatrix.matrix().set(
1308 2.0,
1309 2.0,
1310 SkDoubleToMScalar(1 / expectedEndOfAnimation.matrix().get(3.0, 3.0)));
1311 normalizationMatrix.matrix().set(
1312 3.0,
1313 3.0,
1314 SkDoubleToMScalar(1 / expectedEndOfAnimation.matrix().get(3.0, 3.0)));
1315 normalizedExpectedEndOfAnimation.PreconcatTransform(normalizationMatrix);
1317 EXPECT_TRUE(MatricesAreNearlyEqual(normalizedExpectedEndOfAnimation, to));
1320 TEST(XFormTest, DecomposedTransformCtor) {
1321 DecomposedTransform decomp;
1322 for (int i = 0; i < 3; ++i) {
1323 EXPECT_EQ(0.0, decomp.translate[i]);
1324 EXPECT_EQ(1.0, decomp.scale[i]);
1325 EXPECT_EQ(0.0, decomp.skew[i]);
1326 EXPECT_EQ(0.0, decomp.quaternion[i]);
1327 EXPECT_EQ(0.0, decomp.perspective[i]);
1329 EXPECT_EQ(1.0, decomp.quaternion[3]);
1330 EXPECT_EQ(1.0, decomp.perspective[3]);
1331 Transform identity;
1332 Transform composed = ComposeTransform(decomp);
1333 EXPECT_TRUE(MatricesAreNearlyEqual(identity, composed));
1336 TEST(XFormTest, FactorTRS) {
1337 for (int degrees = 0; degrees < 180; ++degrees) {
1338 // build a transformation matrix.
1339 gfx::Transform transform;
1340 transform.Translate(degrees * 2, -degrees * 3);
1341 transform.Rotate(degrees);
1342 transform.Scale(degrees + 1, 2 * degrees + 1);
1344 // factor the matrix
1345 DecomposedTransform decomp;
1346 bool success = DecomposeTransform(&decomp, transform);
1347 EXPECT_TRUE(success);
1348 EXPECT_FLOAT_EQ(decomp.translate[0], degrees * 2);
1349 EXPECT_FLOAT_EQ(decomp.translate[1], -degrees * 3);
1350 double rotation =
1351 std::acos(SkMScalarToDouble(decomp.quaternion[3])) * 360.0 / M_PI;
1352 while (rotation < 0.0)
1353 rotation += 360.0;
1354 while (rotation > 360.0)
1355 rotation -= 360.0;
1357 const float epsilon = 0.00015f;
1358 EXPECT_NEAR(rotation, degrees, epsilon);
1359 EXPECT_NEAR(decomp.scale[0], degrees + 1, epsilon);
1360 EXPECT_NEAR(decomp.scale[1], 2 * degrees + 1, epsilon);
1364 TEST(XFormTest, DecomposeTransform) {
1365 for (float scale = 0.001f; scale < 2.0f; scale += 0.001f) {
1366 gfx::Transform transform;
1367 transform.Scale(scale, scale);
1368 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
1370 DecomposedTransform decomp;
1371 bool success = DecomposeTransform(&decomp, transform);
1372 EXPECT_TRUE(success);
1374 gfx::Transform compose_transform = ComposeTransform(decomp);
1375 EXPECT_TRUE(compose_transform.Preserves2dAxisAlignment());
1379 TEST(XFormTest, IntegerTranslation) {
1380 gfx::Transform transform;
1381 EXPECT_TRUE(transform.IsIdentityOrIntegerTranslation());
1383 transform.Translate3d(1, 2, 3);
1384 EXPECT_TRUE(transform.IsIdentityOrIntegerTranslation());
1386 transform.MakeIdentity();
1387 transform.Translate3d(-1, -2, -3);
1388 EXPECT_TRUE(transform.IsIdentityOrIntegerTranslation());
1390 transform.MakeIdentity();
1391 transform.Translate3d(4.5f, 0, 0);
1392 EXPECT_FALSE(transform.IsIdentityOrIntegerTranslation());
1394 transform.MakeIdentity();
1395 transform.Translate3d(0, -6.7f, 0);
1396 EXPECT_FALSE(transform.IsIdentityOrIntegerTranslation());
1398 transform.MakeIdentity();
1399 transform.Translate3d(0, 0, 8.9f);
1400 EXPECT_FALSE(transform.IsIdentityOrIntegerTranslation());
1403 TEST(XFormTest, verifyMatrixInversion) {
1405 // Invert a translation
1406 gfx::Transform translation;
1407 translation.Translate3d(2.0, 3.0, 4.0);
1408 EXPECT_TRUE(translation.IsInvertible());
1410 gfx::Transform inverse_translation;
1411 bool is_invertible = translation.GetInverse(&inverse_translation);
1412 EXPECT_TRUE(is_invertible);
1413 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, -2.0f, inverse_translation);
1414 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, -3.0f, inverse_translation);
1415 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, -4.0f, inverse_translation);
1416 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, inverse_translation);
1420 // Invert a non-uniform scale
1421 gfx::Transform scale;
1422 scale.Scale3d(4.0, 10.0, 100.0);
1423 EXPECT_TRUE(scale.IsInvertible());
1425 gfx::Transform inverse_scale;
1426 bool is_invertible = scale.GetInverse(&inverse_scale);
1427 EXPECT_TRUE(is_invertible);
1428 EXPECT_ROW1_EQ(0.25f, 0.0f, 0.0f, 0.0f, inverse_scale);
1429 EXPECT_ROW2_EQ(0.0f, 0.1f, 0.0f, 0.0f, inverse_scale);
1430 EXPECT_ROW3_EQ(0.0f, 0.0f, 0.01f, 0.0f, inverse_scale);
1431 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, inverse_scale);
1435 // Try to invert a matrix that is not invertible.
1436 // The inverse() function should reset the output matrix to identity.
1437 gfx::Transform uninvertible;
1438 uninvertible.matrix().set(0, 0, 0.f);
1439 uninvertible.matrix().set(1, 1, 0.f);
1440 uninvertible.matrix().set(2, 2, 0.f);
1441 uninvertible.matrix().set(3, 3, 0.f);
1442 EXPECT_FALSE(uninvertible.IsInvertible());
1444 gfx::Transform inverse_of_uninvertible;
1446 // Add a scale just to more easily ensure that inverse_of_uninvertible is
1447 // reset to identity.
1448 inverse_of_uninvertible.Scale3d(4.0, 10.0, 100.0);
1450 bool is_invertible = uninvertible.GetInverse(&inverse_of_uninvertible);
1451 EXPECT_FALSE(is_invertible);
1452 EXPECT_TRUE(inverse_of_uninvertible.IsIdentity());
1453 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, inverse_of_uninvertible);
1454 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, inverse_of_uninvertible);
1455 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, inverse_of_uninvertible);
1456 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, inverse_of_uninvertible);
1460 TEST(XFormTest, verifyBackfaceVisibilityBasicCases) {
1461 Transform transform;
1463 transform.MakeIdentity();
1464 EXPECT_FALSE(transform.IsBackFaceVisible());
1466 transform.MakeIdentity();
1467 transform.RotateAboutYAxis(80.0);
1468 EXPECT_FALSE(transform.IsBackFaceVisible());
1470 transform.MakeIdentity();
1471 transform.RotateAboutYAxis(100.0);
1472 EXPECT_TRUE(transform.IsBackFaceVisible());
1474 // Edge case, 90 degree rotation should return false.
1475 transform.MakeIdentity();
1476 transform.RotateAboutYAxis(90.0);
1477 EXPECT_FALSE(transform.IsBackFaceVisible());
1480 TEST(XFormTest, verifyBackfaceVisibilityForPerspective) {
1481 Transform layer_space_to_projection_plane;
1483 // This tests if IsBackFaceVisible works properly under perspective
1484 // transforms. Specifically, layers that may have their back face visible in
1485 // orthographic projection, may not actually have back face visible under
1486 // perspective projection.
1488 // Case 1: Layer is rotated by slightly more than 90 degrees, at the center
1489 // of the prespective projection. In this case, the layer's back-side
1490 // is visible to the camera.
1491 layer_space_to_projection_plane.MakeIdentity();
1492 layer_space_to_projection_plane.ApplyPerspectiveDepth(1.0);
1493 layer_space_to_projection_plane.Translate3d(0.0, 0.0, 0.0);
1494 layer_space_to_projection_plane.RotateAboutYAxis(100.0);
1495 EXPECT_TRUE(layer_space_to_projection_plane.IsBackFaceVisible());
1497 // Case 2: Layer is rotated by slightly more than 90 degrees, but shifted off
1498 // to the side of the camera. Because of the wide field-of-view, the
1499 // layer's front side is still visible.
1501 // |<-- front side of layer is visible to camera
1502 // \ | /
1503 // \ | /
1504 // \| /
1505 // | /
1506 // |\ /<-- camera field of view
1507 // | \ /
1508 // back side of layer -->| \ /
1509 // \./ <-- camera origin
1511 layer_space_to_projection_plane.MakeIdentity();
1512 layer_space_to_projection_plane.ApplyPerspectiveDepth(1.0);
1513 layer_space_to_projection_plane.Translate3d(-10.0, 0.0, 0.0);
1514 layer_space_to_projection_plane.RotateAboutYAxis(100.0);
1515 EXPECT_FALSE(layer_space_to_projection_plane.IsBackFaceVisible());
1517 // Case 3: Additionally rotating the layer by 180 degrees should of course
1518 // show the opposite result of case 2.
1519 layer_space_to_projection_plane.RotateAboutYAxis(180.0);
1520 EXPECT_TRUE(layer_space_to_projection_plane.IsBackFaceVisible());
1523 TEST(XFormTest, verifyDefaultConstructorCreatesIdentityMatrix) {
1524 Transform A;
1525 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
1526 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1527 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1528 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1529 EXPECT_TRUE(A.IsIdentity());
1532 TEST(XFormTest, verifyCopyConstructor) {
1533 Transform A;
1534 InitializeTestMatrix(&A);
1536 // Copy constructor should produce exact same elements as matrix A.
1537 Transform B(A);
1538 EXPECT_ROW1_EQ(10.0f, 14.0f, 18.0f, 22.0f, B);
1539 EXPECT_ROW2_EQ(11.0f, 15.0f, 19.0f, 23.0f, B);
1540 EXPECT_ROW3_EQ(12.0f, 16.0f, 20.0f, 24.0f, B);
1541 EXPECT_ROW4_EQ(13.0f, 17.0f, 21.0f, 25.0f, B);
1544 TEST(XFormTest, verifyConstructorFor16Elements) {
1545 Transform transform(1.0, 2.0, 3.0, 4.0,
1546 5.0, 6.0, 7.0, 8.0,
1547 9.0, 10.0, 11.0, 12.0,
1548 13.0, 14.0, 15.0, 16.0);
1550 EXPECT_ROW1_EQ(1.0f, 2.0f, 3.0f, 4.0f, transform);
1551 EXPECT_ROW2_EQ(5.0f, 6.0f, 7.0f, 8.0f, transform);
1552 EXPECT_ROW3_EQ(9.0f, 10.0f, 11.0f, 12.0f, transform);
1553 EXPECT_ROW4_EQ(13.0f, 14.0f, 15.0f, 16.0f, transform);
1556 TEST(XFormTest, verifyConstructorFor2dElements) {
1557 Transform transform(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
1559 EXPECT_ROW1_EQ(1.0f, 2.0f, 0.0f, 5.0f, transform);
1560 EXPECT_ROW2_EQ(3.0f, 4.0f, 0.0f, 6.0f, transform);
1561 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, transform);
1562 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, transform);
1566 TEST(XFormTest, verifyAssignmentOperator) {
1567 Transform A;
1568 InitializeTestMatrix(&A);
1569 Transform B;
1570 InitializeTestMatrix2(&B);
1571 Transform C;
1572 InitializeTestMatrix2(&C);
1573 C = B = A;
1575 // Both B and C should now have been re-assigned to the value of A.
1576 EXPECT_ROW1_EQ(10.0f, 14.0f, 18.0f, 22.0f, B);
1577 EXPECT_ROW2_EQ(11.0f, 15.0f, 19.0f, 23.0f, B);
1578 EXPECT_ROW3_EQ(12.0f, 16.0f, 20.0f, 24.0f, B);
1579 EXPECT_ROW4_EQ(13.0f, 17.0f, 21.0f, 25.0f, B);
1581 EXPECT_ROW1_EQ(10.0f, 14.0f, 18.0f, 22.0f, C);
1582 EXPECT_ROW2_EQ(11.0f, 15.0f, 19.0f, 23.0f, C);
1583 EXPECT_ROW3_EQ(12.0f, 16.0f, 20.0f, 24.0f, C);
1584 EXPECT_ROW4_EQ(13.0f, 17.0f, 21.0f, 25.0f, C);
1587 TEST(XFormTest, verifyEqualsBooleanOperator) {
1588 Transform A;
1589 InitializeTestMatrix(&A);
1591 Transform B;
1592 InitializeTestMatrix(&B);
1593 EXPECT_TRUE(A == B);
1595 // Modifying multiple elements should cause equals operator to return false.
1596 Transform C;
1597 InitializeTestMatrix2(&C);
1598 EXPECT_FALSE(A == C);
1600 // Modifying any one individual element should cause equals operator to
1601 // return false.
1602 Transform D;
1603 D = A;
1604 D.matrix().set(0, 0, 0.f);
1605 EXPECT_FALSE(A == D);
1607 D = A;
1608 D.matrix().set(1, 0, 0.f);
1609 EXPECT_FALSE(A == D);
1611 D = A;
1612 D.matrix().set(2, 0, 0.f);
1613 EXPECT_FALSE(A == D);
1615 D = A;
1616 D.matrix().set(3, 0, 0.f);
1617 EXPECT_FALSE(A == D);
1619 D = A;
1620 D.matrix().set(0, 1, 0.f);
1621 EXPECT_FALSE(A == D);
1623 D = A;
1624 D.matrix().set(1, 1, 0.f);
1625 EXPECT_FALSE(A == D);
1627 D = A;
1628 D.matrix().set(2, 1, 0.f);
1629 EXPECT_FALSE(A == D);
1631 D = A;
1632 D.matrix().set(3, 1, 0.f);
1633 EXPECT_FALSE(A == D);
1635 D = A;
1636 D.matrix().set(0, 2, 0.f);
1637 EXPECT_FALSE(A == D);
1639 D = A;
1640 D.matrix().set(1, 2, 0.f);
1641 EXPECT_FALSE(A == D);
1643 D = A;
1644 D.matrix().set(2, 2, 0.f);
1645 EXPECT_FALSE(A == D);
1647 D = A;
1648 D.matrix().set(3, 2, 0.f);
1649 EXPECT_FALSE(A == D);
1651 D = A;
1652 D.matrix().set(0, 3, 0.f);
1653 EXPECT_FALSE(A == D);
1655 D = A;
1656 D.matrix().set(1, 3, 0.f);
1657 EXPECT_FALSE(A == D);
1659 D = A;
1660 D.matrix().set(2, 3, 0.f);
1661 EXPECT_FALSE(A == D);
1663 D = A;
1664 D.matrix().set(3, 3, 0.f);
1665 EXPECT_FALSE(A == D);
1668 TEST(XFormTest, verifyMultiplyOperator) {
1669 Transform A;
1670 InitializeTestMatrix(&A);
1672 Transform B;
1673 InitializeTestMatrix2(&B);
1675 Transform C = A * B;
1676 EXPECT_ROW1_EQ(2036.0f, 2292.0f, 2548.0f, 2804.0f, C);
1677 EXPECT_ROW2_EQ(2162.0f, 2434.0f, 2706.0f, 2978.0f, C);
1678 EXPECT_ROW3_EQ(2288.0f, 2576.0f, 2864.0f, 3152.0f, C);
1679 EXPECT_ROW4_EQ(2414.0f, 2718.0f, 3022.0f, 3326.0f, C);
1681 // Just an additional sanity check; matrix multiplication is not commutative.
1682 EXPECT_FALSE(A * B == B * A);
1685 TEST(XFormTest, verifyMultiplyAndAssignOperator) {
1686 Transform A;
1687 InitializeTestMatrix(&A);
1689 Transform B;
1690 InitializeTestMatrix2(&B);
1692 A *= B;
1693 EXPECT_ROW1_EQ(2036.0f, 2292.0f, 2548.0f, 2804.0f, A);
1694 EXPECT_ROW2_EQ(2162.0f, 2434.0f, 2706.0f, 2978.0f, A);
1695 EXPECT_ROW3_EQ(2288.0f, 2576.0f, 2864.0f, 3152.0f, A);
1696 EXPECT_ROW4_EQ(2414.0f, 2718.0f, 3022.0f, 3326.0f, A);
1698 // Just an additional sanity check; matrix multiplication is not commutative.
1699 Transform C = A;
1700 C *= B;
1701 Transform D = B;
1702 D *= A;
1703 EXPECT_FALSE(C == D);
1706 TEST(XFormTest, verifyMatrixMultiplication) {
1707 Transform A;
1708 InitializeTestMatrix(&A);
1710 Transform B;
1711 InitializeTestMatrix2(&B);
1713 A.PreconcatTransform(B);
1714 EXPECT_ROW1_EQ(2036.0f, 2292.0f, 2548.0f, 2804.0f, A);
1715 EXPECT_ROW2_EQ(2162.0f, 2434.0f, 2706.0f, 2978.0f, A);
1716 EXPECT_ROW3_EQ(2288.0f, 2576.0f, 2864.0f, 3152.0f, A);
1717 EXPECT_ROW4_EQ(2414.0f, 2718.0f, 3022.0f, 3326.0f, A);
1720 TEST(XFormTest, verifyMakeIdentiy) {
1721 Transform A;
1722 InitializeTestMatrix(&A);
1723 A.MakeIdentity();
1724 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
1725 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1726 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1727 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1728 EXPECT_TRUE(A.IsIdentity());
1731 TEST(XFormTest, verifyTranslate) {
1732 Transform A;
1733 A.Translate(2.0, 3.0);
1734 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 2.0f, A);
1735 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 3.0f, A);
1736 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1737 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1739 // Verify that Translate() post-multiplies the existing matrix.
1740 A.MakeIdentity();
1741 A.Scale(5.0, 5.0);
1742 A.Translate(2.0, 3.0);
1743 EXPECT_ROW1_EQ(5.0f, 0.0f, 0.0f, 10.0f, A);
1744 EXPECT_ROW2_EQ(0.0f, 5.0f, 0.0f, 15.0f, A);
1745 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1746 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1749 TEST(XFormTest, verifyTranslate3d) {
1750 Transform A;
1751 A.Translate3d(2.0, 3.0, 4.0);
1752 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 2.0f, A);
1753 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 3.0f, A);
1754 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 4.0f, A);
1755 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1757 // Verify that Translate3d() post-multiplies the existing matrix.
1758 A.MakeIdentity();
1759 A.Scale3d(6.0, 7.0, 8.0);
1760 A.Translate3d(2.0, 3.0, 4.0);
1761 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 12.0f, A);
1762 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 21.0f, A);
1763 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 32.0f, A);
1764 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1767 TEST(XFormTest, verifyScale) {
1768 Transform A;
1769 A.Scale(6.0, 7.0);
1770 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 0.0f, A);
1771 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 0.0f, A);
1772 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1773 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1775 // Verify that Scale() post-multiplies the existing matrix.
1776 A.MakeIdentity();
1777 A.Translate3d(2.0, 3.0, 4.0);
1778 A.Scale(6.0, 7.0);
1779 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 2.0f, A);
1780 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 3.0f, A);
1781 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 4.0f, A);
1782 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1785 TEST(XFormTest, verifyScale3d) {
1786 Transform A;
1787 A.Scale3d(6.0, 7.0, 8.0);
1788 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 0.0f, A);
1789 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 0.0f, A);
1790 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
1791 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1793 // Verify that scale3d() post-multiplies the existing matrix.
1794 A.MakeIdentity();
1795 A.Translate3d(2.0, 3.0, 4.0);
1796 A.Scale3d(6.0, 7.0, 8.0);
1797 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 2.0f, A);
1798 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 3.0f, A);
1799 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 4.0f, A);
1800 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1803 TEST(XFormTest, verifyRotate) {
1804 Transform A;
1805 A.Rotate(90.0);
1806 EXPECT_ROW1_NEAR(0.0, -1.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1807 EXPECT_ROW2_NEAR(1.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1808 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1809 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1811 // Verify that Rotate() post-multiplies the existing matrix.
1812 A.MakeIdentity();
1813 A.Scale3d(6.0, 7.0, 8.0);
1814 A.Rotate(90.0);
1815 EXPECT_ROW1_NEAR(0.0, -6.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1816 EXPECT_ROW2_NEAR(7.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1817 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
1818 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1821 TEST(XFormTest, verifyRotateAboutXAxis) {
1822 Transform A;
1823 double sin45 = 0.5 * sqrt(2.0);
1824 double cos45 = sin45;
1826 A.MakeIdentity();
1827 A.RotateAboutXAxis(90.0);
1828 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
1829 EXPECT_ROW2_NEAR(0.0, 0.0, -1.0, 0.0, A, ERROR_THRESHOLD);
1830 EXPECT_ROW3_NEAR(0.0, 1.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1831 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1833 A.MakeIdentity();
1834 A.RotateAboutXAxis(45.0);
1835 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
1836 EXPECT_ROW2_NEAR(0.0, cos45, -sin45, 0.0, A, ERROR_THRESHOLD);
1837 EXPECT_ROW3_NEAR(0.0, sin45, cos45, 0.0, A, ERROR_THRESHOLD);
1838 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1840 // Verify that RotateAboutXAxis(angle) post-multiplies the existing matrix.
1841 A.MakeIdentity();
1842 A.Scale3d(6.0, 7.0, 8.0);
1843 A.RotateAboutXAxis(90.0);
1844 EXPECT_ROW1_NEAR(6.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1845 EXPECT_ROW2_NEAR(0.0, 0.0, -7.0, 0.0, A, ERROR_THRESHOLD);
1846 EXPECT_ROW3_NEAR(0.0, 8.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1847 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1850 TEST(XFormTest, verifyRotateAboutYAxis) {
1851 Transform A;
1852 double sin45 = 0.5 * sqrt(2.0);
1853 double cos45 = sin45;
1855 // Note carefully, the expected pattern is inverted compared to rotating
1856 // about x axis or z axis.
1857 A.MakeIdentity();
1858 A.RotateAboutYAxis(90.0);
1859 EXPECT_ROW1_NEAR(0.0, 0.0, 1.0, 0.0, A, ERROR_THRESHOLD);
1860 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1861 EXPECT_ROW3_NEAR(-1.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1862 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1864 A.MakeIdentity();
1865 A.RotateAboutYAxis(45.0);
1866 EXPECT_ROW1_NEAR(cos45, 0.0, sin45, 0.0, A, ERROR_THRESHOLD);
1867 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1868 EXPECT_ROW3_NEAR(-sin45, 0.0, cos45, 0.0, A, ERROR_THRESHOLD);
1869 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1871 // Verify that RotateAboutYAxis(angle) post-multiplies the existing matrix.
1872 A.MakeIdentity();
1873 A.Scale3d(6.0, 7.0, 8.0);
1874 A.RotateAboutYAxis(90.0);
1875 EXPECT_ROW1_NEAR(0.0, 0.0, 6.0, 0.0, A, ERROR_THRESHOLD);
1876 EXPECT_ROW2_NEAR(0.0, 7.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1877 EXPECT_ROW3_NEAR(-8.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1878 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1881 TEST(XFormTest, verifyRotateAboutZAxis) {
1882 Transform A;
1883 double sin45 = 0.5 * sqrt(2.0);
1884 double cos45 = sin45;
1886 A.MakeIdentity();
1887 A.RotateAboutZAxis(90.0);
1888 EXPECT_ROW1_NEAR(0.0, -1.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1889 EXPECT_ROW2_NEAR(1.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1890 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1891 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1893 A.MakeIdentity();
1894 A.RotateAboutZAxis(45.0);
1895 EXPECT_ROW1_NEAR(cos45, -sin45, 0.0, 0.0, A, ERROR_THRESHOLD);
1896 EXPECT_ROW2_NEAR(sin45, cos45, 0.0, 0.0, A, ERROR_THRESHOLD);
1897 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1898 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1900 // Verify that RotateAboutZAxis(angle) post-multiplies the existing matrix.
1901 A.MakeIdentity();
1902 A.Scale3d(6.0, 7.0, 8.0);
1903 A.RotateAboutZAxis(90.0);
1904 EXPECT_ROW1_NEAR(0.0, -6.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1905 EXPECT_ROW2_NEAR(7.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1906 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
1907 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1910 TEST(XFormTest, verifyRotateAboutForAlignedAxes) {
1911 Transform A;
1913 // Check rotation about z-axis
1914 A.MakeIdentity();
1915 A.RotateAbout(Vector3dF(0.0, 0.0, 1.0), 90.0);
1916 EXPECT_ROW1_NEAR(0.0, -1.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1917 EXPECT_ROW2_NEAR(1.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1918 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1919 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1921 // Check rotation about x-axis
1922 A.MakeIdentity();
1923 A.RotateAbout(Vector3dF(1.0, 0.0, 0.0), 90.0);
1924 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
1925 EXPECT_ROW2_NEAR(0.0, 0.0, -1.0, 0.0, A, ERROR_THRESHOLD);
1926 EXPECT_ROW3_NEAR(0.0, 1.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1927 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1929 // Check rotation about y-axis. Note carefully, the expected pattern is
1930 // inverted compared to rotating about x axis or z axis.
1931 A.MakeIdentity();
1932 A.RotateAbout(Vector3dF(0.0, 1.0, 0.0), 90.0);
1933 EXPECT_ROW1_NEAR(0.0, 0.0, 1.0, 0.0, A, ERROR_THRESHOLD);
1934 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1935 EXPECT_ROW3_NEAR(-1.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1936 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1938 // Verify that rotate3d(axis, angle) post-multiplies the existing matrix.
1939 A.MakeIdentity();
1940 A.Scale3d(6.0, 7.0, 8.0);
1941 A.RotateAboutZAxis(90.0);
1942 EXPECT_ROW1_NEAR(0.0, -6.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1943 EXPECT_ROW2_NEAR(7.0, 0.0, 0.0, 0.0, A, ERROR_THRESHOLD);
1944 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
1945 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1948 TEST(XFormTest, verifyRotateAboutForArbitraryAxis) {
1949 // Check rotation about an arbitrary non-axis-aligned vector.
1950 Transform A;
1951 A.RotateAbout(Vector3dF(1.0, 1.0, 1.0), 90.0);
1952 EXPECT_ROW1_NEAR(0.3333333333333334258519187,
1953 -0.2440169358562924717404030,
1954 0.9106836025229592124219380,
1955 0.0, A, ERROR_THRESHOLD);
1956 EXPECT_ROW2_NEAR(0.9106836025229592124219380,
1957 0.3333333333333334258519187,
1958 -0.2440169358562924717404030,
1959 0.0, A, ERROR_THRESHOLD);
1960 EXPECT_ROW3_NEAR(-0.2440169358562924717404030,
1961 0.9106836025229592124219380,
1962 0.3333333333333334258519187,
1963 0.0, A, ERROR_THRESHOLD);
1964 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1967 TEST(XFormTest, verifyRotateAboutForDegenerateAxis) {
1968 // Check rotation about a degenerate zero vector.
1969 // It is expected to skip applying the rotation.
1970 Transform A;
1972 A.RotateAbout(Vector3dF(0.0, 0.0, 0.0), 45.0);
1973 // Verify that A remains unchanged.
1974 EXPECT_TRUE(A.IsIdentity());
1976 InitializeTestMatrix(&A);
1977 A.RotateAbout(Vector3dF(0.0, 0.0, 0.0), 35.0);
1979 // Verify that A remains unchanged.
1980 EXPECT_ROW1_EQ(10.0f, 14.0f, 18.0f, 22.0f, A);
1981 EXPECT_ROW2_EQ(11.0f, 15.0f, 19.0f, 23.0f, A);
1982 EXPECT_ROW3_EQ(12.0f, 16.0f, 20.0f, 24.0f, A);
1983 EXPECT_ROW4_EQ(13.0f, 17.0f, 21.0f, 25.0f, A);
1986 TEST(XFormTest, verifySkewX) {
1987 Transform A;
1988 A.SkewX(45.0);
1989 EXPECT_ROW1_EQ(1.0f, 1.0f, 0.0f, 0.0f, A);
1990 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
1991 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
1992 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
1994 // Verify that skewX() post-multiplies the existing matrix. Row 1, column 2,
1995 // would incorrectly have value "7" if the matrix is pre-multiplied instead
1996 // of post-multiplied.
1997 A.MakeIdentity();
1998 A.Scale3d(6.0, 7.0, 8.0);
1999 A.SkewX(45.0);
2000 EXPECT_ROW1_EQ(6.0f, 6.0f, 0.0f, 0.0f, A);
2001 EXPECT_ROW2_EQ(0.0f, 7.0f, 0.0f, 0.0f, A);
2002 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
2003 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
2006 TEST(XFormTest, verifySkewY) {
2007 Transform A;
2008 A.SkewY(45.0);
2009 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
2010 EXPECT_ROW2_EQ(1.0f, 1.0f, 0.0f, 0.0f, A);
2011 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
2012 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
2014 // Verify that skewY() post-multiplies the existing matrix. Row 2, column 1 ,
2015 // would incorrectly have value "6" if the matrix is pre-multiplied instead
2016 // of post-multiplied.
2017 A.MakeIdentity();
2018 A.Scale3d(6.0, 7.0, 8.0);
2019 A.SkewY(45.0);
2020 EXPECT_ROW1_EQ(6.0f, 0.0f, 0.0f, 0.0f, A);
2021 EXPECT_ROW2_EQ(7.0f, 7.0f, 0.0f, 0.0f, A);
2022 EXPECT_ROW3_EQ(0.0f, 0.0f, 8.0f, 0.0f, A);
2023 EXPECT_ROW4_EQ(0.0f, 0.0f, 0.0f, 1.0f, A);
2026 TEST(XFormTest, verifyPerspectiveDepth) {
2027 Transform A;
2028 A.ApplyPerspectiveDepth(1.0);
2029 EXPECT_ROW1_EQ(1.0f, 0.0f, 0.0f, 0.0f, A);
2030 EXPECT_ROW2_EQ(0.0f, 1.0f, 0.0f, 0.0f, A);
2031 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
2032 EXPECT_ROW4_EQ(0.0f, 0.0f, -1.0f, 1.0f, A);
2034 // Verify that PerspectiveDepth() post-multiplies the existing matrix.
2035 A.MakeIdentity();
2036 A.Translate3d(2.0, 3.0, 4.0);
2037 A.ApplyPerspectiveDepth(1.0);
2038 EXPECT_ROW1_EQ(1.0f, 0.0f, -2.0f, 2.0f, A);
2039 EXPECT_ROW2_EQ(0.0f, 1.0f, -3.0f, 3.0f, A);
2040 EXPECT_ROW3_EQ(0.0f, 0.0f, -3.0f, 4.0f, A);
2041 EXPECT_ROW4_EQ(0.0f, 0.0f, -1.0f, 1.0f, A);
2044 TEST(XFormTest, verifyHasPerspective) {
2045 Transform A;
2046 A.ApplyPerspectiveDepth(1.0);
2047 EXPECT_TRUE(A.HasPerspective());
2049 A.MakeIdentity();
2050 A.ApplyPerspectiveDepth(0.0);
2051 EXPECT_FALSE(A.HasPerspective());
2053 A.MakeIdentity();
2054 A.matrix().set(3, 0, -1.f);
2055 EXPECT_TRUE(A.HasPerspective());
2057 A.MakeIdentity();
2058 A.matrix().set(3, 1, -1.f);
2059 EXPECT_TRUE(A.HasPerspective());
2061 A.MakeIdentity();
2062 A.matrix().set(3, 2, -0.3f);
2063 EXPECT_TRUE(A.HasPerspective());
2065 A.MakeIdentity();
2066 A.matrix().set(3, 3, 0.5f);
2067 EXPECT_TRUE(A.HasPerspective());
2069 A.MakeIdentity();
2070 A.matrix().set(3, 3, 0.f);
2071 EXPECT_TRUE(A.HasPerspective());
2074 TEST(XFormTest, verifyIsInvertible) {
2075 Transform A;
2077 // Translations, rotations, scales, skews and arbitrary combinations of them
2078 // are invertible.
2079 A.MakeIdentity();
2080 EXPECT_TRUE(A.IsInvertible());
2082 A.MakeIdentity();
2083 A.Translate3d(2.0, 3.0, 4.0);
2084 EXPECT_TRUE(A.IsInvertible());
2086 A.MakeIdentity();
2087 A.Scale3d(6.0, 7.0, 8.0);
2088 EXPECT_TRUE(A.IsInvertible());
2090 A.MakeIdentity();
2091 A.RotateAboutXAxis(10.0);
2092 A.RotateAboutYAxis(20.0);
2093 A.RotateAboutZAxis(30.0);
2094 EXPECT_TRUE(A.IsInvertible());
2096 A.MakeIdentity();
2097 A.SkewX(45.0);
2098 EXPECT_TRUE(A.IsInvertible());
2100 // A perspective matrix (projection plane at z=0) is invertible. The
2101 // intuitive explanation is that perspective is eqivalent to a skew of the
2102 // w-axis; skews are invertible.
2103 A.MakeIdentity();
2104 A.ApplyPerspectiveDepth(1.0);
2105 EXPECT_TRUE(A.IsInvertible());
2107 // A "pure" perspective matrix derived by similar triangles, with m44() set
2108 // to zero (i.e. camera positioned at the origin), is not invertible.
2109 A.MakeIdentity();
2110 A.ApplyPerspectiveDepth(1.0);
2111 A.matrix().set(3, 3, 0.f);
2112 EXPECT_FALSE(A.IsInvertible());
2114 // Adding more to a non-invertible matrix will not make it invertible in the
2115 // general case.
2116 A.MakeIdentity();
2117 A.ApplyPerspectiveDepth(1.0);
2118 A.matrix().set(3, 3, 0.f);
2119 A.Scale3d(6.0, 7.0, 8.0);
2120 A.RotateAboutXAxis(10.0);
2121 A.RotateAboutYAxis(20.0);
2122 A.RotateAboutZAxis(30.0);
2123 A.Translate3d(6.0, 7.0, 8.0);
2124 EXPECT_FALSE(A.IsInvertible());
2126 // A degenerate matrix of all zeros is not invertible.
2127 A.MakeIdentity();
2128 A.matrix().set(0, 0, 0.f);
2129 A.matrix().set(1, 1, 0.f);
2130 A.matrix().set(2, 2, 0.f);
2131 A.matrix().set(3, 3, 0.f);
2132 EXPECT_FALSE(A.IsInvertible());
2135 TEST(XFormTest, verifyIsIdentity) {
2136 Transform A;
2138 InitializeTestMatrix(&A);
2139 EXPECT_FALSE(A.IsIdentity());
2141 A.MakeIdentity();
2142 EXPECT_TRUE(A.IsIdentity());
2144 // Modifying any one individual element should cause the matrix to no longer
2145 // be identity.
2146 A.MakeIdentity();
2147 A.matrix().set(0, 0, 2.f);
2148 EXPECT_FALSE(A.IsIdentity());
2150 A.MakeIdentity();
2151 A.matrix().set(1, 0, 2.f);
2152 EXPECT_FALSE(A.IsIdentity());
2154 A.MakeIdentity();
2155 A.matrix().set(2, 0, 2.f);
2156 EXPECT_FALSE(A.IsIdentity());
2158 A.MakeIdentity();
2159 A.matrix().set(3, 0, 2.f);
2160 EXPECT_FALSE(A.IsIdentity());
2162 A.MakeIdentity();
2163 A.matrix().set(0, 1, 2.f);
2164 EXPECT_FALSE(A.IsIdentity());
2166 A.MakeIdentity();
2167 A.matrix().set(1, 1, 2.f);
2168 EXPECT_FALSE(A.IsIdentity());
2170 A.MakeIdentity();
2171 A.matrix().set(2, 1, 2.f);
2172 EXPECT_FALSE(A.IsIdentity());
2174 A.MakeIdentity();
2175 A.matrix().set(3, 1, 2.f);
2176 EXPECT_FALSE(A.IsIdentity());
2178 A.MakeIdentity();
2179 A.matrix().set(0, 2, 2.f);
2180 EXPECT_FALSE(A.IsIdentity());
2182 A.MakeIdentity();
2183 A.matrix().set(1, 2, 2.f);
2184 EXPECT_FALSE(A.IsIdentity());
2186 A.MakeIdentity();
2187 A.matrix().set(2, 2, 2.f);
2188 EXPECT_FALSE(A.IsIdentity());
2190 A.MakeIdentity();
2191 A.matrix().set(3, 2, 2.f);
2192 EXPECT_FALSE(A.IsIdentity());
2194 A.MakeIdentity();
2195 A.matrix().set(0, 3, 2.f);
2196 EXPECT_FALSE(A.IsIdentity());
2198 A.MakeIdentity();
2199 A.matrix().set(1, 3, 2.f);
2200 EXPECT_FALSE(A.IsIdentity());
2202 A.MakeIdentity();
2203 A.matrix().set(2, 3, 2.f);
2204 EXPECT_FALSE(A.IsIdentity());
2206 A.MakeIdentity();
2207 A.matrix().set(3, 3, 2.f);
2208 EXPECT_FALSE(A.IsIdentity());
2211 TEST(XFormTest, verifyIsIdentityOrTranslation) {
2212 Transform A;
2214 InitializeTestMatrix(&A);
2215 EXPECT_FALSE(A.IsIdentityOrTranslation());
2217 A.MakeIdentity();
2218 EXPECT_TRUE(A.IsIdentityOrTranslation());
2220 // Modifying any non-translation components should cause
2221 // IsIdentityOrTranslation() to return false. NOTE: (0, 3), (1, 3), and
2222 // (2, 3) are the translation components, so modifying them should still
2223 // return true.
2224 A.MakeIdentity();
2225 A.matrix().set(0, 0, 2.f);
2226 EXPECT_FALSE(A.IsIdentityOrTranslation());
2228 A.MakeIdentity();
2229 A.matrix().set(1, 0, 2.f);
2230 EXPECT_FALSE(A.IsIdentityOrTranslation());
2232 A.MakeIdentity();
2233 A.matrix().set(2, 0, 2.f);
2234 EXPECT_FALSE(A.IsIdentityOrTranslation());
2236 A.MakeIdentity();
2237 A.matrix().set(3, 0, 2.f);
2238 EXPECT_FALSE(A.IsIdentityOrTranslation());
2240 A.MakeIdentity();
2241 A.matrix().set(0, 1, 2.f);
2242 EXPECT_FALSE(A.IsIdentityOrTranslation());
2244 A.MakeIdentity();
2245 A.matrix().set(1, 1, 2.f);
2246 EXPECT_FALSE(A.IsIdentityOrTranslation());
2248 A.MakeIdentity();
2249 A.matrix().set(2, 1, 2.f);
2250 EXPECT_FALSE(A.IsIdentityOrTranslation());
2252 A.MakeIdentity();
2253 A.matrix().set(3, 1, 2.f);
2254 EXPECT_FALSE(A.IsIdentityOrTranslation());
2256 A.MakeIdentity();
2257 A.matrix().set(0, 2, 2.f);
2258 EXPECT_FALSE(A.IsIdentityOrTranslation());
2260 A.MakeIdentity();
2261 A.matrix().set(1, 2, 2.f);
2262 EXPECT_FALSE(A.IsIdentityOrTranslation());
2264 A.MakeIdentity();
2265 A.matrix().set(2, 2, 2.f);
2266 EXPECT_FALSE(A.IsIdentityOrTranslation());
2268 A.MakeIdentity();
2269 A.matrix().set(3, 2, 2.f);
2270 EXPECT_FALSE(A.IsIdentityOrTranslation());
2272 // Note carefully - expecting true here.
2273 A.MakeIdentity();
2274 A.matrix().set(0, 3, 2.f);
2275 EXPECT_TRUE(A.IsIdentityOrTranslation());
2277 // Note carefully - expecting true here.
2278 A.MakeIdentity();
2279 A.matrix().set(1, 3, 2.f);
2280 EXPECT_TRUE(A.IsIdentityOrTranslation());
2282 // Note carefully - expecting true here.
2283 A.MakeIdentity();
2284 A.matrix().set(2, 3, 2.f);
2285 EXPECT_TRUE(A.IsIdentityOrTranslation());
2287 A.MakeIdentity();
2288 A.matrix().set(3, 3, 2.f);
2289 EXPECT_FALSE(A.IsIdentityOrTranslation());
2292 TEST(XFormTest, verifyIsApproximatelyIdentityOrTranslation) {
2293 Transform A;
2294 SkMatrix44& matrix = A.matrix();
2296 // Exact pure translation.
2297 A.MakeIdentity();
2299 // Set translate values to values other than 0 or 1.
2300 matrix.set(0, 3, 3.4f);
2301 matrix.set(1, 3, 4.4f);
2302 matrix.set(2, 3, 5.6f);
2304 EXPECT_TRUE(A.IsApproximatelyIdentityOrTranslation(0));
2305 EXPECT_TRUE(A.IsApproximatelyIdentityOrTranslation(kApproxZero));
2307 // Approximately pure translation.
2308 InitializeApproxIdentityMatrix(&A);
2310 // Some values must be exact.
2311 matrix.set(3, 0, 0);
2312 matrix.set(3, 1, 0);
2313 matrix.set(3, 2, 0);
2314 matrix.set(3, 3, 1);
2316 // Set translate values to values other than 0 or 1.
2317 matrix.set(0, 3, 3.4f);
2318 matrix.set(1, 3, 4.4f);
2319 matrix.set(2, 3, 5.6f);
2321 EXPECT_FALSE(A.IsApproximatelyIdentityOrTranslation(0));
2322 EXPECT_TRUE(A.IsApproximatelyIdentityOrTranslation(kApproxZero));
2324 // Not approximately pure translation.
2325 InitializeApproxIdentityMatrix(&A);
2327 // Some values must be exact.
2328 matrix.set(3, 0, 0);
2329 matrix.set(3, 1, 0);
2330 matrix.set(3, 2, 0);
2331 matrix.set(3, 3, 1);
2333 // Set some values (not translate values) to values other than 0 or 1.
2334 matrix.set(0, 1, 3.4f);
2335 matrix.set(3, 2, 4.4f);
2336 matrix.set(2, 0, 5.6f);
2338 EXPECT_FALSE(A.IsApproximatelyIdentityOrTranslation(0));
2339 EXPECT_FALSE(A.IsApproximatelyIdentityOrTranslation(kApproxZero));
2342 TEST(XFormTest, verifyIsScaleOrTranslation) {
2343 Transform A;
2345 InitializeTestMatrix(&A);
2346 EXPECT_FALSE(A.IsScaleOrTranslation());
2348 A.MakeIdentity();
2349 EXPECT_TRUE(A.IsScaleOrTranslation());
2351 // Modifying any non-scale or non-translation components should cause
2352 // IsScaleOrTranslation() to return false. (0, 0), (1, 1), (2, 2), (0, 3),
2353 // (1, 3), and (2, 3) are the scale and translation components, so
2354 // modifying them should still return true.
2356 // Note carefully - expecting true here.
2357 A.MakeIdentity();
2358 A.matrix().set(0, 0, 2.f);
2359 EXPECT_TRUE(A.IsScaleOrTranslation());
2361 A.MakeIdentity();
2362 A.matrix().set(1, 0, 2.f);
2363 EXPECT_FALSE(A.IsScaleOrTranslation());
2365 A.MakeIdentity();
2366 A.matrix().set(2, 0, 2.f);
2367 EXPECT_FALSE(A.IsScaleOrTranslation());
2369 A.MakeIdentity();
2370 A.matrix().set(3, 0, 2.f);
2371 EXPECT_FALSE(A.IsScaleOrTranslation());
2373 A.MakeIdentity();
2374 A.matrix().set(0, 1, 2.f);
2375 EXPECT_FALSE(A.IsScaleOrTranslation());
2377 // Note carefully - expecting true here.
2378 A.MakeIdentity();
2379 A.matrix().set(1, 1, 2.f);
2380 EXPECT_TRUE(A.IsScaleOrTranslation());
2382 A.MakeIdentity();
2383 A.matrix().set(2, 1, 2.f);
2384 EXPECT_FALSE(A.IsScaleOrTranslation());
2386 A.MakeIdentity();
2387 A.matrix().set(3, 1, 2.f);
2388 EXPECT_FALSE(A.IsScaleOrTranslation());
2390 A.MakeIdentity();
2391 A.matrix().set(0, 2, 2.f);
2392 EXPECT_FALSE(A.IsScaleOrTranslation());
2394 A.MakeIdentity();
2395 A.matrix().set(1, 2, 2.f);
2396 EXPECT_FALSE(A.IsScaleOrTranslation());
2398 // Note carefully - expecting true here.
2399 A.MakeIdentity();
2400 A.matrix().set(2, 2, 2.f);
2401 EXPECT_TRUE(A.IsScaleOrTranslation());
2403 A.MakeIdentity();
2404 A.matrix().set(3, 2, 2.f);
2405 EXPECT_FALSE(A.IsScaleOrTranslation());
2407 // Note carefully - expecting true here.
2408 A.MakeIdentity();
2409 A.matrix().set(0, 3, 2.f);
2410 EXPECT_TRUE(A.IsScaleOrTranslation());
2412 // Note carefully - expecting true here.
2413 A.MakeIdentity();
2414 A.matrix().set(1, 3, 2.f);
2415 EXPECT_TRUE(A.IsScaleOrTranslation());
2417 // Note carefully - expecting true here.
2418 A.MakeIdentity();
2419 A.matrix().set(2, 3, 2.f);
2420 EXPECT_TRUE(A.IsScaleOrTranslation());
2422 A.MakeIdentity();
2423 A.matrix().set(3, 3, 2.f);
2424 EXPECT_FALSE(A.IsScaleOrTranslation());
2427 TEST(XFormTest, verifyFlattenTo2d) {
2428 Transform A;
2429 InitializeTestMatrix(&A);
2431 A.FlattenTo2d();
2432 EXPECT_ROW1_EQ(10.0f, 14.0f, 0.0f, 22.0f, A);
2433 EXPECT_ROW2_EQ(11.0f, 15.0f, 0.0f, 23.0f, A);
2434 EXPECT_ROW3_EQ(0.0f, 0.0f, 1.0f, 0.0f, A);
2435 EXPECT_ROW4_EQ(13.0f, 17.0f, 0.0f, 25.0f, A);
2438 // Another implementation of Preserves2dAxisAlignment that isn't as fast,
2439 // good for testing the faster implementation.
2440 static bool EmpiricallyPreserves2dAxisAlignment(const Transform& transform) {
2441 Point3F p1(5.0f, 5.0f, 0.0f);
2442 Point3F p2(10.0f, 5.0f, 0.0f);
2443 Point3F p3(10.0f, 20.0f, 0.0f);
2444 Point3F p4(5.0f, 20.0f, 0.0f);
2446 QuadF test_quad(PointF(p1.x(), p1.y()),
2447 PointF(p2.x(), p2.y()),
2448 PointF(p3.x(), p3.y()),
2449 PointF(p4.x(), p4.y()));
2450 EXPECT_TRUE(test_quad.IsRectilinear());
2452 transform.TransformPoint(&p1);
2453 transform.TransformPoint(&p2);
2454 transform.TransformPoint(&p3);
2455 transform.TransformPoint(&p4);
2457 QuadF transformedQuad(PointF(p1.x(), p1.y()),
2458 PointF(p2.x(), p2.y()),
2459 PointF(p3.x(), p3.y()),
2460 PointF(p4.x(), p4.y()));
2461 return transformedQuad.IsRectilinear();
2464 TEST(XFormTest, Preserves2dAxisAlignment) {
2465 static const struct TestCase {
2466 SkMScalar a; // row 1, column 1
2467 SkMScalar b; // row 1, column 2
2468 SkMScalar c; // row 2, column 1
2469 SkMScalar d; // row 2, column 2
2470 bool expected;
2471 } test_cases[] = {
2472 { 3.f, 0.f,
2473 0.f, 4.f, true }, // basic case
2474 { 0.f, 4.f,
2475 3.f, 0.f, true }, // rotate by 90
2476 { 0.f, 0.f,
2477 0.f, 4.f, true }, // degenerate x
2478 { 3.f, 0.f,
2479 0.f, 0.f, true }, // degenerate y
2480 { 0.f, 0.f,
2481 3.f, 0.f, true }, // degenerate x + rotate by 90
2482 { 0.f, 4.f,
2483 0.f, 0.f, true }, // degenerate y + rotate by 90
2484 { 3.f, 4.f,
2485 0.f, 0.f, false },
2486 { 0.f, 0.f,
2487 3.f, 4.f, false },
2488 { 0.f, 3.f,
2489 0.f, 4.f, false },
2490 { 3.f, 0.f,
2491 4.f, 0.f, false },
2492 { 3.f, 4.f,
2493 5.f, 0.f, false },
2494 { 3.f, 4.f,
2495 0.f, 5.f, false },
2496 { 3.f, 0.f,
2497 4.f, 5.f, false },
2498 { 0.f, 3.f,
2499 4.f, 5.f, false },
2500 { 2.f, 3.f,
2501 4.f, 5.f, false },
2504 Transform transform;
2505 for (size_t i = 0; i < arraysize(test_cases); ++i) {
2506 const TestCase& value = test_cases[i];
2507 transform.MakeIdentity();
2508 transform.matrix().set(0, 0, value.a);
2509 transform.matrix().set(0, 1, value.b);
2510 transform.matrix().set(1, 0, value.c);
2511 transform.matrix().set(1, 1, value.d);
2513 if (value.expected) {
2514 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2515 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2516 } else {
2517 EXPECT_FALSE(EmpiricallyPreserves2dAxisAlignment(transform));
2518 EXPECT_FALSE(transform.Preserves2dAxisAlignment());
2522 // Try the same test cases again, but this time make sure that other matrix
2523 // elements (except perspective) have entries, to test that they are ignored.
2524 for (size_t i = 0; i < arraysize(test_cases); ++i) {
2525 const TestCase& value = test_cases[i];
2526 transform.MakeIdentity();
2527 transform.matrix().set(0, 0, value.a);
2528 transform.matrix().set(0, 1, value.b);
2529 transform.matrix().set(1, 0, value.c);
2530 transform.matrix().set(1, 1, value.d);
2532 transform.matrix().set(0, 2, 1.f);
2533 transform.matrix().set(0, 3, 2.f);
2534 transform.matrix().set(1, 2, 3.f);
2535 transform.matrix().set(1, 3, 4.f);
2536 transform.matrix().set(2, 0, 5.f);
2537 transform.matrix().set(2, 1, 6.f);
2538 transform.matrix().set(2, 2, 7.f);
2539 transform.matrix().set(2, 3, 8.f);
2541 if (value.expected) {
2542 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2543 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2544 } else {
2545 EXPECT_FALSE(EmpiricallyPreserves2dAxisAlignment(transform));
2546 EXPECT_FALSE(transform.Preserves2dAxisAlignment());
2550 // Try the same test cases again, but this time add perspective which is
2551 // always assumed to not-preserve axis alignment.
2552 for (size_t i = 0; i < arraysize(test_cases); ++i) {
2553 const TestCase& value = test_cases[i];
2554 transform.MakeIdentity();
2555 transform.matrix().set(0, 0, value.a);
2556 transform.matrix().set(0, 1, value.b);
2557 transform.matrix().set(1, 0, value.c);
2558 transform.matrix().set(1, 1, value.d);
2560 transform.matrix().set(0, 2, 1.f);
2561 transform.matrix().set(0, 3, 2.f);
2562 transform.matrix().set(1, 2, 3.f);
2563 transform.matrix().set(1, 3, 4.f);
2564 transform.matrix().set(2, 0, 5.f);
2565 transform.matrix().set(2, 1, 6.f);
2566 transform.matrix().set(2, 2, 7.f);
2567 transform.matrix().set(2, 3, 8.f);
2568 transform.matrix().set(3, 0, 9.f);
2569 transform.matrix().set(3, 1, 10.f);
2570 transform.matrix().set(3, 2, 11.f);
2571 transform.matrix().set(3, 3, 12.f);
2573 EXPECT_FALSE(EmpiricallyPreserves2dAxisAlignment(transform));
2574 EXPECT_FALSE(transform.Preserves2dAxisAlignment());
2577 // Try a few more practical situations to check precision
2578 transform.MakeIdentity();
2579 transform.RotateAboutZAxis(90.0);
2580 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2581 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2583 transform.MakeIdentity();
2584 transform.RotateAboutZAxis(180.0);
2585 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2586 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2588 transform.MakeIdentity();
2589 transform.RotateAboutZAxis(270.0);
2590 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2591 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2593 transform.MakeIdentity();
2594 transform.RotateAboutYAxis(90.0);
2595 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2596 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2598 transform.MakeIdentity();
2599 transform.RotateAboutXAxis(90.0);
2600 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2601 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2603 transform.MakeIdentity();
2604 transform.RotateAboutZAxis(90.0);
2605 transform.RotateAboutYAxis(90.0);
2606 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2607 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2609 transform.MakeIdentity();
2610 transform.RotateAboutZAxis(90.0);
2611 transform.RotateAboutXAxis(90.0);
2612 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2613 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2615 transform.MakeIdentity();
2616 transform.RotateAboutYAxis(90.0);
2617 transform.RotateAboutZAxis(90.0);
2618 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2619 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2621 transform.MakeIdentity();
2622 transform.RotateAboutZAxis(45.0);
2623 EXPECT_FALSE(EmpiricallyPreserves2dAxisAlignment(transform));
2624 EXPECT_FALSE(transform.Preserves2dAxisAlignment());
2626 // 3-d case; In 2d after an orthographic projection, this case does
2627 // preserve 2d axis alignment. But in 3d, it does not preserve axis
2628 // alignment.
2629 transform.MakeIdentity();
2630 transform.RotateAboutYAxis(45.0);
2631 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2632 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2634 transform.MakeIdentity();
2635 transform.RotateAboutXAxis(45.0);
2636 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2637 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2639 // Perspective cases.
2640 transform.MakeIdentity();
2641 transform.ApplyPerspectiveDepth(10.0);
2642 transform.RotateAboutYAxis(45.0);
2643 EXPECT_FALSE(EmpiricallyPreserves2dAxisAlignment(transform));
2644 EXPECT_FALSE(transform.Preserves2dAxisAlignment());
2646 transform.MakeIdentity();
2647 transform.ApplyPerspectiveDepth(10.0);
2648 transform.RotateAboutZAxis(90.0);
2649 EXPECT_TRUE(EmpiricallyPreserves2dAxisAlignment(transform));
2650 EXPECT_TRUE(transform.Preserves2dAxisAlignment());
2653 TEST(XFormTest, To2dTranslation) {
2654 Vector2dF translation(3.f, 7.f);
2655 Transform transform;
2656 transform.Translate(translation.x(), translation.y() + 1);
2657 EXPECT_NE(translation.ToString(), transform.To2dTranslation().ToString());
2658 transform.MakeIdentity();
2659 transform.Translate(translation.x(), translation.y());
2660 EXPECT_EQ(translation.ToString(), transform.To2dTranslation().ToString());
2663 TEST(XFormTest, TransformRect) {
2664 Transform translation;
2665 translation.Translate(3.f, 7.f);
2666 RectF rect(1.f, 2.f, 3.f, 4.f);
2667 RectF expected(4.f, 9.f, 3.f, 4.f);
2668 translation.TransformRect(&rect);
2669 EXPECT_EQ(expected.ToString(), rect.ToString());
2672 TEST(XFormTest, TransformRectReverse) {
2673 Transform translation;
2674 translation.Translate(3.f, 7.f);
2675 RectF rect(1.f, 2.f, 3.f, 4.f);
2676 RectF expected(-2.f, -5.f, 3.f, 4.f);
2677 EXPECT_TRUE(translation.TransformRectReverse(&rect));
2678 EXPECT_EQ(expected.ToString(), rect.ToString());
2680 Transform singular;
2681 singular.Scale3d(0.f, 0.f, 0.f);
2682 EXPECT_FALSE(singular.TransformRectReverse(&rect));
2685 TEST(XFormTest, TransformBox) {
2686 Transform translation;
2687 translation.Translate3d(3.f, 7.f, 6.f);
2688 BoxF box(1.f, 2.f, 3.f, 4.f, 5.f, 6.f);
2689 BoxF expected(4.f, 9.f, 9.f, 4.f, 5.f, 6.f);
2690 translation.TransformBox(&box);
2691 EXPECT_EQ(expected.ToString(), box.ToString());
2694 TEST(XFormTest, TransformBoxReverse) {
2695 Transform translation;
2696 translation.Translate3d(3.f, 7.f, 6.f);
2697 BoxF box(1.f, 2.f, 3.f, 4.f, 5.f, 6.f);
2698 BoxF expected(-2.f, -5.f, -3.f, 4.f, 5.f, 6.f);
2699 EXPECT_TRUE(translation.TransformBoxReverse(&box));
2700 EXPECT_EQ(expected.ToString(), box.ToString());
2702 Transform singular;
2703 singular.Scale3d(0.f, 0.f, 0.f);
2704 EXPECT_FALSE(singular.TransformBoxReverse(&box));
2707 } // namespace
2709 } // namespace gfx