blob: e1de7c72a2f12ec3ea523b771f676947d4d1a1ca [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.com8260a892011-06-13 14:02:52 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.com8260a892011-06-13 14:02:52 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.com8260a892011-06-13 14:02:52 +000011#ifndef SkMatrix44_DEFINED
12#define SkMatrix44_DEFINED
13
14#include "SkMatrix.h"
15#include "SkScalar.h"
16
reed@google.com8260a892011-06-13 14:02:52 +000017#ifdef SK_MSCALAR_IS_DOUBLE
18 typedef double SkMScalar;
19 static inline double SkFloatToMScalar(float x) {
20 return static_cast<double>(x);
21 }
22 static inline float SkMScalarToFloat(double x) {
23 return static_cast<float>(x);
24 }
25 static inline double SkDoubleToMScalar(double x) {
26 return x;
27 }
28 static inline double SkMScalarToDouble(double x) {
29 return x;
30 }
31 static const SkMScalar SK_MScalarPI = 3.141592653589793;
vollick@chromium.org5596a692012-11-13 20:12:00 +000032#elif defined SK_MSCALAR_IS_FLOAT
reed@google.com8260a892011-06-13 14:02:52 +000033 typedef float SkMScalar;
34 static inline float SkFloatToMScalar(float x) {
35 return x;
36 }
37 static inline float SkMScalarToFloat(float x) {
38 return x;
39 }
40 static inline float SkDoubleToMScalar(double x) {
41 return static_cast<float>(x);
42 }
43 static inline double SkMScalarToDouble(float x) {
44 return static_cast<double>(x);
45 }
46 static const SkMScalar SK_MScalarPI = 3.14159265f;
47#endif
48
reed@google.com20d44672012-11-09 21:28:55 +000049#define SkMScalarToScalar SkMScalarToFloat
50#define SkScalarToMScalar SkFloatToMScalar
bsalomon@google.com72e49b82011-10-27 21:47:03 +000051
reed@google.com8260a892011-06-13 14:02:52 +000052static const SkMScalar SK_MScalar1 = 1;
53
54///////////////////////////////////////////////////////////////////////////////
55
56struct SkVector4 {
57 SkScalar fData[4];
58
59 SkVector4() {
60 this->set(0, 0, 0, 1);
61 }
62 SkVector4(const SkVector4& src) {
63 memcpy(fData, src.fData, sizeof(fData));
64 }
65 SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
66 fData[0] = x;
67 fData[1] = y;
68 fData[2] = z;
69 fData[3] = w;
70 }
71
72 SkVector4& operator=(const SkVector4& src) {
73 memcpy(fData, src.fData, sizeof(fData));
74 return *this;
75 }
76
77 bool operator==(const SkVector4& v) {
78 return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
79 fData[2] == v.fData[2] && fData[3] == v.fData[3];
80 }
81 bool operator!=(const SkVector4& v) {
82 return !(*this == v);
83 }
84 bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
85 return fData[0] == x && fData[1] == y &&
86 fData[2] == z && fData[3] == w;
87 }
88
89 void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
90 fData[0] = x;
91 fData[1] = y;
92 fData[2] = z;
93 fData[3] = w;
94 }
95};
96
reed@google.com65d8bb02011-07-05 19:12:59 +000097class SK_API SkMatrix44 {
reed@google.com8260a892011-06-13 14:02:52 +000098public:
99 SkMatrix44();
100 SkMatrix44(const SkMatrix44&);
101 SkMatrix44(const SkMatrix44& a, const SkMatrix44& b);
102
103 SkMatrix44& operator=(const SkMatrix44& src) {
104 memcpy(this, &src, sizeof(*this));
105 return *this;
106 }
107
108 bool operator==(const SkMatrix44& other) const {
109 return !memcmp(this, &other, sizeof(*this));
110 }
111 bool operator!=(const SkMatrix44& other) const {
112 return !!memcmp(this, &other, sizeof(*this));
113 }
114
115 SkMatrix44(const SkMatrix&);
116 SkMatrix44& operator=(const SkMatrix& src);
117 operator SkMatrix() const;
118
119 SkMScalar get(int row, int col) const;
120 void set(int row, int col, const SkMScalar& value);
121
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000122 double getDouble(int row, int col) const {
123 return SkMScalarToDouble(this->get(row, col));
124 }
125 void setDouble(int row, int col, double value) {
126 this->set(row, col, SkDoubleToMScalar(value));
127 }
128
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000129 /** These methods allow one to efficiently read matrix entries into an
130 * array. The given array must have room for exactly 16 entries. Whenever
131 * possible, they will try to use memcpy rather than an entry-by-entry
132 * copy.
133 */
reed@google.comda9fac02011-06-13 14:46:52 +0000134 void asColMajorf(float[]) const;
135 void asColMajord(double[]) const;
136 void asRowMajorf(float[]) const;
137 void asRowMajord(double[]) const;
138
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000139 /** These methods allow one to efficiently set all matrix entries from an
140 * array. The given array must have room for exactly 16 entries. Whenever
141 * possible, they will try to use memcpy rather than an entry-by-entry
142 * copy.
143 */
144 void setColMajorf(const float[]);
145 void setColMajord(const double[]);
146 void setRowMajorf(const float[]);
147 void setRowMajord(const double[]);
148
reed@google.com8260a892011-06-13 14:02:52 +0000149 bool isIdentity() const;
150 void setIdentity();
151 void reset() { this->setIdentity();}
152
153 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
154 SkMScalar m10, SkMScalar m11, SkMScalar m12,
155 SkMScalar m20, SkMScalar m21, SkMScalar m22);
156
157 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
158 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
159 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
160
161 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
162 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
163 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
164
165 void setScale(SkMScalar scale) {
166 this->setScale(scale, scale, scale);
167 }
168 void preScale(SkMScalar scale) {
169 this->preScale(scale, scale, scale);
170 }
171 void postScale(SkMScalar scale) {
172 this->postScale(scale, scale, scale);
173 }
174
175 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
176 SkMScalar degrees) {
177 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
178 }
179
180 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
181 it will be automatically resized.
182 */
183 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
184 SkMScalar radians);
185 /** Rotate about the vector [x,y,z]. Does not check the length of the
186 vector, assuming it is unit-length.
187 */
188 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
189 SkMScalar radians);
190
191 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
192 void preConcat(const SkMatrix44& m) {
193 this->setConcat(*this, m);
194 }
195 void postConcat(const SkMatrix44& m) {
196 this->setConcat(m, *this);
197 }
198
199 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
200 return SkMatrix44(a, b);
201 }
202
203 /** If this is invertible, return that in inverse and return true. If it is
204 not invertible, return false and ignore the inverse parameter.
205 */
206 bool invert(SkMatrix44* inverse) const;
207
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000208 /** Transpose this matrix in place. */
209 void transpose();
210
reed@google.com8260a892011-06-13 14:02:52 +0000211 /** Apply the matrix to the src vector, returning the new vector in dst.
212 It is legal for src and dst to point to the same memory.
213 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000214 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
215 void mapScalars(SkScalar vec[4]) const {
216 this->mapScalars(vec, vec);
217 }
218
219 // DEPRECATED: call mapScalars()
220 void map(const SkScalar src[4], SkScalar dst[4]) const {
221 this->mapScalars(src, dst);
222 }
223 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000224 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000225 this->mapScalars(vec, vec);
226 }
227
228#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000229 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000230#elif defined SK_MSCALAR_IS_FLOAT
reed@google.comdd311312012-11-12 20:43:59 +0000231 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000232 this->mapScalars(src, dst);
233 }
234#endif
235 void mapMScalars(SkMScalar vec[4]) const {
236 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000237 }
238
239 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
240 SkVector4 dst;
241 m.map(src.fData, dst.fData);
242 return dst;
243 }
244
245 void dump() const;
246
vollick@chromium.org3959a762012-11-13 15:08:22 +0000247 double determinant() const;
248
reed@google.com8260a892011-06-13 14:02:52 +0000249private:
250 /* Stored in the same order as opengl:
251 [3][0] = tx
252 [3][1] = ty
253 [3][2] = tz
254 */
255 SkMScalar fMat[4][4];
reed@google.com8260a892011-06-13 14:02:52 +0000256};
257
258#endif