blob: 9926b79929593f3ad1bde4ba48819aa5782a949c [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;
32#else
33 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
reed@google.comda9fac02011-06-13 14:46:52 +0000122 void asColMajorf(float[]) const;
123 void asColMajord(double[]) const;
124 void asRowMajorf(float[]) const;
125 void asRowMajord(double[]) const;
126
reed@google.com8260a892011-06-13 14:02:52 +0000127 bool isIdentity() const;
128 void setIdentity();
129 void reset() { this->setIdentity();}
130
131 void set3x3(SkMScalar m00, SkMScalar m01, SkMScalar m02,
132 SkMScalar m10, SkMScalar m11, SkMScalar m12,
133 SkMScalar m20, SkMScalar m21, SkMScalar m22);
134
135 void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
136 void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
137 void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
138
139 void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
140 void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
141 void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
142
143 void setScale(SkMScalar scale) {
144 this->setScale(scale, scale, scale);
145 }
146 void preScale(SkMScalar scale) {
147 this->preScale(scale, scale, scale);
148 }
149 void postScale(SkMScalar scale) {
150 this->postScale(scale, scale, scale);
151 }
152
153 void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
154 SkMScalar degrees) {
155 this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
156 }
157
158 /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
159 it will be automatically resized.
160 */
161 void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
162 SkMScalar radians);
163 /** Rotate about the vector [x,y,z]. Does not check the length of the
164 vector, assuming it is unit-length.
165 */
166 void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
167 SkMScalar radians);
168
169 void setConcat(const SkMatrix44& a, const SkMatrix44& b);
170 void preConcat(const SkMatrix44& m) {
171 this->setConcat(*this, m);
172 }
173 void postConcat(const SkMatrix44& m) {
174 this->setConcat(m, *this);
175 }
176
177 friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
178 return SkMatrix44(a, b);
179 }
180
181 /** If this is invertible, return that in inverse and return true. If it is
182 not invertible, return false and ignore the inverse parameter.
183 */
184 bool invert(SkMatrix44* inverse) const;
185
186 /** Apply the matrix to the src vector, returning the new vector in dst.
187 It is legal for src and dst to point to the same memory.
188 */
reed@google.com1ea95be2012-11-09 21:39:48 +0000189 void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
190 void mapScalars(SkScalar vec[4]) const {
191 this->mapScalars(vec, vec);
192 }
193
194 // DEPRECATED: call mapScalars()
195 void map(const SkScalar src[4], SkScalar dst[4]) const {
196 this->mapScalars(src, dst);
197 }
198 // DEPRECATED: call mapScalars()
reed@google.com8260a892011-06-13 14:02:52 +0000199 void map(SkScalar vec[4]) const {
reed@google.com1ea95be2012-11-09 21:39:48 +0000200 this->mapScalars(vec, vec);
201 }
202
203#ifdef SK_MSCALAR_IS_DOUBLE
204 void mapMScalars(SkMScalar src[4], SkMScalar dst[4]) const;
205#else
206 void mapMScalars(SkMScalar src[4], SkMScalar dst[4]) const {
207 this->mapScalars(src, dst);
208 }
209#endif
210 void mapMScalars(SkMScalar vec[4]) const {
211 this->mapMScalars(vec, vec);
reed@google.com8260a892011-06-13 14:02:52 +0000212 }
213
214 friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
215 SkVector4 dst;
216 m.map(src.fData, dst.fData);
217 return dst;
218 }
219
220 void dump() const;
221
222private:
223 /* Stored in the same order as opengl:
224 [3][0] = tx
225 [3][1] = ty
226 [3][2] = tz
227 */
228 SkMScalar fMat[4][4];
229
230 double determinant() const;
231};
232
233#endif