blob: af61a11c68dbfa675e5b4701950b32bb46171538 [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 typedef int64_t SkMIntScalar;
20
reed@google.com8260a892011-06-13 14:02:52 +000021 static inline double SkFloatToMScalar(float x) {
22 return static_cast<double>(x);
23 }
24 static inline float SkMScalarToFloat(double x) {
25 return static_cast<float>(x);
26 }
27 static inline double SkDoubleToMScalar(double x) {
28 return x;
29 }
30 static inline double SkMScalarToDouble(double x) {
31 return x;
32 }
33 static const SkMScalar SK_MScalarPI = 3.141592653589793;
vollick@chromium.org5596a692012-11-13 20:12:00 +000034#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +000035#ifdef SK_MSCALAR_IS_DOUBLE
36 #error "can't define MSCALAR both as DOUBLE and FLOAT"
37#endif
reed@google.com8260a892011-06-13 14:02:52 +000038 typedef float SkMScalar;
reed@google.com7d683352012-12-03 21:19:52 +000039 typedef int32_t SkMIntScalar;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +000040
reed@google.com8260a892011-06-13 14:02:52 +000041 static inline float SkFloatToMScalar(float x) {
42 return x;
43 }
44 static inline float SkMScalarToFloat(float x) {
45 return x;
46 }
47 static inline float SkDoubleToMScalar(double x) {
48 return static_cast<float>(x);
49 }
50 static inline double SkMScalarToDouble(float x) {
51 return static_cast<double>(x);
52 }
53 static const SkMScalar SK_MScalarPI = 3.14159265f;
54#endif
55
reed@google.com20d44672012-11-09 21:28:55 +000056#define SkMScalarToScalar SkMScalarToFloat
57#define SkScalarToMScalar SkFloatToMScalar
bsalomon@google.com72e49b82011-10-27 21:47:03 +000058
reed@google.com8260a892011-06-13 14:02:52 +000059static const SkMScalar SK_MScalar1 = 1;
60
61///////////////////////////////////////////////////////////////////////////////
62
63struct SkVector4 {
64 SkScalar fData[4];
65
66 SkVector4() {
67 this->set(0, 0, 0, 1);
68 }
69 SkVector4(const SkVector4& src) {
70 memcpy(fData, src.fData, sizeof(fData));
71 }
72 SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
73 fData[0] = x;
74 fData[1] = y;
75 fData[2] = z;
76 fData[3] = w;
77 }
78
79 SkVector4& operator=(const SkVector4& src) {
80 memcpy(fData, src.fData, sizeof(fData));
81 return *this;
82 }
83
84 bool operator==(const SkVector4& v) {
85 return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
86 fData[2] == v.fData[2] && fData[3] == v.fData[3];
87 }
88 bool operator!=(const SkVector4& v) {
89 return !(*this == v);
90 }
91 bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
92 return fData[0] == x && fData[1] == y &&
93 fData[2] == z && fData[3] == w;
94 }
95
96 void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
97 fData[0] = x;
98 fData[1] = y;
99 fData[2] = z;
100 fData[3] = w;
101 }
102};
103
reed@google.com65d8bb02011-07-05 19:12:59 +0000104class SK_API SkMatrix44 {
reed@google.com8260a892011-06-13 14:02:52 +0000105public:
reed@google.com7d683352012-12-03 21:19:52 +0000106 SkMatrix44() { this->setIdentity(); }
reed@google.com8260a892011-06-13 14:02:52 +0000107 SkMatrix44(const SkMatrix44&);
108 SkMatrix44(const SkMatrix44& a, const SkMatrix44& b);
109
110 SkMatrix44& operator=(const SkMatrix44& src) {
reed@google.com7d683352012-12-03 21:19:52 +0000111 SkASSERT(sizeof(src) == sizeof(fMat) + sizeof(SkMIntScalar));
reed@google.com8260a892011-06-13 14:02:52 +0000112 memcpy(this, &src, sizeof(*this));
113 return *this;
114 }
115
reed@google.com631940c2012-11-27 13:13:22 +0000116 bool operator==(const SkMatrix44& other) const;
reed@google.com8260a892011-06-13 14:02:52 +0000117 bool operator!=(const SkMatrix44& other) const {
reed@google.com631940c2012-11-27 13:13:22 +0000118 return !(other == *this);
reed@google.com8260a892011-06-13 14:02:52 +0000119 }
120
121 SkMatrix44(const SkMatrix&);
122 SkMatrix44& operator=(const SkMatrix& src);
123 operator SkMatrix() const;
124
reed@google.com7d683352012-12-03 21:19:52 +0000125 /**
126 * Return a reference to a const identity matrix
127 */
128 static const SkMatrix44& I();
129
130 enum TypeMask {
131 kIdentity_Mask = 0,
132 kTranslate_Mask = 0x01, //!< set if the matrix has translation
133 kScale_Mask = 0x02, //!< set if the matrix has any scale != 1
134 kAffine_Mask = 0x04, //!< set if the matrix skews or rotates
135 kPerspective_Mask = 0x08 //!< set if the matrix is in perspective
136 };
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000137
reed@google.com7d683352012-12-03 21:19:52 +0000138 /**
139 * Returns a bitfield describing the transformations the matrix may
140 * perform. The bitfield is computed conservatively, so it may include
141 * false positives. For example, when kPerspective_Mask is true, all
142 * other bits may be set to true even in the case of a pure perspective
143 * transform.
144 */
145 inline TypeMask getType() const {
146 if (fTypeMask & kUnknown_Mask) {
147 fTypeMask = this->computeTypeMask();
148 }
149 SkASSERT(!(fTypeMask & kUnknown_Mask));
150 return (TypeMask)fTypeMask;
151 }
152
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000153 /**
154 * Return true if the matrix is identity.
155 */
reed@google.com7d683352012-12-03 21:19:52 +0000156 inline bool isIdentity() const {
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000157 return kIdentity_Mask == this->getType();
158 }
159
160 /**
161 * Return true if the matrix contains translate or is identity.
162 */
163 inline bool isTranslate() const {
164 return !(this->getType() & ~kTranslate_Mask);
165 }
166
167 /**
168 * Return true if the matrix only contains scale or translate or is identity.
169 */
170 inline bool isScaleTranslate() const {
171 return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
reed@google.com7d683352012-12-03 21:19:52 +0000172 }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000173
reed@google.com7d683352012-12-03 21:19:52 +0000174 void setIdentity();
175 inline void reset() { this->setIdentity();}
176
177 /**
178 * get a value from the matrix. The row,col parameters work as follows:
179 * (0, 0) scale-x
180 * (0, 3) translate-x
181 * (3, 0) perspective-x
182 */
183 inline SkMScalar get(int row, int col) const {
reed@google.com631940c2012-11-27 13:13:22 +0000184 SkASSERT((unsigned)row <= 3);
185 SkASSERT((unsigned)col <= 3);
186 return fMat[col][row];
187 }
reed@google.com8260a892011-06-13 14:02:52 +0000188
reed@google.com7d683352012-12-03 21:19:52 +0000189 /**
190 * set a value in the matrix. The row,col parameters work as follows:
191 * (0, 0) scale-x
192 * (0, 3) translate-x
193 * (3, 0) perspective-x
194 */
195 inline void set(int row, int col, SkMScalar value) {
reed@google.com631940c2012-11-27 13:13:22 +0000196 SkASSERT((unsigned)row <= 3);
197 SkASSERT((unsigned)col <= 3);
198 fMat[col][row] = value;
reed@google.com7d683352012-12-03 21:19:52 +0000199 this->dirtyTypeMask();
reed@google.com631940c2012-11-27 13:13:22 +0000200 }
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000201
reed@google.com7d683352012-12-03 21:19:52 +0000202 inline double getDouble(int row, int col) const {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000203 return SkMScalarToDouble(this->get(row, col));
204 }
reed@google.com7d683352012-12-03 21:19:52 +0000205 inline void setDouble(int row, int col, double value) {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000206 this->set(row, col, SkDoubleToMScalar(value));
207 }
208
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000209 /** These methods allow one to efficiently read matrix entries into an
210 * array. The given array must have room for exactly 16 entries. Whenever
211 * possible, they will try to use memcpy rather than an entry-by-entry
212 * copy.
213 */
reed@google.comda9fac02011-06-13 14:46:52 +0000214 void asColMajorf(float[]) const;
215 void asColMajord(double[]) const;
216 void asRowMajorf(float[]) const;
217 void asRowMajord(double[]) const;
218
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000219 /** These methods allow one to efficiently set all matrix entries from an
220 * array. The given array must have room for exactly 16 entries. Whenever
221 * possible, they will try to use memcpy rather than an entry-by-entry
222 * copy.
223 */
224 void setColMajorf(const float[]);
225 void setColMajord(const double[]);
226 void setRowMajorf(const float[]);
227 void setRowMajord(const double[]);
228
reed@google.com7d683352012-12-03 21:19:52 +0000229#ifdef SK_MSCALAR_IS_FLOAT
230 void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
231 void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
232#else
233 void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
234 void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
235#endif
reed@google.com8260a892011-06-13 14:02:52 +0000236
237 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
238 SkMScalar m10, SkMScalar m11, SkMScalar m12,
239 SkMScalar m20, SkMScalar m21, SkMScalar m22);
240
241 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
242 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
243 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
244
245 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
246 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
247 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
248
reed@google.com7d683352012-12-03 21:19:52 +0000249 inline void setScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000250 this->setScale(scale, scale, scale);
251 }
reed@google.com7d683352012-12-03 21:19:52 +0000252 inline void preScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000253 this->preScale(scale, scale, scale);
254 }
reed@google.com7d683352012-12-03 21:19:52 +0000255 inline void postScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000256 this->postScale(scale, scale, scale);
257 }
258
259 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
260 SkMScalar degrees) {
261 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
262 }
263
264 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
265 it will be automatically resized.
266 */
267 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
268 SkMScalar radians);
269 /** Rotate about the vector [x,y,z]. Does not check the length of the
270 vector, assuming it is unit-length.
271 */
272 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
273 SkMScalar radians);
274
275 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
reed@google.com7d683352012-12-03 21:19:52 +0000276 inline void preConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000277 this->setConcat(*this, m);
278 }
reed@google.com7d683352012-12-03 21:19:52 +0000279 inline void postConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000280 this->setConcat(m, *this);
281 }
282
283 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
284 return SkMatrix44(a, b);
285 }
286
287 /** If this is invertible, return that in inverse and return true. If it is
288 not invertible, return false and ignore the inverse parameter.
289 */
290 bool invert(SkMatrix44* inverse) const;
291
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000292 /** Transpose this matrix in place. */
293 void transpose();
294
reed@google.com8260a892011-06-13 14:02:52 +0000295 /** Apply the matrix to the src vector, returning the new vector in dst.
296 It is legal for src and dst to point to the same memory.
297 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000298 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
reed@google.com7d683352012-12-03 21:19:52 +0000299 inline void mapScalars(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000300 this->mapScalars(vec, vec);
301 }
302
303 // DEPRECATED: call mapScalars()
304 void map(const SkScalar src[4], SkScalar dst[4]) const {
305 this->mapScalars(src, dst);
306 }
307 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000308 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000309 this->mapScalars(vec, vec);
310 }
311
312#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000313 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000314#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +0000315 inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000316 this->mapScalars(src, dst);
317 }
318#endif
reed@google.com7d683352012-12-03 21:19:52 +0000319 inline void mapMScalars(SkMScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000320 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000321 }
322
323 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
324 SkVector4 dst;
325 m.map(src.fData, dst.fData);
326 return dst;
327 }
328
reed@google.com99b5c7f2012-12-05 22:13:59 +0000329 /**
330 * map an array of [x, y, 0, 1] through the matrix, returning an array
331 * of [x', y', z', w'].
332 *
333 * @param src2 array of [x, y] pairs, with implied z=0 and w=1
334 * @param count number of [x, y] pairs in src2
335 * @param dst4 array of [x', y', z', w'] quads as the output.
336 */
337 void map2(const float src2[], int count, float dst4[]) const;
338 void map2(const double src2[], int count, double dst4[]) const;
339
reed@google.com8260a892011-06-13 14:02:52 +0000340 void dump() const;
341
vollick@chromium.org3959a762012-11-13 15:08:22 +0000342 double determinant() const;
343
reed@google.com8260a892011-06-13 14:02:52 +0000344private:
reed@google.com7d683352012-12-03 21:19:52 +0000345 SkMScalar fMat[4][4];
346 // we use SkMIntScalar instead of just int, as we want to ensure that
347 // we are always packed with no extra bits, allowing us to call memcpy
348 // without fear of copying uninitialized bits.
349 mutable SkMIntScalar fTypeMask;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000350
reed@google.com7d683352012-12-03 21:19:52 +0000351 enum {
352 kUnknown_Mask = 0x80,
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000353
reed@google.com7d683352012-12-03 21:19:52 +0000354 kAllPublic_Masks = 0xF
355 };
356
357 SkMScalar transX() const { return fMat[3][0]; }
358 SkMScalar transY() const { return fMat[3][1]; }
359 SkMScalar transZ() const { return fMat[3][2]; }
360
361 SkMScalar scaleX() const { return fMat[0][0]; }
362 SkMScalar scaleY() const { return fMat[1][1]; }
363 SkMScalar scaleZ() const { return fMat[2][2]; }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000364
reed@google.com7d683352012-12-03 21:19:52 +0000365 SkMScalar perspX() const { return fMat[0][3]; }
366 SkMScalar perspY() const { return fMat[1][3]; }
367 SkMScalar perspZ() const { return fMat[2][3]; }
368
369 int computeTypeMask() const;
370
371 inline void dirtyTypeMask() {
372 fTypeMask = kUnknown_Mask;
373 }
374
375 inline void setTypeMask(int mask) {
376 SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
377 fTypeMask = mask;
378 }
379
380 /**
381 * Does not take the time to 'compute' the typemask. Only returns true if
382 * we already know that this matrix is identity.
383 */
384 inline bool isTriviallyIdentity() const {
385 return 0 == fTypeMask;
386 }
reed@google.com8260a892011-06-13 14:02:52 +0000387};
388
389#endif