blob: 22cbd79ba0744a5fbd85f3e97bfe2a008eb8c252 [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
reed@google.com65d8bb02011-07-05 19:12:59 +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.com7d683352012-12-03 21:19:52 +0000115 SkMatrix44() { this->setIdentity(); }
reed@google.comd5302532013-01-15 14:54:00 +0000116
117 SkMatrix44(const SkMatrix44& src) {
118 memcpy(fMat, src.fMat, sizeof(fMat));
119 fTypeMask = src.fTypeMask;
120 }
121
122 SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) {
123 this->setConcat(a, b);
124 }
reed@google.com8260a892011-06-13 14:02:52 +0000125
126 SkMatrix44& operator=(const SkMatrix44& src) {
reed@google.comd5302532013-01-15 14:54:00 +0000127 memcpy(fMat, src.fMat, sizeof(fMat));
128 fTypeMask = src.fTypeMask;
reed@google.com8260a892011-06-13 14:02:52 +0000129 return *this;
130 }
131
reed@google.com631940c2012-11-27 13:13:22 +0000132 bool operator==(const SkMatrix44& other) const;
reed@google.com8260a892011-06-13 14:02:52 +0000133 bool operator!=(const SkMatrix44& other) const {
reed@google.com631940c2012-11-27 13:13:22 +0000134 return !(other == *this);
reed@google.com8260a892011-06-13 14:02:52 +0000135 }
136
137 SkMatrix44(const SkMatrix&);
138 SkMatrix44& operator=(const SkMatrix& src);
139 operator SkMatrix() const;
140
reed@google.com7d683352012-12-03 21:19:52 +0000141 /**
142 * Return a reference to a const identity matrix
143 */
144 static const SkMatrix44& I();
145
146 enum TypeMask {
147 kIdentity_Mask = 0,
148 kTranslate_Mask = 0x01, //!< set if the matrix has translation
149 kScale_Mask = 0x02, //!< set if the matrix has any scale != 1
150 kAffine_Mask = 0x04, //!< set if the matrix skews or rotates
151 kPerspective_Mask = 0x08 //!< set if the matrix is in perspective
152 };
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000153
reed@google.com7d683352012-12-03 21:19:52 +0000154 /**
155 * Returns a bitfield describing the transformations the matrix may
156 * perform. The bitfield is computed conservatively, so it may include
157 * false positives. For example, when kPerspective_Mask is true, all
158 * other bits may be set to true even in the case of a pure perspective
159 * transform.
160 */
161 inline TypeMask getType() const {
162 if (fTypeMask & kUnknown_Mask) {
163 fTypeMask = this->computeTypeMask();
164 }
165 SkASSERT(!(fTypeMask & kUnknown_Mask));
166 return (TypeMask)fTypeMask;
167 }
168
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000169 /**
170 * Return true if the matrix is identity.
171 */
reed@google.com7d683352012-12-03 21:19:52 +0000172 inline bool isIdentity() const {
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000173 return kIdentity_Mask == this->getType();
174 }
175
176 /**
177 * Return true if the matrix contains translate or is identity.
178 */
179 inline bool isTranslate() const {
180 return !(this->getType() & ~kTranslate_Mask);
181 }
182
183 /**
184 * Return true if the matrix only contains scale or translate or is identity.
185 */
186 inline bool isScaleTranslate() const {
187 return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
reed@google.com7d683352012-12-03 21:19:52 +0000188 }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000189
reed@google.com7d683352012-12-03 21:19:52 +0000190 void setIdentity();
191 inline void reset() { this->setIdentity();}
192
193 /**
194 * get a value from the matrix. The row,col parameters work as follows:
195 * (0, 0) scale-x
196 * (0, 3) translate-x
197 * (3, 0) perspective-x
198 */
199 inline SkMScalar get(int row, int col) const {
reed@google.com631940c2012-11-27 13:13:22 +0000200 SkASSERT((unsigned)row <= 3);
201 SkASSERT((unsigned)col <= 3);
202 return fMat[col][row];
203 }
reed@google.com8260a892011-06-13 14:02:52 +0000204
reed@google.com7d683352012-12-03 21:19:52 +0000205 /**
206 * set a value in the matrix. The row,col parameters work as follows:
207 * (0, 0) scale-x
208 * (0, 3) translate-x
209 * (3, 0) perspective-x
210 */
211 inline void set(int row, int col, SkMScalar value) {
reed@google.com631940c2012-11-27 13:13:22 +0000212 SkASSERT((unsigned)row <= 3);
213 SkASSERT((unsigned)col <= 3);
214 fMat[col][row] = value;
reed@google.com7d683352012-12-03 21:19:52 +0000215 this->dirtyTypeMask();
reed@google.com631940c2012-11-27 13:13:22 +0000216 }
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000217
reed@google.com7d683352012-12-03 21:19:52 +0000218 inline double getDouble(int row, int col) const {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000219 return SkMScalarToDouble(this->get(row, col));
220 }
reed@google.com7d683352012-12-03 21:19:52 +0000221 inline void setDouble(int row, int col, double value) {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000222 this->set(row, col, SkDoubleToMScalar(value));
223 }
224
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000225 /** These methods allow one to efficiently read matrix entries into an
226 * array. The given array must have room for exactly 16 entries. Whenever
227 * possible, they will try to use memcpy rather than an entry-by-entry
228 * copy.
229 */
reed@google.comda9fac02011-06-13 14:46:52 +0000230 void asColMajorf(float[]) const;
231 void asColMajord(double[]) const;
232 void asRowMajorf(float[]) const;
233 void asRowMajord(double[]) const;
234
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000235 /** These methods allow one to efficiently set all matrix entries from an
236 * array. The given array must have room for exactly 16 entries. Whenever
237 * possible, they will try to use memcpy rather than an entry-by-entry
238 * copy.
239 */
240 void setColMajorf(const float[]);
241 void setColMajord(const double[]);
242 void setRowMajorf(const float[]);
243 void setRowMajord(const double[]);
244
reed@google.com7d683352012-12-03 21:19:52 +0000245#ifdef SK_MSCALAR_IS_FLOAT
246 void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
247 void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
248#else
249 void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
250 void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
251#endif
reed@google.com8260a892011-06-13 14:02:52 +0000252
253 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
254 SkMScalar m10, SkMScalar m11, SkMScalar m12,
255 SkMScalar m20, SkMScalar m21, SkMScalar m22);
256
257 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
258 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
259 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
260
261 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
262 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
263 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
264
reed@google.com7d683352012-12-03 21:19:52 +0000265 inline void setScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000266 this->setScale(scale, scale, scale);
267 }
reed@google.com7d683352012-12-03 21:19:52 +0000268 inline void preScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000269 this->preScale(scale, scale, scale);
270 }
reed@google.com7d683352012-12-03 21:19:52 +0000271 inline void postScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000272 this->postScale(scale, scale, scale);
273 }
274
275 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
276 SkMScalar degrees) {
277 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
278 }
279
280 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
281 it will be automatically resized.
282 */
283 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
284 SkMScalar radians);
285 /** Rotate about the vector [x,y,z]. Does not check the length of the
286 vector, assuming it is unit-length.
287 */
288 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
289 SkMScalar radians);
290
291 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
reed@google.com7d683352012-12-03 21:19:52 +0000292 inline void preConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000293 this->setConcat(*this, m);
294 }
reed@google.com7d683352012-12-03 21:19:52 +0000295 inline void postConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000296 this->setConcat(m, *this);
297 }
298
299 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
300 return SkMatrix44(a, b);
301 }
302
303 /** If this is invertible, return that in inverse and return true. If it is
304 not invertible, return false and ignore the inverse parameter.
305 */
306 bool invert(SkMatrix44* inverse) const;
307
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000308 /** Transpose this matrix in place. */
309 void transpose();
310
reed@google.com8260a892011-06-13 14:02:52 +0000311 /** Apply the matrix to the src vector, returning the new vector in dst.
312 It is legal for src and dst to point to the same memory.
313 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000314 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
reed@google.com7d683352012-12-03 21:19:52 +0000315 inline void mapScalars(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000316 this->mapScalars(vec, vec);
317 }
318
319 // DEPRECATED: call mapScalars()
320 void map(const SkScalar src[4], SkScalar dst[4]) const {
321 this->mapScalars(src, dst);
322 }
323 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000324 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000325 this->mapScalars(vec, vec);
326 }
327
328#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000329 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000330#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +0000331 inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000332 this->mapScalars(src, dst);
333 }
334#endif
reed@google.com7d683352012-12-03 21:19:52 +0000335 inline void mapMScalars(SkMScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000336 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000337 }
338
339 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
340 SkVector4 dst;
341 m.map(src.fData, dst.fData);
342 return dst;
343 }
344
reed@google.com99b5c7f2012-12-05 22:13:59 +0000345 /**
346 * map an array of [x, y, 0, 1] through the matrix, returning an array
347 * of [x', y', z', w'].
348 *
349 * @param src2 array of [x, y] pairs, with implied z=0 and w=1
350 * @param count number of [x, y] pairs in src2
351 * @param dst4 array of [x', y', z', w'] quads as the output.
352 */
353 void map2(const float src2[], int count, float dst4[]) const;
354 void map2(const double src2[], int count, double dst4[]) const;
355
reed@google.com8260a892011-06-13 14:02:52 +0000356 void dump() const;
357
vollick@chromium.org3959a762012-11-13 15:08:22 +0000358 double determinant() const;
359
reed@google.com8260a892011-06-13 14:02:52 +0000360private:
reed@google.comd5302532013-01-15 14:54:00 +0000361 SkMScalar fMat[4][4];
362 mutable unsigned fTypeMask;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000363
reed@google.com7d683352012-12-03 21:19:52 +0000364 enum {
365 kUnknown_Mask = 0x80,
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000366
reed@google.com7d683352012-12-03 21:19:52 +0000367 kAllPublic_Masks = 0xF
368 };
369
370 SkMScalar transX() const { return fMat[3][0]; }
371 SkMScalar transY() const { return fMat[3][1]; }
372 SkMScalar transZ() const { return fMat[3][2]; }
373
374 SkMScalar scaleX() const { return fMat[0][0]; }
375 SkMScalar scaleY() const { return fMat[1][1]; }
376 SkMScalar scaleZ() const { return fMat[2][2]; }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000377
reed@google.com7d683352012-12-03 21:19:52 +0000378 SkMScalar perspX() const { return fMat[0][3]; }
379 SkMScalar perspY() const { return fMat[1][3]; }
380 SkMScalar perspZ() const { return fMat[2][3]; }
381
382 int computeTypeMask() const;
383
384 inline void dirtyTypeMask() {
385 fTypeMask = kUnknown_Mask;
386 }
387
388 inline void setTypeMask(int mask) {
389 SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
390 fTypeMask = mask;
391 }
392
393 /**
394 * Does not take the time to 'compute' the typemask. Only returns true if
395 * we already know that this matrix is identity.
396 */
397 inline bool isTriviallyIdentity() const {
398 return 0 == fTypeMask;
399 }
reed@google.com8260a892011-06-13 14:02:52 +0000400};
401
402#endif