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