Add SkMatrix44::setTranspose
authorvollick@chromium.org <vollick@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 14 Nov 2012 21:33:55 +0000 (21:33 +0000)
committervollick@chromium.org <vollick@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 14 Nov 2012 21:33:55 +0000 (21:33 +0000)
It turned out that adding getDouble(...) and setDouble(...) made this change
easier, so I've included that in this cl as well.

Review URL: https://codereview.appspot.com/6845048

git-svn-id: http://skia.googlecode.com/svn/trunk@6424 2bbb7eff-a529-9590-31e7-b0007b416f81

include/utils/SkMatrix44.h
src/utils/SkMatrix44.cpp
tests/Matrix44Test.cpp

index 97faf78..a046a07 100644 (file)
@@ -119,6 +119,13 @@ public:
     SkMScalar get(int row, int col) const;
     void set(int row, int col, const SkMScalar& value);
 
+    double getDouble(int row, int col) const {
+        return SkMScalarToDouble(this->get(row, col));
+    }
+    void setDouble(int row, int col, double value) {
+        this->set(row, col, SkDoubleToMScalar(value));
+    }
+
     void asColMajorf(float[]) const;
     void asColMajord(double[]) const;
     void asRowMajorf(float[]) const;
@@ -183,6 +190,9 @@ public:
      */
     bool invert(SkMatrix44* inverse) const;
 
+    /** Transpose this matrix in place. */
+    void transpose();
+
     /** Apply the matrix to the src vector, returning the new vector in dst.
         It is legal for src and dst to point to the same memory.
      */
index abef0b3..0e7ad16 100644 (file)
@@ -323,6 +323,17 @@ bool SkMatrix44::invert(SkMatrix44* inverse) const {
 
 ///////////////////////////////////////////////////////////////////////////////
 
+void SkMatrix44::transpose() {
+    SkTSwap(fMat[0][1], fMat[1][0]);
+    SkTSwap(fMat[0][2], fMat[2][0]);
+    SkTSwap(fMat[0][3], fMat[3][0]);
+    SkTSwap(fMat[1][2], fMat[2][1]);
+    SkTSwap(fMat[1][3], fMat[3][1]);
+    SkTSwap(fMat[2][3], fMat[3][2]);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
 void SkMatrix44::mapScalars(const SkScalar src[4], SkScalar dst[4]) const {
     SkScalar result[4];
     for (int i = 0; i < 4; i++) {
index af10e00..782b51e 100644 (file)
@@ -147,6 +147,35 @@ static void test_determinant(skiatest::Reporter* reporter) {
   REPORTER_ASSERT(reporter, nearly_equal_double(0, e.determinant()));
 }
 
+static void test_transpose(skiatest::Reporter* reporter) {
+  SkMatrix44 a;
+  SkMatrix44 b;
+
+  int i = 0;
+  for (int row = 0; row < 4; ++row) {
+    for (int col = 0; col < 4; ++col) {
+      a.setDouble(row, col, i);
+      b.setDouble(col, row, i++);
+    }
+  }
+
+  a.transpose();
+  REPORTER_ASSERT(reporter, nearly_equal(a, b));
+}
+
+static void test_get_set_double(skiatest::Reporter* reporter) {
+  SkMatrix44 a;
+  for (int row = 0; row < 4; ++row) {
+    for (int col = 0; col < 4; ++col) {
+      a.setDouble(row, col, 3.141592653589793);
+      REPORTER_ASSERT(reporter, nearly_equal_double(3.141592653589793,
+                                                    a.getDouble(row, col)));
+      a.setDouble(row, col, 0);
+      REPORTER_ASSERT(reporter, nearly_equal_double(0, a.getDouble(row, col)));
+    }
+  }
+}
+
 static void TestMatrix44(skiatest::Reporter* reporter) {
 #ifdef SK_SCALAR_IS_FLOAT
     SkMatrix44 mat, inverse, iden1, iden2, rot;
@@ -217,6 +246,8 @@ static void TestMatrix44(skiatest::Reporter* reporter) {
     }
 
     test_determinant(reporter);
+    test_transpose(reporter);
+    test_get_set_double(reporter);
 #endif
 }