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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMath.h"
|
|
|
|
#include "include/core/SkPoint3.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkMatrixPriv.h"
|
|
|
|
#include "src/core/SkMatrixUtils.h"
|
|
|
|
#include "tests/Test.h"
|
2009-02-27 16:24:51 +00:00
|
|
|
|
|
|
|
static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
|
2011-05-17 17:36:59 +00:00
|
|
|
const SkScalar tolerance = SK_Scalar1 / 200000;
|
2009-02-27 16:24:51 +00:00
|
|
|
return SkScalarAbs(a - b) <= tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool nearly_equal(const SkMatrix& a, const SkMatrix& b) {
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
if (!nearly_equal_scalar(a[i], b[i])) {
|
2018-07-10 14:11:01 +00:00
|
|
|
SkDebugf("matrices not equal [%d] %g %g\n", i, (float)a[i], (float)b[i]);
|
2009-02-27 16:24:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-14 18:41:22 +00:00
|
|
|
static int float_bits(float f) {
|
|
|
|
int result;
|
|
|
|
memcpy(&result, &f, 4);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-26 15:24:27 +00:00
|
|
|
static bool are_equal(skiatest::Reporter* reporter,
|
|
|
|
const SkMatrix& a,
|
|
|
|
const SkMatrix& b) {
|
|
|
|
bool equal = a == b;
|
2019-12-18 21:47:47 +00:00
|
|
|
bool cheapEqual = SkMatrixPriv::CheapEqual(a, b);
|
2012-03-26 15:24:27 +00:00
|
|
|
if (equal != cheapEqual) {
|
2012-03-26 17:25:45 +00:00
|
|
|
if (equal) {
|
2012-03-26 15:24:27 +00:00
|
|
|
bool foundZeroSignDiff = false;
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
|
|
|
float aVal = a.get(i);
|
|
|
|
float bVal = b.get(i);
|
2018-06-14 18:41:22 +00:00
|
|
|
int aValI = float_bits(aVal);
|
|
|
|
int bValI = float_bits(bVal);
|
2012-03-26 15:24:27 +00:00
|
|
|
if (0 == aVal && 0 == bVal && aValI != bValI) {
|
|
|
|
foundZeroSignDiff = true;
|
|
|
|
} else {
|
2016-05-20 17:36:50 +00:00
|
|
|
REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
|
2012-03-26 15:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, foundZeroSignDiff);
|
|
|
|
} else {
|
|
|
|
bool foundNaN = false;
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
|
|
|
float aVal = a.get(i);
|
|
|
|
float bVal = b.get(i);
|
2018-06-14 18:41:22 +00:00
|
|
|
int aValI = float_bits(aVal);
|
|
|
|
int bValI = float_bits(bVal);
|
2012-03-26 15:24:27 +00:00
|
|
|
if (sk_float_isnan(aVal) && aValI == bValI) {
|
|
|
|
foundNaN = true;
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, foundNaN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
static bool is_identity(const SkMatrix& m) {
|
|
|
|
SkMatrix identity;
|
2009-04-02 16:59:40 +00:00
|
|
|
identity.reset();
|
2009-02-27 16:24:51 +00:00
|
|
|
return nearly_equal(m, identity);
|
|
|
|
}
|
|
|
|
|
2014-12-13 16:46:48 +00:00
|
|
|
static void assert9(skiatest::Reporter* reporter, const SkMatrix& m,
|
|
|
|
SkScalar a, SkScalar b, SkScalar c,
|
|
|
|
SkScalar d, SkScalar e, SkScalar f,
|
|
|
|
SkScalar g, SkScalar h, SkScalar i) {
|
|
|
|
SkScalar buffer[9];
|
|
|
|
m.get9(buffer);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[0] == a);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[1] == b);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[2] == c);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[3] == d);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[4] == e);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[5] == f);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[6] == g);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[7] == h);
|
|
|
|
REPORTER_ASSERT(reporter, buffer[8] == i);
|
2020-09-22 20:52:40 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(0, 0) == a);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(0, 1) == b);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(0, 2) == c);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(1, 0) == d);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(1, 1) == e);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(1, 2) == f);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(2, 0) == g);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(2, 1) == h);
|
|
|
|
REPORTER_ASSERT(reporter, m.rc(2, 2) == i);
|
2014-12-13 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_set9(skiatest::Reporter* reporter) {
|
|
|
|
|
|
|
|
SkMatrix m;
|
|
|
|
m.reset();
|
|
|
|
assert9(reporter, m, 1, 0, 0, 0, 1, 0, 0, 0, 1);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-12-13 16:46:48 +00:00
|
|
|
m.setScale(2, 3);
|
|
|
|
assert9(reporter, m, 2, 0, 0, 0, 3, 0, 0, 0, 1);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2014-12-13 16:46:48 +00:00
|
|
|
m.postTranslate(4, 5);
|
|
|
|
assert9(reporter, m, 2, 0, 4, 0, 3, 5, 0, 0, 1);
|
|
|
|
|
|
|
|
SkScalar buffer[9];
|
|
|
|
sk_bzero(buffer, sizeof(buffer));
|
|
|
|
buffer[SkMatrix::kMScaleX] = 1;
|
|
|
|
buffer[SkMatrix::kMScaleY] = 1;
|
|
|
|
buffer[SkMatrix::kMPersp2] = 1;
|
|
|
|
REPORTER_ASSERT(reporter, !m.isIdentity());
|
|
|
|
m.set9(buffer);
|
|
|
|
REPORTER_ASSERT(reporter, m.isIdentity());
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:35:48 +00:00
|
|
|
static void test_matrix_recttorect(skiatest::Reporter* reporter) {
|
|
|
|
SkRect src, dst;
|
|
|
|
SkMatrix matrix;
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2019-08-24 23:39:13 +00:00
|
|
|
src.setLTRB(0, 0, 10, 10);
|
2012-10-12 14:35:48 +00:00
|
|
|
dst = src;
|
2021-01-15 17:26:22 +00:00
|
|
|
matrix = SkMatrix::RectToRect(src, dst);
|
2012-10-12 14:35:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
dst.offset(1, 1);
|
2021-01-15 17:26:22 +00:00
|
|
|
matrix = SkMatrix::RectToRect(src, dst);
|
2012-10-12 14:35:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
2012-10-13 02:01:56 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
dst.fRight += 1;
|
2021-01-15 17:26:22 +00:00
|
|
|
matrix = SkMatrix::RectToRect(src, dst);
|
2012-12-04 02:01:25 +00:00
|
|
|
REPORTER_ASSERT(reporter,
|
2012-12-03 17:35:19 +00:00
|
|
|
(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask) == matrix.getType());
|
2012-10-12 14:35:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
|
|
|
|
|
|
|
dst = src;
|
|
|
|
dst.fRight = src.fRight * 2;
|
2021-01-15 17:26:22 +00:00
|
|
|
matrix = SkMatrix::RectToRect(src, dst);
|
2012-10-12 14:35:48 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
|
|
|
}
|
|
|
|
|
2009-06-29 16:14:41 +00:00
|
|
|
static void test_flatten(skiatest::Reporter* reporter, const SkMatrix& m) {
|
|
|
|
// add 100 in case we have a bug, I don't want to kill my stack in the test
|
2017-10-20 16:14:33 +00:00
|
|
|
static const size_t kBufferSize = SkMatrixPriv::kMaxFlattenSize + 100;
|
2013-11-05 15:46:56 +00:00
|
|
|
char buffer[kBufferSize];
|
2017-10-20 16:14:33 +00:00
|
|
|
size_t size1 = SkMatrixPriv::WriteToMemory(m, nullptr);
|
|
|
|
size_t size2 = SkMatrixPriv::WriteToMemory(m, buffer);
|
2009-06-29 16:14:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, size1 == size2);
|
2017-10-20 16:14:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, size1 <= SkMatrixPriv::kMaxFlattenSize);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2009-06-29 16:14:41 +00:00
|
|
|
SkMatrix m2;
|
2017-10-20 16:14:33 +00:00
|
|
|
size_t size3 = SkMatrixPriv::ReadFromMemory(&m2, buffer, kBufferSize);
|
2012-06-08 18:30:46 +00:00
|
|
|
REPORTER_ASSERT(reporter, size1 == size3);
|
2012-03-26 15:24:27 +00:00
|
|
|
REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2013-11-05 15:46:56 +00:00
|
|
|
char buffer2[kBufferSize];
|
2017-10-20 16:14:33 +00:00
|
|
|
size3 = SkMatrixPriv::WriteToMemory(m2, buffer2);
|
2012-06-08 18:30:46 +00:00
|
|
|
REPORTER_ASSERT(reporter, size1 == size3);
|
2009-06-29 16:14:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:53:45 +00:00
|
|
|
static void test_matrix_min_max_scale(skiatest::Reporter* reporter) {
|
2014-05-20 17:02:03 +00:00
|
|
|
SkScalar scales[2];
|
|
|
|
bool success;
|
|
|
|
|
2011-09-09 19:32:04 +00:00
|
|
|
SkMatrix identity;
|
|
|
|
identity.reset();
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, 1 == identity.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == identity.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
success = identity.getMinMaxScales(scales);
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, success && 1 == scales[0] && 1 == scales[1]);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix scale;
|
2019-07-18 15:37:43 +00:00
|
|
|
scale.setScale(2, 4);
|
|
|
|
REPORTER_ASSERT(reporter, 2 == scale.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, 4 == scale.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
success = scale.getMinMaxScales(scales);
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, success && 2 == scales[0] && 4 == scales[1]);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix rot90Scale;
|
2019-07-18 15:37:43 +00:00
|
|
|
rot90Scale.setRotate(90).postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
|
2014-05-20 14:53:45 +00:00
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 / 4 == rot90Scale.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
success = rot90Scale.getMinMaxScales(scales);
|
|
|
|
REPORTER_ASSERT(reporter, success && SK_Scalar1 / 4 == scales[0] && SK_Scalar1 / 2 == scales[1]);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix rotate;
|
2019-07-18 15:37:43 +00:00
|
|
|
rotate.setRotate(128);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, rotate.getMinScale(), SK_ScalarNearlyZero));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, rotate.getMaxScale(), SK_ScalarNearlyZero));
|
2014-05-20 17:02:03 +00:00
|
|
|
success = rotate.getMinMaxScales(scales);
|
|
|
|
REPORTER_ASSERT(reporter, success);
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, scales[0], SK_ScalarNearlyZero));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(1, scales[1], SK_ScalarNearlyZero));
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix translate;
|
2019-07-18 15:37:43 +00:00
|
|
|
translate.setTranslate(10, -5);
|
|
|
|
REPORTER_ASSERT(reporter, 1 == translate.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, 1 == translate.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
success = translate.getMinMaxScales(scales);
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, success && 1 == scales[0] && 1 == scales[1]);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix perspX;
|
2019-07-18 15:37:43 +00:00
|
|
|
perspX.reset().setPerspX(SK_Scalar1 / 1000);
|
|
|
|
REPORTER_ASSERT(reporter, -1 == perspX.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, -1 == perspX.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
success = perspX.getMinMaxScales(scales);
|
2015-12-17 23:33:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !success);
|
|
|
|
|
|
|
|
// skbug.com/4718
|
|
|
|
SkMatrix big;
|
|
|
|
big.setAll(2.39394089e+36f, 8.85347779e+36f, 9.26526204e+36f,
|
|
|
|
3.9159619e+36f, 1.44823453e+37f, 1.51559342e+37f,
|
|
|
|
0.f, 0.f, 1.f);
|
|
|
|
success = big.getMinMaxScales(scales);
|
|
|
|
REPORTER_ASSERT(reporter, !success);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
2016-07-16 18:52:37 +00:00
|
|
|
// skbug.com/4718
|
|
|
|
SkMatrix givingNegativeNearlyZeros;
|
|
|
|
givingNegativeNearlyZeros.setAll(0.00436534f, 0.114138f, 0.37141f,
|
|
|
|
0.00358857f, 0.0936228f, -0.0174198f,
|
|
|
|
0.f, 0.f, 1.f);
|
|
|
|
success = givingNegativeNearlyZeros.getMinMaxScales(scales);
|
|
|
|
REPORTER_ASSERT(reporter, success && 0 == scales[0]);
|
|
|
|
|
2011-09-09 19:32:04 +00:00
|
|
|
SkMatrix perspY;
|
2019-07-18 15:37:43 +00:00
|
|
|
perspY.reset().setPerspY(-SK_Scalar1 / 500);
|
|
|
|
REPORTER_ASSERT(reporter, -1 == perspY.getMinScale());
|
|
|
|
REPORTER_ASSERT(reporter, -1 == perspY.getMaxScale());
|
2014-05-20 17:02:03 +00:00
|
|
|
scales[0] = -5;
|
|
|
|
scales[1] = -5;
|
|
|
|
success = perspY.getMinMaxScales(scales);
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, !success && -5 == scales[0] && -5 == scales[1]);
|
2011-09-09 19:32:04 +00:00
|
|
|
|
|
|
|
SkMatrix baseMats[] = {scale, rot90Scale, rotate,
|
|
|
|
translate, perspX, perspY};
|
|
|
|
SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
|
2011-10-27 15:27:51 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
|
2011-09-09 19:32:04 +00:00
|
|
|
mats[i] = baseMats[i];
|
2016-08-24 19:01:31 +00:00
|
|
|
bool invertible = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
|
|
|
|
REPORTER_ASSERT(reporter, invertible);
|
2011-09-09 19:32:04 +00:00
|
|
|
}
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2011-09-09 19:32:04 +00:00
|
|
|
for (int m = 0; m < 1000; ++m) {
|
|
|
|
SkMatrix mat;
|
|
|
|
mat.reset();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
int x = rand.nextU() % SK_ARRAY_COUNT(mats);
|
|
|
|
mat.postConcat(mats[x]);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2014-05-20 14:53:45 +00:00
|
|
|
SkScalar minScale = mat.getMinScale();
|
|
|
|
SkScalar maxScale = mat.getMaxScale();
|
|
|
|
REPORTER_ASSERT(reporter, (minScale < 0) == (maxScale < 0));
|
|
|
|
REPORTER_ASSERT(reporter, (maxScale < 0) == mat.hasPerspective());
|
2011-09-09 19:32:04 +00:00
|
|
|
|
2021-08-12 14:48:09 +00:00
|
|
|
success = mat.getMinMaxScales(scales);
|
2014-05-20 17:02:03 +00:00
|
|
|
REPORTER_ASSERT(reporter, success == !mat.hasPerspective());
|
|
|
|
REPORTER_ASSERT(reporter, !success || (scales[0] == minScale && scales[1] == maxScale));
|
|
|
|
|
2011-09-09 19:32:04 +00:00
|
|
|
if (mat.hasPerspective()) {
|
|
|
|
m -= 1; // try another non-persp matrix
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:53:45 +00:00
|
|
|
// test a bunch of vectors. All should be scaled by between minScale and maxScale
|
2013-12-09 19:15:37 +00:00
|
|
|
// (modulo some error) and we should find a vector that is scaled by almost each.
|
2014-05-20 14:53:45 +00:00
|
|
|
static const SkScalar gVectorScaleTol = (105 * SK_Scalar1) / 100;
|
|
|
|
static const SkScalar gCloseScaleTol = (97 * SK_Scalar1) / 100;
|
2013-12-09 19:15:37 +00:00
|
|
|
SkScalar max = 0, min = SK_ScalarMax;
|
2011-09-09 19:32:04 +00:00
|
|
|
SkVector vectors[1000];
|
2011-10-27 15:27:51 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
|
2011-09-09 19:32:04 +00:00
|
|
|
vectors[i].fX = rand.nextSScalar1();
|
|
|
|
vectors[i].fY = rand.nextSScalar1();
|
|
|
|
if (!vectors[i].normalize()) {
|
|
|
|
i -= 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mat.mapVectors(vectors, SK_ARRAY_COUNT(vectors));
|
2011-10-27 15:27:51 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
|
2011-09-09 19:32:04 +00:00
|
|
|
SkScalar d = vectors[i].length();
|
2015-05-12 17:37:34 +00:00
|
|
|
REPORTER_ASSERT(reporter, d / maxScale < gVectorScaleTol);
|
|
|
|
REPORTER_ASSERT(reporter, minScale / d < gVectorScaleTol);
|
2011-09-09 19:32:04 +00:00
|
|
|
if (max < d) {
|
|
|
|
max = d;
|
|
|
|
}
|
2013-12-09 19:15:37 +00:00
|
|
|
if (min > d) {
|
|
|
|
min = d;
|
|
|
|
}
|
2011-09-09 19:32:04 +00:00
|
|
|
}
|
2015-05-12 17:37:34 +00:00
|
|
|
REPORTER_ASSERT(reporter, max / maxScale >= gCloseScaleTol);
|
|
|
|
REPORTER_ASSERT(reporter, minScale / min >= gCloseScaleTol);
|
2011-09-09 19:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 20:15:40 +00:00
|
|
|
static void test_matrix_preserve_shape(skiatest::Reporter* reporter) {
|
2012-04-25 15:07:40 +00:00
|
|
|
SkMatrix mat;
|
|
|
|
|
|
|
|
// identity
|
|
|
|
mat.setIdentity();
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// translation only
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setTranslate(100, 100);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// scale with same size
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(15, 15);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// scale with one negative
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(-15, 15);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// scale with different size
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(15, 20);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// scale with same size at a pivot point
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(15, 15, 2, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// scale with different size at a pivot point
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(15, 20, 2, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// skew with same size
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setSkew(15, 15);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// skew with different size
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setSkew(15, 20);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// skew with same size at a pivot point
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setSkew(15, 15, 2, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// skew with different size at a pivot point
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setSkew(15, 20, 2, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// perspective x
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.reset().setPerspX(SK_Scalar1 / 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// perspective y
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.reset().setPerspY(SK_Scalar1 / 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// rotate
|
|
|
|
for (int angle = 0; angle < 360; ++angle) {
|
|
|
|
mat.setRotate(SkIntToScalar(angle));
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// see if there are any accumulated precision issues
|
|
|
|
mat.reset();
|
|
|
|
for (int i = 1; i < 360; i++) {
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.postRotate(1);
|
2012-04-25 15:07:40 +00:00
|
|
|
}
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// rotate + translate
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(30).postTranslate(10, 20);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// rotate + uniform scale
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(30).postScale(2, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// rotate + non-uniform scale
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(30).postScale(3, 2);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
|
|
|
|
|
|
|
// non-uniform scale + rotate
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(3, 2).postRotate(30);
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// all zero
|
|
|
|
mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
|
|
|
// all zero except perspective
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 1);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.preservesRightAngles());
|
|
|
|
|
|
|
|
// scales zero, only skews (rotation)
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setAll(0, 1, 0,
|
|
|
|
-1, 0, 0,
|
|
|
|
0, 0, 1);
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
|
2014-09-02 20:15:40 +00:00
|
|
|
// scales zero, only skews (reflection)
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setAll(0, 1, 0,
|
|
|
|
1, 0, 0,
|
|
|
|
0, 0, 1);
|
2013-01-22 13:34:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.isSimilarity());
|
2014-09-02 20:15:40 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.preservesRightAngles());
|
2012-04-25 15:07:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:08:08 +00:00
|
|
|
// For test_matrix_decomposition, below.
|
2013-07-25 07:01:00 +00:00
|
|
|
static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
|
2013-07-24 18:08:08 +00:00
|
|
|
SkScalar tolerance = SK_ScalarNearlyZero) {
|
|
|
|
// from Bruce Dawson
|
2013-09-03 19:08:14 +00:00
|
|
|
// absolute check
|
2013-07-24 18:08:08 +00:00
|
|
|
SkScalar diff = SkScalarAbs(a - b);
|
|
|
|
if (diff < tolerance) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
// relative check
|
2013-07-24 18:08:08 +00:00
|
|
|
a = SkScalarAbs(a);
|
|
|
|
b = SkScalarAbs(b);
|
|
|
|
SkScalar largest = (b > a) ? b : a;
|
|
|
|
|
|
|
|
if (diff <= largest*tolerance) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
static bool check_matrix_recomposition(const SkMatrix& mat,
|
|
|
|
const SkPoint& rotation1,
|
|
|
|
const SkPoint& scale,
|
|
|
|
const SkPoint& rotation2) {
|
|
|
|
SkScalar c1 = rotation1.fX;
|
|
|
|
SkScalar s1 = rotation1.fY;
|
|
|
|
SkScalar scaleX = scale.fX;
|
|
|
|
SkScalar scaleY = scale.fY;
|
|
|
|
SkScalar c2 = rotation2.fX;
|
|
|
|
SkScalar s2 = rotation2.fY;
|
2013-09-04 07:01:39 +00:00
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
// We do a relative check here because large scale factors cause problems with an absolute check
|
|
|
|
bool result = scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
|
|
|
|
scaleX*c1*c2 - scaleY*s1*s2) &&
|
|
|
|
scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
|
|
|
|
-scaleX*s1*c2 - scaleY*c1*s2) &&
|
|
|
|
scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
|
|
|
|
scaleX*c1*s2 + scaleY*s1*c2) &&
|
|
|
|
scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
|
|
|
|
-scaleX*s1*s2 + scaleY*c1*c2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:08:08 +00:00
|
|
|
static void test_matrix_decomposition(skiatest::Reporter* reporter) {
|
|
|
|
SkMatrix mat;
|
2013-09-03 19:08:14 +00:00
|
|
|
SkPoint rotation1, scale, rotation2;
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
const float kRotation0 = 15.5f;
|
|
|
|
const float kRotation1 = -50.f;
|
|
|
|
const float kScale0 = 5000.f;
|
|
|
|
const float kScale1 = 0.001f;
|
|
|
|
|
|
|
|
// identity
|
|
|
|
mat.reset();
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
// make sure it doesn't crash if we pass in NULLs
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, nullptr, nullptr, nullptr));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation only
|
|
|
|
mat.setRotate(kRotation0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// uniform scale only
|
|
|
|
mat.setScale(kScale0, kScale0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// anisotropic scale only
|
|
|
|
mat.setScale(kScale1, kScale0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation then uniform scale
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation1).postScale(kScale0, kScale0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// uniform scale then rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(kScale0, kScale0).postRotate(kRotation1);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation then uniform scale+reflection
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation0).postScale(kScale1, -kScale1);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// uniform scale+reflection, then rotate
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(kScale0, -kScale0).postRotate(kRotation1);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation then anisotropic scale
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation1).postScale(kScale1, kScale0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
// rotation then anisotropic scale
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(90).postScale(kScale1, kScale0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-09-04 07:01:39 +00:00
|
|
|
|
2013-07-24 18:08:08 +00:00
|
|
|
// anisotropic scale then rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(kScale1, kScale0).postRotate(kRotation0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-09-04 07:01:39 +00:00
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
// anisotropic scale then rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(kScale1, kScale0).postRotate(90);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation, uniform scale, then different rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation1).postScale(kScale0, kScale0).postRotate(kRotation0);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// rotation, anisotropic scale, then different rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation0).postScale(kScale1, kScale0).postRotate(kRotation1);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-09-04 07:01:39 +00:00
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
// rotation, anisotropic scale + reflection, then different rotation
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(kRotation0).postScale(-kScale1, kScale0).postRotate(kRotation1);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// try some random matrices
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-07-24 18:08:08 +00:00
|
|
|
for (int m = 0; m < 1000; ++m) {
|
2013-09-03 19:08:14 +00:00
|
|
|
SkScalar rot0 = rand.nextRangeF(-180, 180);
|
2013-07-24 18:08:08 +00:00
|
|
|
SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
|
|
|
|
SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
|
2013-09-03 19:08:14 +00:00
|
|
|
SkScalar rot1 = rand.nextRangeF(-180, 180);
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setRotate(rot0).postScale(sx, sy).postRotate(rot1);
|
2013-07-24 18:08:08 +00:00
|
|
|
|
2013-09-03 19:08:14 +00:00
|
|
|
if (SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2)) {
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
} else {
|
|
|
|
// if the matrix is degenerate, the basis vectors should be near-parallel or near-zero
|
|
|
|
SkScalar perpdot = mat[SkMatrix::kMScaleX]*mat[SkMatrix::kMScaleY] -
|
|
|
|
mat[SkMatrix::kMSkewX]*mat[SkMatrix::kMSkewY];
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyZero(perpdot));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// translation shouldn't affect this
|
|
|
|
mat.postTranslate(-1000.f, 1000.f);
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// perspective shouldn't affect this
|
2013-07-24 18:44:10 +00:00
|
|
|
mat[SkMatrix::kMPersp0] = 12.f;
|
|
|
|
mat[SkMatrix::kMPersp1] = 4.f;
|
|
|
|
mat[SkMatrix::kMPersp2] = 1872.f;
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
|
|
|
REPORTER_ASSERT(reporter, check_matrix_recomposition(mat, rotation1, scale, rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
|
|
|
|
// degenerate matrices
|
|
|
|
// mostly zero entries
|
|
|
|
mat.reset();
|
|
|
|
mat[SkMatrix::kMScaleX] = 0.f;
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
mat.reset();
|
|
|
|
mat[SkMatrix::kMScaleY] = 0.f;
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
mat.reset();
|
|
|
|
// linearly dependent entries
|
|
|
|
mat[SkMatrix::kMScaleX] = 1.f;
|
|
|
|
mat[SkMatrix::kMSkewX] = 2.f;
|
|
|
|
mat[SkMatrix::kMSkewY] = 4.f;
|
|
|
|
mat[SkMatrix::kMScaleY] = 8.f;
|
2013-09-03 19:08:14 +00:00
|
|
|
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation1, &scale, &rotation2));
|
2013-07-24 18:08:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 21:12:11 +00:00
|
|
|
// For test_matrix_homogeneous, below.
|
2017-10-18 15:46:18 +00:00
|
|
|
static bool point3_array_nearly_equal_relative(const SkPoint3 a[], const SkPoint3 b[], int count) {
|
2013-08-15 21:12:11 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2017-10-18 15:46:18 +00:00
|
|
|
if (!scalar_nearly_equal_relative(a[i].fX, b[i].fX)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!scalar_nearly_equal_relative(a[i].fY, b[i].fY)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!scalar_nearly_equal_relative(a[i].fZ, b[i].fZ)) {
|
2013-08-15 21:12:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For test_matrix_homogeneous, below.
|
|
|
|
// Maps a single triple in src using m and compares results to those in dst
|
2017-10-18 15:46:18 +00:00
|
|
|
static bool naive_homogeneous_mapping(const SkMatrix& m, const SkPoint3& src,
|
|
|
|
const SkPoint3& dst) {
|
|
|
|
SkPoint3 res;
|
2013-08-15 21:12:11 +00:00
|
|
|
SkScalar ms[9] = {m[0], m[1], m[2],
|
|
|
|
m[3], m[4], m[5],
|
|
|
|
m[6], m[7], m[8]};
|
2017-10-18 15:46:18 +00:00
|
|
|
res.fX = src.fX * ms[0] + src.fY * ms[1] + src.fZ * ms[2];
|
|
|
|
res.fY = src.fX * ms[3] + src.fY * ms[4] + src.fZ * ms[5];
|
|
|
|
res.fZ = src.fX * ms[6] + src.fY * ms[7] + src.fZ * ms[8];
|
|
|
|
return point3_array_nearly_equal_relative(&res, &dst, 1);
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_matrix_homogeneous(skiatest::Reporter* reporter) {
|
|
|
|
SkMatrix mat;
|
|
|
|
|
|
|
|
const float kRotation0 = 15.5f;
|
|
|
|
const float kRotation1 = -50.f;
|
|
|
|
const float kScale0 = 5000.f;
|
|
|
|
|
2017-12-19 14:09:33 +00:00
|
|
|
#if defined(SK_BUILD_FOR_GOOGLE3)
|
|
|
|
// Stack frame size is limited in SK_BUILD_FOR_GOOGLE3.
|
2015-10-19 20:55:55 +00:00
|
|
|
const int kTripleCount = 100;
|
|
|
|
const int kMatrixCount = 100;
|
|
|
|
#else
|
2013-08-15 21:12:11 +00:00
|
|
|
const int kTripleCount = 1000;
|
|
|
|
const int kMatrixCount = 1000;
|
2015-10-19 20:55:55 +00:00
|
|
|
#endif
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand;
|
2013-08-15 21:12:11 +00:00
|
|
|
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 randTriples[kTripleCount];
|
|
|
|
for (int i = 0; i < kTripleCount; ++i) {
|
|
|
|
randTriples[i].fX = rand.nextRangeF(-3000.f, 3000.f);
|
|
|
|
randTriples[i].fY = rand.nextRangeF(-3000.f, 3000.f);
|
|
|
|
randTriples[i].fZ = rand.nextRangeF(-3000.f, 3000.f);
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix mats[kMatrixCount];
|
|
|
|
for (int i = 0; i < kMatrixCount; ++i) {
|
|
|
|
for (int j = 0; j < 9; ++j) {
|
|
|
|
mats[i].set(j, rand.nextRangeF(-3000.f, 3000.f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// identity
|
|
|
|
{
|
|
|
|
mat.reset();
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst[kTripleCount];
|
2013-08-15 21:12:11 +00:00
|
|
|
mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(randTriples, dst, kTripleCount));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 15:46:18 +00:00
|
|
|
const SkPoint3 zeros = {0.f, 0.f, 0.f};
|
2013-08-15 21:12:11 +00:00
|
|
|
// zero matrix
|
|
|
|
{
|
|
|
|
mat.setAll(0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f);
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst[kTripleCount];
|
2013-08-15 21:12:11 +00:00
|
|
|
mat.mapHomogeneousPoints(dst, randTriples, kTripleCount);
|
|
|
|
for (int i = 0; i < kTripleCount; ++i) {
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst[i], &zeros, 1));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// zero point
|
|
|
|
{
|
|
|
|
for (int i = 0; i < kMatrixCount; ++i) {
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst;
|
|
|
|
mats[i].mapHomogeneousPoints(&dst, &zeros, 1);
|
|
|
|
REPORTER_ASSERT(reporter, point3_array_nearly_equal_relative(&dst, &zeros, 1));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// doesn't crash with null dst, src, count == 0
|
|
|
|
{
|
2019-12-17 22:25:27 +00:00
|
|
|
mats[0].mapHomogeneousPoints(nullptr, (const SkPoint3*)nullptr, 0);
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// uniform scale of point
|
|
|
|
{
|
|
|
|
mat.setScale(kScale0, kScale0);
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst;
|
|
|
|
SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
|
2013-08-15 21:12:11 +00:00
|
|
|
SkPoint pnt;
|
2017-10-18 15:46:18 +00:00
|
|
|
pnt.set(src.fX, src.fY);
|
|
|
|
mat.mapHomogeneousPoints(&dst, &src, 1);
|
2013-08-15 21:12:11 +00:00
|
|
|
mat.mapPoints(&pnt, &pnt, 1);
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rotation of point
|
|
|
|
{
|
|
|
|
mat.setRotate(kRotation0);
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst;
|
|
|
|
SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
|
2013-08-15 21:12:11 +00:00
|
|
|
SkPoint pnt;
|
2017-10-18 15:46:18 +00:00
|
|
|
pnt.set(src.fX, src.fY);
|
|
|
|
mat.mapHomogeneousPoints(&dst, &src, 1);
|
2013-08-15 21:12:11 +00:00
|
|
|
mat.mapPoints(&pnt, &pnt, 1);
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rotation, scale, rotation of point
|
|
|
|
{
|
|
|
|
mat.setRotate(kRotation1);
|
|
|
|
mat.postScale(kScale0, kScale0);
|
|
|
|
mat.postRotate(kRotation0);
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst;
|
|
|
|
SkPoint3 src = {randTriples[0].fX, randTriples[0].fY, 1.f};
|
2013-08-15 21:12:11 +00:00
|
|
|
SkPoint pnt;
|
2017-10-18 15:46:18 +00:00
|
|
|
pnt.set(src.fX, src.fY);
|
|
|
|
mat.mapHomogeneousPoints(&dst, &src, 1);
|
2013-08-15 21:12:11 +00:00
|
|
|
mat.mapPoints(&pnt, &pnt, 1);
|
2017-10-18 15:46:18 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fX, pnt.fX));
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fY, pnt.fY));
|
2019-07-18 15:37:43 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(dst.fZ, 1));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// compare with naive approach
|
|
|
|
{
|
|
|
|
for (int i = 0; i < kMatrixCount; ++i) {
|
|
|
|
for (int j = 0; j < kTripleCount; ++j) {
|
2017-10-18 15:46:18 +00:00
|
|
|
SkPoint3 dst;
|
|
|
|
mats[i].mapHomogeneousPoints(&dst, &randTriples[j], 1);
|
|
|
|
REPORTER_ASSERT(reporter, naive_homogeneous_mapping(mats[i], randTriples[j], dst));
|
2013-08-15 21:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:11:01 +00:00
|
|
|
static bool check_decompScale(const SkMatrix& original) {
|
2015-03-19 23:10:54 +00:00
|
|
|
SkSize scale;
|
|
|
|
SkMatrix remaining;
|
|
|
|
|
2018-07-10 14:11:01 +00:00
|
|
|
if (!original.decomposeScale(&scale, &remaining)) {
|
2015-03-19 23:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (scale.width() <= 0 || scale.height() <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-10 14:11:01 +00:00
|
|
|
|
|
|
|
// First ensure that the decomposition reconstitutes back to the original
|
|
|
|
{
|
|
|
|
SkMatrix reconstituted = remaining;
|
|
|
|
|
2019-07-19 14:13:47 +00:00
|
|
|
reconstituted.preScale(scale.width(), scale.height());
|
2018-07-10 14:11:01 +00:00
|
|
|
if (!nearly_equal(original, reconstituted)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then push some points through both paths and make sure they are the same.
|
|
|
|
static const int kNumPoints = 5;
|
|
|
|
const SkPoint testPts[kNumPoints] = {
|
|
|
|
{ 0.0f, 0.0f },
|
|
|
|
{ 1.0f, 1.0f },
|
|
|
|
{ 1.0f, 0.5f },
|
|
|
|
{ -1.0f, -0.5f },
|
|
|
|
{ -1.0f, 2.0f }
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPoint v1[kNumPoints];
|
|
|
|
original.mapPoints(v1, testPts, kNumPoints);
|
|
|
|
|
|
|
|
SkPoint v2[kNumPoints];
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix scaleMat = SkMatrix::Scale(scale.width(), scale.height());
|
2018-07-10 14:11:01 +00:00
|
|
|
|
|
|
|
// Note, we intend the decomposition to be applied in the order scale and then remainder but,
|
|
|
|
// due to skbug.com/7211, the order is reversed!
|
2019-07-19 14:13:47 +00:00
|
|
|
scaleMat.mapPoints(v2, testPts, kNumPoints);
|
|
|
|
remaining.mapPoints(v2, kNumPoints);
|
2018-07-10 14:11:01 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kNumPoints; ++i) {
|
|
|
|
if (!SkPointPriv::EqualsWithinTolerance(v1[i], v2[i], 0.00001f)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-03-19 23:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_decompScale(skiatest::Reporter* reporter) {
|
|
|
|
SkMatrix m;
|
|
|
|
|
|
|
|
m.reset();
|
|
|
|
REPORTER_ASSERT(reporter, check_decompScale(m));
|
|
|
|
m.setScale(2, 3);
|
|
|
|
REPORTER_ASSERT(reporter, check_decompScale(m));
|
|
|
|
m.setRotate(35, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, check_decompScale(m));
|
|
|
|
|
|
|
|
m.setScale(1, 0);
|
|
|
|
REPORTER_ASSERT(reporter, !check_decompScale(m));
|
2018-07-10 14:11:01 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
m.setRotate(35, 0, 0).preScale(2, 3);
|
2018-07-10 14:11:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, check_decompScale(m));
|
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
m.setRotate(35, 0, 0).postScale(2, 3);
|
2018-07-10 14:11:01 +00:00
|
|
|
REPORTER_ASSERT(reporter, check_decompScale(m));
|
2015-03-19 23:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Matrix, reporter) {
|
2009-02-27 16:24:51 +00:00
|
|
|
SkMatrix mat, inverse, iden1, iden2;
|
|
|
|
|
|
|
|
mat.reset();
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setTranslate(1, 1);
|
2012-04-19 18:59:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 16:24:51 +00:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(2, 4);
|
2012-04-19 18:59:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 16:24:51 +00:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2009-06-29 16:14:41 +00:00
|
|
|
test_flatten(reporter, mat);
|
2009-02-27 16:24:51 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(SK_Scalar1/2, 2);
|
2012-04-19 18:59:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 16:24:51 +00:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2009-06-29 16:14:41 +00:00
|
|
|
test_flatten(reporter, mat);
|
2009-02-27 16:24:51 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(3, 5, 20, 0).postRotate(25);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(nullptr));
|
2012-04-19 18:59:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 16:24:51 +00:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
iden2.setConcat(inverse, mat);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden2));
|
2009-06-29 16:14:41 +00:00
|
|
|
test_flatten(reporter, mat);
|
|
|
|
test_flatten(reporter, iden2);
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(0, 1);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(nullptr));
|
2013-01-04 17:02:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(&inverse));
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(1, 0);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(nullptr));
|
2013-01-04 17:02:33 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(&inverse));
|
|
|
|
|
2015-06-19 12:14:26 +00:00
|
|
|
// Inverting this matrix results in a non-finite matrix
|
|
|
|
mat.setAll(0.0f, 1.0f, 2.0f,
|
|
|
|
0.0f, 1.0f, -3.40277175e+38f,
|
|
|
|
1.00003040f, 1.0f, 0.0f);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(nullptr));
|
2015-06-19 12:14:26 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.invert(&inverse));
|
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
// rectStaysRect test
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
SkScalar m00, m01, m10, m11;
|
|
|
|
bool mStaysRect;
|
|
|
|
}
|
|
|
|
gRectStaysRectSamples[] = {
|
2019-07-18 15:37:43 +00:00
|
|
|
{ 0, 0, 0, 0, false },
|
|
|
|
{ 0, 0, 0, 1, false },
|
|
|
|
{ 0, 0, 1, 0, false },
|
|
|
|
{ 0, 0, 1, 1, false },
|
|
|
|
{ 0, 1, 0, 0, false },
|
|
|
|
{ 0, 1, 0, 1, false },
|
|
|
|
{ 0, 1, 1, 0, true },
|
|
|
|
{ 0, 1, 1, 1, false },
|
|
|
|
{ 1, 0, 0, 0, false },
|
|
|
|
{ 1, 0, 0, 1, true },
|
|
|
|
{ 1, 0, 1, 0, false },
|
|
|
|
{ 1, 0, 1, 1, false },
|
|
|
|
{ 1, 1, 0, 0, false },
|
|
|
|
{ 1, 1, 0, 1, false },
|
|
|
|
{ 1, 1, 1, 0, false },
|
|
|
|
{ 1, 1, 1, 1, false }
|
2009-02-27 16:24:51 +00:00
|
|
|
};
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
|
|
|
|
SkMatrix m;
|
2009-04-02 16:59:40 +00:00
|
|
|
|
2009-02-27 16:24:51 +00:00
|
|
|
m.reset();
|
|
|
|
m.set(SkMatrix::kMScaleX, gRectStaysRectSamples[i].m00);
|
|
|
|
m.set(SkMatrix::kMSkewX, gRectStaysRectSamples[i].m01);
|
|
|
|
m.set(SkMatrix::kMSkewY, gRectStaysRectSamples[i].m10);
|
|
|
|
m.set(SkMatrix::kMScaleY, gRectStaysRectSamples[i].m11);
|
|
|
|
REPORTER_ASSERT(reporter,
|
|
|
|
m.rectStaysRect() == gRectStaysRectSamples[i].mStaysRect);
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 19:41:55 +00:00
|
|
|
|
2011-07-13 20:18:16 +00:00
|
|
|
mat.reset();
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.set(SkMatrix::kMScaleX, 1)
|
|
|
|
.set(SkMatrix::kMSkewX, 2)
|
|
|
|
.set(SkMatrix::kMTransX, 3)
|
|
|
|
.set(SkMatrix::kMSkewY, 4)
|
|
|
|
.set(SkMatrix::kMScaleY, 5)
|
|
|
|
.set(SkMatrix::kMTransY, 6);
|
2011-07-13 19:41:55 +00:00
|
|
|
SkScalar affine[6];
|
|
|
|
REPORTER_ASSERT(reporter, mat.asAffine(affine));
|
|
|
|
|
2017-10-13 13:26:00 +00:00
|
|
|
#define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
|
2011-07-13 19:41:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, affineEqual(ScaleX));
|
|
|
|
REPORTER_ASSERT(reporter, affineEqual(SkewY));
|
|
|
|
REPORTER_ASSERT(reporter, affineEqual(SkewX));
|
|
|
|
REPORTER_ASSERT(reporter, affineEqual(ScaleY));
|
|
|
|
REPORTER_ASSERT(reporter, affineEqual(TransX));
|
|
|
|
REPORTER_ASSERT(reporter, affineEqual(TransY));
|
|
|
|
#undef affineEqual
|
|
|
|
|
2015-01-21 03:58:36 +00:00
|
|
|
mat.set(SkMatrix::kMPersp1, SK_Scalar1 / 2);
|
2011-07-13 19:41:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, !mat.asAffine(affine));
|
2011-09-09 19:32:04 +00:00
|
|
|
|
2012-03-26 15:24:27 +00:00
|
|
|
SkMatrix mat2;
|
|
|
|
mat2.reset();
|
|
|
|
mat.reset();
|
|
|
|
SkScalar zero = 0;
|
|
|
|
mat.set(SkMatrix::kMSkewX, -zero);
|
|
|
|
REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
|
|
|
|
|
|
|
|
mat2.reset();
|
|
|
|
mat.reset();
|
|
|
|
mat.set(SkMatrix::kMSkewX, SK_ScalarNaN);
|
|
|
|
mat2.set(SkMatrix::kMSkewX, SK_ScalarNaN);
|
|
|
|
REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
|
|
|
|
|
2014-05-20 14:53:45 +00:00
|
|
|
test_matrix_min_max_scale(reporter);
|
2014-09-02 20:15:40 +00:00
|
|
|
test_matrix_preserve_shape(reporter);
|
2012-10-12 14:35:48 +00:00
|
|
|
test_matrix_recttorect(reporter);
|
2013-07-24 18:08:08 +00:00
|
|
|
test_matrix_decomposition(reporter);
|
2013-08-15 21:12:11 +00:00
|
|
|
test_matrix_homogeneous(reporter);
|
2014-12-13 16:46:48 +00:00
|
|
|
test_set9(reporter);
|
2015-03-19 23:10:54 +00:00
|
|
|
|
|
|
|
test_decompScale(reporter);
|
2016-07-06 03:10:42 +00:00
|
|
|
|
|
|
|
mat.setScaleTranslate(2, 3, 1, 4);
|
2019-07-18 15:37:43 +00:00
|
|
|
mat2.setScale(2, 3).postTranslate(1, 4);
|
2016-07-06 03:10:42 +00:00
|
|
|
REPORTER_ASSERT(reporter, mat == mat2);
|
2009-02-27 16:24:51 +00:00
|
|
|
}
|
2014-05-19 15:51:12 +00:00
|
|
|
|
|
|
|
DEF_TEST(Matrix_Concat, r) {
|
|
|
|
SkMatrix a;
|
|
|
|
a.setTranslate(10, 20);
|
|
|
|
|
|
|
|
SkMatrix b;
|
|
|
|
b.setScale(3, 5);
|
|
|
|
|
|
|
|
SkMatrix expected;
|
|
|
|
expected.setConcat(a,b);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(r, expected == SkMatrix::Concat(a, b));
|
|
|
|
}
|
2016-06-30 13:38:54 +00:00
|
|
|
|
|
|
|
// Test that all variants of maprect are correct.
|
|
|
|
DEF_TEST(Matrix_maprects, r) {
|
|
|
|
const SkScalar scale = 1000;
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-06-30 13:38:54 +00:00
|
|
|
SkMatrix mat;
|
2019-07-18 15:37:43 +00:00
|
|
|
mat.setScale(2, 3).postTranslate(1, 4);
|
2016-06-30 13:38:54 +00:00
|
|
|
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < 10000; ++i) {
|
|
|
|
SkRect src = SkRect::MakeLTRB(rand.nextSScalar1() * scale,
|
|
|
|
rand.nextSScalar1() * scale,
|
|
|
|
rand.nextSScalar1() * scale,
|
|
|
|
rand.nextSScalar1() * scale);
|
2018-05-17 01:28:55 +00:00
|
|
|
SkRect dst[4];
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2016-06-30 13:38:54 +00:00
|
|
|
mat.mapPoints((SkPoint*)&dst[0].fLeft, (SkPoint*)&src.fLeft, 2);
|
|
|
|
dst[0].sort();
|
|
|
|
mat.mapRect(&dst[1], src);
|
2016-08-10 14:13:21 +00:00
|
|
|
mat.mapRectScaleTranslate(&dst[2], src);
|
2018-05-17 01:28:55 +00:00
|
|
|
dst[3] = mat.mapRect(src);
|
2016-06-30 13:38:54 +00:00
|
|
|
|
|
|
|
REPORTER_ASSERT(r, dst[0] == dst[1]);
|
|
|
|
REPORTER_ASSERT(r, dst[0] == dst[2]);
|
2018-05-17 01:28:55 +00:00
|
|
|
REPORTER_ASSERT(r, dst[0] == dst[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should report nonfinite-ness after a mapping
|
|
|
|
{
|
|
|
|
// We have special-cases in mapRect for different matrix types
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix m0 = SkMatrix::Scale(1e20f, 1e20f);
|
2018-05-17 01:28:55 +00:00
|
|
|
SkMatrix m1; m1.setRotate(30); m1.postScale(1e20f, 1e20f);
|
|
|
|
|
|
|
|
for (const auto& m : { m0, m1 }) {
|
|
|
|
SkRect rect = { 0, 0, 1e20f, 1e20f };
|
|
|
|
REPORTER_ASSERT(r, rect.isFinite());
|
|
|
|
rect = m.mapRect(rect);
|
|
|
|
REPORTER_ASSERT(r, !rect.isFinite());
|
|
|
|
}
|
2016-06-30 13:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-15 21:12:31 +00:00
|
|
|
|
2021-08-13 13:24:25 +00:00
|
|
|
DEF_TEST(Matrix_mapRect_skbug12335, r) {
|
|
|
|
// Stripped down test case from skbug.com/12335. Essentially, the corners of this rect would
|
|
|
|
// map to homogoneous coords with very small w's (below the old value of kW0PlaneDistance) and
|
|
|
|
// so they would be clipped "behind" the plane, resulting in an empty mapped rect. Coordinates
|
|
|
|
// with positive that wouldn't overflow when divided by w should still be included in the mapped
|
|
|
|
// rectangle.
|
|
|
|
SkRect rect = SkRect::MakeLTRB(0, 0, 319, 620);
|
|
|
|
SkMatrix m = SkMatrix::MakeAll( 0.000152695269f, 0.00000000f, -6.53848401e-05f,
|
|
|
|
-1.75697533e-05f, 0.000157153074f, -1.10847975e-06f,
|
|
|
|
-6.00415362e-08f, 0.00000000f, 0.000169880834f);
|
|
|
|
SkRect out = m.mapRect(rect);
|
|
|
|
REPORTER_ASSERT(r, !out.isEmpty());
|
|
|
|
}
|
|
|
|
|
2019-04-15 21:12:31 +00:00
|
|
|
DEF_TEST(Matrix_Ctor, r) {
|
|
|
|
REPORTER_ASSERT(r, SkMatrix{} == SkMatrix::I());
|
|
|
|
}
|