blob: 67486b38a114c560c8a5a70d715325c74cad2632 [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
reed@google.com631940c2012-11-27 13:13:22 +0000108 bool operator==(const SkMatrix44& other) const;
reed@google.com8260a892011-06-13 14:02:52 +0000109 bool operator!=(const SkMatrix44& other) const {
reed@google.com631940c2012-11-27 13:13:22 +0000110 return !(other == *this);
reed@google.com8260a892011-06-13 14:02:52 +0000111 }
112
113 SkMatrix44(const SkMatrix&);
114 SkMatrix44& operator=(const SkMatrix& src);
115 operator SkMatrix() const;
116
reed@google.com631940c2012-11-27 13:13:22 +0000117 SkMScalar get(int row, int col) const {
118 SkASSERT((unsigned)row <= 3);
119 SkASSERT((unsigned)col <= 3);
120 return fMat[col][row];
121 }
reed@google.com8260a892011-06-13 14:02:52 +0000122
reed@google.com631940c2012-11-27 13:13:22 +0000123 void set(int row, int col, SkMScalar value) {
124 SkASSERT((unsigned)row <= 3);
125 SkASSERT((unsigned)col <= 3);
126 fMat[col][row] = value;
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000127 fIdentity = false;
reed@google.com631940c2012-11-27 13:13:22 +0000128 }
skia.committer@gmail.comab38f7a2012-11-28 02:02:11 +0000129
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000130 double getDouble(int row, int col) const {
131 return SkMScalarToDouble(this->get(row, col));
132 }
133 void setDouble(int row, int col, double value) {
134 this->set(row, col, SkDoubleToMScalar(value));
135 }
136
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000137 /** These methods allow one to efficiently read matrix entries into an
138 * array. The given array must have room for exactly 16 entries. Whenever
139 * possible, they will try to use memcpy rather than an entry-by-entry
140 * copy.
141 */
reed@google.comda9fac02011-06-13 14:46:52 +0000142 void asColMajorf(float[]) const;
143 void asColMajord(double[]) const;
144 void asRowMajorf(float[]) const;
145 void asRowMajord(double[]) const;
146
vollick@chromium.orgf11cf9f2012-11-19 21:02:06 +0000147 /** These methods allow one to efficiently set all matrix entries from an
148 * array. The given array must have room for exactly 16 entries. Whenever
149 * possible, they will try to use memcpy rather than an entry-by-entry
150 * copy.
151 */
152 void setColMajorf(const float[]);
153 void setColMajord(const double[]);
154 void setRowMajorf(const float[]);
155 void setRowMajord(const double[]);
156
reed@google.com8260a892011-06-13 14:02:52 +0000157 bool isIdentity() const;
158 void setIdentity();
159 void reset() { this->setIdentity();}
160
161 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
162 SkMScalar m10, SkMScalar m11, SkMScalar m12,
163 SkMScalar m20, SkMScalar m21, SkMScalar m22);
164
165 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
166 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
167 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
168
169 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
170 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
171 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
172
173 void setScale(SkMScalar scale) {
174 this->setScale(scale, scale, scale);
175 }
176 void preScale(SkMScalar scale) {
177 this->preScale(scale, scale, scale);
178 }
179 void postScale(SkMScalar scale) {
180 this->postScale(scale, scale, scale);
181 }
182
183 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
184 SkMScalar degrees) {
185 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
186 }
187
188 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
189 it will be automatically resized.
190 */
191 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
192 SkMScalar radians);
193 /** Rotate about the vector [x,y,z]. Does not check the length of the
194 vector, assuming it is unit-length.
195 */
196 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
197 SkMScalar radians);
198
199 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
200 void preConcat(const SkMatrix44& m) {
201 this->setConcat(*this, m);
202 }
203 void postConcat(const SkMatrix44& m) {
204 this->setConcat(m, *this);
205 }
206
207 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
208 return SkMatrix44(a, b);
209 }
210
211 /** If this is invertible, return that in inverse and return true. If it is
212 not invertible, return false and ignore the inverse parameter.
213 */
214 bool invert(SkMatrix44* inverse) const;
215
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000216 /** Transpose this matrix in place. */
217 void transpose();
218
reed@google.com8260a892011-06-13 14:02:52 +0000219 /** Apply the matrix to the src vector, returning the new vector in dst.
220 It is legal for src and dst to point to the same memory.
221 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000222 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
223 void mapScalars(SkScalar vec[4]) const {
224 this->mapScalars(vec, vec);
225 }
226
227 // DEPRECATED: call mapScalars()
228 void map(const SkScalar src[4], SkScalar dst[4]) const {
229 this->mapScalars(src, dst);
230 }
231 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000232 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000233 this->mapScalars(vec, vec);
234 }
235
236#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000237 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000238#elif defined SK_MSCALAR_IS_FLOAT
reed@google.comdd311312012-11-12 20:43:59 +0000239 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000240 this->mapScalars(src, dst);
241 }
242#endif
243 void mapMScalars(SkMScalar vec[4]) const {
244 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000245 }
246
247 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
248 SkVector4 dst;
249 m.map(src.fData, dst.fData);
250 return dst;
251 }
252
253 void dump() const;
254
vollick@chromium.org3959a762012-11-13 15:08:22 +0000255 double determinant() const;
256
reed@google.com8260a892011-06-13 14:02:52 +0000257private:
258 /* Stored in the same order as opengl:
259 [3][0] = tx
260 [3][1] = ty
261 [3][2] = tz
262 */
263 SkMScalar fMat[4][4];
jamesr@chromium.orgdeb4c162012-11-29 21:17:16 +0000264
265 bool fIdentity;
reed@google.com8260a892011-06-13 14:02:52 +0000266};
267
268#endif