2005-04-06 05:54:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-04-11 08:36:18 +04:00
|
|
|
#ifndef GFX_MATRIX_H
|
|
|
|
#define GFX_MATRIX_H
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-06-30 08:58:27 +04:00
|
|
|
#include "gfxPoint.h"
|
2005-04-08 09:44:32 +04:00
|
|
|
#include "gfxTypes.h"
|
2005-08-20 09:36:47 +04:00
|
|
|
#include "gfxRect.h"
|
2008-11-05 01:01:21 +03:00
|
|
|
#include "nsMathUtils.h"
|
2005-04-08 09:44:32 +04:00
|
|
|
|
2005-06-28 13:18:55 +04:00
|
|
|
// XX - I don't think this class should use gfxFloat at all,
|
|
|
|
// but should use 'double' and be called gfxDoubleMatrix;
|
|
|
|
// we can then typedef that to gfxMatrix where we typedef
|
|
|
|
// double to be gfxFloat.
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* A matrix that represents an affine transformation. Projective
|
|
|
|
* transformations are not supported. This matrix looks like:
|
|
|
|
*
|
2008-09-20 18:39:31 +04:00
|
|
|
* / a b 0 \
|
|
|
|
* | c d 0 |
|
|
|
|
* \ tx ty 1 /
|
2005-08-20 09:36:47 +04:00
|
|
|
*
|
|
|
|
* So, transforming a point (x, y) results in:
|
|
|
|
*
|
|
|
|
* / a b 0 \ / a * x + c * y + tx \ T
|
|
|
|
* (x y 1) * | c d 0 | = | b * x + d * y + ty |
|
|
|
|
* \ tx ty 1 / \ 1 /
|
|
|
|
*
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
struct THEBES_API gfxMatrix {
|
|
|
|
double xx; double yx;
|
|
|
|
double xy; double yy;
|
|
|
|
double x0; double y0;
|
2005-04-06 05:54:26 +04:00
|
|
|
|
|
|
|
public:
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Initializes this matrix as the identity matrix.
|
|
|
|
*/
|
2005-04-06 05:54:26 +04:00
|
|
|
gfxMatrix() { Reset(); }
|
2007-01-27 04:26:49 +03:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Initializes the matrix from individual components. See the class
|
|
|
|
* description for the layout of the matrix.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxMatrix(gfxFloat a, gfxFloat b, gfxFloat c, gfxFloat d, gfxFloat tx, gfxFloat ty) :
|
|
|
|
xx(a), yx(b),
|
|
|
|
xy(c), yy(d),
|
|
|
|
x0(tx), y0(ty) { }
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Post-multiplies m onto the matrix.
|
|
|
|
*/
|
2005-04-06 05:54:26 +04:00
|
|
|
const gfxMatrix& operator *= (const gfxMatrix& m) {
|
|
|
|
return Multiply(m);
|
|
|
|
}
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Multiplies *this with m and returns the result.
|
|
|
|
*/
|
|
|
|
gfxMatrix operator * (const gfxMatrix& m) const {
|
2005-04-06 05:54:26 +04:00
|
|
|
return gfxMatrix(*this).Multiply(m);
|
|
|
|
}
|
|
|
|
|
2012-06-02 03:53:57 +04:00
|
|
|
/* Returns true if the other matrix is fuzzy-equal to this matrix.
|
|
|
|
* Note that this isn't a cheap comparison!
|
|
|
|
*/
|
|
|
|
bool operator==(const gfxMatrix& other) const
|
|
|
|
{
|
|
|
|
return FuzzyEqual(xx, other.xx) && FuzzyEqual(yx, other.yx) &&
|
|
|
|
FuzzyEqual(xy, other.xy) && FuzzyEqual(yy, other.yy) &&
|
|
|
|
FuzzyEqual(x0, other.x0) && FuzzyEqual(y0, other.y0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const gfxMatrix& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2005-06-28 13:18:55 +04:00
|
|
|
// matrix operations
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Resets this matrix to the identity matrix.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Reset();
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsIdentity() const {
|
2011-07-23 02:28:07 +04:00
|
|
|
return xx == 1.0 && yx == 0.0 &&
|
|
|
|
xy == 0.0 && yy == 1.0 &&
|
|
|
|
x0 == 0.0 && y0 == 0.0;
|
|
|
|
}
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Inverts this matrix, if possible. Otherwise, the matrix is left
|
|
|
|
* unchanged.
|
|
|
|
*
|
|
|
|
* XXX should this do something with the return value of
|
|
|
|
* cairo_matrix_invert?
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Invert();
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2007-01-22 21:14:47 +03:00
|
|
|
/**
|
|
|
|
* Check if matrix is singular (no inverse exists).
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsSingular() const {
|
2007-01-22 21:14:47 +03:00
|
|
|
// if the determinant (ad - bc) is zero it's singular
|
2007-01-27 04:26:49 +03:00
|
|
|
return (xx * yy) == (yx * xy);
|
2007-01-22 21:14:47 +03:00
|
|
|
}
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Scales this matrix. The scale is pre-multiplied onto this matrix,
|
|
|
|
* i.e. the scaling takes place before the other transformations.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Scale(gfxFloat x, gfxFloat y);
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Translates this matrix. The translation is pre-multiplied onto this matrix,
|
|
|
|
* i.e. the translation takes place before the other transformations.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Translate(const gfxPoint& pt);
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Rotates this matrix. The rotation is pre-multiplied onto this matrix,
|
|
|
|
* i.e. the translation takes place after the other transformations.
|
|
|
|
*
|
|
|
|
* @param radians Angle in radians.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Rotate(gfxFloat radians);
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Multiplies the current matrix with m.
|
|
|
|
* This is a post-multiplication, i.e. the transformations of m are
|
|
|
|
* applied _after_ the existing transformations.
|
|
|
|
*
|
|
|
|
* XXX is that difference (compared to Rotate etc) a good thing?
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
const gfxMatrix& Multiply(const gfxMatrix& m);
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2009-04-29 08:31:34 +04:00
|
|
|
/**
|
|
|
|
* Multiplies the current matrix with m.
|
|
|
|
* This is a pre-multiplication, i.e. the transformations of m are
|
|
|
|
* applied _before_ the existing transformations.
|
|
|
|
*/
|
|
|
|
const gfxMatrix& PreMultiply(const gfxMatrix& m);
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Transforms a point according to this matrix.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxPoint Transform(const gfxPoint& point) const;
|
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a distance according to this matrix. This does not apply
|
|
|
|
* any translation components.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxSize Transform(const gfxSize& size) const;
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Transforms both the point and distance according to this matrix.
|
|
|
|
*/
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxRect Transform(const gfxRect& rect) const;
|
2005-06-30 08:58:27 +04:00
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
gfxRect TransformBounds(const gfxRect& rect) const;
|
2006-10-26 06:02:36 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Returns the translation component of this matrix.
|
|
|
|
*/
|
2005-07-02 05:05:26 +04:00
|
|
|
gfxPoint GetTranslation() const {
|
2007-01-27 04:26:49 +03:00
|
|
|
return gfxPoint(x0, y0);
|
2005-06-30 08:58:27 +04:00
|
|
|
}
|
|
|
|
|
2008-11-05 01:01:21 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix is anything other than a straight
|
|
|
|
* translation by integers.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNonIntegerTranslation() const {
|
2008-11-05 01:01:21 +03:00
|
|
|
return HasNonTranslation() ||
|
2011-05-30 23:08:55 +04:00
|
|
|
!FuzzyEqual(x0, floor(x0 + 0.5)) ||
|
|
|
|
!FuzzyEqual(y0, floor(y0 + 0.5));
|
2008-11-05 01:01:21 +03:00
|
|
|
}
|
|
|
|
|
2005-10-06 08:02:10 +04:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix has any transform other
|
|
|
|
* than a straight translation
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNonTranslation() const {
|
2010-07-01 20:43:33 +04:00
|
|
|
return !FuzzyEqual(xx, 1.0) || !FuzzyEqual(yy, 1.0) ||
|
|
|
|
!FuzzyEqual(xy, 0.0) || !FuzzyEqual(yx, 0.0);
|
2005-06-30 08:58:27 +04:00
|
|
|
}
|
2006-08-31 02:01:27 +04:00
|
|
|
|
2010-12-12 08:18:21 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix only has an integer translation.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasOnlyIntegerTranslation() const {
|
2010-12-12 08:18:21 +03:00
|
|
|
return !HasNonIntegerTranslation();
|
|
|
|
}
|
|
|
|
|
2007-03-03 03:18:34 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix has any transform other
|
|
|
|
* than a translation or a -1 y scale (y axis flip)
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNonTranslationOrFlip() const {
|
2010-07-01 20:43:33 +04:00
|
|
|
return !FuzzyEqual(xx, 1.0) ||
|
|
|
|
(!FuzzyEqual(yy, 1.0) && !FuzzyEqual(yy, -1.0)) ||
|
|
|
|
!FuzzyEqual(xy, 0.0) || !FuzzyEqual(yx, 0.0);
|
2006-08-31 02:01:27 +04:00
|
|
|
}
|
2007-03-03 03:18:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the matrix has any transform other
|
|
|
|
* than a translation or scale; this is, if there is
|
|
|
|
* no rotation.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNonAxisAlignedTransform() const {
|
2010-07-01 20:43:33 +04:00
|
|
|
return !FuzzyEqual(xy, 0.0) || !FuzzyEqual(yx, 0.0);
|
2007-03-03 03:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the determinant of this matrix.
|
|
|
|
*/
|
|
|
|
double Determinant() const {
|
|
|
|
return xx*yy - yx*xy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Computes the scale factors of this matrix; that is,
|
|
|
|
* the amounts each basis vector is scaled by.
|
|
|
|
* The xMajor parameter indicates if the larger scale is
|
|
|
|
* to be assumed to be in the X direction or not.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
gfxSize ScaleFactors(bool xMajor) const {
|
2007-03-03 03:18:34 +03:00
|
|
|
double det = Determinant();
|
|
|
|
|
|
|
|
if (det == 0.0)
|
|
|
|
return gfxSize(0.0, 0.0);
|
|
|
|
|
2011-10-18 23:17:32 +04:00
|
|
|
gfxSize sz = xMajor ? gfxSize(1.0, 0.0) : gfxSize(0.0, 1.0);
|
2007-03-03 03:18:34 +03:00
|
|
|
sz = Transform(sz);
|
|
|
|
|
|
|
|
double major = sqrt(sz.width * sz.width + sz.height * sz.height);
|
|
|
|
double minor = 0.0;
|
|
|
|
|
|
|
|
// ignore mirroring
|
|
|
|
if (det < 0.0)
|
|
|
|
det = - det;
|
|
|
|
|
|
|
|
if (major)
|
|
|
|
minor = det / major;
|
|
|
|
|
|
|
|
if (xMajor)
|
|
|
|
return gfxSize(major, minor);
|
|
|
|
|
|
|
|
return gfxSize(minor, major);
|
|
|
|
}
|
2010-07-01 20:43:33 +04:00
|
|
|
|
2010-09-24 03:06:27 +04:00
|
|
|
/**
|
|
|
|
* Snap matrix components that are close to integers
|
|
|
|
* to integers. In particular, components that are integral when
|
|
|
|
* converted to single precision are set to those integers.
|
|
|
|
*/
|
|
|
|
void NudgeToIntegers(void);
|
|
|
|
|
2010-11-24 12:35:21 +03:00
|
|
|
/**
|
|
|
|
* Returns true if matrix is multiple of 90 degrees rotation with flipping,
|
|
|
|
* scaling and translation.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool PreservesAxisAlignedRectangles() const {
|
2010-11-24 12:35:21 +03:00
|
|
|
return ((FuzzyEqual(xx, 0.0) && FuzzyEqual(yy, 0.0))
|
|
|
|
|| (FuzzyEqual(xy, 0.0) && FuzzyEqual(yx, 0.0)));
|
|
|
|
}
|
|
|
|
|
2011-02-04 22:47:06 +03:00
|
|
|
/**
|
|
|
|
* Returns true if the matrix has non-integer scale
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNonIntegerScale() const {
|
2011-05-30 23:08:55 +04:00
|
|
|
return !FuzzyEqual(xx, floor(xx + 0.5)) ||
|
|
|
|
!FuzzyEqual(yy, floor(yy + 0.5));
|
2011-02-04 22:47:06 +03:00
|
|
|
}
|
|
|
|
|
2010-07-01 20:43:33 +04:00
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool FuzzyEqual(gfxFloat aV1, gfxFloat aV2) {
|
2010-07-01 20:43:33 +04:00
|
|
|
return fabs(aV2 - aV1) < 1e-6;
|
|
|
|
}
|
2005-04-06 05:54:26 +04:00
|
|
|
};
|
|
|
|
|
2005-04-11 08:36:18 +04:00
|
|
|
#endif /* GFX_MATRIX_H */
|