Home | History | Annotate | Download | only in transforms
      1 /*
      2  * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef TransformationMatrix_h
     27 #define TransformationMatrix_h
     28 
     29 #include "SkMatrix44.h"
     30 #include <string.h> //for memcpy
     31 #include "platform/geometry/FloatPoint.h"
     32 #include "platform/geometry/FloatPoint3D.h"
     33 #include "platform/geometry/IntPoint.h"
     34 #include "wtf/CPU.h"
     35 #include "wtf/FastAllocBase.h"
     36 
     37 namespace WebCore {
     38 
     39 class AffineTransform;
     40 class IntRect;
     41 class LayoutRect;
     42 class FloatRect;
     43 class FloatQuad;
     44 
     45 #if CPU(X86_64)
     46 #define TRANSFORMATION_MATRIX_USE_X86_64_SSE2
     47 #endif
     48 
     49 class PLATFORM_EXPORT TransformationMatrix {
     50     WTF_MAKE_FAST_ALLOCATED;
     51 public:
     52 
     53 #if CPU(APPLE_ARMV7S) || defined(TRANSFORMATION_MATRIX_USE_X86_64_SSE2)
     54 #if COMPILER(MSVC)
     55     __declspec(align(16)) typedef double Matrix4[4][4];
     56 #else
     57     typedef double Matrix4[4][4] __attribute__((aligned (16)));
     58 #endif
     59 #else
     60     typedef double Matrix4[4][4];
     61 #endif
     62 
     63     TransformationMatrix() { makeIdentity(); }
     64     TransformationMatrix(const AffineTransform& t);
     65     TransformationMatrix(const TransformationMatrix& t) { *this = t; }
     66     TransformationMatrix(double a, double b, double c, double d, double e, double f) { setMatrix(a, b, c, d, e, f); }
     67     TransformationMatrix(double m11, double m12, double m13, double m14,
     68                          double m21, double m22, double m23, double m24,
     69                          double m31, double m32, double m33, double m34,
     70                          double m41, double m42, double m43, double m44)
     71     {
     72         setMatrix(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
     73     }
     74 
     75     void setMatrix(double a, double b, double c, double d, double e, double f)
     76     {
     77         m_matrix[0][0] = a; m_matrix[0][1] = b; m_matrix[0][2] = 0; m_matrix[0][3] = 0;
     78         m_matrix[1][0] = c; m_matrix[1][1] = d; m_matrix[1][2] = 0; m_matrix[1][3] = 0;
     79         m_matrix[2][0] = 0; m_matrix[2][1] = 0; m_matrix[2][2] = 1; m_matrix[2][3] = 0;
     80         m_matrix[3][0] = e; m_matrix[3][1] = f; m_matrix[3][2] = 0; m_matrix[3][3] = 1;
     81     }
     82 
     83     void setMatrix(double m11, double m12, double m13, double m14,
     84                    double m21, double m22, double m23, double m24,
     85                    double m31, double m32, double m33, double m34,
     86                    double m41, double m42, double m43, double m44)
     87     {
     88         m_matrix[0][0] = m11; m_matrix[0][1] = m12; m_matrix[0][2] = m13; m_matrix[0][3] = m14;
     89         m_matrix[1][0] = m21; m_matrix[1][1] = m22; m_matrix[1][2] = m23; m_matrix[1][3] = m24;
     90         m_matrix[2][0] = m31; m_matrix[2][1] = m32; m_matrix[2][2] = m33; m_matrix[2][3] = m34;
     91         m_matrix[3][0] = m41; m_matrix[3][1] = m42; m_matrix[3][2] = m43; m_matrix[3][3] = m44;
     92     }
     93 
     94     TransformationMatrix& operator =(const TransformationMatrix &t)
     95     {
     96         setMatrix(t.m_matrix);
     97         return *this;
     98     }
     99 
    100     TransformationMatrix& makeIdentity()
    101     {
    102         setMatrix(1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1);
    103         return *this;
    104     }
    105 
    106     bool isIdentity() const
    107     {
    108         return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 && m_matrix[0][3] == 0 &&
    109                m_matrix[1][0] == 0 && m_matrix[1][1] == 1 && m_matrix[1][2] == 0 && m_matrix[1][3] == 0 &&
    110                m_matrix[2][0] == 0 && m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0 &&
    111                m_matrix[3][0] == 0 && m_matrix[3][1] == 0 && m_matrix[3][2] == 0 && m_matrix[3][3] == 1;
    112     }
    113 
    114     // This form preserves the double math from input to output
    115     void map(double x, double y, double& x2, double& y2) const { multVecMatrix(x, y, x2, y2); }
    116 
    117     // Map a 3D point through the transform, returning a 3D point.
    118     FloatPoint3D mapPoint(const FloatPoint3D&) const;
    119 
    120     // Map a 2D point through the transform, returning a 2D point.
    121     // Note that this ignores the z component, effectively projecting the point into the z=0 plane.
    122     FloatPoint mapPoint(const FloatPoint&) const;
    123 
    124     // Like the version above, except that it rounds the mapped point to the nearest integer value.
    125     IntPoint mapPoint(const IntPoint& p) const
    126     {
    127         return roundedIntPoint(mapPoint(FloatPoint(p)));
    128     }
    129 
    130     // If the matrix has 3D components, the z component of the result is
    131     // dropped, effectively projecting the rect into the z=0 plane
    132     FloatRect mapRect(const FloatRect&) const;
    133 
    134     // Rounds the resulting mapped rectangle out. This is helpful for bounding
    135     // box computations but may not be what is wanted in other contexts.
    136     IntRect mapRect(const IntRect&) const;
    137     LayoutRect mapRect(const LayoutRect&) const;
    138 
    139     // If the matrix has 3D components, the z component of the result is
    140     // dropped, effectively projecting the quad into the z=0 plane
    141     FloatQuad mapQuad(const FloatQuad&) const;
    142 
    143     // Map a point on the z=0 plane into a point on
    144     // the plane with with the transform applied, by extending
    145     // a ray perpendicular to the source plane and computing
    146     // the local x,y position of the point where that ray intersects
    147     // with the destination plane.
    148     FloatPoint projectPoint(const FloatPoint&, bool* clamped = 0) const;
    149     // Projects the four corners of the quad
    150     FloatQuad projectQuad(const FloatQuad&,  bool* clamped = 0) const;
    151     // Projects the four corners of the quad and takes a bounding box,
    152     // while sanitizing values created when the w component is negative.
    153     LayoutRect clampedBoundsOfProjectedQuad(const FloatQuad&) const;
    154 
    155     double m11() const { return m_matrix[0][0]; }
    156     void setM11(double f) { m_matrix[0][0] = f; }
    157     double m12() const { return m_matrix[0][1]; }
    158     void setM12(double f) { m_matrix[0][1] = f; }
    159     double m13() const { return m_matrix[0][2]; }
    160     void setM13(double f) { m_matrix[0][2] = f; }
    161     double m14() const { return m_matrix[0][3]; }
    162     void setM14(double f) { m_matrix[0][3] = f; }
    163     double m21() const { return m_matrix[1][0]; }
    164     void setM21(double f) { m_matrix[1][0] = f; }
    165     double m22() const { return m_matrix[1][1]; }
    166     void setM22(double f) { m_matrix[1][1] = f; }
    167     double m23() const { return m_matrix[1][2]; }
    168     void setM23(double f) { m_matrix[1][2] = f; }
    169     double m24() const { return m_matrix[1][3]; }
    170     void setM24(double f) { m_matrix[1][3] = f; }
    171     double m31() const { return m_matrix[2][0]; }
    172     void setM31(double f) { m_matrix[2][0] = f; }
    173     double m32() const { return m_matrix[2][1]; }
    174     void setM32(double f) { m_matrix[2][1] = f; }
    175     double m33() const { return m_matrix[2][2]; }
    176     void setM33(double f) { m_matrix[2][2] = f; }
    177     double m34() const { return m_matrix[2][3]; }
    178     void setM34(double f) { m_matrix[2][3] = f; }
    179     double m41() const { return m_matrix[3][0]; }
    180     void setM41(double f) { m_matrix[3][0] = f; }
    181     double m42() const { return m_matrix[3][1]; }
    182     void setM42(double f) { m_matrix[3][1] = f; }
    183     double m43() const { return m_matrix[3][2]; }
    184     void setM43(double f) { m_matrix[3][2] = f; }
    185     double m44() const { return m_matrix[3][3]; }
    186     void setM44(double f) { m_matrix[3][3] = f; }
    187 
    188     double a() const { return m_matrix[0][0]; }
    189     void setA(double a) { m_matrix[0][0] = a; }
    190 
    191     double b() const { return m_matrix[0][1]; }
    192     void setB(double b) { m_matrix[0][1] = b; }
    193 
    194     double c() const { return m_matrix[1][0]; }
    195     void setC(double c) { m_matrix[1][0] = c; }
    196 
    197     double d() const { return m_matrix[1][1]; }
    198     void setD(double d) { m_matrix[1][1] = d; }
    199 
    200     double e() const { return m_matrix[3][0]; }
    201     void setE(double e) { m_matrix[3][0] = e; }
    202 
    203     double f() const { return m_matrix[3][1]; }
    204     void setF(double f) { m_matrix[3][1] = f; }
    205 
    206     // this = mat * this.
    207     TransformationMatrix& multiply(const TransformationMatrix&);
    208 
    209     TransformationMatrix& scale(double);
    210     TransformationMatrix& scaleNonUniform(double sx, double sy);
    211     TransformationMatrix& scale3d(double sx, double sy, double sz);
    212 
    213     TransformationMatrix& rotate(double d) { return rotate3d(0, 0, d); }
    214     TransformationMatrix& rotateFromVector(double x, double y);
    215     TransformationMatrix& rotate3d(double rx, double ry, double rz);
    216 
    217     // The vector (x,y,z) is normalized if it's not already. A vector of
    218     // (0,0,0) uses a vector of (0,0,1).
    219     TransformationMatrix& rotate3d(double x, double y, double z, double angle);
    220 
    221     TransformationMatrix& translate(double tx, double ty);
    222     TransformationMatrix& translate3d(double tx, double ty, double tz);
    223 
    224     // translation added with a post-multiply
    225     TransformationMatrix& translateRight(double tx, double ty);
    226     TransformationMatrix& translateRight3d(double tx, double ty, double tz);
    227 
    228     TransformationMatrix& flipX();
    229     TransformationMatrix& flipY();
    230     TransformationMatrix& skew(double angleX, double angleY);
    231     TransformationMatrix& skewX(double angle) { return skew(angle, 0); }
    232     TransformationMatrix& skewY(double angle) { return skew(0, angle); }
    233 
    234     TransformationMatrix& applyPerspective(double p);
    235     bool hasPerspective() const { return m_matrix[2][3] != 0.0f; }
    236 
    237     // returns a transformation that maps a rect to a rect
    238     static TransformationMatrix rectToRect(const FloatRect&, const FloatRect&);
    239 
    240     bool isInvertible() const;
    241 
    242     // This method returns the identity matrix if it is not invertible.
    243     // Use isInvertible() before calling this if you need to know.
    244     TransformationMatrix inverse() const;
    245 
    246     // decompose the matrix into its component parts
    247     typedef struct {
    248         double scaleX, scaleY, scaleZ;
    249         double skewXY, skewXZ, skewYZ;
    250         double quaternionX, quaternionY, quaternionZ, quaternionW;
    251         double translateX, translateY, translateZ;
    252         double perspectiveX, perspectiveY, perspectiveZ, perspectiveW;
    253     } DecomposedType;
    254 
    255     bool decompose(DecomposedType& decomp) const;
    256     void recompose(const DecomposedType& decomp);
    257 
    258     void blend(const TransformationMatrix& from, double progress);
    259 
    260     bool isAffine() const
    261     {
    262         return (m13() == 0 && m14() == 0 && m23() == 0 && m24() == 0 &&
    263                 m31() == 0 && m32() == 0 && m33() == 1 && m34() == 0 && m43() == 0 && m44() == 1);
    264     }
    265 
    266     // Throw away the non-affine parts of the matrix (lossy!)
    267     void makeAffine();
    268 
    269     AffineTransform toAffineTransform() const;
    270 
    271     bool operator==(const TransformationMatrix& m2) const
    272     {
    273         return (m_matrix[0][0] == m2.m_matrix[0][0] &&
    274                 m_matrix[0][1] == m2.m_matrix[0][1] &&
    275                 m_matrix[0][2] == m2.m_matrix[0][2] &&
    276                 m_matrix[0][3] == m2.m_matrix[0][3] &&
    277                 m_matrix[1][0] == m2.m_matrix[1][0] &&
    278                 m_matrix[1][1] == m2.m_matrix[1][1] &&
    279                 m_matrix[1][2] == m2.m_matrix[1][2] &&
    280                 m_matrix[1][3] == m2.m_matrix[1][3] &&
    281                 m_matrix[2][0] == m2.m_matrix[2][0] &&
    282                 m_matrix[2][1] == m2.m_matrix[2][1] &&
    283                 m_matrix[2][2] == m2.m_matrix[2][2] &&
    284                 m_matrix[2][3] == m2.m_matrix[2][3] &&
    285                 m_matrix[3][0] == m2.m_matrix[3][0] &&
    286                 m_matrix[3][1] == m2.m_matrix[3][1] &&
    287                 m_matrix[3][2] == m2.m_matrix[3][2] &&
    288                 m_matrix[3][3] == m2.m_matrix[3][3]);
    289     }
    290 
    291     bool operator!=(const TransformationMatrix& other) const { return !(*this == other); }
    292 
    293     // *this = *this * t
    294     TransformationMatrix& operator*=(const TransformationMatrix& t)
    295     {
    296         return multiply(t);
    297     }
    298 
    299     // result = *this * t
    300     TransformationMatrix operator*(const TransformationMatrix& t) const
    301     {
    302         TransformationMatrix result = *this;
    303         result.multiply(t);
    304         return result;
    305     }
    306 
    307     bool isIdentityOrTranslation() const
    308     {
    309         return m_matrix[0][0] == 1 && m_matrix[0][1] == 0 && m_matrix[0][2] == 0 && m_matrix[0][3] == 0
    310             && m_matrix[1][0] == 0 && m_matrix[1][1] == 1 && m_matrix[1][2] == 0 && m_matrix[1][3] == 0
    311             && m_matrix[2][0] == 0 && m_matrix[2][1] == 0 && m_matrix[2][2] == 1 && m_matrix[2][3] == 0
    312             && m_matrix[3][3] == 1;
    313     }
    314 
    315     bool isIntegerTranslation() const;
    316 
    317     // This method returns the matrix without 3D components.
    318     TransformationMatrix to2dTransform() const;
    319 
    320     typedef float FloatMatrix4[16];
    321     void toColumnMajorFloatArray(FloatMatrix4& result) const;
    322 
    323     // A local-space layer is implicitly defined at the z = 0 plane, with its front side
    324     // facing the positive z-axis (i.e. a camera looking along the negative z-axis sees
    325     // the front side of the layer). This function checks if the transformed layer's back
    326     // face would be visible to a camera looking along the negative z-axis in the target space.
    327     bool isBackFaceVisible() const;
    328 
    329     static SkMatrix44 toSkMatrix44(const TransformationMatrix&);
    330 
    331 private:
    332     // multiply passed 2D point by matrix (assume z=0)
    333     void multVecMatrix(double x, double y, double& dstX, double& dstY) const;
    334     FloatPoint internalMapPoint(const FloatPoint& sourcePoint) const
    335     {
    336         double resultX;
    337         double resultY;
    338         multVecMatrix(sourcePoint.x(), sourcePoint.y(), resultX, resultY);
    339         return FloatPoint(static_cast<float>(resultX), static_cast<float>(resultY));
    340     }
    341 
    342     // multiply passed 3D point by matrix
    343     void multVecMatrix(double x, double y, double z, double& dstX, double& dstY, double& dstZ) const;
    344     FloatPoint3D internalMapPoint(const FloatPoint3D& sourcePoint) const
    345     {
    346         double resultX;
    347         double resultY;
    348         double resultZ;
    349         multVecMatrix(sourcePoint.x(), sourcePoint.y(), sourcePoint.z(), resultX, resultY, resultZ);
    350         return FloatPoint3D(static_cast<float>(resultX), static_cast<float>(resultY), static_cast<float>(resultZ));
    351     }
    352 
    353     void setMatrix(const Matrix4 m)
    354     {
    355         if (m && m != m_matrix)
    356             memcpy(m_matrix, m, sizeof(Matrix4));
    357     }
    358 
    359     Matrix4 m_matrix;
    360 };
    361 
    362 } // namespace WebCore
    363 
    364 #endif // TransformationMatrix_h
    365