Add basic SVD support to SkMatrix. Allows you to pull out the x- and y-scale factors, sandwiched by two rotations.

R=reed@google.com

Author: jvanverth@google.com

Review URL: https://chromiumcodereview.appspot.com/19569007

git-svn-id: http://skia.googlecode.com/svn/trunk@10322 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2013-07-24 18:08:08 +00:00
parent 0449a569b9
commit 08284e4d24
3 changed files with 342 additions and 0 deletions

View File

@ -1966,3 +1966,86 @@ bool SkTreatAsSprite(const SkMatrix& mat, int width, int height,
dst.round(&idst);
return isrc == idst;
}
bool SkDecomposeUpper2x2(const SkMatrix& matrix,
SkScalar* rotation0,
SkScalar* xScale, SkScalar* yScale,
SkScalar* rotation1) {
// borrowed from Jim Blinn's article "Consider the Lowly 2x2 Matrix"
// Note: he uses row vectors, so we have to do some swapping of terms
SkScalar A = matrix[SkMatrix::kMScaleX];
SkScalar B = matrix[SkMatrix::kMSkewX];
SkScalar C = matrix[SkMatrix::kMSkewY];
SkScalar D = matrix[SkMatrix::kMScaleY];
SkScalar E = SK_ScalarHalf*(A + D);
SkScalar F = SK_ScalarHalf*(A - D);
SkScalar G = SK_ScalarHalf*(C + B);
SkScalar H = SK_ScalarHalf*(C - B);
SkScalar sqrt0 = SkScalarSqrt(E*E + H*H);
SkScalar sqrt1 = SkScalarSqrt(F*F + G*G);
SkScalar xs, ys, r0, r1;
// can't have zero yScale, must be degenerate
if (SkScalarNearlyEqual(sqrt0, sqrt1)) {
return false;
}
xs = sqrt0 + sqrt1;
ys = sqrt0 - sqrt1;
// uniformly scaled rotation
if (SkScalarNearlyZero(F) && SkScalarNearlyZero(G)) {
SkASSERT(!SkScalarNearlyZero(E));
r0 = SkScalarATan2(H, E);
r1 = 0;
// uniformly scaled reflection
} else if (SkScalarNearlyZero(E) && SkScalarNearlyZero(H)) {
SkASSERT(!SkScalarNearlyZero(F));
r0 = -SkScalarATan2(G, F);
r1 = 0;
} else {
SkASSERT(!SkScalarNearlyZero(E));
SkASSERT(!SkScalarNearlyZero(F));
SkScalar arctan0 = SkScalarATan2(H, E);
SkScalar arctan1 = SkScalarATan2(G, F);
r0 = SK_ScalarHalf*(arctan0 - arctan1);
r1 = SK_ScalarHalf*(arctan0 + arctan1);
// simplify the results
const SkScalar kHalfPI = SK_ScalarHalf*SK_ScalarPI;
if (SkScalarNearlyEqual(SkScalarAbs(r0), kHalfPI)) {
SkScalar tmp = xs;
xs = ys;
ys = tmp;
r1 += r0;
r0 = 0;
} else if (SkScalarNearlyEqual(SkScalarAbs(r1), kHalfPI)) {
SkScalar tmp = xs;
xs = ys;
ys = tmp;
r0 += r1;
r1 = 0;
}
}
if (NULL != xScale) {
*xScale = xs;
}
if (NULL != yScale) {
*yScale = ys;
}
if (NULL != rotation0) {
*rotation0 = r0;
}
if (NULL != rotation1) {
*rotation1 = r1;
}
return true;
}

View File

@ -40,4 +40,15 @@ static inline bool SkTreatAsSpriteFilter(const SkMatrix& matrix,
return SkTreatAsSprite(matrix, width, height, kSkSubPixelBitsForBilerp);
}
/** Decomposes the upper-left 2x2 of the matrix into a rotation, followed by a non-uniform scale,
followed by another rotation. Returns true if successful.
If the scale factors are uniform, then rotation1 will be 0.
If there is a reflection, yScale will be negative.
Returns false if the matrix is degenerate.
*/
bool SkDecomposeUpper2x2(const SkMatrix& matrix,
SkScalar* rotation0,
SkScalar* xScale, SkScalar* yScale,
SkScalar* rotation1);
#endif

View File

@ -8,6 +8,7 @@
#include "Test.h"
#include "SkMath.h"
#include "SkMatrix.h"
#include "SkMatrixUtils.h"
#include "SkRandom.h"
static bool nearly_equal_scalar(SkScalar a, SkScalar b) {
@ -345,6 +346,252 @@ static void test_matrix_is_similarity(skiatest::Reporter* reporter) {
REPORTER_ASSERT(reporter, mat.isSimilarity());
}
// For test_matrix_decomposition, below.
static bool scalar_nearly_equal_relative(SkScalar a, SkScalar b,
SkScalar tolerance = SK_ScalarNearlyZero) {
// from Bruce Dawson
SkScalar diff = SkScalarAbs(a - b);
if (diff < tolerance) {
return true;
}
a = SkScalarAbs(a);
b = SkScalarAbs(b);
SkScalar largest = (b > a) ? b : a;
if (diff <= largest*tolerance) {
return true;
}
return false;
}
static void test_matrix_decomposition(skiatest::Reporter* reporter) {
SkMatrix mat;
SkScalar rotation0, scaleX, scaleY, rotation1;
const float kRotation0 = 15.5f;
const float kRotation1 = -50.f;
const float kScale0 = 5000.f;
const float kScale1 = 0.001f;
// identity
mat.reset();
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// make sure it doesn't crash if we pass in NULLs
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, NULL, NULL, NULL, NULL));
// rotation only
mat.setRotate(kRotation0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, SK_Scalar1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, SK_Scalar1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// uniform scale only
mat.setScale(kScale0, kScale0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// anisotropic scale only
mat.setScale(kScale1, kScale0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// rotation then uniform scale
mat.setRotate(kRotation1);
mat.postScale(kScale0, kScale0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// uniform scale then rotation
mat.setScale(kScale0, kScale0);
mat.postRotate(kRotation1);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// rotation then uniform scale+reflection
mat.setRotate(kRotation0);
mat.postScale(kScale1, -kScale1);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation0)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// uniform scale+reflection, then rotate
mat.setScale(kScale0, -kScale0);
mat.postRotate(kRotation1);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(-kRotation1)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, -kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// rotation then anisotropic scale
mat.setRotate(kRotation1);
mat.postScale(kScale1, kScale0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0, SkDegreesToRadians(kRotation1)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// anisotropic scale then rotation
mat.setScale(kScale1, kScale0);
mat.postRotate(kRotation0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation1, SkDegreesToRadians(kRotation0)));
// rotation, uniform scale, then different rotation
mat.setRotate(kRotation1);
mat.postScale(kScale0, kScale0);
mat.postRotate(kRotation0);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rotation0,
SkDegreesToRadians(kRotation0 + kRotation1)));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleX, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyEqual(scaleY, kScale0));
REPORTER_ASSERT(reporter, SkScalarNearlyZero(rotation1));
// rotation, anisotropic scale, then different rotation
mat.setRotate(kRotation0);
mat.postScale(kScale1, kScale0);
mat.postRotate(kRotation1);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
// Because of the shear/skew we won't get the same results, so we need to multiply it out.
// Generating the matrices requires doing a radian-to-degree calculation, then degree-to-radian
// calculation (in setRotate()), which adds error, so this just computes the matrix elements
// directly.
SkScalar c0;
SkScalar s0 = SkScalarSinCos(rotation0, &c0);
SkScalar c1;
SkScalar s1 = SkScalarSinCos(rotation1, &c1);
// We do a relative check here because large scale factors cause problems with an absolute check
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
scaleX*c0*c1 - scaleY*s0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
-scaleX*s0*c1 - scaleY*c0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
scaleX*c0*s1 + scaleY*s0*c1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
-scaleX*s0*s1 + scaleY*c0*c1));
// try some random matrices
SkMWCRandom rand;
for (int m = 0; m < 1000; ++m) {
SkScalar rot0 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI);
SkScalar sx = rand.nextRangeF(-3000.f, 3000.f);
SkScalar sy = rand.nextRangeF(-3000.f, 3000.f);
SkScalar rot1 = rand.nextRangeF(-SK_ScalarPI, SK_ScalarPI);
mat.setRotate(rot0);
mat.postScale(sx, sy);
mat.postRotate(rot1);
if (SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1)) {
SkScalar c0;
SkScalar s0 = SkScalarSinCos(rotation0, &c0);
SkScalar c1;
SkScalar s1 = SkScalarSinCos(rotation1, &c1);
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
scaleX*c0*c1 - scaleY*s0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
-scaleX*s0*c1 - scaleY*c0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
scaleX*c0*s1 + scaleY*s0*c1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
-scaleX*s0*s1 + scaleY*c0*c1));
} 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);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
s0 = SkScalarSinCos(rotation0, &c0);
s1 = SkScalarSinCos(rotation1, &c1);
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
scaleX*c0*c1 - scaleY*s0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
-scaleX*s0*c1 - scaleY*c0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
scaleX*c0*s1 + scaleY*s0*c1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
-scaleX*s0*s1 + scaleY*c0*c1));
// perspective shouldn't affect this
mat[SkMatrix::kMPersp0] = 12.0;
mat[SkMatrix::kMPersp1] = 4.0;
mat[SkMatrix::kMPersp2] = 1872.0;
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
s0 = SkScalarSinCos(rotation0, &c0);
s1 = SkScalarSinCos(rotation1, &c1);
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
scaleX*c0*c1 - scaleY*s0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
-scaleX*s0*c1 - scaleY*c0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
scaleX*c0*s1 + scaleY*s0*c1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
-scaleX*s0*s1 + scaleY*c0*c1));
// rotation, anisotropic scale + reflection, then different rotation
mat.setRotate(kRotation0);
mat.postScale(-kScale1, kScale0);
mat.postRotate(kRotation1);
REPORTER_ASSERT(reporter, SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
s0 = SkScalarSinCos(rotation0, &c0);
s1 = SkScalarSinCos(rotation1, &c1);
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleX],
scaleX*c0*c1 - scaleY*s0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewX],
-scaleX*s0*c1 - scaleY*c0*s1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMSkewY],
scaleX*c0*s1 + scaleY*s0*c1));
REPORTER_ASSERT(reporter, scalar_nearly_equal_relative(mat[SkMatrix::kMScaleY],
-scaleX*s0*s1 + scaleY*c0*c1));
// degenerate matrices
// mostly zero entries
mat.reset();
mat[SkMatrix::kMScaleX] = 0.f;
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
mat.reset();
mat[SkMatrix::kMScaleY] = 0.f;
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
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;
REPORTER_ASSERT(reporter, !SkDecomposeUpper2x2(mat, &rotation0, &scaleX, &scaleY, &rotation1));
}
static void TestMatrix(skiatest::Reporter* reporter) {
SkMatrix mat, inverse, iden1, iden2;
@ -465,6 +712,7 @@ static void TestMatrix(skiatest::Reporter* reporter) {
test_matrix_max_stretch(reporter);
test_matrix_is_similarity(reporter);
test_matrix_recttorect(reporter);
test_matrix_decomposition(reporter);
}
#include "TestClassDef.h"