michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkMatrix44_DEFINED michael@0: #define SkMatrix44_DEFINED michael@0: michael@0: #include "SkMatrix.h" michael@0: #include "SkScalar.h" michael@0: michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: #ifdef SK_MSCALAR_IS_FLOAT michael@0: #error "can't define MSCALAR both as DOUBLE and FLOAT" michael@0: #endif michael@0: typedef double SkMScalar; michael@0: michael@0: static inline double SkFloatToMScalar(float x) { michael@0: return static_cast(x); michael@0: } michael@0: static inline float SkMScalarToFloat(double x) { michael@0: return static_cast(x); michael@0: } michael@0: static inline double SkDoubleToMScalar(double x) { michael@0: return x; michael@0: } michael@0: static inline double SkMScalarToDouble(double x) { michael@0: return x; michael@0: } michael@0: static const SkMScalar SK_MScalarPI = 3.141592653589793; michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: #error "can't define MSCALAR both as DOUBLE and FLOAT" michael@0: #endif michael@0: typedef float SkMScalar; michael@0: michael@0: static inline float SkFloatToMScalar(float x) { michael@0: return x; michael@0: } michael@0: static inline float SkMScalarToFloat(float x) { michael@0: return x; michael@0: } michael@0: static inline float SkDoubleToMScalar(double x) { michael@0: return static_cast(x); michael@0: } michael@0: static inline double SkMScalarToDouble(float x) { michael@0: return static_cast(x); michael@0: } michael@0: static const SkMScalar SK_MScalarPI = 3.14159265f; michael@0: #endif michael@0: michael@0: #define SkMScalarToScalar SkMScalarToFloat michael@0: #define SkScalarToMScalar SkFloatToMScalar michael@0: michael@0: static const SkMScalar SK_MScalar1 = 1; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: struct SkVector4 { michael@0: SkScalar fData[4]; michael@0: michael@0: SkVector4() { michael@0: this->set(0, 0, 0, 1); michael@0: } michael@0: SkVector4(const SkVector4& src) { michael@0: memcpy(fData, src.fData, sizeof(fData)); michael@0: } michael@0: SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { michael@0: fData[0] = x; michael@0: fData[1] = y; michael@0: fData[2] = z; michael@0: fData[3] = w; michael@0: } michael@0: michael@0: SkVector4& operator=(const SkVector4& src) { michael@0: memcpy(fData, src.fData, sizeof(fData)); michael@0: return *this; michael@0: } michael@0: michael@0: bool operator==(const SkVector4& v) { michael@0: return fData[0] == v.fData[0] && fData[1] == v.fData[1] && michael@0: fData[2] == v.fData[2] && fData[3] == v.fData[3]; michael@0: } michael@0: bool operator!=(const SkVector4& v) { michael@0: return !(*this == v); michael@0: } michael@0: bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { michael@0: return fData[0] == x && fData[1] == y && michael@0: fData[2] == z && fData[3] == w; michael@0: } michael@0: michael@0: void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { michael@0: fData[0] = x; michael@0: fData[1] = y; michael@0: fData[2] = z; michael@0: fData[3] = w; michael@0: } michael@0: }; michael@0: michael@0: class SK_API SkMatrix44 { michael@0: public: michael@0: michael@0: enum Uninitialized_Constructor { michael@0: kUninitialized_Constructor michael@0: }; michael@0: enum Identity_Constructor { michael@0: kIdentity_Constructor michael@0: }; michael@0: michael@0: SkMatrix44(Uninitialized_Constructor) { } michael@0: SkMatrix44(Identity_Constructor) { this->setIdentity(); } michael@0: michael@0: SK_ATTR_DEPRECATED("use the constructors that take an enum") michael@0: SkMatrix44() { this->setIdentity(); } michael@0: michael@0: SkMatrix44(const SkMatrix44& src) { michael@0: memcpy(fMat, src.fMat, sizeof(fMat)); michael@0: fTypeMask = src.fTypeMask; michael@0: } michael@0: michael@0: SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) { michael@0: this->setConcat(a, b); michael@0: } michael@0: michael@0: SkMatrix44& operator=(const SkMatrix44& src) { michael@0: if (&src != this) { michael@0: memcpy(fMat, src.fMat, sizeof(fMat)); michael@0: fTypeMask = src.fTypeMask; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: bool operator==(const SkMatrix44& other) const; michael@0: bool operator!=(const SkMatrix44& other) const { michael@0: return !(other == *this); michael@0: } michael@0: michael@0: /* When converting from SkMatrix44 to SkMatrix, the third row and michael@0: * column is dropped. When converting from SkMatrix to SkMatrix44 michael@0: * the third row and column remain as identity: michael@0: * [ a b c ] [ a b 0 c ] michael@0: * [ d e f ] -> [ d e 0 f ] michael@0: * [ g h i ] [ 0 0 1 0 ] michael@0: * [ g h 0 i ] michael@0: */ michael@0: SkMatrix44(const SkMatrix&); michael@0: SkMatrix44& operator=(const SkMatrix& src); michael@0: operator SkMatrix() const; michael@0: michael@0: /** michael@0: * Return a reference to a const identity matrix michael@0: */ michael@0: static const SkMatrix44& I(); michael@0: michael@0: enum TypeMask { michael@0: kIdentity_Mask = 0, michael@0: kTranslate_Mask = 0x01, //!< set if the matrix has translation michael@0: kScale_Mask = 0x02, //!< set if the matrix has any scale != 1 michael@0: kAffine_Mask = 0x04, //!< set if the matrix skews or rotates michael@0: kPerspective_Mask = 0x08 //!< set if the matrix is in perspective michael@0: }; michael@0: michael@0: /** michael@0: * Returns a bitfield describing the transformations the matrix may michael@0: * perform. The bitfield is computed conservatively, so it may include michael@0: * false positives. For example, when kPerspective_Mask is true, all michael@0: * other bits may be set to true even in the case of a pure perspective michael@0: * transform. michael@0: */ michael@0: inline TypeMask getType() const { michael@0: if (fTypeMask & kUnknown_Mask) { michael@0: fTypeMask = this->computeTypeMask(); michael@0: } michael@0: SkASSERT(!(fTypeMask & kUnknown_Mask)); michael@0: return (TypeMask)fTypeMask; michael@0: } michael@0: michael@0: /** michael@0: * Return true if the matrix is identity. michael@0: */ michael@0: inline bool isIdentity() const { michael@0: return kIdentity_Mask == this->getType(); michael@0: } michael@0: michael@0: /** michael@0: * Return true if the matrix contains translate or is identity. michael@0: */ michael@0: inline bool isTranslate() const { michael@0: return !(this->getType() & ~kTranslate_Mask); michael@0: } michael@0: michael@0: /** michael@0: * Return true if the matrix only contains scale or translate or is identity. michael@0: */ michael@0: inline bool isScaleTranslate() const { michael@0: return !(this->getType() & ~(kScale_Mask | kTranslate_Mask)); michael@0: } michael@0: michael@0: void setIdentity(); michael@0: inline void reset() { this->setIdentity();} michael@0: michael@0: /** michael@0: * get a value from the matrix. The row,col parameters work as follows: michael@0: * (0, 0) scale-x michael@0: * (0, 3) translate-x michael@0: * (3, 0) perspective-x michael@0: */ michael@0: inline SkMScalar get(int row, int col) const { michael@0: SkASSERT((unsigned)row <= 3); michael@0: SkASSERT((unsigned)col <= 3); michael@0: return fMat[col][row]; michael@0: } michael@0: michael@0: /** michael@0: * set a value in the matrix. The row,col parameters work as follows: michael@0: * (0, 0) scale-x michael@0: * (0, 3) translate-x michael@0: * (3, 0) perspective-x michael@0: */ michael@0: inline void set(int row, int col, SkMScalar value) { michael@0: SkASSERT((unsigned)row <= 3); michael@0: SkASSERT((unsigned)col <= 3); michael@0: fMat[col][row] = value; michael@0: this->dirtyTypeMask(); michael@0: } michael@0: michael@0: inline double getDouble(int row, int col) const { michael@0: return SkMScalarToDouble(this->get(row, col)); michael@0: } michael@0: inline void setDouble(int row, int col, double value) { michael@0: this->set(row, col, SkDoubleToMScalar(value)); michael@0: } michael@0: inline float getFloat(int row, int col) const { michael@0: return SkMScalarToFloat(this->get(row, col)); michael@0: } michael@0: inline void setFloat(int row, int col, float value) { michael@0: this->set(row, col, SkFloatToMScalar(value)); michael@0: } michael@0: michael@0: /** These methods allow one to efficiently read matrix entries into an michael@0: * array. The given array must have room for exactly 16 entries. Whenever michael@0: * possible, they will try to use memcpy rather than an entry-by-entry michael@0: * copy. michael@0: */ michael@0: void asColMajorf(float[]) const; michael@0: void asColMajord(double[]) const; michael@0: void asRowMajorf(float[]) const; michael@0: void asRowMajord(double[]) const; michael@0: michael@0: /** These methods allow one to efficiently set all matrix entries from an michael@0: * array. The given array must have room for exactly 16 entries. Whenever michael@0: * possible, they will try to use memcpy rather than an entry-by-entry michael@0: * copy. michael@0: */ michael@0: void setColMajorf(const float[]); michael@0: void setColMajord(const double[]); michael@0: void setRowMajorf(const float[]); michael@0: void setRowMajord(const double[]); michael@0: michael@0: #ifdef SK_MSCALAR_IS_FLOAT michael@0: void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); } michael@0: void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); } michael@0: #else michael@0: void setColMajor(const SkMScalar data[]) { this->setColMajord(data); } michael@0: void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); } michael@0: #endif michael@0: michael@0: /* This sets the top-left of the matrix and clears the translation and michael@0: * perspective components (with [3][3] set to 1). */ michael@0: void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02, michael@0: SkMScalar m10, SkMScalar m11, SkMScalar m12, michael@0: SkMScalar m20, SkMScalar m21, SkMScalar m22); michael@0: michael@0: void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); michael@0: void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); michael@0: void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); michael@0: michael@0: void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); michael@0: void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); michael@0: void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); michael@0: michael@0: inline void setScale(SkMScalar scale) { michael@0: this->setScale(scale, scale, scale); michael@0: } michael@0: inline void preScale(SkMScalar scale) { michael@0: this->preScale(scale, scale, scale); michael@0: } michael@0: inline void postScale(SkMScalar scale) { michael@0: this->postScale(scale, scale, scale); michael@0: } michael@0: michael@0: void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z, michael@0: SkMScalar degrees) { michael@0: this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180); michael@0: } michael@0: michael@0: /** Rotate about the vector [x,y,z]. If that vector is not unit-length, michael@0: it will be automatically resized. michael@0: */ michael@0: void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z, michael@0: SkMScalar radians); michael@0: /** Rotate about the vector [x,y,z]. Does not check the length of the michael@0: vector, assuming it is unit-length. michael@0: */ michael@0: void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z, michael@0: SkMScalar radians); michael@0: michael@0: void setConcat(const SkMatrix44& a, const SkMatrix44& b); michael@0: inline void preConcat(const SkMatrix44& m) { michael@0: this->setConcat(*this, m); michael@0: } michael@0: inline void postConcat(const SkMatrix44& m) { michael@0: this->setConcat(m, *this); michael@0: } michael@0: michael@0: friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) { michael@0: return SkMatrix44(a, b); michael@0: } michael@0: michael@0: /** If this is invertible, return that in inverse and return true. If it is michael@0: not invertible, return false and ignore the inverse parameter. michael@0: */ michael@0: bool invert(SkMatrix44* inverse) const; michael@0: michael@0: /** Transpose this matrix in place. */ michael@0: void transpose(); michael@0: michael@0: /** Apply the matrix to the src vector, returning the new vector in dst. michael@0: It is legal for src and dst to point to the same memory. michael@0: */ michael@0: void mapScalars(const SkScalar src[4], SkScalar dst[4]) const; michael@0: inline void mapScalars(SkScalar vec[4]) const { michael@0: this->mapScalars(vec, vec); michael@0: } michael@0: michael@0: SK_ATTR_DEPRECATED("use mapScalars") michael@0: void map(const SkScalar src[4], SkScalar dst[4]) const { michael@0: this->mapScalars(src, dst); michael@0: } michael@0: michael@0: SK_ATTR_DEPRECATED("use mapScalars") michael@0: void map(SkScalar vec[4]) const { michael@0: this->mapScalars(vec, vec); michael@0: } michael@0: michael@0: #ifdef SK_MSCALAR_IS_DOUBLE michael@0: void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const; michael@0: #elif defined SK_MSCALAR_IS_FLOAT michael@0: inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const { michael@0: this->mapScalars(src, dst); michael@0: } michael@0: #endif michael@0: inline void mapMScalars(SkMScalar vec[4]) const { michael@0: this->mapMScalars(vec, vec); michael@0: } michael@0: michael@0: friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) { michael@0: SkVector4 dst; michael@0: m.mapScalars(src.fData, dst.fData); michael@0: return dst; michael@0: } michael@0: michael@0: /** michael@0: * map an array of [x, y, 0, 1] through the matrix, returning an array michael@0: * of [x', y', z', w']. michael@0: * michael@0: * @param src2 array of [x, y] pairs, with implied z=0 and w=1 michael@0: * @param count number of [x, y] pairs in src2 michael@0: * @param dst4 array of [x', y', z', w'] quads as the output. michael@0: */ michael@0: void map2(const float src2[], int count, float dst4[]) const; michael@0: void map2(const double src2[], int count, double dst4[]) const; michael@0: michael@0: void dump() const; michael@0: michael@0: double determinant() const; michael@0: michael@0: private: michael@0: SkMScalar fMat[4][4]; michael@0: mutable unsigned fTypeMask; michael@0: michael@0: enum { michael@0: kUnknown_Mask = 0x80, michael@0: michael@0: kAllPublic_Masks = 0xF michael@0: }; michael@0: michael@0: SkMScalar transX() const { return fMat[3][0]; } michael@0: SkMScalar transY() const { return fMat[3][1]; } michael@0: SkMScalar transZ() const { return fMat[3][2]; } michael@0: michael@0: SkMScalar scaleX() const { return fMat[0][0]; } michael@0: SkMScalar scaleY() const { return fMat[1][1]; } michael@0: SkMScalar scaleZ() const { return fMat[2][2]; } michael@0: michael@0: SkMScalar perspX() const { return fMat[0][3]; } michael@0: SkMScalar perspY() const { return fMat[1][3]; } michael@0: SkMScalar perspZ() const { return fMat[2][3]; } michael@0: michael@0: int computeTypeMask() const; michael@0: michael@0: inline void dirtyTypeMask() { michael@0: fTypeMask = kUnknown_Mask; michael@0: } michael@0: michael@0: inline void setTypeMask(int mask) { michael@0: SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask)); michael@0: fTypeMask = mask; michael@0: } michael@0: michael@0: /** michael@0: * Does not take the time to 'compute' the typemask. Only returns true if michael@0: * we already know that this matrix is identity. michael@0: */ michael@0: inline bool isTriviallyIdentity() const { michael@0: return 0 == fTypeMask; michael@0: } michael@0: }; michael@0: michael@0: #endif