epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 11 | #ifndef SkMatrix44_DEFINED |
| 12 | #define SkMatrix44_DEFINED |
| 13 | |
| 14 | #include "SkMatrix.h" |
| 15 | #include "SkScalar.h" |
| 16 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 17 | #ifdef SK_MSCALAR_IS_DOUBLE |
| 18 | typedef double SkMScalar; |
| 19 | static inline double SkFloatToMScalar(float x) { |
| 20 | return static_cast<double>(x); |
| 21 | } |
| 22 | static inline float SkMScalarToFloat(double x) { |
| 23 | return static_cast<float>(x); |
| 24 | } |
| 25 | static inline double SkDoubleToMScalar(double x) { |
| 26 | return x; |
| 27 | } |
| 28 | static inline double SkMScalarToDouble(double x) { |
| 29 | return x; |
| 30 | } |
| 31 | static const SkMScalar SK_MScalarPI = 3.141592653589793; |
vollick@chromium.org | 5596a69 | 2012-11-13 20:12:00 +0000 | [diff] [blame] | 32 | #elif defined SK_MSCALAR_IS_FLOAT |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 33 | typedef float SkMScalar; |
| 34 | static inline float SkFloatToMScalar(float x) { |
| 35 | return x; |
| 36 | } |
| 37 | static inline float SkMScalarToFloat(float x) { |
| 38 | return x; |
| 39 | } |
| 40 | static inline float SkDoubleToMScalar(double x) { |
| 41 | return static_cast<float>(x); |
| 42 | } |
| 43 | static inline double SkMScalarToDouble(float x) { |
| 44 | return static_cast<double>(x); |
| 45 | } |
| 46 | static const SkMScalar SK_MScalarPI = 3.14159265f; |
| 47 | #endif |
| 48 | |
reed@google.com | 20d4467 | 2012-11-09 21:28:55 +0000 | [diff] [blame] | 49 | #define SkMScalarToScalar SkMScalarToFloat |
| 50 | #define SkScalarToMScalar SkFloatToMScalar |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 51 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 52 | static const SkMScalar SK_MScalar1 = 1; |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | struct SkVector4 { |
| 57 | SkScalar fData[4]; |
| 58 | |
| 59 | SkVector4() { |
| 60 | this->set(0, 0, 0, 1); |
| 61 | } |
| 62 | SkVector4(const SkVector4& src) { |
| 63 | memcpy(fData, src.fData, sizeof(fData)); |
| 64 | } |
| 65 | SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { |
| 66 | fData[0] = x; |
| 67 | fData[1] = y; |
| 68 | fData[2] = z; |
| 69 | fData[3] = w; |
| 70 | } |
| 71 | |
| 72 | SkVector4& operator=(const SkVector4& src) { |
| 73 | memcpy(fData, src.fData, sizeof(fData)); |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | bool operator==(const SkVector4& v) { |
| 78 | return fData[0] == v.fData[0] && fData[1] == v.fData[1] && |
| 79 | fData[2] == v.fData[2] && fData[3] == v.fData[3]; |
| 80 | } |
| 81 | bool operator!=(const SkVector4& v) { |
| 82 | return !(*this == v); |
| 83 | } |
| 84 | bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { |
| 85 | return fData[0] == x && fData[1] == y && |
| 86 | fData[2] == z && fData[3] == w; |
| 87 | } |
| 88 | |
| 89 | void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) { |
| 90 | fData[0] = x; |
| 91 | fData[1] = y; |
| 92 | fData[2] = z; |
| 93 | fData[3] = w; |
| 94 | } |
| 95 | }; |
| 96 | |
reed@google.com | 65d8bb0 | 2011-07-05 19:12:59 +0000 | [diff] [blame] | 97 | class SK_API SkMatrix44 { |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 98 | public: |
| 99 | SkMatrix44(); |
| 100 | SkMatrix44(const SkMatrix44&); |
| 101 | SkMatrix44(const SkMatrix44& a, const SkMatrix44& b); |
| 102 | |
| 103 | SkMatrix44& operator=(const SkMatrix44& src) { |
| 104 | memcpy(this, &src, sizeof(*this)); |
| 105 | return *this; |
| 106 | } |
| 107 | |
reed@google.com | 631940c | 2012-11-27 13:13:22 +0000 | [diff] [blame^] | 108 | bool operator==(const SkMatrix44& other) const; |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 109 | bool operator!=(const SkMatrix44& other) const { |
reed@google.com | 631940c | 2012-11-27 13:13:22 +0000 | [diff] [blame^] | 110 | return !(other == *this); |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SkMatrix44(const SkMatrix&); |
| 114 | SkMatrix44& operator=(const SkMatrix& src); |
| 115 | operator SkMatrix() const; |
| 116 | |
reed@google.com | 631940c | 2012-11-27 13:13:22 +0000 | [diff] [blame^] | 117 | SkMScalar get(int row, int col) const { |
| 118 | SkASSERT((unsigned)row <= 3); |
| 119 | SkASSERT((unsigned)col <= 3); |
| 120 | return fMat[col][row]; |
| 121 | } |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 122 | |
reed@google.com | 631940c | 2012-11-27 13:13:22 +0000 | [diff] [blame^] | 123 | void set(int row, int col, SkMScalar value) { |
| 124 | SkASSERT((unsigned)row <= 3); |
| 125 | SkASSERT((unsigned)col <= 3); |
| 126 | fMat[col][row] = value; |
| 127 | } |
| 128 | |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 129 | double getDouble(int row, int col) const { |
| 130 | return SkMScalarToDouble(this->get(row, col)); |
| 131 | } |
| 132 | void setDouble(int row, int col, double value) { |
| 133 | this->set(row, col, SkDoubleToMScalar(value)); |
| 134 | } |
| 135 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 136 | /** These methods allow one to efficiently read matrix entries into an |
| 137 | * array. The given array must have room for exactly 16 entries. Whenever |
| 138 | * possible, they will try to use memcpy rather than an entry-by-entry |
| 139 | * copy. |
| 140 | */ |
reed@google.com | da9fac0 | 2011-06-13 14:46:52 +0000 | [diff] [blame] | 141 | void asColMajorf(float[]) const; |
| 142 | void asColMajord(double[]) const; |
| 143 | void asRowMajorf(float[]) const; |
| 144 | void asRowMajord(double[]) const; |
| 145 | |
vollick@chromium.org | f11cf9f | 2012-11-19 21:02:06 +0000 | [diff] [blame] | 146 | /** These methods allow one to efficiently set all matrix entries from an |
| 147 | * array. The given array must have room for exactly 16 entries. Whenever |
| 148 | * possible, they will try to use memcpy rather than an entry-by-entry |
| 149 | * copy. |
| 150 | */ |
| 151 | void setColMajorf(const float[]); |
| 152 | void setColMajord(const double[]); |
| 153 | void setRowMajorf(const float[]); |
| 154 | void setRowMajord(const double[]); |
| 155 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 156 | bool isIdentity() const; |
| 157 | void setIdentity(); |
| 158 | void reset() { this->setIdentity();} |
| 159 | |
| 160 | void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02, |
| 161 | SkMScalar m10, SkMScalar m11, SkMScalar m12, |
| 162 | SkMScalar m20, SkMScalar m21, SkMScalar m22); |
| 163 | |
| 164 | void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); |
| 165 | void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); |
| 166 | void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz); |
| 167 | |
| 168 | void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); |
| 169 | void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); |
| 170 | void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz); |
| 171 | |
| 172 | void setScale(SkMScalar scale) { |
| 173 | this->setScale(scale, scale, scale); |
| 174 | } |
| 175 | void preScale(SkMScalar scale) { |
| 176 | this->preScale(scale, scale, scale); |
| 177 | } |
| 178 | void postScale(SkMScalar scale) { |
| 179 | this->postScale(scale, scale, scale); |
| 180 | } |
| 181 | |
| 182 | void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z, |
| 183 | SkMScalar degrees) { |
| 184 | this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180); |
| 185 | } |
| 186 | |
| 187 | /** Rotate about the vector [x,y,z]. If that vector is not unit-length, |
| 188 | it will be automatically resized. |
| 189 | */ |
| 190 | void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z, |
| 191 | SkMScalar radians); |
| 192 | /** Rotate about the vector [x,y,z]. Does not check the length of the |
| 193 | vector, assuming it is unit-length. |
| 194 | */ |
| 195 | void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z, |
| 196 | SkMScalar radians); |
| 197 | |
| 198 | void setConcat(const SkMatrix44& a, const SkMatrix44& b); |
| 199 | void preConcat(const SkMatrix44& m) { |
| 200 | this->setConcat(*this, m); |
| 201 | } |
| 202 | void postConcat(const SkMatrix44& m) { |
| 203 | this->setConcat(m, *this); |
| 204 | } |
| 205 | |
| 206 | friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) { |
| 207 | return SkMatrix44(a, b); |
| 208 | } |
| 209 | |
| 210 | /** If this is invertible, return that in inverse and return true. If it is |
| 211 | not invertible, return false and ignore the inverse parameter. |
| 212 | */ |
| 213 | bool invert(SkMatrix44* inverse) const; |
| 214 | |
vollick@chromium.org | 9b21c25 | 2012-11-14 21:33:55 +0000 | [diff] [blame] | 215 | /** Transpose this matrix in place. */ |
| 216 | void transpose(); |
| 217 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 218 | /** Apply the matrix to the src vector, returning the new vector in dst. |
| 219 | It is legal for src and dst to point to the same memory. |
| 220 | */ |
reed@google.com | 1ea95be | 2012-11-09 21:39:48 +0000 | [diff] [blame] | 221 | void mapScalars(const SkScalar src[4], SkScalar dst[4]) const; |
| 222 | void mapScalars(SkScalar vec[4]) const { |
| 223 | this->mapScalars(vec, vec); |
| 224 | } |
| 225 | |
| 226 | // DEPRECATED: call mapScalars() |
| 227 | void map(const SkScalar src[4], SkScalar dst[4]) const { |
| 228 | this->mapScalars(src, dst); |
| 229 | } |
| 230 | // DEPRECATED: call mapScalars() |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 231 | void map(SkScalar vec[4]) const { |
reed@google.com | 1ea95be | 2012-11-09 21:39:48 +0000 | [diff] [blame] | 232 | this->mapScalars(vec, vec); |
| 233 | } |
| 234 | |
| 235 | #ifdef SK_MSCALAR_IS_DOUBLE |
reed@google.com | dd31131 | 2012-11-12 20:43:59 +0000 | [diff] [blame] | 236 | void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const; |
vollick@chromium.org | 5596a69 | 2012-11-13 20:12:00 +0000 | [diff] [blame] | 237 | #elif defined SK_MSCALAR_IS_FLOAT |
reed@google.com | dd31131 | 2012-11-12 20:43:59 +0000 | [diff] [blame] | 238 | void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const { |
reed@google.com | 1ea95be | 2012-11-09 21:39:48 +0000 | [diff] [blame] | 239 | this->mapScalars(src, dst); |
| 240 | } |
| 241 | #endif |
| 242 | void mapMScalars(SkMScalar vec[4]) const { |
| 243 | this->mapMScalars(vec, vec); |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) { |
| 247 | SkVector4 dst; |
| 248 | m.map(src.fData, dst.fData); |
| 249 | return dst; |
| 250 | } |
| 251 | |
| 252 | void dump() const; |
| 253 | |
vollick@chromium.org | 3959a76 | 2012-11-13 15:08:22 +0000 | [diff] [blame] | 254 | double determinant() const; |
| 255 | |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 256 | private: |
| 257 | /* Stored in the same order as opengl: |
| 258 | [3][0] = tx |
| 259 | [3][1] = ty |
| 260 | [3][2] = tz |
| 261 | */ |
| 262 | SkMScalar fMat[4][4]; |
reed@google.com | 8260a89 | 2011-06-13 14:02:52 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | #endif |