2011-07-28 18:26:00 +04: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.
|
|
|
|
*/
|
2009-02-27 19:24:51 +03:00
|
|
|
#include "Test.h"
|
2011-09-27 21:38:17 +04:00
|
|
|
#include "SkMath.h"
|
2009-02-27 19:24:51 +03:00
|
|
|
#include "SkMatrix.h"
|
2011-09-09 23:32:04 +04:00
|
|
|
#include "SkRandom.h"
|
2009-02-27 19:24:51 +03:00
|
|
|
|
|
|
|
static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
|
2011-05-17 21:36:59 +04:00
|
|
|
// Note that we get more compounded error for multiple operations when
|
|
|
|
// SK_SCALAR_IS_FIXED.
|
2009-02-27 19:24:51 +03:00
|
|
|
#ifdef SK_SCALAR_IS_FLOAT
|
2011-05-17 21:36:59 +04:00
|
|
|
const SkScalar tolerance = SK_Scalar1 / 200000;
|
2009-02-27 19:24:51 +03:00
|
|
|
#else
|
2011-05-17 21:36:59 +04:00
|
|
|
const SkScalar tolerance = SK_Scalar1 / 1024;
|
2009-02-27 19:24:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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])) {
|
2011-02-09 05:24:26 +03:00
|
|
|
printf("not equal %g %g\n", (float)a[i], (float)b[i]);
|
2009-02-27 19:24:51 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-26 19:24:27 +04:00
|
|
|
static bool are_equal(skiatest::Reporter* reporter,
|
|
|
|
const SkMatrix& a,
|
|
|
|
const SkMatrix& b) {
|
|
|
|
bool equal = a == b;
|
|
|
|
bool cheapEqual = a.cheapEqualTo(b);
|
|
|
|
if (equal != cheapEqual) {
|
|
|
|
#if SK_SCALAR_IS_FLOAT
|
2012-03-26 21:25:45 +04:00
|
|
|
if (equal) {
|
2012-03-26 19:24:27 +04:00
|
|
|
bool foundZeroSignDiff = false;
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
|
|
|
float aVal = a.get(i);
|
|
|
|
float bVal = b.get(i);
|
2012-09-26 17:08:56 +04:00
|
|
|
int aValI = *SkTCast<int*>(&aVal);
|
|
|
|
int bValI = *SkTCast<int*>(&bVal);
|
2012-03-26 19:24:27 +04:00
|
|
|
if (0 == aVal && 0 == bVal && aValI != bValI) {
|
|
|
|
foundZeroSignDiff = true;
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, aVal == bVal && aValI == aValI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2012-09-26 17:08:56 +04:00
|
|
|
int aValI = *SkTCast<int*>(&aVal);
|
|
|
|
int bValI = *SkTCast<int*>(&bVal);
|
2012-03-26 19:24:27 +04:00
|
|
|
if (sk_float_isnan(aVal) && aValI == bValI) {
|
|
|
|
foundNaN = true;
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, aVal == bVal && aValI == bValI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, foundNaN);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
|
2009-02-27 19:24:51 +03:00
|
|
|
static bool is_identity(const SkMatrix& m) {
|
|
|
|
SkMatrix identity;
|
2009-04-02 20:59:40 +04:00
|
|
|
identity.reset();
|
2009-02-27 19:24:51 +03:00
|
|
|
return nearly_equal(m, identity);
|
|
|
|
}
|
|
|
|
|
2012-10-12 18:35:48 +04:00
|
|
|
static void test_matrix_recttorect(skiatest::Reporter* reporter) {
|
|
|
|
SkRect src, dst;
|
|
|
|
SkMatrix matrix;
|
2012-10-13 06:01:56 +04:00
|
|
|
|
2012-10-12 18:35:48 +04:00
|
|
|
src.set(0, 0, SK_Scalar1*10, SK_Scalar1*10);
|
|
|
|
dst = src;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kIdentity_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
2012-10-13 06:01:56 +04:00
|
|
|
|
2012-10-12 18:35:48 +04:00
|
|
|
dst.offset(SK_Scalar1, SK_Scalar1);
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
2012-10-13 06:01:56 +04:00
|
|
|
|
2012-10-12 18:35:48 +04:00
|
|
|
dst.fRight += SK_Scalar1;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
|
|
|
|
|
|
|
dst = src;
|
|
|
|
dst.fRight = src.fRight * 2;
|
|
|
|
matrix.setRectToRect(src, dst, SkMatrix::kFill_ScaleToFit);
|
|
|
|
REPORTER_ASSERT(reporter, SkMatrix::kScale_Mask == matrix.getType());
|
|
|
|
REPORTER_ASSERT(reporter, matrix.rectStaysRect());
|
|
|
|
}
|
|
|
|
|
2009-06-29 20:14:41 +04: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
|
|
|
|
char buffer[SkMatrix::kMaxFlattenSize + 100];
|
2012-06-08 22:30:46 +04:00
|
|
|
uint32_t size1 = m.writeToMemory(NULL);
|
|
|
|
uint32_t size2 = m.writeToMemory(buffer);
|
2009-06-29 20:14:41 +04:00
|
|
|
REPORTER_ASSERT(reporter, size1 == size2);
|
|
|
|
REPORTER_ASSERT(reporter, size1 <= SkMatrix::kMaxFlattenSize);
|
2012-08-23 22:14:13 +04:00
|
|
|
|
2009-06-29 20:14:41 +04:00
|
|
|
SkMatrix m2;
|
2012-06-08 22:30:46 +04:00
|
|
|
uint32_t size3 = m2.readFromMemory(buffer);
|
|
|
|
REPORTER_ASSERT(reporter, size1 == size3);
|
2012-03-26 19:24:27 +04:00
|
|
|
REPORTER_ASSERT(reporter, are_equal(reporter, m, m2));
|
2012-08-23 22:14:13 +04:00
|
|
|
|
2009-06-29 20:14:41 +04:00
|
|
|
char buffer2[SkMatrix::kMaxFlattenSize + 100];
|
2012-06-08 22:30:46 +04:00
|
|
|
size3 = m2.writeToMemory(buffer2);
|
|
|
|
REPORTER_ASSERT(reporter, size1 == size3);
|
2009-06-29 20:14:41 +04:00
|
|
|
REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0);
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:03:39 +04:00
|
|
|
static void test_matrix_max_stretch(skiatest::Reporter* reporter) {
|
2011-09-09 23:32:04 +04:00
|
|
|
SkMatrix identity;
|
|
|
|
identity.reset();
|
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 == identity.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix scale;
|
|
|
|
scale.setScale(SK_Scalar1 * 2, SK_Scalar1 * 4);
|
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 * 4 == scale.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix rot90Scale;
|
|
|
|
rot90Scale.setRotate(90 * SK_Scalar1);
|
|
|
|
rot90Scale.postScale(SK_Scalar1 / 4, SK_Scalar1 / 2);
|
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 / 2 == rot90Scale.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix rotate;
|
|
|
|
rotate.setRotate(128 * SK_Scalar1);
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarAbs(SK_Scalar1 - rotate.getMaxStretch()) <= SK_ScalarNearlyZero);
|
|
|
|
|
|
|
|
SkMatrix translate;
|
|
|
|
translate.setTranslate(10 * SK_Scalar1, -5 * SK_Scalar1);
|
|
|
|
REPORTER_ASSERT(reporter, SK_Scalar1 == translate.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix perspX;
|
|
|
|
perspX.reset();
|
2011-10-08 01:55:56 +04:00
|
|
|
perspX.setPerspX(SkScalarToPersp(SK_Scalar1 / 1000));
|
2011-09-09 23:32:04 +04:00
|
|
|
REPORTER_ASSERT(reporter, -SK_Scalar1 == perspX.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix perspY;
|
|
|
|
perspY.reset();
|
2011-10-08 01:55:56 +04:00
|
|
|
perspY.setPerspX(SkScalarToPersp(-SK_Scalar1 / 500));
|
2011-09-09 23:32:04 +04:00
|
|
|
REPORTER_ASSERT(reporter, -SK_Scalar1 == perspY.getMaxStretch());
|
|
|
|
|
|
|
|
SkMatrix baseMats[] = {scale, rot90Scale, rotate,
|
|
|
|
translate, perspX, perspY};
|
|
|
|
SkMatrix mats[2*SK_ARRAY_COUNT(baseMats)];
|
2011-10-27 19:27:51 +04:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(baseMats); ++i) {
|
2011-09-09 23:32:04 +04:00
|
|
|
mats[i] = baseMats[i];
|
|
|
|
bool invertable = mats[i].invert(&mats[i + SK_ARRAY_COUNT(baseMats)]);
|
|
|
|
REPORTER_ASSERT(reporter, invertable);
|
|
|
|
}
|
|
|
|
SkRandom rand;
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
SkScalar stretch = mat.getMaxStretch();
|
2012-08-23 22:14:13 +04:00
|
|
|
|
2011-09-09 23:32:04 +04:00
|
|
|
if ((stretch < 0) != mat.hasPerspective()) {
|
|
|
|
stretch = mat.getMaxStretch();
|
|
|
|
}
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, (stretch < 0) == mat.hasPerspective());
|
|
|
|
|
|
|
|
if (mat.hasPerspective()) {
|
|
|
|
m -= 1; // try another non-persp matrix
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// test a bunch of vectors. None should be scaled by more than stretch
|
|
|
|
// (modulo some error) and we should find a vector that is scaled by
|
|
|
|
// almost stretch.
|
|
|
|
static const SkScalar gStretchTol = (105 * SK_Scalar1) / 100;
|
|
|
|
static const SkScalar gMaxStretchTol = (97 * SK_Scalar1) / 100;
|
|
|
|
SkScalar max = 0;
|
|
|
|
SkVector vectors[1000];
|
2011-10-27 19:27:51 +04:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
|
2011-09-09 23:32:04 +04: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 19:27:51 +04:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(vectors); ++i) {
|
2011-09-09 23:32:04 +04:00
|
|
|
SkScalar d = vectors[i].length();
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarDiv(d, stretch) < gStretchTol);
|
|
|
|
if (max < d) {
|
|
|
|
max = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, SkScalarDiv(max, stretch) >= gMaxStretchTol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-25 19:07:40 +04:00
|
|
|
// This function is extracted from src/gpu/SkGpuDevice.cpp,
|
|
|
|
// in order to make sure this function works correctly.
|
|
|
|
static bool isSimilarityTransformation(const SkMatrix& matrix,
|
|
|
|
SkScalar tol = SK_ScalarNearlyZero) {
|
|
|
|
if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (matrix.hasPerspective()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar mx = matrix.get(SkMatrix::kMScaleX);
|
|
|
|
SkScalar sx = matrix.get(SkMatrix::kMSkewX);
|
|
|
|
SkScalar my = matrix.get(SkMatrix::kMScaleY);
|
|
|
|
SkScalar sy = matrix.get(SkMatrix::kMSkewY);
|
|
|
|
|
|
|
|
if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// it has scales or skews, but it could also be rotation, check it out.
|
|
|
|
SkVector vec[2];
|
|
|
|
vec[0].set(mx, sx);
|
|
|
|
vec[1].set(sy, my);
|
|
|
|
|
|
|
|
return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
|
|
|
|
SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
|
|
|
|
SkScalarSquare(tol));
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:03:39 +04:00
|
|
|
static void test_matrix_is_similarity_transform(skiatest::Reporter* reporter) {
|
2012-04-25 19:07:40 +04:00
|
|
|
SkMatrix mat;
|
|
|
|
|
|
|
|
// identity
|
|
|
|
mat.setIdentity();
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// translation only
|
|
|
|
mat.reset();
|
|
|
|
mat.setTranslate(SkIntToScalar(100), SkIntToScalar(100));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scale with same size
|
|
|
|
mat.reset();
|
|
|
|
mat.setScale(SkIntToScalar(15), SkIntToScalar(15));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scale with one negative
|
|
|
|
mat.reset();
|
|
|
|
mat.setScale(SkIntToScalar(-15), SkIntToScalar(15));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scale with different size
|
|
|
|
mat.reset();
|
|
|
|
mat.setScale(SkIntToScalar(15), SkIntToScalar(20));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scale with same size at a pivot point
|
|
|
|
mat.reset();
|
|
|
|
mat.setScale(SkIntToScalar(15), SkIntToScalar(15),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scale with different size at a pivot point
|
|
|
|
mat.reset();
|
|
|
|
mat.setScale(SkIntToScalar(15), SkIntToScalar(20),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// skew with same size
|
|
|
|
mat.reset();
|
|
|
|
mat.setSkew(SkIntToScalar(15), SkIntToScalar(15));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// skew with different size
|
|
|
|
mat.reset();
|
|
|
|
mat.setSkew(SkIntToScalar(15), SkIntToScalar(20));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// skew with same size at a pivot point
|
|
|
|
mat.reset();
|
|
|
|
mat.setSkew(SkIntToScalar(15), SkIntToScalar(15),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// skew with different size at a pivot point
|
|
|
|
mat.reset();
|
|
|
|
mat.setSkew(SkIntToScalar(15), SkIntToScalar(20),
|
|
|
|
SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// perspective x
|
|
|
|
mat.reset();
|
|
|
|
mat.setPerspX(SkScalarToPersp(SK_Scalar1 / 2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// perspective y
|
|
|
|
mat.reset();
|
|
|
|
mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
#if SK_SCALAR_IS_FLOAT
|
|
|
|
/* We bypass the following tests for SK_SCALAR_IS_FIXED build.
|
|
|
|
* The long discussion can be found in this issue:
|
|
|
|
* http://codereview.appspot.com/5999050/
|
|
|
|
* In short, we haven't found a perfect way to fix the precision
|
|
|
|
* issue, i.e. the way we use tolerance in isSimilarityTransformation
|
|
|
|
* is incorrect. The situation becomes worse in fixed build, so
|
|
|
|
* we disabled rotation related tests for fixed build.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// rotate
|
|
|
|
for (int angle = 0; angle < 360; ++angle) {
|
|
|
|
mat.reset();
|
|
|
|
mat.setRotate(SkIntToScalar(angle));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if there are any accumulated precision issues
|
|
|
|
mat.reset();
|
|
|
|
for (int i = 1; i < 360; i++) {
|
|
|
|
mat.postRotate(SkIntToScalar(1));
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// rotate + translate
|
|
|
|
mat.reset();
|
|
|
|
mat.setRotate(SkIntToScalar(30));
|
|
|
|
mat.postTranslate(SkIntToScalar(10), SkIntToScalar(20));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// rotate + uniform scale
|
|
|
|
mat.reset();
|
|
|
|
mat.setRotate(SkIntToScalar(30));
|
|
|
|
mat.postScale(SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// rotate + non-uniform scale
|
|
|
|
mat.reset();
|
|
|
|
mat.setRotate(SkIntToScalar(30));
|
|
|
|
mat.postScale(SkIntToScalar(3), SkIntToScalar(2));
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// all zero
|
|
|
|
mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// all zero except perspective
|
|
|
|
mat.setAll(0, 0, 0, 0, 0, 0, 0, 0, SK_Scalar1);
|
|
|
|
REPORTER_ASSERT(reporter, !isSimilarityTransformation(mat));
|
|
|
|
|
|
|
|
// scales zero, only skews
|
|
|
|
mat.setAll(0, SK_Scalar1, 0,
|
|
|
|
SK_Scalar1, 0, 0,
|
|
|
|
0, 0, SkMatrix::I()[8]);
|
|
|
|
REPORTER_ASSERT(reporter, isSimilarityTransformation(mat));
|
|
|
|
}
|
|
|
|
|
2012-06-06 16:03:39 +04:00
|
|
|
static void TestMatrix(skiatest::Reporter* reporter) {
|
2009-02-27 19:24:51 +03:00
|
|
|
SkMatrix mat, inverse, iden1, iden2;
|
|
|
|
|
|
|
|
mat.reset();
|
|
|
|
mat.setTranslate(SK_Scalar1, SK_Scalar1);
|
2012-04-19 22:59:25 +04:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 19:24:51 +03:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
|
|
|
mat.setScale(SkIntToScalar(2), SkIntToScalar(2));
|
2012-04-19 22:59:25 +04:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 19:24:51 +03:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2009-06-29 20:14:41 +04:00
|
|
|
test_flatten(reporter, mat);
|
2009-02-27 19:24:51 +03:00
|
|
|
|
|
|
|
mat.setScale(SK_Scalar1/2, SK_Scalar1/2);
|
2012-04-19 22:59:25 +04:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 19:24:51 +03:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
2009-06-29 20:14:41 +04:00
|
|
|
test_flatten(reporter, mat);
|
2009-02-27 19:24:51 +03:00
|
|
|
|
|
|
|
mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20), 0);
|
|
|
|
mat.postRotate(SkIntToScalar(25));
|
|
|
|
REPORTER_ASSERT(reporter, mat.invert(NULL));
|
2012-04-19 22:59:25 +04:00
|
|
|
REPORTER_ASSERT(reporter, mat.invert(&inverse));
|
2009-02-27 19:24:51 +03:00
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
iden2.setConcat(inverse, mat);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden2));
|
2009-06-29 20:14:41 +04:00
|
|
|
test_flatten(reporter, mat);
|
|
|
|
test_flatten(reporter, iden2);
|
2009-04-02 20:59:40 +04:00
|
|
|
|
2009-02-27 19:24:51 +03:00
|
|
|
// rectStaysRect test
|
|
|
|
{
|
|
|
|
static const struct {
|
|
|
|
SkScalar m00, m01, m10, m11;
|
|
|
|
bool mStaysRect;
|
|
|
|
}
|
|
|
|
gRectStaysRectSamples[] = {
|
|
|
|
{ 0, 0, 0, 0, false },
|
|
|
|
{ 0, 0, 0, SK_Scalar1, false },
|
|
|
|
{ 0, 0, SK_Scalar1, 0, false },
|
|
|
|
{ 0, 0, SK_Scalar1, SK_Scalar1, false },
|
|
|
|
{ 0, SK_Scalar1, 0, 0, false },
|
|
|
|
{ 0, SK_Scalar1, 0, SK_Scalar1, false },
|
|
|
|
{ 0, SK_Scalar1, SK_Scalar1, 0, true },
|
|
|
|
{ 0, SK_Scalar1, SK_Scalar1, SK_Scalar1, false },
|
|
|
|
{ SK_Scalar1, 0, 0, 0, false },
|
|
|
|
{ SK_Scalar1, 0, 0, SK_Scalar1, true },
|
|
|
|
{ SK_Scalar1, 0, SK_Scalar1, 0, false },
|
|
|
|
{ SK_Scalar1, 0, SK_Scalar1, SK_Scalar1, false },
|
|
|
|
{ SK_Scalar1, SK_Scalar1, 0, 0, false },
|
|
|
|
{ SK_Scalar1, SK_Scalar1, 0, SK_Scalar1, false },
|
|
|
|
{ SK_Scalar1, SK_Scalar1, SK_Scalar1, 0, false },
|
|
|
|
{ SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1, false }
|
|
|
|
};
|
2009-04-02 20:59:40 +04:00
|
|
|
|
2009-02-27 19:24:51 +03:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gRectStaysRectSamples); i++) {
|
|
|
|
SkMatrix m;
|
2009-04-02 20:59:40 +04:00
|
|
|
|
2009-02-27 19:24:51 +03: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 23:41:55 +04:00
|
|
|
|
2011-07-14 00:18:16 +04:00
|
|
|
mat.reset();
|
2011-07-13 23:41:55 +04:00
|
|
|
mat.set(SkMatrix::kMScaleX, SkIntToScalar(1));
|
|
|
|
mat.set(SkMatrix::kMSkewX, SkIntToScalar(2));
|
|
|
|
mat.set(SkMatrix::kMTransX, SkIntToScalar(3));
|
|
|
|
mat.set(SkMatrix::kMSkewY, SkIntToScalar(4));
|
|
|
|
mat.set(SkMatrix::kMScaleY, SkIntToScalar(5));
|
|
|
|
mat.set(SkMatrix::kMTransY, SkIntToScalar(6));
|
|
|
|
SkScalar affine[6];
|
|
|
|
REPORTER_ASSERT(reporter, mat.asAffine(affine));
|
|
|
|
|
|
|
|
#define affineEqual(e) affine[SkMatrix::kA##e] == mat.get(SkMatrix::kM##e)
|
|
|
|
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
|
|
|
|
|
2011-10-08 01:55:56 +04:00
|
|
|
mat.set(SkMatrix::kMPersp1, SkScalarToPersp(SK_Scalar1 / 2));
|
2011-07-13 23:41:55 +04:00
|
|
|
REPORTER_ASSERT(reporter, !mat.asAffine(affine));
|
2011-09-09 23:32:04 +04:00
|
|
|
|
2012-03-26 19:24:27 +04: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);
|
2012-03-26 19:57:37 +04:00
|
|
|
// fixed pt doesn't have the property that NaN does not equal itself.
|
|
|
|
#ifdef SK_SCALAR_IS_FIXED
|
|
|
|
REPORTER_ASSERT(reporter, are_equal(reporter, mat, mat2));
|
|
|
|
#else
|
2012-03-26 19:24:27 +04:00
|
|
|
REPORTER_ASSERT(reporter, !are_equal(reporter, mat, mat2));
|
2012-03-26 19:57:37 +04:00
|
|
|
#endif
|
2012-03-26 19:24:27 +04:00
|
|
|
|
2011-09-09 23:32:04 +04:00
|
|
|
test_matrix_max_stretch(reporter);
|
2012-04-25 19:07:40 +04:00
|
|
|
test_matrix_is_similarity_transform(reporter);
|
2012-10-12 18:35:48 +04:00
|
|
|
test_matrix_recttorect(reporter);
|
2009-02-27 19:24:51 +03:00
|
|
|
}
|
|
|
|
|
2009-02-28 01:06:06 +03:00
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix)
|