blob: 26247a01e99c0aa19dad0152d61309d1c75b577e [file] [log] [blame]
reed@google.com8260a892011-06-13 14:02:52 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.com8260a892011-06-13 14:02:52 +00006 */
7
reed@google.com8260a892011-06-13 14:02:52 +00008#ifndef SkMatrix44_DEFINED
9#define SkMatrix44_DEFINED
10
11#include "SkMatrix.h"
12#include "SkScalar.h"
13
reed@google.com8260a892011-06-13 14:02:52 +000014#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.com7d683352012-12-03 21:19:52 +000015#ifdef SK_MSCALAR_IS_FLOAT
16 #error "can't define MSCALAR both as DOUBLE and FLOAT"
17#endif
reed@google.com8260a892011-06-13 14:02:52 +000018 typedef double SkMScalar;
reed@google.com7d683352012-12-03 21:19:52 +000019
reed@google.com8260a892011-06-13 14:02:52 +000020 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;
vollick@chromium.org5596a692012-11-13 20:12:00 +000033#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +000034#ifdef SK_MSCALAR_IS_DOUBLE
35 #error "can't define MSCALAR both as DOUBLE and FLOAT"
36#endif
reed@google.com8260a892011-06-13 14:02:52 +000037 typedef float SkMScalar;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +000038
reed@google.com8260a892011-06-13 14:02:52 +000039 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
reed@google.com20d44672012-11-09 21:28:55 +000054#define SkMScalarToScalar SkMScalarToFloat
55#define SkScalarToMScalar SkFloatToMScalar
bsalomon@google.com72e49b82011-10-27 21:47:03 +000056
reed@google.com8260a892011-06-13 14:02:52 +000057static const SkMScalar SK_MScalar1 = 1;
58
59///////////////////////////////////////////////////////////////////////////////
60
61struct 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
tomhudson@google.com7cfb9c72013-01-17 13:29:35 +0000102class SK_API SkMatrix44 {
reed@google.com8260a892011-06-13 14:02:52 +0000103public:
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000104
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
reed@google.com44699382013-10-31 17:28:30 +0000115 SK_ATTR_DEPRECATED("use the constructors that take an enum")
reed@google.com7d683352012-12-03 21:19:52 +0000116 SkMatrix44() { this->setIdentity(); }
reed@google.comd5302532013-01-15 14:54:00 +0000117
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 }
reed@google.com8260a892011-06-13 14:02:52 +0000126
127 SkMatrix44& operator=(const SkMatrix44& src) {
reed@google.com3fda0ea2013-02-06 12:47:14 +0000128 if (&src != this) {
129 memcpy(fMat, src.fMat, sizeof(fMat));
130 fTypeMask = src.fTypeMask;
131 }
reed@google.com8260a892011-06-13 14:02:52 +0000132 return *this;
133 }
134
reed@google.com631940c2012-11-27 13:13:22 +0000135 bool operator==(const SkMatrix44& other) const;
reed@google.com8260a892011-06-13 14:02:52 +0000136 bool operator!=(const SkMatrix44& other) const {
reed@google.com631940c2012-11-27 13:13:22 +0000137 return !(other == *this);
reed@google.com8260a892011-06-13 14:02:52 +0000138 }
139
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000140 /* When converting from SkMatrix44 to SkMatrix, the third row and
141 * column is dropped. When converting from SkMatrix to SkMatrix44
142 * the third row and column remain as identity:
143 * [ a b c ] [ a b 0 c ]
144 * [ d e f ] -> [ d e 0 f ]
145 * [ g h i ] [ 0 0 1 0 ]
146 * [ g h 0 i ]
147 */
reed@google.com8260a892011-06-13 14:02:52 +0000148 SkMatrix44(const SkMatrix&);
149 SkMatrix44& operator=(const SkMatrix& src);
150 operator SkMatrix() const;
151
reed@google.com7d683352012-12-03 21:19:52 +0000152 /**
153 * Return a reference to a const identity matrix
154 */
155 static const SkMatrix44& I();
156
157 enum TypeMask {
158 kIdentity_Mask = 0,
159 kTranslate_Mask = 0x01, //!< set if the matrix has translation
160 kScale_Mask = 0x02, //!< set if the matrix has any scale != 1
161 kAffine_Mask = 0x04, //!< set if the matrix skews or rotates
162 kPerspective_Mask = 0x08 //!< set if the matrix is in perspective
163 };
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000164
reed@google.com7d683352012-12-03 21:19:52 +0000165 /**
166 * Returns a bitfield describing the transformations the matrix may
167 * perform. The bitfield is computed conservatively, so it may include
168 * false positives. For example, when kPerspective_Mask is true, all
169 * other bits may be set to true even in the case of a pure perspective
170 * transform.
171 */
172 inline TypeMask getType() const {
173 if (fTypeMask & kUnknown_Mask) {
174 fTypeMask = this->computeTypeMask();
175 }
176 SkASSERT(!(fTypeMask & kUnknown_Mask));
177 return (TypeMask)fTypeMask;
178 }
179
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000180 /**
181 * Return true if the matrix is identity.
182 */
reed@google.com7d683352012-12-03 21:19:52 +0000183 inline bool isIdentity() const {
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000184 return kIdentity_Mask == this->getType();
185 }
186
187 /**
188 * Return true if the matrix contains translate or is identity.
189 */
190 inline bool isTranslate() const {
191 return !(this->getType() & ~kTranslate_Mask);
192 }
193
194 /**
195 * Return true if the matrix only contains scale or translate or is identity.
196 */
197 inline bool isScaleTranslate() const {
198 return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
reed@google.com7d683352012-12-03 21:19:52 +0000199 }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000200
reed@google.com7d683352012-12-03 21:19:52 +0000201 void setIdentity();
202 inline void reset() { this->setIdentity();}
203
204 /**
205 * get a value from the matrix. The row,col parameters work as follows:
206 * (0, 0) scale-x
207 * (0, 3) translate-x
208 * (3, 0) perspective-x
209 */
210 inline SkMScalar get(int row, int col) const {
reed@google.com631940c2012-11-27 13:13:22 +0000211 SkASSERT((unsigned)row <= 3);
212 SkASSERT((unsigned)col <= 3);
213 return fMat[col][row];
214 }
reed@google.com8260a892011-06-13 14:02:52 +0000215
reed@google.com7d683352012-12-03 21:19:52 +0000216 /**
217 * set a value in the matrix. The row,col parameters work as follows:
218 * (0, 0) scale-x
219 * (0, 3) translate-x
220 * (3, 0) perspective-x
221 */
222 inline void set(int row, int col, SkMScalar value) {
reed@google.com631940c2012-11-27 13:13:22 +0000223 SkASSERT((unsigned)row <= 3);
224 SkASSERT((unsigned)col <= 3);
225 fMat[col][row] = value;
reed@google.com7d683352012-12-03 21:19:52 +0000226 this->dirtyTypeMask();
reed@google.com631940c2012-11-27 13:13:22 +0000227 }
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000228
reed@google.com7d683352012-12-03 21:19:52 +0000229 inline double getDouble(int row, int col) const {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000230 return SkMScalarToDouble(this->get(row, col));
231 }
reed@google.com7d683352012-12-03 21:19:52 +0000232 inline void setDouble(int row, int col, double value) {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000233 this->set(row, col, SkDoubleToMScalar(value));
234 }
commit-bot@chromium.orge2419cc2013-09-18 18:52:09 +0000235 inline float getFloat(int row, int col) const {
236 return SkMScalarToFloat(this->get(row, col));
237 }
238 inline void setFloat(int row, int col, float value) {
239 this->set(row, col, SkFloatToMScalar(value));
240 }
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000241
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000242 /** These methods allow one to efficiently read matrix entries into an
243 * array. The given array must have room for exactly 16 entries. Whenever
244 * possible, they will try to use memcpy rather than an entry-by-entry
245 * copy.
246 */
reed@google.comda9fac02011-06-13 14:46:52 +0000247 void asColMajorf(float[]) const;
248 void asColMajord(double[]) const;
249 void asRowMajorf(float[]) const;
250 void asRowMajord(double[]) const;
251
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000252 /** These methods allow one to efficiently set all matrix entries from an
253 * array. The given array must have room for exactly 16 entries. Whenever
254 * possible, they will try to use memcpy rather than an entry-by-entry
255 * copy.
256 */
257 void setColMajorf(const float[]);
258 void setColMajord(const double[]);
259 void setRowMajorf(const float[]);
260 void setRowMajord(const double[]);
261
reed@google.com7d683352012-12-03 21:19:52 +0000262#ifdef SK_MSCALAR_IS_FLOAT
263 void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
264 void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
265#else
266 void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
267 void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
268#endif
reed@google.com8260a892011-06-13 14:02:52 +0000269
commit-bot@chromium.org722555b2013-10-05 01:16:30 +0000270 /* This sets the top-left of the matrix and clears the translation and
271 * perspective components (with [3][3] set to 1). */
reed@google.com8260a892011-06-13 14:02:52 +0000272 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
273 SkMScalar m10, SkMScalar m11, SkMScalar m12,
274 SkMScalar m20, SkMScalar m21, SkMScalar m22);
275
276 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
277 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
278 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
279
280 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
281 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
282 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
283
reed@google.com7d683352012-12-03 21:19:52 +0000284 inline void setScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000285 this->setScale(scale, scale, scale);
286 }
reed@google.com7d683352012-12-03 21:19:52 +0000287 inline void preScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000288 this->preScale(scale, scale, scale);
289 }
reed@google.com7d683352012-12-03 21:19:52 +0000290 inline void postScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000291 this->postScale(scale, scale, scale);
292 }
293
294 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
295 SkMScalar degrees) {
296 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
297 }
298
299 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
300 it will be automatically resized.
301 */
302 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
303 SkMScalar radians);
304 /** Rotate about the vector [x,y,z]. Does not check the length of the
305 vector, assuming it is unit-length.
306 */
307 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
308 SkMScalar radians);
309
310 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
reed@google.com7d683352012-12-03 21:19:52 +0000311 inline void preConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000312 this->setConcat(*this, m);
313 }
reed@google.com7d683352012-12-03 21:19:52 +0000314 inline void postConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000315 this->setConcat(m, *this);
316 }
317
318 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
319 return SkMatrix44(a, b);
320 }
321
322 /** If this is invertible, return that in inverse and return true. If it is
323 not invertible, return false and ignore the inverse parameter.
324 */
325 bool invert(SkMatrix44* inverse) const;
326
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000327 /** Transpose this matrix in place. */
328 void transpose();
329
reed@google.com8260a892011-06-13 14:02:52 +0000330 /** Apply the matrix to the src vector, returning the new vector in dst.
331 It is legal for src and dst to point to the same memory.
332 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000333 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
reed@google.com7d683352012-12-03 21:19:52 +0000334 inline void mapScalars(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000335 this->mapScalars(vec, vec);
336 }
337
reed@google.com44699382013-10-31 17:28:30 +0000338 SK_ATTR_DEPRECATED("use mapScalars")
reed@google.com1ea95be2012-11-09 21:39:48 +0000339 void map(const SkScalar src[4], SkScalar dst[4]) const {
340 this->mapScalars(src, dst);
341 }
reed@google.com44699382013-10-31 17:28:30 +0000342
343 SK_ATTR_DEPRECATED("use mapScalars")
reed@google.com8260a892011-06-13 14:02:52 +0000344 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000345 this->mapScalars(vec, vec);
346 }
347
348#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000349 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000350#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +0000351 inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000352 this->mapScalars(src, dst);
353 }
354#endif
reed@google.com7d683352012-12-03 21:19:52 +0000355 inline void mapMScalars(SkMScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000356 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000357 }
358
359 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
360 SkVector4 dst;
reed@google.com44699382013-10-31 17:28:30 +0000361 m.mapScalars(src.fData, dst.fData);
reed@google.com8260a892011-06-13 14:02:52 +0000362 return dst;
363 }
364
reed@google.com99b5c7f2012-12-05 22:13:59 +0000365 /**
366 * map an array of [x, y, 0, 1] through the matrix, returning an array
367 * of [x', y', z', w'].
368 *
369 * @param src2 array of [x, y] pairs, with implied z=0 and w=1
370 * @param count number of [x, y] pairs in src2
371 * @param dst4 array of [x', y', z', w'] quads as the output.
372 */
373 void map2(const float src2[], int count, float dst4[]) const;
374 void map2(const double src2[], int count, double dst4[]) const;
375
reed@google.com8260a892011-06-13 14:02:52 +0000376 void dump() const;
377
vollick@chromium.org3959a762012-11-13 15:08:22 +0000378 double determinant() const;
379
reed@google.com8260a892011-06-13 14:02:52 +0000380private:
reed@google.comd5302532013-01-15 14:54:00 +0000381 SkMScalar fMat[4][4];
382 mutable unsigned fTypeMask;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000383
reed@google.com7d683352012-12-03 21:19:52 +0000384 enum {
385 kUnknown_Mask = 0x80,
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000386
reed@google.com7d683352012-12-03 21:19:52 +0000387 kAllPublic_Masks = 0xF
388 };
389
390 SkMScalar transX() const { return fMat[3][0]; }
391 SkMScalar transY() const { return fMat[3][1]; }
392 SkMScalar transZ() const { return fMat[3][2]; }
393
394 SkMScalar scaleX() const { return fMat[0][0]; }
395 SkMScalar scaleY() const { return fMat[1][1]; }
396 SkMScalar scaleZ() const { return fMat[2][2]; }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000397
reed@google.com7d683352012-12-03 21:19:52 +0000398 SkMScalar perspX() const { return fMat[0][3]; }
399 SkMScalar perspY() const { return fMat[1][3]; }
400 SkMScalar perspZ() const { return fMat[2][3]; }
401
402 int computeTypeMask() const;
403
404 inline void dirtyTypeMask() {
405 fTypeMask = kUnknown_Mask;
406 }
407
408 inline void setTypeMask(int mask) {
409 SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
410 fTypeMask = mask;
411 }
412
413 /**
414 * Does not take the time to 'compute' the typemask. Only returns true if
415 * we already know that this matrix is identity.
416 */
417 inline bool isTriviallyIdentity() const {
418 return 0 == fTypeMask;
419 }
tomhudson@google.com7cfb9c72013-01-17 13:29:35 +0000420};
reed@google.com8260a892011-06-13 14:02:52 +0000421
422#endif