Skia
2D Graphics Library
SkMatrix44.h
Go to the documentation of this file.
1 /*
2  * 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.
6  */
7 
8 #ifndef SkMatrix44_DEFINED
9 #define SkMatrix44_DEFINED
10 
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkScalar.h"
13 
14 #include <atomic>
15 #include <cstring>
16 
17 // This entire file is DEPRECATED, and will be removed at some point.
18 // SkCanvas has full support for 4x4 matrices using SkM44
19 
20 // DEPRECATED
21 struct SkVector4 {
23 
25  this->set(0, 0, 0, 1);
26  }
27  SkVector4(const SkVector4& src) {
28  memcpy(fData, src.fData, sizeof(fData));
29  }
31  fData[0] = x;
32  fData[1] = y;
33  fData[2] = z;
34  fData[3] = w;
35  }
36 
37  SkVector4& operator=(const SkVector4& src) {
38  memcpy(fData, src.fData, sizeof(fData));
39  return *this;
40  }
41 
42  bool operator==(const SkVector4& v) const {
43  return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
44  fData[2] == v.fData[2] && fData[3] == v.fData[3];
45  }
46  bool operator!=(const SkVector4& v) const { return !(*this == v); }
48  return fData[0] == x && fData[1] == y &&
49  fData[2] == z && fData[3] == w;
50  }
51 
53  fData[0] = x;
54  fData[1] = y;
55  fData[2] = z;
56  fData[3] = w;
57  }
58 };
59 
60 // DEPRECATED
62 public:
63 
65  kUninitialized_Constructor
66  };
68  kIdentity_Constructor
69  };
71  kNaN_Constructor
72  };
73 
74  SkMatrix44(Uninitialized_Constructor) {} // ironically, cannot be constexpr
75 
77  : fMat{{ 1, 0, 0, 0, },
78  { 0, 1, 0, 0, },
79  { 0, 0, 1, 0, },
80  { 0, 0, 0, 1, }}
81  , fTypeMask(kIdentity_Mask) {}
82 
88  , fTypeMask(kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask) {}
89 
90  constexpr SkMatrix44() : SkMatrix44{kIdentity_Constructor} {}
91 
92  SkMatrix44(const SkMatrix44& src) = default;
93 
94  SkMatrix44& operator=(const SkMatrix44& src) = default;
95 
96  SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) {
97  this->setConcat(a, b);
98  }
99 
100  bool operator==(const SkMatrix44& other) const;
101  bool operator!=(const SkMatrix44& other) const {
102  return !(other == *this);
103  }
104 
105  /* When converting from SkMatrix44 to SkMatrix, the third row and
106  * column is dropped. When converting from SkMatrix to SkMatrix44
107  * the third row and column remain as identity:
108  * [ a b c ] [ a b 0 c ]
109  * [ d e f ] -> [ d e 0 f ]
110  * [ g h i ] [ 0 0 1 0 ]
111  * [ g h 0 i ]
112  */
115 
116  // TODO: make this explicit (will need to guard that change to update chrome, etc.
117 #ifndef SK_SUPPORT_LEGACY_IMPLICIT_CONVERSION_MATRIX44
118  explicit
119 #endif
120  operator SkMatrix() const;
121 
125  static const SkMatrix44& I();
126 
127  using TypeMask = uint8_t;
128  enum : TypeMask {
129  kIdentity_Mask = 0,
130  kTranslate_Mask = 1 << 0,
131  kScale_Mask = 1 << 1,
132  kAffine_Mask = 1 << 2,
133  kPerspective_Mask = 1 << 3,
134  };
135 
143  inline TypeMask getType() const { return fTypeMask; }
144 
148  inline bool isIdentity() const {
149  return kIdentity_Mask == this->getType();
150  }
151 
155  inline bool isTranslate() const {
156  return !(this->getType() & ~kTranslate_Mask);
157  }
158 
162  inline bool isScaleTranslate() const {
163  return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
164  }
165 
169  inline bool isScale() const {
170  return !(this->getType() & ~kScale_Mask);
171  }
172 
173  inline bool hasPerspective() const {
174  return SkToBool(this->getType() & kPerspective_Mask);
175  }
176 
177  void setIdentity();
178  inline void reset() { this->setIdentity();}
179 
186  inline SkScalar get(int row, int col) const {
187  SkASSERT((unsigned)row <= 3);
188  SkASSERT((unsigned)col <= 3);
189  return fMat[col][row];
190  }
191 
198  inline void set(int row, int col, SkScalar value) {
199  SkASSERT((unsigned)row <= 3);
200  SkASSERT((unsigned)col <= 3);
201  fMat[col][row] = value;
202  this->recomputeTypeMask();
203  }
204 
205  inline double getDouble(int row, int col) const {
206  return double(this->get(row, col));
207  }
208  inline void setDouble(int row, int col, double value) {
209  this->set(row, col, SkScalar(value));
210  }
211  inline float getFloat(int row, int col) const {
212  return float(this->get(row, col));
213  }
214  inline void setFloat(int row, int col, float value) {
215  this->set(row, col, value);
216  }
217 
227  void asColMajorf(float[]) const;
228  void asColMajord(double[]) const;
229  void asRowMajorf(float[]) const;
230  void asRowMajord(double[]) const;
231 
242  void setColMajorf(const float[]);
243  void setColMajord(const double[]);
244  void setRowMajorf(const float[]);
245  void setRowMajord(const double[]);
246 
247  void setColMajor(const SkScalar data[]) { this->setColMajorf(data); }
248  void setRowMajor(const SkScalar data[]) { this->setRowMajorf(data); }
249 
250  /* This sets the top-left of the matrix and clears the translation and
251  * perspective components (with [3][3] set to 1). m_ij is interpreted
252  * as the matrix entry at row = i, col = j. */
253  void set3x3(SkScalar m_00, SkScalar m_10, SkScalar m_20,
254  SkScalar m_01, SkScalar m_11, SkScalar m_21,
255  SkScalar m_02, SkScalar m_12, SkScalar m_22);
256  void set3x3RowMajorf(const float[]);
257 
258  void set4x4(SkScalar m_00, SkScalar m_10, SkScalar m_20, SkScalar m_30,
259  SkScalar m_01, SkScalar m_11, SkScalar m_21, SkScalar m_31,
260  SkScalar m_02, SkScalar m_12, SkScalar m_22, SkScalar m_32,
261  SkScalar m_03, SkScalar m_13, SkScalar m_23, SkScalar m_33);
262 
266 
270 
271  inline SkMatrix44& setScale(SkScalar scale) {
272  return this->setScale(scale, scale, scale);
273  }
274  inline SkMatrix44& preScale(SkScalar scale) {
275  return this->preScale(scale, scale, scale);
276  }
277  inline SkMatrix44& postScale(SkScalar scale) {
278  return this->postScale(scale, scale, scale);
279  }
280 
282  this->setRotateAbout(x, y, z, degrees * SK_ScalarPI / 180);
283  }
284 
293 
294  void setConcat(const SkMatrix44& a, const SkMatrix44& b);
295  inline void preConcat(const SkMatrix44& m) {
296  this->setConcat(*this, m);
297  }
298  inline void postConcat(const SkMatrix44& m) {
299  this->setConcat(m, *this);
300  }
301 
302  friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
303  return SkMatrix44(a, b);
304  }
305 
310  bool invert(SkMatrix44* inverse) const;
311 
313  void transpose();
314 
318  void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
319  inline void mapScalars(SkScalar vec[4]) const {
320  this->mapScalars(vec, vec);
321  }
322 
323  friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
324  SkVector4 dst;
325  m.mapScalars(src.fData, dst.fData);
326  return dst;
327  }
328 
337  void map2(const float src2[], int count, float dst4[]) const;
338  void map2(const double src2[], int count, double dst4[]) const;
339 
351 
352  void dump() const;
353 
354  double determinant() const;
355 
356 private:
357  /* This is indexed by [col][row]. */
358  SkScalar fMat[4][4];
359  TypeMask fTypeMask;
360 
361  static constexpr int kAllPublic_Masks = 0xF;
362 
363  void as3x4RowMajorf(float[]) const;
364  void set3x4RowMajorf(const float[]);
365 
366  SkScalar transX() const { return fMat[3][0]; }
367  SkScalar transY() const { return fMat[3][1]; }
368  SkScalar transZ() const { return fMat[3][2]; }
369 
370  SkScalar scaleX() const { return fMat[0][0]; }
371  SkScalar scaleY() const { return fMat[1][1]; }
372  SkScalar scaleZ() const { return fMat[2][2]; }
373 
374  SkScalar perspX() const { return fMat[0][3]; }
375  SkScalar perspY() const { return fMat[1][3]; }
376  SkScalar perspZ() const { return fMat[2][3]; }
377 
378  void recomputeTypeMask();
379 
380  inline void setTypeMask(TypeMask mask) {
381  SkASSERT(0 == (~kAllPublic_Masks & mask));
382  fTypeMask = mask;
383  }
384 
385  inline const SkScalar* values() const { return &fMat[0][0]; }
386 
387  friend class SkColorSpace;
388  friend class SkCanvas;
389  friend class SkM44;
390 };
391 
392 #endif
SkMatrix44::setRowMajorf
void setRowMajorf(const float[])
SkMatrix44::setRotateAboutUnit
void setRotateAboutUnit(SkScalar x, SkScalar y, SkScalar z, SkScalar radians)
Rotate about the vector [x,y,z].
SkMatrix44::isTranslate
bool isTranslate() const
Return true if the matrix contains translate or is identity.
Definition: SkMatrix44.h:155
SkMatrix44::setScale
SkMatrix44 & setScale(SkScalar sx, SkScalar sy, SkScalar sz)
SkVector4::operator=
SkVector4 & operator=(const SkVector4 &src)
Definition: SkMatrix44.h:37
SkMatrix44::setColMajord
void setColMajord(const double[])
SkMatrix44::postConcat
void postConcat(const SkMatrix44 &m)
Definition: SkMatrix44.h:298
SkMatrix44::setColMajorf
void setColMajorf(const float[])
These methods allow one to efficiently set all matrix entries from an array.
SkMatrix44::SkMatrix44
SkMatrix44(const SkMatrix44 &src)=default
SkMatrix44::preScale
SkMatrix44 & preScale(SkScalar sx, SkScalar sy, SkScalar sz)
SkMatrix44::I
static const SkMatrix44 & I()
Return a reference to a const identity matrix.
SkM44
4x4 matrix used by SkCanvas and other parts of Skia.
Definition: SkM44.h:133
SkMatrix44::set4x4
void set4x4(SkScalar m_00, SkScalar m_10, SkScalar m_20, SkScalar m_30, SkScalar m_01, SkScalar m_11, SkScalar m_21, SkScalar m_31, SkScalar m_02, SkScalar m_12, SkScalar m_22, SkScalar m_32, SkScalar m_03, SkScalar m_13, SkScalar m_23, SkScalar m_33)
SkMatrix44::operator==
bool operator==(const SkMatrix44 &other) const
SkMatrix44::isScaleTranslate
bool isScaleTranslate() const
Return true if the matrix only contains scale or translate or is identity.
Definition: SkMatrix44.h:162
SkVector4::operator!=
bool operator!=(const SkVector4 &v) const
Definition: SkMatrix44.h:46
SkMatrix44::SkMatrix44
SkMatrix44(const SkMatrix44 &a, const SkMatrix44 &b)
Definition: SkMatrix44.h:96
SkMatrix44::preScale
SkMatrix44 & preScale(SkScalar scale)
Definition: SkMatrix44.h:274
SkMatrix44::asColMajord
void asColMajord(double[]) const
SkMatrix44::set3x3
void set3x3(SkScalar m_00, SkScalar m_10, SkScalar m_20, SkScalar m_01, SkScalar m_11, SkScalar m_21, SkScalar m_02, SkScalar m_12, SkScalar m_22)
SkMatrix44::operator*
friend SkVector4 operator*(const SkMatrix44 &m, const SkVector4 &src)
Definition: SkMatrix44.h:323
SkMatrix44::setColMajor
void setColMajor(const SkScalar data[])
Definition: SkMatrix44.h:247
SkVector4::SkVector4
SkVector4()
Definition: SkMatrix44.h:24
SK_ScalarNaN
#define SK_ScalarNaN
Definition: SkScalar.h:27
SkMatrix44::dump
void dump() const
SkMatrix44::postTranslate
SkMatrix44 & postTranslate(SkScalar dx, SkScalar dy, SkScalar dz)
SkScalar.h
SkMatrix44::SkMatrix44
constexpr SkMatrix44(Identity_Constructor)
Definition: SkMatrix44.h:76
SkMatrix44::setConcat
void setConcat(const SkMatrix44 &a, const SkMatrix44 &b)
SkMatrix44::isIdentity
bool isIdentity() const
Return true if the matrix is identity.
Definition: SkMatrix44.h:148
SkMatrix44::Identity_Constructor
Identity_Constructor
Definition: SkMatrix44.h:67
SkMatrix44::operator!=
bool operator!=(const SkMatrix44 &other) const
Definition: SkMatrix44.h:101
SkMatrix44::mapScalars
void mapScalars(const SkScalar src[4], SkScalar dst[4]) const
Apply the matrix to the src vector, returning the new vector in dst.
SkVector4::operator==
bool operator==(const SkVector4 &v) const
Definition: SkMatrix44.h:42
SkVector4::SkVector4
SkVector4(const SkVector4 &src)
Definition: SkMatrix44.h:27
SkToBool
static constexpr bool SkToBool(const T &x)
Definition: SkTypes.h:497
SK_API
#define SK_API
Definition: SkTypes.h:181
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SK_ScalarNearlyZero
#define SK_ScalarNearlyZero
Definition: SkScalar.h:134
SkMatrix44::determinant
double determinant() const
SkMatrix44::SkMatrix44
SkMatrix44(NaN_Constructor)
Definition: SkMatrix44.h:83
SK_ScalarPI
#define SK_ScalarPI
Definition: SkScalar.h:21
SkMatrix44::TypeMask
uint8_t TypeMask
Definition: SkMatrix44.h:127
SkMatrix44::postScale
SkMatrix44 & postScale(SkScalar scale)
Definition: SkMatrix44.h:277
SkMatrix44::getFloat
float getFloat(int row, int col) const
Definition: SkMatrix44.h:211
SkMatrix44::setIdentity
void setIdentity()
SkMatrix44::Uninitialized_Constructor
Uninitialized_Constructor
Definition: SkMatrix44.h:64
SkVector4
Definition: SkMatrix44.h:21
SkMatrix44::operator*
friend SkMatrix44 operator*(const SkMatrix44 &a, const SkMatrix44 &b)
Definition: SkMatrix44.h:302
SkCanvas
Definition: SkCanvas.h:87
SkMatrix44::set
void set(int row, int col, SkScalar value)
set a value in the matrix.
Definition: SkMatrix44.h:198
SkMatrix44::operator=
SkMatrix44 & operator=(const SkMatrix &src)
SkMatrix44::setFloat
void setFloat(int row, int col, float value)
Definition: SkMatrix44.h:214
SkMatrix44::setRowMajor
void setRowMajor(const SkScalar data[])
Definition: SkMatrix44.h:248
SK_Scalar1
#define SK_Scalar1
Definition: SkScalar.h:18
SkMatrix44::getType
TypeMask getType() const
Returns a bitfield describing the transformations the matrix may perform.
Definition: SkMatrix44.h:143
SkMatrix44::isScale
bool isScale() const
Returns true if the matrix only contains scale or is identity.
Definition: SkMatrix44.h:169
SkMatrix44::map2
void map2(const float src2[], int count, float dst4[]) const
map an array of [x, y, 0, 1] through the matrix, returning an array of [x', y', z',...
SkMatrix44::asColMajorf
void asColMajorf(float[]) const
These methods allow one to efficiently read matrix entries into an array.
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkMatrix44::hasPerspective
bool hasPerspective() const
Definition: SkMatrix44.h:173
SkMatrix44::SkMatrix44
SkMatrix44(Uninitialized_Constructor)
Definition: SkMatrix44.h:74
SkMatrix44::preTranslate
SkMatrix44 & preTranslate(SkScalar dx, SkScalar dy, SkScalar dz)
SkMatrix44::transpose
void transpose()
Transpose this matrix in place.
SkMatrix44::setRotateAbout
void setRotateAbout(SkScalar x, SkScalar y, SkScalar z, SkScalar radians)
Rotate about the vector [x,y,z].
SkMatrix44::mapScalars
void mapScalars(SkScalar vec[4]) const
Definition: SkMatrix44.h:319
SkMatrix44::invert
bool invert(SkMatrix44 *inverse) const
If this is invertible, return that in inverse and return true.
SkMatrix44::setScale
SkMatrix44 & setScale(SkScalar scale)
Definition: SkMatrix44.h:271
SkMatrix44::preserves2dAxisAlignment
bool preserves2dAxisAlignment(SkScalar epsilon=SK_ScalarNearlyZero) const
Returns true if transformating an axis-aligned square in 2d by this matrix will produce another 2d ax...
SkMatrix
Definition: SkMatrix.h:48
SkMatrix44::setRotateDegreesAbout
void setRotateDegreesAbout(SkScalar x, SkScalar y, SkScalar z, SkScalar degrees)
Definition: SkMatrix44.h:281
SkMatrix.h
SkMatrix44::preConcat
void preConcat(const SkMatrix44 &m)
Definition: SkMatrix44.h:295
SkMatrix44
Definition: SkMatrix44.h:61
SkMatrix44::get
SkScalar get(int row, int col) const
get a value from the matrix.
Definition: SkMatrix44.h:186
SkMatrix44::getDouble
double getDouble(int row, int col) const
Definition: SkMatrix44.h:205
SkMatrix44::postScale
SkMatrix44 & postScale(SkScalar sx, SkScalar sy, SkScalar sz)
SkVector4::fData
SkScalar fData[4]
Definition: SkMatrix44.h:22
SkMatrix44::SkMatrix44
SkMatrix44(const SkMatrix &)
SkMatrix44::asRowMajord
void asRowMajord(double[]) const
SkMatrix44::reset
void reset()
Definition: SkMatrix44.h:178
SkMatrix44::set3x3RowMajorf
void set3x3RowMajorf(const float[])
SkMatrix44::setRowMajord
void setRowMajord(const double[])
SkMatrix44::map2
void map2(const double src2[], int count, double dst4[]) const
SkMatrix44::operator=
SkMatrix44 & operator=(const SkMatrix44 &src)=default
SkMatrix44::setDouble
void setDouble(int row, int col, double value)
Definition: SkMatrix44.h:208
SkVector4::set
void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w=SK_Scalar1)
Definition: SkMatrix44.h:52
SkVector4::SkVector4
SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w=SK_Scalar1)
Definition: SkMatrix44.h:30
SkMatrix44::setTranslate
SkMatrix44 & setTranslate(SkScalar dx, SkScalar dy, SkScalar dz)
SkMatrix44::SkMatrix44
constexpr SkMatrix44()
Definition: SkMatrix44.h:90
SkMatrix44::asRowMajorf
void asRowMajorf(float[]) const
SkVector4::equals
bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w=SK_Scalar1)
Definition: SkMatrix44.h:47
SkMatrix44::NaN_Constructor
NaN_Constructor
Definition: SkMatrix44.h:70
SkColorSpace
Definition: SkColorSpace.h:104