From 9c85e7597e7242156377bdc7837b84c4a0de4cb1 Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Mon, 8 Jun 2009 21:58:27 -0500 Subject: [PATCH] Revert "Overhaul of the newly-wrapped Matrix API (+documentation)" This reverts commit dc26aaf781da09b69598cf106ec79c8c06fca8fa. Conflicts: .gitignore ChangeLog cairomm/matrix.h tests/test-font-face.cc tests/test-matrix.cc --- .gitignore | 1 - cairomm/matrix.cc | 34 +++-------- cairomm/matrix.h | 150 ++--------------------------------------------- tests/test-font-face.cc | 2 + tests/test-matrix.cc | 60 ++++--------------- 5 files changed, 30 insertions(+), 217 deletions(-) diff --git a/.gitignore b/.gitignore index bf32af4..fde2141 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,6 @@ tests/test-surface tests/test-scaled-font tests/test-font-options tests/test-matrix -tests/test-user-font cairomm/cairommconfig.h* cairomm/stamp-* diff --git a/cairomm/matrix.cc b/cairomm/matrix.cc index 8c1d4d6..db65ad3 100644 --- a/cairomm/matrix.cc +++ b/cairomm/matrix.cc @@ -23,6 +23,7 @@ namespace Cairo Matrix::Matrix() { + init_identity(); } Matrix::Matrix(double xx, double yx, double xy, double yy, double x0, double y0) @@ -30,32 +31,24 @@ Matrix::Matrix(double xx, double yx, double xy, double yy, double x0, double y0) cairo_matrix_init(this, xx, yx, xy, yy, x0, y0); } -Matrix identity_matrix() +void Matrix::init_identity() { - Matrix m; - cairo_matrix_init_identity(&m); - return m; + cairo_matrix_init_identity(this); } -Matrix translation_matrix(double tx, double ty) +void Matrix::init_translate(double tx, double ty) { - Matrix m; - cairo_matrix_init_translate(&m, tx, ty); - return m; + cairo_matrix_init_translate(this, tx, ty); } -Matrix scaled_matrix(double sx, double sy) +void Matrix::init_scale(double sx, double sy) { - Matrix m; - cairo_matrix_init_scale(&m, sx, sy); - return m; + cairo_matrix_init_scale(this, sx, sy); } -Matrix rotation_matrix(double radians) +void Matrix::init_rotate(double radians) { - Matrix m; - cairo_matrix_init_rotate(&m, radians); - return m; + cairo_matrix_init_rotate(this, radians); } void Matrix::translate(double tx, double ty) @@ -80,7 +73,7 @@ void Matrix::invert() } // throws exception -void Matrix::multiply(Matrix& a, Matrix& b) +void Matrix::muiltiply(Matrix& a, Matrix& b) { cairo_matrix_multiply(this, &a, &b); } @@ -95,11 +88,4 @@ void Matrix::transform_point(double& x, double& y) const cairo_matrix_transform_point(this, &x, &y); } -Matrix operator*(const Matrix& a, const Matrix& b) -{ - Matrix m; - cairo_matrix_multiply(&m, &a, &b); - return m; -} - } // namespace Cairo diff --git a/cairomm/matrix.h b/cairomm/matrix.h index 559eaa5..46a3429 100644 --- a/cairomm/matrix.h +++ b/cairomm/matrix.h @@ -50,164 +50,24 @@ namespace Cairo class Matrix : public cairo_matrix_t { public: - /** Creates an uninitialized matrix. If you want a matrix initialized to a - * certain value, either specify the values explicitly with the other - * constructor or use one of the free functions for initializing matrices with - * specific scales, rotations, etc. - * - * @sa identity_matrix() - * @sa rotation_matrix() - * @sa translation_matrix() - * @sa scaled_matrix() - */ Matrix(); - - /** Creates a matrix Sets to be the affine transformation given by xx, yx, xy, - * yy, x0, y0. The transformation is given by: - * - * @code - * x_new = xx * x + xy * y + x0; - * y_new = yx * x + yy * y + y0; - * @endcode - * - * @param xx xx component of the affine transformation - * @param yx yx component of the affine transformation - * @param xy xy component of the affine transformation - * @param yy yy component of the affine transformation - * @param x0 X translation component of the affine transformation - * @param y0 Y translation component of the affine transformation - */ Matrix(double xx, double yx, double xy, double yy, double x0, double y0); - /** Applies a translation by tx, ty to the transformation in matrix. The - * effect of the new transformation is to first translate the coordinates by - * tx and ty, then apply the original transformation to the coordinates. - * - * @param tx amount to translate in the X direction - * @param ty amount to translate in the Y direction - */ + void init_identity(); + void init_translate(double tx, double ty); + void init_scale(double sx, double sy); + void init_rotate(double radians); void translate(double tx, double ty); - - /** Applies scaling by sx, sy to the transformation in matrix. The effect of - * the new transformation is to first scale the coordinates by sx and sy, then - * apply the original transformation to the coordinates. - * - * @param sx scale factor in the X direction - * @param sy scale factor in the Y direction - */ void scale(double sx, double sy); - - /** Applies rotation by radians to the transformation in matrix. The effect of - * the new transformation is to first rotate the coordinates by radians, then - * apply the original transformation to the coordinates. - * - * @param radians angle of rotation, in radians. The direction of rotation is - * defined such that positive angles rotate in the direction from the positive - * X axis toward the positive Y axis. With the default axis orientation of - * cairo, positive angles rotate in a clockwise direction. - */ void rotate(double radians); - /** Changes matrix to be the inverse of it's original value. Not all - * transformation matrices have inverses; if the matrix collapses points - * together (it is degenerate), then it has no inverse and this function will - * throw an exception. - */ void invert(); // throws exception + void muiltiply(Matrix& a, Matrix& b); // FIXME: operator*? - /** Multiplies the affine transformations in a and b together and stores the - * result in this matrix. The effect of the resulting transformation is to first - * apply the transformation in a to the coordinates and then apply the - * transformation in b to the coordinates. - * - * It is allowable for result to be identical to either a or b. - * - * @param a a Matrix - * @param b a Matrix - * - * @sa operator*() - */ - void multiply(Matrix& a, Matrix& b); - - /** Transforms the distance vector (dx,dy) by matrix. This is similar to - * transform_point() except that the translation components of the - * transformation are ignored. The calculation of the returned vector is as - * follows: - * - * @code - * dx2 = dx1 * a + dy1 * c; - * dy2 = dx1 * b + dy1 * d; - * @endcode - * - * Affine transformations are position invariant, so the same vector always - * transforms to the same vector. If (x1,y1) transforms to (x2,y2) then - * (x1+dx1,y1+dy1) will transform to (x1+dx2,y1+dy2) for all values of x1 and - * x2. - * - * @param dx X component of a distance vector. An in/out parameter - * @param dy Y component of a distance vector. An in/out parameter - */ void transform_distance(double& dx, double& dy) const; - - /** Transforms the point (x, y) by this matrix. - * - * @param x X position. An in/out parameter - * @param y Y position. An in/out parameter - */ void transform_point(double& x, double& y) const; }; -/** Returns a Matrix initialized to the identity matrix - * - * @relates Matrix - */ -Matrix identity_matrix(); - -/** Returns a Matrix initialized to a transformation that translates by tx and - * ty in the X and Y dimensions, respectively. - * - * @param tx amount to translate in the X direction - * @param ty amount to translate in the Y direction - * - * @relates Matrix - */ -Matrix translation_matrix(double tx, double ty); - -/** Returns a Matrix initialized to a transformation that scales by sx and sy in - * the X and Y dimensions, respectively. - * - * @param sx scale factor in the X direction - * @param sy scale factor in the Y direction - * - * @relates Matrix - */ -Matrix scaled_matrix(double sx, double sy); - -/** Returns a Matrix initialized to a transformation that rotates by radians. - * - * @param radians angle of rotation, in radians. The direction of rotation is - * defined such that positive angles rotate in the direction from the positive X - * axis toward the positive Y axis. With the default axis orientation of cairo, - * positive angles rotate in a clockwise direction. - * - * @relates Matrix - */ -Matrix rotation_matrix(double radians); - -/** Multiplies the affine transformations in a and b together and returns the - * result. The effect of the resulting transformation is to first - * apply the transformation in a to the coordinates and then apply the - * transformation in b to the coordinates. - * - * It is allowable for result to be identical to either a or b. - * - * @param a a Matrix - * @param b a Matrix - * - * @relates Matrix - */ -Matrix operator*(const Matrix& a, const Matrix& b); - } // namespace Cairo #endif // __CAIROMM_MATRIX_H diff --git a/tests/test-font-face.cc b/tests/test-font-face.cc index 466f408..b054709 100644 --- a/tests/test-font-face.cc +++ b/tests/test-font-face.cc @@ -23,6 +23,8 @@ using namespace boost::unit_test; #include #endif // CAIRO_HAS_WIN32_FONT +static Cairo::Matrix identity_matrix; + void test_create_toy () { diff --git a/tests/test-matrix.cc b/tests/test-matrix.cc index 02ed4a2..fe4492f 100644 --- a/tests/test-matrix.cc +++ b/tests/test-matrix.cc @@ -5,42 +5,18 @@ using namespace boost::unit_test; #include -// this is necessary for BOOST_CHECK_EQUAL, but there's no equivalent in the C -// API, so I'm reluctant to include it in cairomm right now -bool operator==(const Cairo::Matrix& A, const Cairo::Matrix& B) -{ - return - A.xx == B.xx && - A.yx == B.yx && - A.xy == B.xy && - A.yy == B.yy && - A.x0 == B.x0 && - A.y0 == B.y0; -} - -// this is necessary for BOOST_CHECK_EQUAL to work but doesn't seem useful -// enough to put in the actual implementation -std::ostream& operator<<(std::ostream& out, const Cairo::Matrix& matrix) -{ - return out << "[ " - << matrix.xx << ", " - << matrix.yx << ", " - << matrix.xy << ", " - << matrix.yy << ", " - << matrix.x0 << ", " - << matrix.y0 << " ]"; -} - void test_constructors() { cairo_matrix_t c_identity; cairo_matrix_init_identity(&c_identity); - BOOST_CHECK_EQUAL(c_identity.xx, Cairo::identity_matrix().xx); - BOOST_CHECK_EQUAL(c_identity.xy, Cairo::identity_matrix().xy); - BOOST_CHECK_EQUAL(c_identity.yx, Cairo::identity_matrix().yx); - BOOST_CHECK_EQUAL(c_identity.yy, Cairo::identity_matrix().yy); - BOOST_CHECK_EQUAL(c_identity.x0, Cairo::identity_matrix().x0); - BOOST_CHECK_EQUAL(c_identity.y0, Cairo::identity_matrix().y0); + Cairo::Matrix cpp_identity; + + BOOST_CHECK_EQUAL(c_identity.xx, cpp_identity.xx); + BOOST_CHECK_EQUAL(c_identity.yx, cpp_identity.yx); + BOOST_CHECK_EQUAL(c_identity.xy, cpp_identity.xy); + BOOST_CHECK_EQUAL(c_identity.yy, cpp_identity.yy); + BOOST_CHECK_EQUAL(c_identity.x0, cpp_identity.x0); + BOOST_CHECK_EQUAL(c_identity.y0, cpp_identity.y0); // nonsense values, just for testing const double xx=1, yx=2, xy=3, yy=5, x0=6, y0=7; @@ -59,7 +35,8 @@ void test_constructors() void test_invert() { // test a valid matrix - BOOST_CHECK_NO_THROW(Cairo::identity_matrix().invert()); + Cairo::Matrix identity; + BOOST_CHECK_NO_THROW(identity.invert()); // check a degenerate matrix Cairo::Matrix degenerate(0,0,0,0,0,0); BOOST_CHECK_THROW(degenerate.invert(), std::logic_error); @@ -74,8 +51,8 @@ static void foo(cairo_matrix_t* matrix) void test_cast() { // make sure that we can cast between C++ and C types without ill effect - Cairo::Matrix matrix = Cairo::identity_matrix(); - cairo_matrix_t casted = (cairo_matrix_t) Cairo::identity_matrix(); + Cairo::Matrix matrix; + cairo_matrix_t casted = (cairo_matrix_t) matrix; // check that it's equal to the identity matrix cairo_matrix_t identity; cairo_matrix_init_identity(&identity); @@ -90,23 +67,13 @@ void test_cast() // pass C++ type as an argument to C foo(&matrix); BOOST_CHECK_EQUAL(matrix.xx, test_matrix->xx); - BOOST_CHECK_EQUAL(matrix.xy, test_matrix->xy); BOOST_CHECK_EQUAL(matrix.yx, test_matrix->yx); + BOOST_CHECK_EQUAL(matrix.xy, test_matrix->xy); BOOST_CHECK_EQUAL(matrix.yy, test_matrix->yy); BOOST_CHECK_EQUAL(matrix.x0, test_matrix->x0); BOOST_CHECK_EQUAL(matrix.y0, test_matrix->y0); } -void test_multiply() -{ - Cairo::Matrix A = Cairo::scaling_matrix(2, 4); - Cairo::Matrix B = Cairo::translation_matrix(5.3, 1.2); - Cairo::Matrix C = A * B; - Cairo::Matrix D; - D.multiply(A, B); - BOOST_CHECK_EQUAL(C, D); -} - test_suite* init_unit_test_suite(int /*argc*/, char** /*argv*/) { @@ -115,7 +82,6 @@ init_unit_test_suite(int /*argc*/, char** /*argv*/) test->add (BOOST_TEST_CASE (&test_constructors)); test->add (BOOST_TEST_CASE (&test_invert)); test->add (BOOST_TEST_CASE (&test_cast)); - test->add (BOOST_TEST_CASE (&test_multiply)); return test; } -- 1.6.3.1