blob: a046a079dfe5e71f88716bf8c67577a8edcea9bf [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
reed@google.comda9fac02011-06-13 14:46:52 +0000129 void asColMajorf(float[]) const;
130 void asColMajord(double[]) const;
131 void asRowMajorf(float[]) const;
132 void asRowMajord(double[]) const;
133
reed@google.com8260a892011-06-13 14:02:52 +0000134 bool isIdentity() const;
135 void setIdentity();
136 void reset() { this->setIdentity();}
137
138 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
139 SkMScalar m10, SkMScalar m11, SkMScalar m12,
140 SkMScalar m20, SkMScalar m21, SkMScalar m22);
141
142 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
143 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
144 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
145
146 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
147 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
148 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
149
150 void setScale(SkMScalar scale) {
151 this->setScale(scale, scale, scale);
152 }
153 void preScale(SkMScalar scale) {
154 this->preScale(scale, scale, scale);
155 }
156 void postScale(SkMScalar scale) {
157 this->postScale(scale, scale, scale);
158 }
159
160 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
161 SkMScalar degrees) {
162 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
163 }
164
165 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
166 it will be automatically resized.
167 */
168 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
169 SkMScalar radians);
170 /** Rotate about the vector [x,y,z]. Does not check the length of the
171 vector, assuming it is unit-length.
172 */
173 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
174 SkMScalar radians);
175
176 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
177 void preConcat(const SkMatrix44& m) {
178 this->setConcat(*this, m);
179 }
180 void postConcat(const SkMatrix44& m) {
181 this->setConcat(m, *this);
182 }
183
184 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
185 return SkMatrix44(a, b);
186 }
187
188 /** If this is invertible, return that in inverse and return true. If it is
189 not invertible, return false and ignore the inverse parameter.
190 */
191 bool invert(SkMatrix44* inverse) const;
192
vollick@chromium.org9b21c252012-11-14 21:33:55 +0000193 /** Transpose this matrix in place. */
194 void transpose();
195
reed@google.com8260a892011-06-13 14:02:52 +0000196 /** Apply the matrix to the src vector, returning the new vector in dst.
197 It is legal for src and dst to point to the same memory.
198 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000199 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
200 void mapScalars(SkScalar vec[4]) const {
201 this->mapScalars(vec, vec);
202 }
203
204 // DEPRECATED: call mapScalars()
205 void map(const SkScalar src[4], SkScalar dst[4]) const {
206 this->mapScalars(src, dst);
207 }
208 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000209 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000210 this->mapScalars(vec, vec);
211 }
212
213#ifdef SK_MSCALAR_IS_DOUBLE
reed@google.comdd311312012-11-12 20:43:59 +0000214 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
vollick@chromium.org5596a692012-11-13 20:12:00 +0000215#elif defined SK_MSCALAR_IS_FLOAT
reed@google.comdd311312012-11-12 20:43:59 +0000216 void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000217 this->mapScalars(src, dst);
218 }
219#endif
220 void mapMScalars(SkMScalar vec[4]) const {
221 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000222 }
223
224 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
225 SkVector4 dst;
226 m.map(src.fData, dst.fData);
227 return dst;
228 }
229
230 void dump() const;
231
vollick@chromium.org3959a762012-11-13 15:08:22 +0000232 double determinant() const;
233
reed@google.com8260a892011-06-13 14:02:52 +0000234private:
235 /* Stored in the same order as opengl:
236 [3][0] = tx
237 [3][1] = ty
238 [3][2] = tz
239 */
240 SkMScalar fMat[4][4];
reed@google.com8260a892011-06-13 14:02:52 +0000241};
242
243#endif