blob: 83fb7c994615e226fe9465d4ae3b538592b7ae30 [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
tomhudson@google.com21e43222013-01-17 12:17:00 +000054#if (defined(__x86_64__) || defined(_M_X64) || defined(__SSE2__)) && \
55defined(SK_MSCALAR_IS_DOUBLE)
56#define SK_MATRIX44_USE_SSE2
57#endif
58
reed@google.com20d44672012-11-09 21:28:55 +000059#define SkMScalarToScalar SkMScalarToFloat
60#define SkScalarToMScalar SkFloatToMScalar
bsalomon@google.com72e49b82011-10-27 21:47:03 +000061
reed@google.com8260a892011-06-13 14:02:52 +000062static const SkMScalar SK_MScalar1 = 1;
63
64///////////////////////////////////////////////////////////////////////////////
65
66struct SkVector4 {
67 SkScalar fData[4];
68
69 SkVector4() {
70 this->set(0, 0, 0, 1);
71 }
72 SkVector4(const SkVector4& src) {
73 memcpy(fData, src.fData, sizeof(fData));
74 }
75 SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
76 fData[0] = x;
77 fData[1] = y;
78 fData[2] = z;
79 fData[3] = w;
80 }
81
82 SkVector4& operator=(const SkVector4& src) {
83 memcpy(fData, src.fData, sizeof(fData));
84 return *this;
85 }
86
87 bool operator==(const SkVector4& v) {
88 return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
89 fData[2] == v.fData[2] && fData[3] == v.fData[3];
90 }
91 bool operator!=(const SkVector4& v) {
92 return !(*this == v);
93 }
94 bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
95 return fData[0] == x && fData[1] == y &&
96 fData[2] == z && fData[3] == w;
97 }
98
99 void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
100 fData[0] = x;
101 fData[1] = y;
102 fData[2] = z;
103 fData[3] = w;
104 }
105};
106
tomhudson@google.com21e43222013-01-17 12:17:00 +0000107class
108#if defined(SK_MATRIX44_USE_SSE2) && defined(_MSC_VER)
109__declspec(align(16))
110#endif
111SK_API SkMatrix44 {
reed@google.com8260a892011-06-13 14:02:52 +0000112public:
vollick@chromium.org57a54e32012-12-10 20:16:10 +0000113
114 enum Uninitialized_Constructor {
115 kUninitialized_Constructor
116 };
117 enum Identity_Constructor {
118 kIdentity_Constructor
119 };
120
121 SkMatrix44(Uninitialized_Constructor) { }
122 SkMatrix44(Identity_Constructor) { this->setIdentity(); }
123
reed@google.comedb77132013-01-16 16:28:58 +0000124 // DEPRECATED: use the constructors that take an enum
reed@google.com7d683352012-12-03 21:19:52 +0000125 SkMatrix44() { this->setIdentity(); }
reed@google.comd5302532013-01-15 14:54:00 +0000126
127 SkMatrix44(const SkMatrix44& src) {
128 memcpy(fMat, src.fMat, sizeof(fMat));
129 fTypeMask = src.fTypeMask;
130 }
131
132 SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) {
133 this->setConcat(a, b);
134 }
reed@google.com8260a892011-06-13 14:02:52 +0000135
136 SkMatrix44& operator=(const SkMatrix44& src) {
reed@google.comd5302532013-01-15 14:54:00 +0000137 memcpy(fMat, src.fMat, sizeof(fMat));
138 fTypeMask = src.fTypeMask;
reed@google.com8260a892011-06-13 14:02:52 +0000139 return *this;
140 }
141
reed@google.com631940c2012-11-27 13:13:22 +0000142 bool operator==(const SkMatrix44& other) const;
reed@google.com8260a892011-06-13 14:02:52 +0000143 bool operator!=(const SkMatrix44& other) const {
reed@google.com631940c2012-11-27 13:13:22 +0000144 return !(other == *this);
reed@google.com8260a892011-06-13 14:02:52 +0000145 }
146
147 SkMatrix44(const SkMatrix&);
148 SkMatrix44& operator=(const SkMatrix& src);
149 operator SkMatrix() const;
150
reed@google.com7d683352012-12-03 21:19:52 +0000151 /**
152 * Return a reference to a const identity matrix
153 */
154 static const SkMatrix44& I();
155
156 enum TypeMask {
157 kIdentity_Mask = 0,
158 kTranslate_Mask = 0x01, //!< set if the matrix has translation
159 kScale_Mask = 0x02, //!< set if the matrix has any scale != 1
160 kAffine_Mask = 0x04, //!< set if the matrix skews or rotates
161 kPerspective_Mask = 0x08 //!< set if the matrix is in perspective
162 };
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000163
reed@google.com7d683352012-12-03 21:19:52 +0000164 /**
165 * Returns a bitfield describing the transformations the matrix may
166 * perform. The bitfield is computed conservatively, so it may include
167 * false positives. For example, when kPerspective_Mask is true, all
168 * other bits may be set to true even in the case of a pure perspective
169 * transform.
170 */
171 inline TypeMask getType() const {
172 if (fTypeMask & kUnknown_Mask) {
173 fTypeMask = this->computeTypeMask();
174 }
175 SkASSERT(!(fTypeMask & kUnknown_Mask));
176 return (TypeMask)fTypeMask;
177 }
178
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000179 /**
180 * Return true if the matrix is identity.
181 */
reed@google.com7d683352012-12-03 21:19:52 +0000182 inline bool isIdentity() const {
mike@reedtribe.orgf8b1ebc2012-12-10 03:27:47 +0000183 return kIdentity_Mask == this->getType();
184 }
185
186 /**
187 * Return true if the matrix contains translate or is identity.
188 */
189 inline bool isTranslate() const {
190 return !(this->getType() & ~kTranslate_Mask);
191 }
192
193 /**
194 * Return true if the matrix only contains scale or translate or is identity.
195 */
196 inline bool isScaleTranslate() const {
197 return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
reed@google.com7d683352012-12-03 21:19:52 +0000198 }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000199
reed@google.com7d683352012-12-03 21:19:52 +0000200 void setIdentity();
201 inline void reset() { this->setIdentity();}
202
203 /**
204 * get a value from the matrix. The row,col parameters work as follows:
205 * (0, 0) scale-x
206 * (0, 3) translate-x
207 * (3, 0) perspective-x
208 */
209 inline SkMScalar get(int row, int col) const {
reed@google.com631940c2012-11-27 13:13:22 +0000210 SkASSERT((unsigned)row <= 3);
211 SkASSERT((unsigned)col <= 3);
212 return fMat[col][row];
213 }
reed@google.com8260a892011-06-13 14:02:52 +0000214
reed@google.com7d683352012-12-03 21:19:52 +0000215 /**
216 * set a value in the matrix. The row,col parameters work as follows:
217 * (0, 0) scale-x
218 * (0, 3) translate-x
219 * (3, 0) perspective-x
220 */
221 inline void set(int row, int col, SkMScalar value) {
reed@google.com631940c2012-11-27 13:13:22 +0000222 SkASSERT((unsigned)row <= 3);
223 SkASSERT((unsigned)col <= 3);
224 fMat[col][row] = value;
reed@google.com7d683352012-12-03 21:19:52 +0000225 this->dirtyTypeMask();
reed@google.com631940c2012-11-27 13:13:22 +0000226 }
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000227
reed@google.com7d683352012-12-03 21:19:52 +0000228 inline double getDouble(int row, int col) const {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000229 return SkMScalarToDouble(this->get(row, col));
230 }
reed@google.com7d683352012-12-03 21:19:52 +0000231 inline void setDouble(int row, int col, double value) {
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000232 this->set(row, col, SkDoubleToMScalar(value));
233 }
234
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000235 /** These methods allow one to efficiently read matrix entries into 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 */
reed@google.comda9fac02011-06-13 14:46:52 +0000240 void asColMajorf(float[]) const;
241 void asColMajord(double[]) const;
242 void asRowMajorf(float[]) const;
243 void asRowMajord(double[]) const;
244
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000245 /** These methods allow one to efficiently set all matrix entries from an
246 * array. The given array must have room for exactly 16 entries. Whenever
247 * possible, they will try to use memcpy rather than an entry-by-entry
248 * copy.
249 */
250 void setColMajorf(const float[]);
251 void setColMajord(const double[]);
252 void setRowMajorf(const float[]);
253 void setRowMajord(const double[]);
254
reed@google.com7d683352012-12-03 21:19:52 +0000255#ifdef SK_MSCALAR_IS_FLOAT
256 void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
257 void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
258#else
259 void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
260 void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
261#endif
reed@google.com8260a892011-06-13 14:02:52 +0000262
263 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
264 SkMScalar m10, SkMScalar m11, SkMScalar m12,
265 SkMScalar m20, SkMScalar m21, SkMScalar m22);
266
267 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
268 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
269 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
270
271 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
272 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
273 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
274
reed@google.com7d683352012-12-03 21:19:52 +0000275 inline void setScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000276 this->setScale(scale, scale, scale);
277 }
reed@google.com7d683352012-12-03 21:19:52 +0000278 inline void preScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000279 this->preScale(scale, scale, scale);
280 }
reed@google.com7d683352012-12-03 21:19:52 +0000281 inline void postScale(SkMScalar scale) {
reed@google.com8260a892011-06-13 14:02:52 +0000282 this->postScale(scale, scale, scale);
283 }
284
285 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
286 SkMScalar degrees) {
287 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
288 }
289
290 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
291 it will be automatically resized.
292 */
293 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
294 SkMScalar radians);
295 /** Rotate about the vector [x,y,z]. Does not check the length of the
296 vector, assuming it is unit-length.
297 */
298 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
299 SkMScalar radians);
300
301 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
reed@google.com7d683352012-12-03 21:19:52 +0000302 inline void preConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000303 this->setConcat(*this, m);
304 }
reed@google.com7d683352012-12-03 21:19:52 +0000305 inline void postConcat(const SkMatrix44& m) {
reed@google.com8260a892011-06-13 14:02:52 +0000306 this->setConcat(m, *this);
307 }
308
309 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
310 return SkMatrix44(a, b);
311 }
312
313 /** If this is invertible, return that in inverse and return true. If it is
314 not invertible, return false and ignore the inverse parameter.
315 */
316 bool invert(SkMatrix44* inverse) const;
317
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000318 /** Transpose this matrix in place. */
319 void transpose();
320
reed@google.com8260a892011-06-13 14:02:52 +0000321 /** Apply the matrix to the src vector, returning the new vector in dst.
322 It is legal for src and dst to point to the same memory.
323 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000324 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
reed@google.com7d683352012-12-03 21:19:52 +0000325 inline void mapScalars(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000326 this->mapScalars(vec, vec);
327 }
328
329 // DEPRECATED: call mapScalars()
330 void map(const SkScalar src[4], SkScalar dst[4]) const {
331 this->mapScalars(src, dst);
332 }
333 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000334 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000335 this->mapScalars(vec, vec);
336 }
337
338#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000339 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000340#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com7d683352012-12-03 21:19:52 +0000341 inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000342 this->mapScalars(src, dst);
343 }
344#endif
reed@google.com7d683352012-12-03 21:19:52 +0000345 inline void mapMScalars(SkMScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000346 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000347 }
348
349 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
350 SkVector4 dst;
351 m.map(src.fData, dst.fData);
352 return dst;
353 }
354
reed@google.com99b5c7f2012-12-05 22:13:59 +0000355 /**
356 * map an array of [x, y, 0, 1] through the matrix, returning an array
357 * of [x', y', z', w'].
358 *
359 * @param src2 array of [x, y] pairs, with implied z=0 and w=1
360 * @param count number of [x, y] pairs in src2
361 * @param dst4 array of [x', y', z', w'] quads as the output.
362 */
363 void map2(const float src2[], int count, float dst4[]) const;
364 void map2(const double src2[], int count, double dst4[]) const;
365
reed@google.com8260a892011-06-13 14:02:52 +0000366 void dump() const;
367
vollick@chromium.org3959a762012-11-13 15:08:22 +0000368 double determinant() const;
369
reed@google.com8260a892011-06-13 14:02:52 +0000370private:
reed@google.comd5302532013-01-15 14:54:00 +0000371 SkMScalar fMat[4][4];
372 mutable unsigned fTypeMask;
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000373
reed@google.com7d683352012-12-03 21:19:52 +0000374 enum {
375 kUnknown_Mask = 0x80,
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000376
reed@google.com7d683352012-12-03 21:19:52 +0000377 kAllPublic_Masks = 0xF
378 };
379
380 SkMScalar transX() const { return fMat[3][0]; }
381 SkMScalar transY() const { return fMat[3][1]; }
382 SkMScalar transZ() const { return fMat[3][2]; }
383
384 SkMScalar scaleX() const { return fMat[0][0]; }
385 SkMScalar scaleY() const { return fMat[1][1]; }
386 SkMScalar scaleZ() const { return fMat[2][2]; }
skia.committer@gmail.com0264fb42012-12-06 02:01:25 +0000387
reed@google.com7d683352012-12-03 21:19:52 +0000388 SkMScalar perspX() const { return fMat[0][3]; }
389 SkMScalar perspY() const { return fMat[1][3]; }
390 SkMScalar perspZ() const { return fMat[2][3]; }
391
392 int computeTypeMask() const;
393
394 inline void dirtyTypeMask() {
395 fTypeMask = kUnknown_Mask;
396 }
397
398 inline void setTypeMask(int mask) {
399 SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
400 fTypeMask = mask;
401 }
402
403 /**
404 * Does not take the time to 'compute' the typemask. Only returns true if
405 * we already know that this matrix is identity.
406 */
407 inline bool isTriviallyIdentity() const {
408 return 0 == fTypeMask;
409 }
tomhudson@google.com21e43222013-01-17 12:17:00 +0000410}
411#if defined(SK_MATRIX44_USE_SSE2) && !defined(_MSC_VER)
412__attribute__ ((aligned (16)))
413#endif
414;
reed@google.com8260a892011-06-13 14:02:52 +0000415
416#endif