2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-12-03 21:19:52 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMatrix44.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
2020-01-08 15:40:39 +00:00
|
|
|
#include "include/private/SkM44.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2011-06-09 19:13:41 +00:00
|
|
|
|
2012-11-13 15:08:22 +00:00
|
|
|
static bool nearly_equal_double(double a, double b) {
|
|
|
|
const double tolerance = 1e-7;
|
|
|
|
double diff = a - b;
|
|
|
|
if (diff < 0)
|
|
|
|
diff = -diff;
|
|
|
|
return diff <= tolerance;
|
|
|
|
}
|
|
|
|
|
2013-10-05 01:16:30 +00:00
|
|
|
static bool nearly_equal_mscalar(SkMScalar a, SkMScalar b) {
|
|
|
|
const SkMScalar tolerance = SK_MScalar1 / 200000;
|
|
|
|
|
|
|
|
return SkTAbs<SkMScalar>(a - b) <= tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
|
2011-06-09 19:13:41 +00:00
|
|
|
const SkScalar tolerance = SK_Scalar1 / 200000;
|
2013-10-05 01:16:30 +00:00
|
|
|
return SkScalarAbs(a - b) <= tolerance;
|
2011-06-09 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2011-06-13 14:46:52 +00:00
|
|
|
template <typename T> void assert16(skiatest::Reporter* reporter, const T data[],
|
|
|
|
T m0, T m1, T m2, T m3,
|
|
|
|
T m4, T m5, T m6, T m7,
|
|
|
|
T m8, T m9, T m10, T m11,
|
|
|
|
T m12, T m13, T m14, T m15) {
|
|
|
|
REPORTER_ASSERT(reporter, data[0] == m0);
|
|
|
|
REPORTER_ASSERT(reporter, data[1] == m1);
|
|
|
|
REPORTER_ASSERT(reporter, data[2] == m2);
|
|
|
|
REPORTER_ASSERT(reporter, data[3] == m3);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, data[4] == m4);
|
|
|
|
REPORTER_ASSERT(reporter, data[5] == m5);
|
|
|
|
REPORTER_ASSERT(reporter, data[6] == m6);
|
|
|
|
REPORTER_ASSERT(reporter, data[7] == m7);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, data[8] == m8);
|
|
|
|
REPORTER_ASSERT(reporter, data[9] == m9);
|
|
|
|
REPORTER_ASSERT(reporter, data[10] == m10);
|
|
|
|
REPORTER_ASSERT(reporter, data[11] == m11);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, data[12] == m12);
|
|
|
|
REPORTER_ASSERT(reporter, data[13] == m13);
|
|
|
|
REPORTER_ASSERT(reporter, data[14] == m14);
|
|
|
|
REPORTER_ASSERT(reporter, data[15] == m15);
|
|
|
|
}
|
|
|
|
|
2011-06-09 19:13:41 +00:00
|
|
|
static bool nearly_equal(const SkMatrix44& a, const SkMatrix44& b) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
for (int j = 0; j < 4; ++j) {
|
2013-10-05 01:16:30 +00:00
|
|
|
if (!nearly_equal_mscalar(a.get(i, j), b.get(i, j))) {
|
2013-10-11 18:50:45 +00:00
|
|
|
SkDebugf("not equal %g %g\n", a.get(i, j), b.get(i, j));
|
2011-06-09 19:13:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_identity(const SkMatrix44& m) {
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 identity(SkMatrix44::kIdentity_Constructor);
|
2011-06-09 19:13:41 +00:00
|
|
|
return nearly_equal(m, identity);
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static bool bits_isonly(int value, int mask) {
|
|
|
|
return 0 == (value & ~mask);
|
|
|
|
}
|
|
|
|
|
2012-12-10 20:16:10 +00:00
|
|
|
static void test_constructor(skiatest::Reporter* reporter) {
|
|
|
|
// Allocate a matrix on the heap
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44* placeholderMatrix = new SkMatrix44;
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkMatrix44> deleteMe(placeholderMatrix);
|
2013-03-21 17:38:49 +00:00
|
|
|
|
2012-12-10 20:16:10 +00:00
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
|
|
|
placeholderMatrix->setDouble(row, col, row * col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use placement-new syntax to trigger the constructor on top of the heap
|
|
|
|
// address we already initialized. This allows us to check that the
|
|
|
|
// constructor did avoid initializing the matrix contents.
|
|
|
|
SkMatrix44* testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kUninitialized_Constructor);
|
|
|
|
REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
|
|
|
|
REPORTER_ASSERT(reporter, !testMatrix->isIdentity());
|
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(row * col, testMatrix->getDouble(row, col)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that kIdentity_Constructor really does initialize to an identity matrix.
|
|
|
|
testMatrix = 0;
|
2012-12-11 02:01:20 +00:00
|
|
|
testMatrix = new(placeholderMatrix) SkMatrix44(SkMatrix44::kIdentity_Constructor);
|
2012-12-10 20:16:10 +00:00
|
|
|
REPORTER_ASSERT(reporter, testMatrix == placeholderMatrix);
|
|
|
|
REPORTER_ASSERT(reporter, testMatrix->isIdentity());
|
|
|
|
REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I());
|
2014-12-16 16:36:11 +00:00
|
|
|
|
|
|
|
// Verify that that constructing from an SkMatrix initializes everything.
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 scaleMatrix;
|
2014-12-16 16:36:11 +00:00
|
|
|
scaleMatrix.setScale(3, 4, 5);
|
|
|
|
REPORTER_ASSERT(reporter, scaleMatrix.isScale());
|
|
|
|
testMatrix = new(&scaleMatrix) SkMatrix44(SkMatrix::I());
|
|
|
|
REPORTER_ASSERT(reporter, testMatrix->isIdentity());
|
|
|
|
REPORTER_ASSERT(reporter, *testMatrix == SkMatrix44::I());
|
2012-12-10 20:16:10 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
static void test_translate(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
|
|
|
SkMatrix44 inverse;
|
2012-12-05 22:13:59 +00:00
|
|
|
|
|
|
|
mat.setTranslate(0, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
|
|
|
|
mat.setTranslate(1, 2, 3);
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kTranslate_Mask));
|
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kTranslate_Mask));
|
2012-12-06 02:01:25 +00:00
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a,b,c;
|
2012-12-05 22:13:59 +00:00
|
|
|
a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
b.setTranslate(10, 11, 12);
|
|
|
|
|
|
|
|
c.setConcat(a, b);
|
|
|
|
mat = a;
|
|
|
|
mat.preTranslate(10, 11, 12);
|
|
|
|
REPORTER_ASSERT(reporter, mat == c);
|
|
|
|
|
|
|
|
c.setConcat(b, a);
|
|
|
|
mat = a;
|
|
|
|
mat.postTranslate(10, 11, 12);
|
|
|
|
REPORTER_ASSERT(reporter, mat == c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_scale(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
|
|
|
SkMatrix44 inverse;
|
2012-12-06 02:01:25 +00:00
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
mat.setScale(1, 1, 1);
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kIdentity_Mask));
|
|
|
|
mat.setScale(1, 2, 3);
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(mat.getType(), SkMatrix44::kScale_Mask));
|
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
|
|
|
REPORTER_ASSERT(reporter, bits_isonly(inverse.getType(), SkMatrix44::kScale_Mask));
|
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a,b,c;
|
2012-12-05 22:13:59 +00:00
|
|
|
a.set3x3(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
|
b.setScale(10, 11, 12);
|
2012-12-06 02:01:25 +00:00
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
c.setConcat(a, b);
|
|
|
|
mat = a;
|
|
|
|
mat.preScale(10, 11, 12);
|
|
|
|
REPORTER_ASSERT(reporter, mat == c);
|
2012-12-06 02:01:25 +00:00
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
c.setConcat(b, a);
|
|
|
|
mat = a;
|
|
|
|
mat.postScale(10, 11, 12);
|
|
|
|
REPORTER_ASSERT(reporter, mat == c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_i(SkMatrix44* mat) { mat->setIdentity(); }
|
|
|
|
static void make_t(SkMatrix44* mat) { mat->setTranslate(1, 2, 3); }
|
|
|
|
static void make_s(SkMatrix44* mat) { mat->setScale(1, 2, 3); }
|
|
|
|
static void make_st(SkMatrix44* mat) {
|
|
|
|
mat->setScale(1, 2, 3);
|
|
|
|
mat->postTranslate(1, 2, 3);
|
|
|
|
}
|
|
|
|
static void make_a(SkMatrix44* mat) {
|
|
|
|
mat->setRotateDegreesAbout(1, 2, 3, 45);
|
|
|
|
}
|
|
|
|
static void make_p(SkMatrix44* mat) {
|
|
|
|
SkMScalar data[] = {
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8,
|
|
|
|
1, 2, 3, 4, 5, 6, 7, 8,
|
|
|
|
};
|
|
|
|
mat->setRowMajor(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*Make44Proc)(SkMatrix44*);
|
|
|
|
|
|
|
|
static const Make44Proc gMakeProcs[] = {
|
|
|
|
make_i, make_t, make_s, make_st, make_a, make_p
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_map2(skiatest::Reporter* reporter, const SkMatrix44& mat) {
|
|
|
|
SkMScalar src2[] = { 1, 2 };
|
|
|
|
SkMScalar src4[] = { src2[0], src2[1], 0, 1 };
|
|
|
|
SkMScalar dstA[4], dstB[4];
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2015-06-09 18:47:01 +00:00
|
|
|
dstA[i] = SkDoubleToMScalar(123456789);
|
|
|
|
dstB[i] = SkDoubleToMScalar(987654321);
|
2012-12-05 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mat.map2(src2, 1, dstA);
|
|
|
|
mat.mapMScalars(src4, dstB);
|
2012-12-06 02:01:25 +00:00
|
|
|
|
2012-12-05 22:13:59 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, dstA[i] == dstB[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_map2(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
2012-12-05 22:13:59 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProcs); ++i) {
|
|
|
|
gMakeProcs[i](&mat);
|
|
|
|
test_map2(reporter, mat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
static void test_gettype(skiatest::Reporter* reporter) {
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 matrix(SkMatrix44::kIdentity_Constructor);
|
2012-12-04 02:01:25 +00:00
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
REPORTER_ASSERT(reporter, matrix.isIdentity());
|
|
|
|
REPORTER_ASSERT(reporter, SkMatrix44::kIdentity_Mask == matrix.getType());
|
2012-12-04 02:01:25 +00:00
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
int expectedMask;
|
|
|
|
|
|
|
|
matrix.set(1, 1, 0);
|
|
|
|
expectedMask = SkMatrix44::kScale_Mask;
|
|
|
|
REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
|
|
|
|
|
|
|
|
matrix.set(0, 3, 1); // translate-x
|
|
|
|
expectedMask |= SkMatrix44::kTranslate_Mask;
|
|
|
|
REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
|
|
|
|
|
|
|
|
matrix.set(2, 0, 1);
|
|
|
|
expectedMask |= SkMatrix44::kAffine_Mask;
|
|
|
|
REPORTER_ASSERT(reporter, matrix.getType() == expectedMask);
|
2012-12-04 02:01:25 +00:00
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
matrix.set(3, 2, 1);
|
|
|
|
REPORTER_ASSERT(reporter, matrix.getType() & SkMatrix44::kPerspective_Mask);
|
2013-04-19 12:25:00 +00:00
|
|
|
|
|
|
|
// ensure that negative zero is treated as zero
|
|
|
|
SkMScalar dx = 0;
|
|
|
|
SkMScalar dy = 0;
|
|
|
|
SkMScalar dz = 0;
|
|
|
|
matrix.setTranslate(-dx, -dy, -dz);
|
|
|
|
REPORTER_ASSERT(reporter, matrix.isIdentity());
|
|
|
|
matrix.preTranslate(-dx, -dy, -dz);
|
|
|
|
REPORTER_ASSERT(reporter, matrix.isIdentity());
|
|
|
|
matrix.postTranslate(-dx, -dy, -dz);
|
|
|
|
REPORTER_ASSERT(reporter, matrix.isIdentity());
|
2012-12-03 21:19:52 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 18:13:39 +00:00
|
|
|
static void test_common_angles(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 rot;
|
2011-06-24 18:13:39 +00:00
|
|
|
// Test precision of rotation in common cases
|
|
|
|
int common_angles[] = { 0, 90, -90, 180, -180, 270, -270, 360, -360 };
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
2012-04-06 20:01:46 +00:00
|
|
|
rot.setRotateDegreesAbout(0, 0, -1, SkIntToScalar(common_angles[i]));
|
2011-06-24 18:13:39 +00:00
|
|
|
|
|
|
|
SkMatrix rot3x3 = rot;
|
|
|
|
REPORTER_ASSERT(reporter, rot3x3.rectStaysRect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-09 21:25:06 +00:00
|
|
|
static void test_concat(skiatest::Reporter* reporter) {
|
|
|
|
int i;
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a,b,c,d;
|
2012-11-09 21:25:06 +00:00
|
|
|
|
|
|
|
a.setTranslate(10, 10, 10);
|
|
|
|
b.setScale(2, 2, 2);
|
|
|
|
|
|
|
|
SkScalar src[8] = {
|
|
|
|
0, 0, 0, 1,
|
|
|
|
1, 1, 1, 1
|
|
|
|
};
|
|
|
|
SkScalar dst[8];
|
|
|
|
|
|
|
|
c.setConcat(a, b);
|
|
|
|
|
|
|
|
d = a;
|
|
|
|
d.preConcat(b);
|
|
|
|
REPORTER_ASSERT(reporter, d == c);
|
2012-11-10 02:01:26 +00:00
|
|
|
|
2012-11-09 21:39:48 +00:00
|
|
|
c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
|
2012-11-09 21:25:06 +00:00
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, 10 == dst[i]);
|
|
|
|
REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
|
|
|
|
}
|
2012-11-10 02:01:26 +00:00
|
|
|
|
2012-11-09 21:25:06 +00:00
|
|
|
c.setConcat(b, a);
|
|
|
|
|
|
|
|
d = a;
|
|
|
|
d.postConcat(b);
|
|
|
|
REPORTER_ASSERT(reporter, d == c);
|
|
|
|
|
2012-11-09 21:39:48 +00:00
|
|
|
c.mapScalars(src, dst); c.mapScalars(src + 4, dst + 4);
|
2012-11-09 21:25:06 +00:00
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, 20 == dst[i]);
|
|
|
|
REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 15:08:22 +00:00
|
|
|
static void test_determinant(skiatest::Reporter* reporter) {
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 a(SkMatrix44::kIdentity_Constructor);
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(1, a.determinant()));
|
2012-12-03 21:19:52 +00:00
|
|
|
a.set(1, 1, 2);
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(2, a.determinant()));
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 b;
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, a.invert(&b));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(0.5, b.determinant()));
|
|
|
|
SkMatrix44 c = b = a;
|
2012-12-03 21:19:52 +00:00
|
|
|
c.set(0, 1, 4);
|
|
|
|
b.set(1, 0, 4);
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
nearly_equal_double(a.determinant(),
|
|
|
|
b.determinant()));
|
|
|
|
SkMatrix44 d = a;
|
2012-12-03 21:19:52 +00:00
|
|
|
d.set(0, 0, 8);
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(16, d.determinant()));
|
|
|
|
|
|
|
|
SkMatrix44 e = a;
|
|
|
|
e.postConcat(d);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(32, e.determinant()));
|
2012-12-03 21:19:52 +00:00
|
|
|
e.set(0, 0, 0);
|
2012-11-19 21:02:06 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
|
2012-11-13 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 20:15:24 +00:00
|
|
|
static void test_invert(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 inverse;
|
2013-08-20 20:15:24 +00:00
|
|
|
double inverseData[16];
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 identity(SkMatrix44::kIdentity_Constructor);
|
2013-08-20 20:15:24 +00:00
|
|
|
identity.invert(&inverse);
|
|
|
|
inverse.asRowMajord(inverseData);
|
|
|
|
assert16<double>(reporter, inverseData,
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 translation;
|
2013-08-20 20:15:24 +00:00
|
|
|
translation.setTranslate(2, 3, 4);
|
|
|
|
translation.invert(&inverse);
|
|
|
|
inverse.asRowMajord(inverseData);
|
|
|
|
assert16<double>(reporter, inverseData,
|
|
|
|
1, 0, 0, -2,
|
|
|
|
0, 1, 0, -3,
|
|
|
|
0, 0, 1, -4,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 scale;
|
2013-08-20 20:15:24 +00:00
|
|
|
scale.setScale(2, 4, 8);
|
|
|
|
scale.invert(&inverse);
|
|
|
|
inverse.asRowMajord(inverseData);
|
|
|
|
assert16<double>(reporter, inverseData,
|
|
|
|
0.5, 0, 0, 0,
|
|
|
|
0, 0.25, 0, 0,
|
|
|
|
0, 0, 0.125, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 scaleTranslation;
|
2015-06-09 22:06:22 +00:00
|
|
|
scaleTranslation.setScale(32, 128, 1024);
|
2013-08-20 20:15:24 +00:00
|
|
|
scaleTranslation.preTranslate(2, 3, 4);
|
|
|
|
scaleTranslation.invert(&inverse);
|
|
|
|
inverse.asRowMajord(inverseData);
|
|
|
|
assert16<double>(reporter, inverseData,
|
2015-06-09 22:06:22 +00:00
|
|
|
0.03125, 0, 0, -2,
|
|
|
|
0, 0.0078125, 0, -3,
|
|
|
|
0, 0, 0.0009765625, -4,
|
|
|
|
0, 0, 0, 1);
|
2013-08-20 20:15:24 +00:00
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 rotation;
|
2013-08-20 20:15:24 +00:00
|
|
|
rotation.setRotateDegreesAbout(0, 0, 1, 90);
|
|
|
|
rotation.invert(&inverse);
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 expected;
|
2013-08-20 20:15:24 +00:00
|
|
|
double expectedInverseRotation[16] =
|
|
|
|
{0, 1, 0, 0,
|
|
|
|
-1, 0, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1};
|
|
|
|
expected.setRowMajord(expectedInverseRotation);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
|
2013-08-21 07:01:29 +00:00
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 affine;
|
2013-08-20 20:15:24 +00:00
|
|
|
affine.setRotateDegreesAbout(0, 0, 1, 90);
|
|
|
|
affine.preScale(10, 20, 100);
|
|
|
|
affine.preTranslate(2, 3, 4);
|
|
|
|
affine.invert(&inverse);
|
|
|
|
double expectedInverseAffine[16] =
|
|
|
|
{0, 0.1, 0, -2,
|
|
|
|
-0.05, 0, 0, -3,
|
|
|
|
0, 0, 0.01, -4,
|
|
|
|
0, 0, 0, 1};
|
|
|
|
expected.setRowMajord(expectedInverseAffine);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 perspective(SkMatrix44::kIdentity_Constructor);
|
2013-08-20 20:15:24 +00:00
|
|
|
perspective.setDouble(3, 2, 1.0);
|
|
|
|
perspective.invert(&inverse);
|
|
|
|
double expectedInversePerspective[16] =
|
|
|
|
{1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, -1, 1};
|
|
|
|
expected.setRowMajord(expectedInversePerspective);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
|
|
|
|
|
2013-10-31 17:28:30 +00:00
|
|
|
SkMatrix44 affineAndPerspective(SkMatrix44::kIdentity_Constructor);
|
2013-08-20 20:15:24 +00:00
|
|
|
affineAndPerspective.setDouble(3, 2, 1.0);
|
|
|
|
affineAndPerspective.preScale(10, 20, 100);
|
|
|
|
affineAndPerspective.preTranslate(2, 3, 4);
|
|
|
|
affineAndPerspective.invert(&inverse);
|
|
|
|
double expectedInverseAffineAndPerspective[16] =
|
|
|
|
{0.1, 0, 2, -2,
|
|
|
|
0, 0.05, 3, -3,
|
|
|
|
0, 0, 4.01, -4,
|
|
|
|
0, 0, -1, 1};
|
|
|
|
expected.setRowMajord(expectedInverseAffineAndPerspective);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(expected, inverse));
|
2015-06-30 20:36:04 +00:00
|
|
|
|
|
|
|
SkMatrix44 tinyScale(SkMatrix44::kIdentity_Constructor);
|
|
|
|
tinyScale.setDouble(0, 0, 1e-39);
|
|
|
|
REPORTER_ASSERT(reporter, tinyScale.getType() == SkMatrix44::kScale_Mask);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScale.invert(nullptr));
|
2015-06-30 20:36:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScale.invert(&inverse));
|
|
|
|
|
|
|
|
SkMatrix44 tinyScaleTranslate(SkMatrix44::kIdentity_Constructor);
|
|
|
|
tinyScaleTranslate.setDouble(0, 0, 1e-38);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, tinyScaleTranslate.invert(nullptr));
|
2015-06-30 20:36:04 +00:00
|
|
|
tinyScaleTranslate.setDouble(0, 3, 10);
|
|
|
|
REPORTER_ASSERT(
|
|
|
|
reporter, tinyScaleTranslate.getType() ==
|
|
|
|
(SkMatrix44::kScale_Mask | SkMatrix44::kTranslate_Mask));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScaleTranslate.invert(nullptr));
|
2015-06-30 20:36:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScaleTranslate.invert(&inverse));
|
|
|
|
|
|
|
|
SkMatrix44 tinyScalePerspective(SkMatrix44::kIdentity_Constructor);
|
|
|
|
tinyScalePerspective.setDouble(0, 0, 1e-39);
|
|
|
|
tinyScalePerspective.setDouble(3, 2, -1);
|
|
|
|
REPORTER_ASSERT(reporter, (tinyScalePerspective.getType() &
|
|
|
|
SkMatrix44::kPerspective_Mask) ==
|
|
|
|
SkMatrix44::kPerspective_Mask);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScalePerspective.invert(nullptr));
|
2015-06-30 20:36:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, !tinyScalePerspective.invert(&inverse));
|
2013-08-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 21:33:55 +00:00
|
|
|
static void test_transpose(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a,b;
|
2012-11-19 21:02:06 +00:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
|
|
|
a.setDouble(row, col, i);
|
|
|
|
b.setDouble(col, row, i++);
|
|
|
|
}
|
2012-11-14 21:33:55 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 21:02:06 +00:00
|
|
|
a.transpose();
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a, b));
|
2012-11-14 21:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_get_set_double(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a;
|
2012-11-19 21:02:06 +00:00
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
|
|
|
a.setDouble(row, col, 3.141592653589793);
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
nearly_equal_double(3.141592653589793,
|
|
|
|
a.getDouble(row, col)));
|
|
|
|
a.setDouble(row, col, 0);
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
nearly_equal_double(0, a.getDouble(row, col)));
|
|
|
|
}
|
2012-11-14 21:33:55 +00:00
|
|
|
}
|
2012-11-19 21:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 19:42:29 +00:00
|
|
|
static void test_set_3x3(skiatest::Reporter* r) {
|
|
|
|
static float vals[9] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, };
|
|
|
|
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
2016-06-23 19:42:29 +00:00
|
|
|
mat.set3x3RowMajorf(vals);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, 1.0f == mat.getFloat(0, 0));
|
|
|
|
REPORTER_ASSERT(r, 2.0f == mat.getFloat(0, 1));
|
|
|
|
REPORTER_ASSERT(r, 3.0f == mat.getFloat(0, 2));
|
|
|
|
REPORTER_ASSERT(r, 4.0f == mat.getFloat(1, 0));
|
|
|
|
REPORTER_ASSERT(r, 5.0f == mat.getFloat(1, 1));
|
|
|
|
REPORTER_ASSERT(r, 6.0f == mat.getFloat(1, 2));
|
|
|
|
REPORTER_ASSERT(r, 7.0f == mat.getFloat(2, 0));
|
|
|
|
REPORTER_ASSERT(r, 8.0f == mat.getFloat(2, 1));
|
|
|
|
REPORTER_ASSERT(r, 9.0f == mat.getFloat(2, 2));
|
|
|
|
}
|
|
|
|
|
2012-11-19 21:02:06 +00:00
|
|
|
static void test_set_row_col_major(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a,b;
|
2013-10-31 17:28:30 +00:00
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
2012-11-19 21:02:06 +00:00
|
|
|
a.setDouble(row, col, row * 4 + col);
|
2012-12-03 21:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-04 02:01:25 +00:00
|
|
|
|
2012-11-19 21:02:06 +00:00
|
|
|
double bufferd[16];
|
|
|
|
float bufferf[16];
|
|
|
|
a.asColMajord(bufferd);
|
|
|
|
b.setColMajord(bufferd);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a, b));
|
|
|
|
b.setRowMajord(bufferd);
|
|
|
|
b.transpose();
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a, b));
|
|
|
|
a.asColMajorf(bufferf);
|
|
|
|
b.setColMajorf(bufferf);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a, b));
|
|
|
|
b.setRowMajorf(bufferf);
|
|
|
|
b.transpose();
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a, b));
|
2012-11-14 21:33:55 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 01:16:30 +00:00
|
|
|
static void test_3x3_conversion(skiatest::Reporter* reporter) {
|
|
|
|
SkMScalar values4x4[16] = { 1, 2, 3, 4,
|
|
|
|
5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16 };
|
|
|
|
SkScalar values3x3[9] = { 1, 2, 4,
|
|
|
|
5, 6, 8,
|
|
|
|
13, 14, 16 };
|
|
|
|
SkMScalar values4x4flattened[16] = { 1, 2, 0, 4,
|
|
|
|
5, 6, 0, 8,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
13, 14, 0, 16 };
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 a44;
|
2013-10-05 01:16:30 +00:00
|
|
|
a44.setRowMajor(values4x4);
|
|
|
|
|
|
|
|
SkMatrix a33 = a44;
|
|
|
|
SkMatrix expected33;
|
|
|
|
for (int i = 0; i < 9; i++) expected33[i] = values3x3[i];
|
|
|
|
REPORTER_ASSERT(reporter, expected33 == a33);
|
|
|
|
|
|
|
|
SkMatrix44 a44flattened = a33;
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 expected44flattened;
|
2013-10-05 01:16:30 +00:00
|
|
|
expected44flattened.setRowMajor(values4x4flattened);
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal(a44flattened, expected44flattened));
|
|
|
|
|
|
|
|
// Test that a point with a Z value of 0 is transformed the same way.
|
|
|
|
SkScalar vec4[4] = { 2, 4, 0, 8 };
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 vec3 = { 2, 4, 8 };
|
2013-10-05 01:16:30 +00:00
|
|
|
|
|
|
|
SkScalar vec4transformed[4];
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 vec3transformed;
|
2013-10-05 01:16:30 +00:00
|
|
|
SkScalar vec4transformed2[4];
|
|
|
|
a44.mapScalars(vec4, vec4transformed);
|
2017-10-18 15:46:18 +00:00
|
|
|
a33.mapHomogeneousPoints(&vec3transformed, &vec3, 1);
|
2013-10-05 01:16:30 +00:00
|
|
|
a44flattened.mapScalars(vec4, vec4transformed2);
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec3transformed.fX));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec3transformed.fY));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec3transformed.fZ));
|
2013-10-05 01:16:30 +00:00
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[0], vec4transformed2[0]));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[1], vec4transformed2[1]));
|
|
|
|
REPORTER_ASSERT(reporter, !nearly_equal_scalar(vec4transformed[2], vec4transformed2[2]));
|
|
|
|
REPORTER_ASSERT(reporter, nearly_equal_scalar(vec4transformed[3], vec4transformed2[3]));
|
|
|
|
}
|
|
|
|
|
2014-09-16 15:29:29 +00:00
|
|
|
static void test_has_perspective(skiatest::Reporter* reporter) {
|
|
|
|
SkMatrix44 transform(SkMatrix44::kIdentity_Constructor);
|
|
|
|
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 2, -0.1);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
|
|
|
|
|
|
|
transform.reset();
|
|
|
|
REPORTER_ASSERT(reporter, !transform.hasPerspective());
|
|
|
|
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 0, -1.0);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
|
|
|
|
|
|
|
transform.reset();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 1, -1.0);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
|
|
|
|
|
|
|
transform.reset();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 2, -0.3);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
|
|
|
|
|
|
|
transform.reset();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 3, 0.5);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
2015-06-09 18:47:01 +00:00
|
|
|
|
2014-09-16 15:29:29 +00:00
|
|
|
transform.reset();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 3, 0.0);
|
2014-09-16 15:29:29 +00:00
|
|
|
REPORTER_ASSERT(reporter, transform.hasPerspective());
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:45:48 +00:00
|
|
|
static bool is_rectilinear (SkVector4& p1, SkVector4& p2, SkVector4& p3, SkVector4& p4) {
|
|
|
|
return (SkScalarNearlyEqual(p1.fData[0], p2.fData[0]) &&
|
|
|
|
SkScalarNearlyEqual(p2.fData[1], p3.fData[1]) &&
|
|
|
|
SkScalarNearlyEqual(p3.fData[0], p4.fData[0]) &&
|
|
|
|
SkScalarNearlyEqual(p4.fData[1], p1.fData[1])) ||
|
|
|
|
(SkScalarNearlyEqual(p1.fData[1], p2.fData[1]) &&
|
|
|
|
SkScalarNearlyEqual(p2.fData[0], p3.fData[0]) &&
|
|
|
|
SkScalarNearlyEqual(p3.fData[1], p4.fData[1]) &&
|
|
|
|
SkScalarNearlyEqual(p4.fData[0], p1.fData[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkVector4 mul_with_persp_divide(const SkMatrix44& transform, const SkVector4& target) {
|
|
|
|
SkVector4 result = transform * target;
|
|
|
|
if (result.fData[3] != 0.0f && result.fData[3] != SK_Scalar1) {
|
|
|
|
float wInverse = SK_Scalar1 / result.fData[3];
|
|
|
|
result.set(result.fData[0] * wInverse,
|
|
|
|
result.fData[1] * wInverse,
|
|
|
|
result.fData[2] * wInverse,
|
|
|
|
SK_Scalar1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool empirically_preserves_2d_axis_alignment(skiatest::Reporter* reporter,
|
|
|
|
const SkMatrix44& transform) {
|
|
|
|
SkVector4 p1(5.0f, 5.0f, 0.0f);
|
|
|
|
SkVector4 p2(10.0f, 5.0f, 0.0f);
|
|
|
|
SkVector4 p3(10.0f, 20.0f, 0.0f);
|
|
|
|
SkVector4 p4(5.0f, 20.0f, 0.0f);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, is_rectilinear(p1, p2, p3, p4));
|
|
|
|
|
|
|
|
p1 = mul_with_persp_divide(transform, p1);
|
|
|
|
p2 = mul_with_persp_divide(transform, p2);
|
|
|
|
p3 = mul_with_persp_divide(transform, p3);
|
|
|
|
p4 = mul_with_persp_divide(transform, p4);
|
|
|
|
|
|
|
|
return is_rectilinear(p1, p2, p3, p4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test(bool expected, skiatest::Reporter* reporter, const SkMatrix44& transform) {
|
|
|
|
if (expected) {
|
|
|
|
REPORTER_ASSERT(reporter, empirically_preserves_2d_axis_alignment(reporter, transform));
|
|
|
|
REPORTER_ASSERT(reporter, transform.preserves2dAxisAlignment());
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, !empirically_preserves_2d_axis_alignment(reporter, transform));
|
|
|
|
REPORTER_ASSERT(reporter, !transform.preserves2dAxisAlignment());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_preserves_2d_axis_alignment(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 transform;
|
|
|
|
SkMatrix44 transform2;
|
2014-09-26 18:45:48 +00:00
|
|
|
|
|
|
|
static const struct TestCase {
|
|
|
|
SkMScalar a; // row 1, column 1
|
|
|
|
SkMScalar b; // row 1, column 2
|
|
|
|
SkMScalar c; // row 2, column 1
|
|
|
|
SkMScalar d; // row 2, column 2
|
|
|
|
bool expected;
|
|
|
|
} test_cases[] = {
|
|
|
|
{ 3.f, 0.f,
|
|
|
|
0.f, 4.f, true }, // basic case
|
|
|
|
{ 0.f, 4.f,
|
|
|
|
3.f, 0.f, true }, // rotate by 90
|
|
|
|
{ 0.f, 0.f,
|
|
|
|
0.f, 4.f, true }, // degenerate x
|
|
|
|
{ 3.f, 0.f,
|
|
|
|
0.f, 0.f, true }, // degenerate y
|
|
|
|
{ 0.f, 0.f,
|
|
|
|
3.f, 0.f, true }, // degenerate x + rotate by 90
|
|
|
|
{ 0.f, 4.f,
|
|
|
|
0.f, 0.f, true }, // degenerate y + rotate by 90
|
|
|
|
{ 3.f, 4.f,
|
|
|
|
0.f, 0.f, false },
|
|
|
|
{ 0.f, 0.f,
|
|
|
|
3.f, 4.f, false },
|
|
|
|
{ 0.f, 3.f,
|
|
|
|
0.f, 4.f, false },
|
|
|
|
{ 3.f, 0.f,
|
|
|
|
4.f, 0.f, false },
|
|
|
|
{ 3.f, 4.f,
|
|
|
|
5.f, 0.f, false },
|
|
|
|
{ 3.f, 4.f,
|
|
|
|
0.f, 5.f, false },
|
|
|
|
{ 3.f, 0.f,
|
|
|
|
4.f, 5.f, false },
|
|
|
|
{ 0.f, 3.f,
|
|
|
|
4.f, 5.f, false },
|
|
|
|
{ 2.f, 3.f,
|
|
|
|
4.f, 5.f, false },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
|
|
|
|
const TestCase& value = test_cases[i];
|
|
|
|
transform.setIdentity();
|
|
|
|
transform.set(0, 0, value.a);
|
|
|
|
transform.set(0, 1, value.b);
|
|
|
|
transform.set(1, 0, value.c);
|
|
|
|
transform.set(1, 1, value.d);
|
|
|
|
|
|
|
|
test(value.expected, reporter, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the same test cases again, but this time make sure that other matrix
|
|
|
|
// elements (except perspective) have entries, to test that they are ignored.
|
|
|
|
for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
|
|
|
|
const TestCase& value = test_cases[i];
|
|
|
|
transform.setIdentity();
|
|
|
|
transform.set(0, 0, value.a);
|
|
|
|
transform.set(0, 1, value.b);
|
|
|
|
transform.set(1, 0, value.c);
|
|
|
|
transform.set(1, 1, value.d);
|
|
|
|
|
|
|
|
transform.set(0, 2, 1.f);
|
|
|
|
transform.set(0, 3, 2.f);
|
|
|
|
transform.set(1, 2, 3.f);
|
|
|
|
transform.set(1, 3, 4.f);
|
|
|
|
transform.set(2, 0, 5.f);
|
|
|
|
transform.set(2, 1, 6.f);
|
|
|
|
transform.set(2, 2, 7.f);
|
|
|
|
transform.set(2, 3, 8.f);
|
|
|
|
|
|
|
|
test(value.expected, reporter, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the same test cases again, but this time add perspective which is
|
|
|
|
// always assumed to not-preserve axis alignment.
|
|
|
|
for (size_t i = 0; i < sizeof(test_cases)/sizeof(TestCase); ++i) {
|
|
|
|
const TestCase& value = test_cases[i];
|
|
|
|
transform.setIdentity();
|
|
|
|
transform.set(0, 0, value.a);
|
|
|
|
transform.set(0, 1, value.b);
|
|
|
|
transform.set(1, 0, value.c);
|
|
|
|
transform.set(1, 1, value.d);
|
|
|
|
|
|
|
|
transform.set(0, 2, 1.f);
|
|
|
|
transform.set(0, 3, 2.f);
|
|
|
|
transform.set(1, 2, 3.f);
|
|
|
|
transform.set(1, 3, 4.f);
|
|
|
|
transform.set(2, 0, 5.f);
|
|
|
|
transform.set(2, 1, 6.f);
|
|
|
|
transform.set(2, 2, 7.f);
|
|
|
|
transform.set(2, 3, 8.f);
|
|
|
|
transform.set(3, 0, 9.f);
|
|
|
|
transform.set(3, 1, 10.f);
|
|
|
|
transform.set(3, 2, 11.f);
|
|
|
|
transform.set(3, 3, 12.f);
|
|
|
|
|
|
|
|
test(false, reporter, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a few more practical situations to check precision
|
|
|
|
// Reuse TestCase (a, b, c, d) as (x, y, z, degrees) axis to rotate about.
|
|
|
|
TestCase rotation_tests[] = {
|
|
|
|
{ 0.0, 0.0, 1.0, 90.0, true },
|
|
|
|
{ 0.0, 0.0, 1.0, 180.0, true },
|
|
|
|
{ 0.0, 0.0, 1.0, 270.0, true },
|
|
|
|
{ 0.0, 1.0, 0.0, 90.0, true },
|
|
|
|
{ 1.0, 0.0, 0.0, 90.0, true },
|
|
|
|
{ 0.0, 0.0, 1.0, 45.0, false },
|
|
|
|
// In 3d these next two are non-preserving, but we're testing in 2d after
|
|
|
|
// orthographic projection, where they are.
|
|
|
|
{ 0.0, 1.0, 0.0, 45.0, true },
|
|
|
|
{ 1.0, 0.0, 0.0, 45.0, true },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(rotation_tests)/sizeof(TestCase); ++i) {
|
|
|
|
const TestCase& value = rotation_tests[i];
|
|
|
|
transform.setRotateDegreesAbout(value.a, value.b, value.c, value.d);
|
|
|
|
test(value.expected, reporter, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct DoubleRotationCase {
|
|
|
|
SkMScalar x1;
|
|
|
|
SkMScalar y1;
|
|
|
|
SkMScalar z1;
|
|
|
|
SkMScalar degrees1;
|
|
|
|
SkMScalar x2;
|
|
|
|
SkMScalar y2;
|
|
|
|
SkMScalar z2;
|
|
|
|
SkMScalar degrees2;
|
|
|
|
bool expected;
|
|
|
|
} double_rotation_tests[] = {
|
|
|
|
{ 0.0, 0.0, 1.0, 90.0, 0.0, 1.0, 0.0, 90.0, true },
|
|
|
|
{ 0.0, 0.0, 1.0, 90.0, 1.0, 0.0, 0.0, 90.0, true },
|
|
|
|
{ 0.0, 1.0, 0.0, 90.0, 0.0, 0.0, 1.0, 90.0, true },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(double_rotation_tests)/sizeof(DoubleRotationCase); ++i) {
|
|
|
|
const DoubleRotationCase& value = double_rotation_tests[i];
|
|
|
|
transform.setRotateDegreesAbout(value.x1, value.y1, value.z1, value.degrees1);
|
|
|
|
transform2.setRotateDegreesAbout(value.x2, value.y2, value.z2, value.degrees2);
|
|
|
|
transform.postConcat(transform2);
|
|
|
|
test(value.expected, reporter, transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perspective cases.
|
|
|
|
transform.setIdentity();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 2, -0.1); // Perspective depth 10
|
2014-09-26 18:45:48 +00:00
|
|
|
transform2.setRotateDegreesAbout(0.0, 1.0, 0.0, 45.0);
|
|
|
|
transform.preConcat(transform2);
|
|
|
|
test(false, reporter, transform);
|
|
|
|
|
|
|
|
transform.setIdentity();
|
2015-06-09 18:47:01 +00:00
|
|
|
transform.setDouble(3, 2, -0.1); // Perspective depth 10
|
2014-09-26 18:45:48 +00:00
|
|
|
transform2.setRotateDegreesAbout(0.0, 0.0, 1.0, 90.0);
|
|
|
|
transform.preConcat(transform2);
|
|
|
|
test(true, reporter, transform);
|
|
|
|
}
|
|
|
|
|
2014-10-21 19:33:21 +00:00
|
|
|
// just want to exercise the various converters for MScalar
|
|
|
|
static void test_toint(skiatest::Reporter* reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
2014-10-21 19:33:21 +00:00
|
|
|
mat.setScale(3, 3, 3);
|
|
|
|
|
|
|
|
SkMScalar sum = SkMScalarFloor(mat.get(0, 0)) +
|
|
|
|
SkMScalarRound(mat.get(1, 0)) +
|
|
|
|
SkMScalarCeil(mat.get(2, 0));
|
|
|
|
int isum = SkMScalarFloorToInt(mat.get(0, 1)) +
|
|
|
|
SkMScalarRoundToInt(mat.get(1, 2)) +
|
|
|
|
SkMScalarCeilToInt(mat.get(2, 3));
|
|
|
|
REPORTER_ASSERT(reporter, sum >= 0);
|
|
|
|
REPORTER_ASSERT(reporter, isum >= 0);
|
|
|
|
REPORTER_ASSERT(reporter, static_cast<SkMScalar>(isum) == SkIntToMScalar(isum));
|
|
|
|
}
|
2014-09-26 18:45:48 +00:00
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Matrix44, reporter) {
|
2018-10-04 13:06:00 +00:00
|
|
|
SkMatrix44 mat;
|
|
|
|
SkMatrix44 inverse;
|
|
|
|
SkMatrix44 iden1;
|
|
|
|
SkMatrix44 iden2;
|
|
|
|
SkMatrix44 rot;
|
2011-06-09 19:13:41 +00:00
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
mat.setTranslate(1, 1, 1);
|
2011-06-09 19:13:41 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
mat.setScale(2, 2, 2);
|
2011-06-09 19:13:41 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
mat.setScale(SK_MScalar1/2, SK_MScalar1/2, SK_MScalar1/2);
|
2011-06-09 19:13:41 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
2012-12-03 21:19:52 +00:00
|
|
|
mat.setScale(3, 3, 3);
|
|
|
|
rot.setRotateDegreesAbout(0, 0, -1, 90);
|
2011-06-09 19:13:41 +00:00
|
|
|
mat.postConcat(rot);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(nullptr));
|
2011-06-09 19:13:41 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
iden2.setConcat(inverse, mat);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden2));
|
2011-06-13 14:46:52 +00:00
|
|
|
|
2013-08-20 15:25:04 +00:00
|
|
|
// test tiny-valued matrix inverse
|
|
|
|
mat.reset();
|
2015-06-09 18:47:01 +00:00
|
|
|
auto v = SkDoubleToMScalar(1.0e-12);
|
|
|
|
mat.setScale(v,v,v);
|
2013-08-20 15:25:04 +00:00
|
|
|
rot.setRotateDegreesAbout(0, 0, -1, 90);
|
|
|
|
mat.postConcat(rot);
|
2015-06-09 18:47:01 +00:00
|
|
|
mat.postTranslate(v,v,v);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(nullptr));
|
2013-08-20 15:25:04 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2013-08-21 07:01:29 +00:00
|
|
|
|
2013-08-20 15:25:04 +00:00
|
|
|
// test mixed-valued matrix inverse
|
|
|
|
mat.reset();
|
2015-06-09 22:06:22 +00:00
|
|
|
mat.setScale(SkDoubleToMScalar(1.0e-2),
|
2015-06-09 18:47:01 +00:00
|
|
|
SkDoubleToMScalar(3.0),
|
2015-06-09 22:06:22 +00:00
|
|
|
SkDoubleToMScalar(1.0e+2));
|
2013-08-20 15:25:04 +00:00
|
|
|
rot.setRotateDegreesAbout(0, 0, -1, 90);
|
|
|
|
mat.postConcat(rot);
|
2015-06-09 22:06:22 +00:00
|
|
|
mat.postTranslate(SkDoubleToMScalar(1.0e+2),
|
2015-06-09 18:47:01 +00:00
|
|
|
SkDoubleToMScalar(3.0),
|
2015-06-09 22:06:22 +00:00
|
|
|
SkDoubleToMScalar(1.0e-2));
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(nullptr));
|
2013-08-20 15:25:04 +00:00
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2013-08-21 07:01:29 +00:00
|
|
|
|
2013-08-20 15:25:04 +00:00
|
|
|
// test degenerate matrix
|
|
|
|
mat.reset();
|
|
|
|
mat.set3x3(1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(nullptr));
|
2013-08-21 07:01:29 +00:00
|
|
|
|
2011-06-13 14:46:52 +00:00
|
|
|
// test rol/col Major getters
|
|
|
|
{
|
|
|
|
mat.setTranslate(2, 3, 4);
|
|
|
|
float dataf[16];
|
|
|
|
double datad[16];
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-06-13 14:46:52 +00:00
|
|
|
mat.asColMajorf(dataf);
|
|
|
|
assert16<float>(reporter, dataf,
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
2, 3, 4, 1);
|
|
|
|
mat.asColMajord(datad);
|
|
|
|
assert16<double>(reporter, datad, 1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
2, 3, 4, 1);
|
|
|
|
mat.asRowMajorf(dataf);
|
|
|
|
assert16<float>(reporter, dataf, 1, 0, 0, 2,
|
|
|
|
0, 1, 0, 3,
|
|
|
|
0, 0, 1, 4,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
mat.asRowMajord(datad);
|
|
|
|
assert16<double>(reporter, datad, 1, 0, 0, 2,
|
|
|
|
0, 1, 0, 3,
|
|
|
|
0, 0, 1, 4,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
2011-06-24 18:13:39 +00:00
|
|
|
|
2012-11-09 21:25:06 +00:00
|
|
|
test_concat(reporter);
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
if (false) { // avoid bit rot, suppress warning (working on making this pass)
|
|
|
|
test_common_angles(reporter);
|
|
|
|
}
|
2012-11-13 15:08:22 +00:00
|
|
|
|
2012-12-10 20:16:10 +00:00
|
|
|
test_constructor(reporter);
|
2012-12-03 21:19:52 +00:00
|
|
|
test_gettype(reporter);
|
2012-11-13 15:08:22 +00:00
|
|
|
test_determinant(reporter);
|
2013-08-20 20:15:24 +00:00
|
|
|
test_invert(reporter);
|
2012-11-14 21:33:55 +00:00
|
|
|
test_transpose(reporter);
|
|
|
|
test_get_set_double(reporter);
|
2012-12-03 21:19:52 +00:00
|
|
|
test_set_row_col_major(reporter);
|
2016-06-23 19:42:29 +00:00
|
|
|
test_set_3x3(reporter);
|
2012-12-05 22:13:59 +00:00
|
|
|
test_translate(reporter);
|
|
|
|
test_scale(reporter);
|
|
|
|
test_map2(reporter);
|
2013-10-05 01:16:30 +00:00
|
|
|
test_3x3_conversion(reporter);
|
2014-09-16 15:29:29 +00:00
|
|
|
test_has_perspective(reporter);
|
2014-09-26 18:45:48 +00:00
|
|
|
test_preserves_2d_axis_alignment(reporter);
|
2014-10-21 19:33:21 +00:00
|
|
|
test_toint(reporter);
|
2011-06-09 19:13:41 +00:00
|
|
|
}
|
2020-01-08 15:40:39 +00:00
|
|
|
|
|
|
|
static bool eq(const SkMatrix44& a, const SkM44& b, float tol) {
|
|
|
|
float fa[16], fb[16];
|
|
|
|
a.asColMajorf(fa);
|
|
|
|
b.getColMajor(fb);
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
if (!SkScalarNearlyEqual(fa[i], fb[i], tol)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eq(const SkM44& a, const SkM44& b, float tol) {
|
|
|
|
float fa[16], fb[16];
|
|
|
|
a.getColMajor(fa);
|
|
|
|
b.getColMajor(fb);
|
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
if (!SkScalarNearlyEqual(fa[i], fb[i], tol)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(M44, reporter) {
|
|
|
|
SkM44 m, im;
|
|
|
|
SkMatrix44 m44, im44;
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, eq(m44, m, 0));
|
|
|
|
REPORTER_ASSERT(reporter, SkM44() == m);
|
|
|
|
REPORTER_ASSERT(reporter, m.invert(&im));
|
|
|
|
REPORTER_ASSERT(reporter, SkM44() == im);
|
|
|
|
|
|
|
|
m.setTranslate(3, 4, 2);
|
|
|
|
m44.setTranslate(3, 4, 2);
|
|
|
|
REPORTER_ASSERT(reporter, eq(m44, m, 0));
|
|
|
|
REPORTER_ASSERT(reporter, SkM44(1, 0, 0, 3,
|
|
|
|
0, 1, 0, 4,
|
|
|
|
0, 0, 1, 2,
|
|
|
|
0, 0, 0, 1) == m);
|
|
|
|
|
|
|
|
const float f[] = { 1, 0, 0, 2, 3, 1, 2, 5, 0, 5, 3, 0, 0, 1, 0, 2 };
|
|
|
|
m.setColMajor(f);
|
|
|
|
m44.setColMajorf(f);
|
|
|
|
REPORTER_ASSERT(reporter, eq(m44, m, 0));
|
|
|
|
|
|
|
|
m.setRowMajor(f);
|
|
|
|
m44.setRowMajorf(f);
|
|
|
|
REPORTER_ASSERT(reporter, eq(m44, m, 0));
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, m.invert(&im));
|
|
|
|
REPORTER_ASSERT(reporter, m44.invert(&im44));
|
|
|
|
REPORTER_ASSERT(reporter, eq(im44, im, 0));
|
|
|
|
|
|
|
|
m = m * im;
|
|
|
|
// m should be identity now, but our calc is not perfect...
|
|
|
|
REPORTER_ASSERT(reporter, eq(SkM44(), m, 0.0000005f));
|
|
|
|
REPORTER_ASSERT(reporter, SkM44() != m);
|
|
|
|
}
|