Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkMatrix44_DEFINED
      9 #define SkMatrix44_DEFINED
     10 
     11 #include "SkMatrix.h"
     12 #include "SkScalar.h"
     13 
     14 #ifdef SK_MSCALAR_IS_DOUBLE
     15 #ifdef SK_MSCALAR_IS_FLOAT
     16     #error "can't define MSCALAR both as DOUBLE and FLOAT"
     17 #endif
     18     typedef double SkMScalar;
     19 
     20     static inline double SkFloatToMScalar(float x) {
     21         return static_cast<double>(x);
     22     }
     23     static inline float SkMScalarToFloat(double x) {
     24         return static_cast<float>(x);
     25     }
     26     static inline double SkDoubleToMScalar(double x) {
     27         return x;
     28     }
     29     static inline double SkMScalarToDouble(double x) {
     30         return x;
     31     }
     32     static const SkMScalar SK_MScalarPI = 3.141592653589793;
     33 #elif defined SK_MSCALAR_IS_FLOAT
     34 #ifdef SK_MSCALAR_IS_DOUBLE
     35     #error "can't define MSCALAR both as DOUBLE and FLOAT"
     36 #endif
     37     typedef float SkMScalar;
     38 
     39     static inline float SkFloatToMScalar(float x) {
     40         return x;
     41     }
     42     static inline float SkMScalarToFloat(float x) {
     43         return x;
     44     }
     45     static inline float SkDoubleToMScalar(double x) {
     46         return static_cast<float>(x);
     47     }
     48     static inline double SkMScalarToDouble(float x) {
     49         return static_cast<double>(x);
     50     }
     51     static const SkMScalar SK_MScalarPI = 3.14159265f;
     52 #endif
     53 
     54 #define SkMScalarToScalar SkMScalarToFloat
     55 #define SkScalarToMScalar SkFloatToMScalar
     56 
     57 static const SkMScalar SK_MScalar1 = 1;
     58 
     59 ///////////////////////////////////////////////////////////////////////////////
     60 
     61 struct SkVector4 {
     62     SkScalar fData[4];
     63 
     64     SkVector4() {
     65         this->set(0, 0, 0, 1);
     66     }
     67     SkVector4(const SkVector4& src) {
     68         memcpy(fData, src.fData, sizeof(fData));
     69     }
     70     SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
     71         fData[0] = x;
     72         fData[1] = y;
     73         fData[2] = z;
     74         fData[3] = w;
     75     }
     76 
     77     SkVector4& operator=(const SkVector4& src) {
     78         memcpy(fData, src.fData, sizeof(fData));
     79         return *this;
     80     }
     81 
     82     bool operator==(const SkVector4& v) {
     83         return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
     84                fData[2] == v.fData[2] && fData[3] == v.fData[3];
     85     }
     86     bool operator!=(const SkVector4& v) {
     87         return !(*this == v);
     88     }
     89     bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
     90         return fData[0] == x && fData[1] == y &&
     91                fData[2] == z && fData[3] == w;
     92     }
     93 
     94     void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
     95         fData[0] = x;
     96         fData[1] = y;
     97         fData[2] = z;
     98         fData[3] = w;
     99     }
    100 };
    101 
    102 class SK_API SkMatrix44 {
    103 public:
    104 
    105     enum Uninitialized_Constructor {
    106         kUninitialized_Constructor
    107     };
    108     enum Identity_Constructor {
    109         kIdentity_Constructor
    110     };
    111 
    112     SkMatrix44(Uninitialized_Constructor) { }
    113     SkMatrix44(Identity_Constructor) { this->setIdentity(); }
    114 
    115     // DEPRECATED: use the constructors that take an enum
    116     SkMatrix44() { this->setIdentity(); }
    117 
    118     SkMatrix44(const SkMatrix44& src) {
    119         memcpy(fMat, src.fMat, sizeof(fMat));
    120         fTypeMask = src.fTypeMask;
    121     }
    122 
    123     SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) {
    124         this->setConcat(a, b);
    125     }
    126 
    127     SkMatrix44& operator=(const SkMatrix44& src) {
    128         memcpy(fMat, src.fMat, sizeof(fMat));
    129         fTypeMask = src.fTypeMask;
    130         return *this;
    131     }
    132 
    133     bool operator==(const SkMatrix44& other) const;
    134     bool operator!=(const SkMatrix44& other) const {
    135         return !(other == *this);
    136     }
    137 
    138     SkMatrix44(const SkMatrix&);
    139     SkMatrix44& operator=(const SkMatrix& src);
    140     operator SkMatrix() const;
    141 
    142     /**
    143      *  Return a reference to a const identity matrix
    144      */
    145     static const SkMatrix44& I();
    146 
    147     enum TypeMask {
    148         kIdentity_Mask      = 0,
    149         kTranslate_Mask     = 0x01,  //!< set if the matrix has translation
    150         kScale_Mask         = 0x02,  //!< set if the matrix has any scale != 1
    151         kAffine_Mask        = 0x04,  //!< set if the matrix skews or rotates
    152         kPerspective_Mask   = 0x08   //!< set if the matrix is in perspective
    153     };
    154 
    155     /**
    156      *  Returns a bitfield describing the transformations the matrix may
    157      *  perform. The bitfield is computed conservatively, so it may include
    158      *  false positives. For example, when kPerspective_Mask is true, all
    159      *  other bits may be set to true even in the case of a pure perspective
    160      *  transform.
    161      */
    162     inline TypeMask getType() const {
    163         if (fTypeMask & kUnknown_Mask) {
    164             fTypeMask = this->computeTypeMask();
    165         }
    166         SkASSERT(!(fTypeMask & kUnknown_Mask));
    167         return (TypeMask)fTypeMask;
    168     }
    169 
    170     /**
    171      *  Return true if the matrix is identity.
    172      */
    173     inline bool isIdentity() const {
    174         return kIdentity_Mask == this->getType();
    175     }
    176 
    177     /**
    178      *  Return true if the matrix contains translate or is identity.
    179      */
    180     inline bool isTranslate() const {
    181         return !(this->getType() & ~kTranslate_Mask);
    182     }
    183 
    184     /**
    185      *  Return true if the matrix only contains scale or translate or is identity.
    186      */
    187     inline bool isScaleTranslate() const {
    188         return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
    189     }
    190 
    191     void setIdentity();
    192     inline void reset() { this->setIdentity();}
    193 
    194     /**
    195      *  get a value from the matrix. The row,col parameters work as follows:
    196      *  (0, 0)  scale-x
    197      *  (0, 3)  translate-x
    198      *  (3, 0)  perspective-x
    199      */
    200     inline SkMScalar get(int row, int col) const {
    201         SkASSERT((unsigned)row <= 3);
    202         SkASSERT((unsigned)col <= 3);
    203         return fMat[col][row];
    204     }
    205 
    206     /**
    207      *  set a value in the matrix. The row,col parameters work as follows:
    208      *  (0, 0)  scale-x
    209      *  (0, 3)  translate-x
    210      *  (3, 0)  perspective-x
    211      */
    212     inline void set(int row, int col, SkMScalar value) {
    213         SkASSERT((unsigned)row <= 3);
    214         SkASSERT((unsigned)col <= 3);
    215         fMat[col][row] = value;
    216         this->dirtyTypeMask();
    217     }
    218 
    219     inline double getDouble(int row, int col) const {
    220         return SkMScalarToDouble(this->get(row, col));
    221     }
    222     inline void setDouble(int row, int col, double value) {
    223         this->set(row, col, SkDoubleToMScalar(value));
    224     }
    225 
    226     /** These methods allow one to efficiently read matrix entries into an
    227      *  array. The given array must have room for exactly 16 entries. Whenever
    228      *  possible, they will try to use memcpy rather than an entry-by-entry
    229      *  copy.
    230      */
    231     void asColMajorf(float[]) const;
    232     void asColMajord(double[]) const;
    233     void asRowMajorf(float[]) const;
    234     void asRowMajord(double[]) const;
    235 
    236     /** These methods allow one to efficiently set all matrix entries from an
    237      *  array. The given array must have room for exactly 16 entries. Whenever
    238      *  possible, they will try to use memcpy rather than an entry-by-entry
    239      *  copy.
    240      */
    241     void setColMajorf(const float[]);
    242     void setColMajord(const double[]);
    243     void setRowMajorf(const float[]);
    244     void setRowMajord(const double[]);
    245 
    246 #ifdef SK_MSCALAR_IS_FLOAT
    247     void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
    248     void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
    249 #else
    250     void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
    251     void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
    252 #endif
    253 
    254     void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
    255                 SkMScalar m10, SkMScalar m11, SkMScalar m12,
    256                 SkMScalar m20, SkMScalar m21, SkMScalar m22);
    257 
    258     void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
    259     void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
    260     void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
    261 
    262     void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
    263     void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
    264     void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
    265 
    266     inline void setScale(SkMScalar scale) {
    267         this->setScale(scale, scale, scale);
    268     }
    269     inline void preScale(SkMScalar scale) {
    270         this->preScale(scale, scale, scale);
    271     }
    272     inline void postScale(SkMScalar scale) {
    273         this->postScale(scale, scale, scale);
    274     }
    275 
    276     void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
    277                                SkMScalar degrees) {
    278         this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
    279     }
    280 
    281     /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
    282         it will be automatically resized.
    283      */
    284     void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
    285                         SkMScalar radians);
    286     /** Rotate about the vector [x,y,z]. Does not check the length of the
    287         vector, assuming it is unit-length.
    288      */
    289     void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
    290                             SkMScalar radians);
    291 
    292     void setConcat(const SkMatrix44& a, const SkMatrix44& b);
    293     inline void preConcat(const SkMatrix44& m) {
    294         this->setConcat(*this, m);
    295     }
    296     inline void postConcat(const SkMatrix44& m) {
    297         this->setConcat(m, *this);
    298     }
    299 
    300     friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
    301         return SkMatrix44(a, b);
    302     }
    303 
    304     /** If this is invertible, return that in inverse and return true. If it is
    305         not invertible, return false and ignore the inverse parameter.
    306      */
    307     bool invert(SkMatrix44* inverse) const;
    308 
    309     /** Transpose this matrix in place. */
    310     void transpose();
    311 
    312     /** Apply the matrix to the src vector, returning the new vector in dst.
    313         It is legal for src and dst to point to the same memory.
    314      */
    315     void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
    316     inline void mapScalars(SkScalar vec[4]) const {
    317         this->mapScalars(vec, vec);
    318     }
    319 
    320     // DEPRECATED: call mapScalars()
    321     void map(const SkScalar src[4], SkScalar dst[4]) const {
    322         this->mapScalars(src, dst);
    323     }
    324     // DEPRECATED: call mapScalars()
    325     void map(SkScalar vec[4]) const {
    326         this->mapScalars(vec, vec);
    327     }
    328 
    329 #ifdef SK_MSCALAR_IS_DOUBLE
    330     void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
    331 #elif defined SK_MSCALAR_IS_FLOAT
    332     inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
    333         this->mapScalars(src, dst);
    334     }
    335 #endif
    336     inline void mapMScalars(SkMScalar vec[4]) const {
    337         this->mapMScalars(vec, vec);
    338     }
    339 
    340     friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
    341         SkVector4 dst;
    342         m.map(src.fData, dst.fData);
    343         return dst;
    344     }
    345 
    346     /**
    347      *  map an array of [x, y, 0, 1] through the matrix, returning an array
    348      *  of [x', y', z', w'].
    349      *
    350      *  @param src2     array of [x, y] pairs, with implied z=0 and w=1
    351      *  @param count    number of [x, y] pairs in src2
    352      *  @param dst4     array of [x', y', z', w'] quads as the output.
    353      */
    354     void map2(const float src2[], int count, float dst4[]) const;
    355     void map2(const double src2[], int count, double dst4[]) const;
    356 
    357     void dump() const;
    358 
    359     double determinant() const;
    360 
    361 private:
    362     SkMScalar           fMat[4][4];
    363     mutable unsigned    fTypeMask;
    364 
    365     enum {
    366         kUnknown_Mask = 0x80,
    367 
    368         kAllPublic_Masks = 0xF
    369     };
    370 
    371     SkMScalar transX() const { return fMat[3][0]; }
    372     SkMScalar transY() const { return fMat[3][1]; }
    373     SkMScalar transZ() const { return fMat[3][2]; }
    374 
    375     SkMScalar scaleX() const { return fMat[0][0]; }
    376     SkMScalar scaleY() const { return fMat[1][1]; }
    377     SkMScalar scaleZ() const { return fMat[2][2]; }
    378 
    379     SkMScalar perspX() const { return fMat[0][3]; }
    380     SkMScalar perspY() const { return fMat[1][3]; }
    381     SkMScalar perspZ() const { return fMat[2][3]; }
    382 
    383     int computeTypeMask() const;
    384 
    385     inline void dirtyTypeMask() {
    386         fTypeMask = kUnknown_Mask;
    387     }
    388 
    389     inline void setTypeMask(int mask) {
    390         SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
    391         fTypeMask = mask;
    392     }
    393 
    394     /**
    395      *  Does not take the time to 'compute' the typemask. Only returns true if
    396      *  we already know that this matrix is identity.
    397      */
    398     inline bool isTriviallyIdentity() const {
    399         return 0 == fTypeMask;
    400     }
    401 };
    402 
    403 #endif
    404