From c8c49c573bda7c6d74eed390842b7a0bbdf873f5 Mon Sep 17 00:00:00 2001 From: "reed@android.com" Date: Wed, 24 Feb 2010 15:36:57 +0000 Subject: [PATCH] add tests for translate, concat, scale git-svn-id: http://skia.googlecode.com/svn/trunk@510 2bbb7eff-a529-9590-31e7-b0007b416f81 --- experimental/SkMatrix44.cpp | 20 ++++++++++++--- experimental/SkMatrix44.h | 48 ++++++++++++++++++++++++++++++++--- samplecode/SampleSkLayer.cpp | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/experimental/SkMatrix44.cpp b/experimental/SkMatrix44.cpp index 9ee746a17..1b1b93b98 100644 --- a/experimental/SkMatrix44.cpp +++ b/experimental/SkMatrix44.cpp @@ -14,13 +14,21 @@ SkMatrix44::SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) { /////////////////////////////////////////////////////////////////////////////// +static const SkMatrix44 gIdentity44; + +bool SkMatrix44::isIdentity() const { + return *this == gIdentity44; +} + +/////////////////////////////////////////////////////////////////////////////// + void SkMatrix44::setIdentity() { sk_bzero(fMat, sizeof(fMat)); fMat[0][0] = fMat[1][1] = fMat[2][2] = fMat[3][3] = SK_MScalar1; } void SkMatrix44::setTranslate(SkMScalar tx, SkMScalar ty, SkMScalar tz) { - sk_bzero(fMat, sizeof(fMat)); + this->setIdentity(); fMat[3][0] = tx; fMat[3][1] = ty; fMat[3][2] = tz; @@ -71,7 +79,7 @@ void SkMatrix44::setConcat(const SkMatrix44& a, const SkMatrix44& b) { for (int j = 0; j < 4; j++) { double value = 0; for (int k = 0; k < 4; k++) { - value += (double)a.fMat[k][j] * b.fMat[i][k]; + value += SkMScalarToDouble(a.fMat[k][i]) * b.fMat[j][k]; } result[j][i] = SkDoubleToMScalar(value); } @@ -198,11 +206,17 @@ void SkMatrix44::map(const SkScalar src[4], SkScalar dst[4]) const { void SkMatrix44::dump() const { SkDebugf("[%g %g %g %g][%g %g %g %g][%g %g %g %g][%g %g %g %g]\n", +#if 0 fMat[0][0], fMat[0][1], fMat[0][2], fMat[0][3], fMat[1][0], fMat[1][1], fMat[1][2], fMat[1][3], fMat[2][0], fMat[2][1], fMat[2][2], fMat[2][3], fMat[3][0], fMat[3][1], fMat[3][2], fMat[3][3]); +#else + fMat[0][0], fMat[1][0], fMat[2][0], fMat[3][0], + fMat[0][1], fMat[1][1], fMat[2][1], fMat[3][1], + fMat[0][2], fMat[1][2], fMat[2][2], fMat[3][2], + fMat[0][3], fMat[1][3], fMat[2][3], fMat[3][3]); +#endif } - diff --git a/experimental/SkMatrix44.h b/experimental/SkMatrix44.h index 0e47890e3..cb4fdbf07 100644 --- a/experimental/SkMatrix44.h +++ b/experimental/SkMatrix44.h @@ -3,11 +3,42 @@ #include "SkScalar.h" -typedef float SkMScalar; +// uncomment this to use doubles for matrix44 +#define SK_MSCALAR_IS_DOUBLE + +#ifdef SK_MSCALAR_IS_DOUBLE + typedef double SkMScalar; + static inline double SkFloatToMScalar(float x) { + return static_cast(x); + } + static inline float SkMScalarToFloat(double x) { + return static_cast(x); + } + static inline double SkDoubleToMScalar(double x) { + return x; + } + static inline double SkMScalarToDouble(double x) { + return x; + } +#else + typedef float SkMScalar; + static inline float SkFloatToMScalar(float x) { + return x; + } + static inline float SkMScalarToFloat(float x) { + return x; + } + static inline float SkDoubleToMScalar(double x) { + return static_cast(x); + } + static inline double SkMScalarToDouble(float x) { + return static_cast(x); + } +#endif + static const SkMScalar SK_MScalar1 = 1; -static inline SkMScalar SkDoubleToMScalar(double x) { - return static_cast(x); -} + +/////////////////////////////////////////////////////////////////////////////// struct SkVector4 { SkScalar fData[4]; @@ -19,7 +50,16 @@ public: SkMatrix44(const SkMatrix44&); SkMatrix44(const SkMatrix44& a, const SkMatrix44& b); + bool operator==(const SkMatrix44& other) const { + return !memcmp(this, &other, sizeof(*this)); + } + bool operator!=(const SkMatrix44& other) const { + return !!memcmp(this, &other, sizeof(*this)); + } + + bool isIdentity() const; void setIdentity(); + void reset() { this->setIdentity(); } void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); diff --git a/samplecode/SampleSkLayer.cpp b/samplecode/SampleSkLayer.cpp index a5278dcd2..8b6897bf7 100644 --- a/samplecode/SampleSkLayer.cpp +++ b/samplecode/SampleSkLayer.cpp @@ -4,6 +4,54 @@ #include "SkView.h" #include "SkLayer.h" +#include "SkMatrix44.h" +static void test_inv(const char label[], const SkMatrix44& mat) { + SkDebugf("%s\n", label); + mat.dump(); + + SkMatrix44 inv; + if (mat.invert(&inv)) { + inv.dump(); + } else { + SkDebugf("--- invert failed\n"); + } + + SkMatrix44 a, b; + a.setConcat(mat, inv); + b.setConcat(inv, mat); + SkDebugf("concat mat with inverse pre=%d post=%d\n", a.isIdentity(), b.isIdentity()); + if (!a.isIdentity()) { + a.dump(); + } + if (!b.isIdentity()) { + b.dump(); + } + SkDebugf("\n"); +} + +static void test44() { + SkMatrix44 m0, m1, m2; + + test_inv("identity", m0); + m0.setTranslate(2,3,4); + test_inv("translate", m0); + m0.setScale(2,3,4); + test_inv("scale", m0); + m0.postTranslate(5, 6, 7); + test_inv("postTranslate", m0); + m0.setScale(2,3,4); + m1.setTranslate(5, 6, 7); + m0.setConcat(m0, m1); + test_inv("postTranslate2", m0); + m0.setScale(2,3,4); + m0.preTranslate(5, 6, 7); + test_inv("preTranslate", m0); + + m0.setScale(2, 4, 6); + m0.postScale(SkDoubleToMScalar(0.5)); + test_inv("scale/postscale to 1,2,3", m0); +} + /////////////////////////////////////////////////////////////////////////////// class TestLayer : public SkLayer { @@ -32,6 +80,7 @@ private: public: SkLayerView() { + test44(); static const int W = 600; static const int H = 440; static const struct {