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.
|
|
|
|
*/
|
2011-06-09 19:13:41 +00:00
|
|
|
#include "Test.h"
|
|
|
|
#include "SkMatrix44.h"
|
|
|
|
|
2011-06-13 14:46:52 +00:00
|
|
|
static bool nearly_equal_scalar(SkMScalar a, SkMScalar b) {
|
2011-06-09 19:13:41 +00:00
|
|
|
// Note that we get more compounded error for multiple operations when
|
|
|
|
// SK_SCALAR_IS_FIXED.
|
|
|
|
#ifdef SK_SCALAR_IS_FLOAT
|
|
|
|
const SkScalar tolerance = SK_Scalar1 / 200000;
|
|
|
|
#else
|
|
|
|
const SkScalar tolerance = SK_Scalar1 / 1024;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return SkScalarAbs(a - b) <= tolerance;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (!nearly_equal_scalar(a.get(i, j), b.get(i, j))) {
|
2011-06-13 14:46:52 +00:00
|
|
|
printf("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) {
|
|
|
|
SkMatrix44 identity;
|
|
|
|
identity.reset();
|
|
|
|
return nearly_equal(m, identity);
|
|
|
|
}
|
|
|
|
|
2011-06-24 18:13:39 +00:00
|
|
|
static void test_common_angles(skiatest::Reporter* reporter) {
|
|
|
|
SkMatrix44 rot;
|
|
|
|
// 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;
|
|
|
|
SkMatrix44 a, b, c, d;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
c.map(src, dst); c.map(src + 4, dst + 4);
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, 10 == dst[i]);
|
|
|
|
REPORTER_ASSERT(reporter, 12 == dst[i + 4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setConcat(b, a);
|
|
|
|
|
|
|
|
d = a;
|
|
|
|
d.postConcat(b);
|
|
|
|
REPORTER_ASSERT(reporter, d == c);
|
|
|
|
|
|
|
|
c.map(src, dst); c.map(src + 4, dst + 4);
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
REPORTER_ASSERT(reporter, 20 == dst[i]);
|
|
|
|
REPORTER_ASSERT(reporter, 22 == dst[i + 4]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 12:03:39 +00:00
|
|
|
static void TestMatrix44(skiatest::Reporter* reporter) {
|
2011-06-13 14:46:52 +00:00
|
|
|
#ifdef SK_SCALAR_IS_FLOAT
|
2011-06-09 19:13:41 +00:00
|
|
|
SkMatrix44 mat, inverse, iden1, iden2, rot;
|
|
|
|
|
|
|
|
mat.reset();
|
|
|
|
mat.setTranslate(SK_Scalar1, SK_Scalar1, SK_Scalar1);
|
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
|
|
|
mat.setScale(SkIntToScalar(2), SkIntToScalar(2), SkIntToScalar(2));
|
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
|
|
|
mat.setScale(SK_Scalar1/2, SK_Scalar1/2, SK_Scalar1/2);
|
|
|
|
mat.invert(&inverse);
|
|
|
|
iden1.setConcat(mat, inverse);
|
|
|
|
REPORTER_ASSERT(reporter, is_identity(iden1));
|
|
|
|
|
|
|
|
mat.setScale(SkIntToScalar(3), SkIntToScalar(5), SkIntToScalar(20));
|
|
|
|
rot.setRotateDegreesAbout(
|
|
|
|
SkIntToScalar(0),
|
|
|
|
SkIntToScalar(0),
|
|
|
|
SkIntToScalar(-1),
|
|
|
|
SkIntToScalar(90));
|
|
|
|
mat.postConcat(rot);
|
|
|
|
REPORTER_ASSERT(reporter, mat.invert(NULL));
|
|
|
|
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
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
2011-06-13 14:46:52 +00:00
|
|
|
#endif
|
2011-06-09 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "TestClassDef.h"
|
|
|
|
DEFINE_TESTCLASS("Matrix44", Matrix44TestClass, TestMatrix44)
|