MNN  1.0
Matrix.h
浏览该文件的文档.
1 /*
2  * Copyright 2006 The Android Open Source Project
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 /* Generated by tools/bookmaker from include/core/Matrix.h and docs/SkMatrix_Reference.bmh
9  on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
10  https://skia.org/user/api/SkMatrix_Reference
11 
12  You may edit either file directly. Structural changes to public interfaces require
13  editing both files. After editing docs/SkMatrix_Reference.bmh, run:
14  bookmaker -b docs -i include/core/Matrix.h -p
15  to create an updated version of this file.
16  */
17 
18 
19 //
20 // Modified by jiangxiaotang on 2018/09/19.
21 // Copyright © 2018, Alibaba Group Holding Limited
22 //
23 
24 #ifndef SkMatrix_DEFINED
25 #define SkMatrix_DEFINED
26 
27 #include <string.h>
28 #include <cstdint>
29 #include "Rect.h"
30 
31 namespace MNN {
32 namespace CV {
33 
49 public:
50  Matrix() {
51  setIdentity();
52  }
53 
64  static Matrix MakeScale(float sx, float sy) {
65  Matrix m;
66  m.setScale(sx, sy);
67  return m;
68  }
69 
79  static Matrix MakeScale(float scale) {
80  Matrix m;
81  m.setScale(scale, scale);
82  return m;
83  }
84 
95  static Matrix MakeTrans(float dx, float dy) {
96  Matrix m;
97  m.setTranslate(dx, dy);
98  return m;
99  }
100 
118  static Matrix MakeAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float pers0,
119  float pers1, float pers2) {
120  Matrix m;
121  m.setAll(scaleX, skewX, transX, skewY, scaleY, transY, pers0, pers1, pers2);
122  return m;
123  }
124 
129  enum TypeMask {
130  kIdentity_Mask = 0,
131  kTranslate_Mask = 0x01,
132  kScale_Mask = 0x02,
133  kAffine_Mask = 0x04,
134  kPerspective_Mask = 0x08,
135  };
136 
145  TypeMask getType() const {
146  if (fTypeMask & kUnknown_Mask) {
147  fTypeMask = this->computeTypeMask();
148  }
149  // only return the public masks
150  return (TypeMask)(fTypeMask & 0xF);
151  }
152 
161  bool isIdentity() const {
162  return this->getType() == 0;
163  }
164 
174  bool isScaleTranslate() const {
175  return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
176  }
177 
186  bool isTranslate() const {
187  return !(this->getType() & ~(kTranslate_Mask));
188  }
189 
211  bool rectStaysRect() const {
212  if (fTypeMask & kUnknown_Mask) {
213  fTypeMask = this->computeTypeMask();
214  }
215  return (fTypeMask & kRectStaysRect_Mask) != 0;
216  }
217 
239  bool preservesAxisAlignment() const {
240  return this->rectStaysRect();
241  }
242 
246  static constexpr int kMScaleX = 0;
247  static constexpr int kMSkewX = 1;
248  static constexpr int kMTransX = 2;
249  static constexpr int kMSkewY = 3;
250  static constexpr int kMScaleY = 4;
251  static constexpr int kMTransY = 5;
252  static constexpr int kMPersp0 = 6;
253  static constexpr int kMPersp1 = 7;
254  static constexpr int kMPersp2 = 8;
255 
259  static constexpr int kAScaleX = 0;
260  static constexpr int kASkewY = 1;
261  static constexpr int kASkewX = 2;
262  static constexpr int kAScaleY = 3;
263  static constexpr int kATransX = 4;
264  static constexpr int kATransY = 5;
265 
273  float operator[](int index) const {
274  MNN_ASSERT((unsigned)index < 9);
275  return fMat[index];
276  }
277 
285  float get(int index) const {
286  MNN_ASSERT((unsigned)index < 9);
287  return fMat[index];
288  }
289 
295  float getScaleX() const {
296  return fMat[kMScaleX];
297  }
298 
304  float getScaleY() const {
305  return fMat[kMScaleY];
306  }
307 
314  float getSkewY() const {
315  return fMat[kMSkewY];
316  }
317 
324  float getSkewX() const {
325  return fMat[kMSkewX];
326  }
327 
333  float getTranslateX() const {
334  return fMat[kMTransX];
335  }
336 
342  float getTranslateY() const {
343  return fMat[kMTransY];
344  }
345 
350  float getPerspX() const {
351  return fMat[kMPersp0];
352  }
353 
358  float getPerspY() const {
359  return fMat[kMPersp1];
360  }
361 
372  float& operator[](int index) {
373  MNN_ASSERT((unsigned)index < 9);
374  this->setTypeMask(kUnknown_Mask);
375  return fMat[index];
376  }
377 
385  void set(int index, float value) {
386  MNN_ASSERT((unsigned)index < 9);
387  fMat[index] = value;
388  this->setTypeMask(kUnknown_Mask);
389  }
390 
395  void setScaleX(float v) {
396  this->set(kMScaleX, v);
397  }
398 
403  void setScaleY(float v) {
404  this->set(kMScaleY, v);
405  }
406 
411  void setSkewY(float v) {
412  this->set(kMSkewY, v);
413  }
414 
419  void setSkewX(float v) {
420  this->set(kMSkewX, v);
421  }
422 
427  void setTranslateX(float v) {
428  this->set(kMTransX, v);
429  }
430 
435  void setTranslateY(float v) {
436  this->set(kMTransY, v);
437  }
438 
444  void setPerspX(float v) {
445  this->set(kMPersp0, v);
446  }
447 
453  void setPerspY(float v) {
454  this->set(kMPersp1, v);
455  }
456 
473  void setAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float persp0,
474  float persp1, float persp2) {
475  fMat[kMScaleX] = scaleX;
476  fMat[kMSkewX] = skewX;
477  fMat[kMTransX] = transX;
478  fMat[kMSkewY] = skewY;
479  fMat[kMScaleY] = scaleY;
480  fMat[kMTransY] = transY;
481  fMat[kMPersp0] = persp0;
482  fMat[kMPersp1] = persp1;
483  fMat[kMPersp2] = persp2;
484  this->setTypeMask(kUnknown_Mask);
485  }
486 
493  void get9(float buffer[9]) const {
494  memcpy(buffer, fMat, 9 * sizeof(float));
495  }
496 
513  void set9(const float buffer[9]);
514 
524  void reset();
525 
535  void setIdentity() {
536  this->reset();
537  }
538 
544  void setTranslate(float dx, float dy);
545 
554  void setScale(float sx, float sy, float px, float py);
555 
561  void setScale(float sx, float sy);
562 
572  void setRotate(float degrees, float px, float py);
573 
579  void setRotate(float degrees);
580 
592  void setSinCos(float sinValue, float cosValue, float px, float py);
593 
602  void setSinCos(float sinValue, float cosValue);
603 
612  void setSkew(float kx, float ky, float px, float py);
613 
619  void setSkew(float kx, float ky);
620 
638  void setConcat(const Matrix& a, const Matrix& b);
639 
658  void preTranslate(float dx, float dy);
659 
686  void preScale(float sx, float sy, float px, float py);
687 
707  void preScale(float sx, float sy);
708 
738  void preRotate(float degrees, float px, float py);
739 
765  void preRotate(float degrees);
766 
793  void preSkew(float kx, float ky, float px, float py);
794 
814  void preSkew(float kx, float ky);
815 
833  void preConcat(const Matrix& other);
834 
853  void postTranslate(float dx, float dy);
854 
881  void postScale(float sx, float sy, float px, float py);
882 
902  void postScale(float sx, float sy);
903 
930  bool postIDiv(int divx, int divy);
931 
961  void postRotate(float degrees, float px, float py);
962 
988  void postRotate(float degrees);
989 
1016  void postSkew(float kx, float ky, float px, float py);
1017 
1037  void postSkew(float kx, float ky);
1038 
1056  void postConcat(const Matrix& other);
1057 
1064  enum ScaleToFit {
1069  };
1070 
1086  bool setRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf);
1087 
1103  static Matrix MakeRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf) {
1104  Matrix m;
1105  m.setRectToRect(src, dst, stf);
1106  return m;
1107  }
1108 
1122  bool setPolyToPoly(const Point src[], const Point dst[], int count);
1123 
1132  bool invert(Matrix* inverse) const {
1133  // Allow the trivial case to be inlined.
1134  if (this->isIdentity()) {
1135  if (inverse) {
1136  inverse->reset();
1137  }
1138  return true;
1139  }
1140  return this->invertNonIdentity(inverse);
1141  }
1142 
1153  static void SetAffineIdentity(float affine[6]);
1154 
1165  bool asAffine(float affine[6]) const;
1166 
1181  void setAffine(const float affine[6]);
1182 
1209  void mapPoints(Point dst[], const Point src[], int count) const {
1210  MNN_ASSERT((dst && src && count > 0) || 0 == count);
1211  // no partial overlap
1212  MNN_ASSERT(src == dst || &dst[count] <= &src[0] || &src[count] <= &dst[0]);
1213  this->getMapPtsProc()(*this, dst, src, count);
1214  }
1215 
1239  void mapPoints(Point pts[], int count) const {
1240  this->mapPoints(pts, pts, count);
1241  }
1242 
1259  void mapXY(float x, float y, Point* result) const {
1260  this->getMapXYProc()(*this, x, y, result);
1261  }
1262 
1279  Point mapXY(float x, float y) const {
1280  Point result;
1281  this->getMapXYProc()(*this, x, y, &result);
1282  return result;
1283  }
1284 
1294  bool mapRect(Rect* dst, const Rect& src) const;
1295 
1304  bool mapRect(Rect* rect) const {
1305  return this->mapRect(rect, *rect);
1306  }
1307 
1313  Rect mapRect(const Rect& src) const {
1314  Rect dst;
1315  (void)this->mapRect(&dst, src);
1316  return dst;
1317  }
1318 
1326  void mapRectScaleTranslate(Rect* dst, const Rect& src) const;
1327 
1341  bool cheapEqualTo(const Matrix& m) const {
1342  return 0 == memcmp(fMat, m.fMat, sizeof(fMat));
1343  }
1344 
1353  friend MNN_PUBLIC bool operator==(const Matrix& a, const Matrix& b);
1354 
1363  friend MNN_PUBLIC bool operator!=(const Matrix& a, const Matrix& b) {
1364  return !(a == b);
1365  }
1366 
1371  void dump() const;
1372 
1379  float getMinScale() const;
1380 
1387  float getMaxScale() const;
1388 
1399  bool getMinMaxScales(float scaleFactors[2]) const;
1400 
1409  static const Matrix& I();
1410 
1420  static const Matrix& InvalidMatrix();
1421 
1440  static Matrix Concat(const Matrix& a, const Matrix& b) {
1441  Matrix result;
1442  result.setConcat(a, b);
1443  return result;
1444  }
1445 
1450  this->setTypeMask(kUnknown_Mask);
1451  }
1452 
1464  void setScaleTranslate(float sx, float sy, float tx, float ty) {
1465  fMat[kMScaleX] = sx;
1466  fMat[kMSkewX] = 0;
1467  fMat[kMTransX] = tx;
1468 
1469  fMat[kMSkewY] = 0;
1470  fMat[kMScaleY] = sy;
1471  fMat[kMTransY] = ty;
1472 
1473  fMat[kMPersp0] = 0;
1474  fMat[kMPersp1] = 0;
1475  fMat[kMPersp2] = 1;
1476 
1477  unsigned mask = 0;
1478  if (sx != 1 || sy != 1) {
1479  mask |= kScale_Mask;
1480  }
1481  if (tx || ty) {
1482  mask |= kTranslate_Mask;
1483  }
1484  this->setTypeMask(mask | kRectStaysRect_Mask);
1485  }
1486 
1493 private:
1500  static constexpr int kRectStaysRect_Mask = 0x10;
1501 
1505  static constexpr int kOnlyPerspectiveValid_Mask = 0x40;
1506 
1507  static constexpr int kUnknown_Mask = 0x80;
1508 
1509  static constexpr int kORableMasks = kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask;
1510 
1511  static constexpr int kAllMasks =
1512  kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask | kRectStaysRect_Mask;
1513 
1514  float fMat[9];
1515  mutable uint32_t fTypeMask;
1516 
1517  static void ComputeInv(float dst[9], const float src[9], double invDet, bool isPersp);
1518 
1519  uint8_t computeTypeMask() const;
1520  uint8_t computePerspectiveTypeMask() const;
1521 
1522  void setTypeMask(int mask) {
1523  // allow kUnknown or a valid mask
1524  MNN_ASSERT(kUnknown_Mask == mask || (mask & kAllMasks) == mask ||
1525  ((kUnknown_Mask | kOnlyPerspectiveValid_Mask) & mask) ==
1526  (kUnknown_Mask | kOnlyPerspectiveValid_Mask));
1527  fTypeMask = (uint8_t)(mask);
1528  }
1529 
1530  void orTypeMask(int mask) {
1531  MNN_ASSERT((mask & kORableMasks) == mask);
1532  fTypeMask = (uint8_t)(fTypeMask | mask);
1533  }
1534 
1535  void clearTypeMask(int mask) {
1536  // only allow a valid mask
1537  MNN_ASSERT((mask & kAllMasks) == mask);
1538  fTypeMask = fTypeMask & ~mask;
1539  }
1540 
1541  TypeMask getPerspectiveTypeMaskOnly() const {
1542  if ((fTypeMask & kUnknown_Mask) && !(fTypeMask & kOnlyPerspectiveValid_Mask)) {
1543  fTypeMask = this->computePerspectiveTypeMask();
1544  }
1545  return (TypeMask)(fTypeMask & 0xF);
1546  }
1547 
1551  bool isTriviallyIdentity() const {
1552  if (fTypeMask & kUnknown_Mask) {
1553  return false;
1554  }
1555  return ((fTypeMask & 0xF) == 0);
1556  }
1557 
1558  inline void updateTranslateMask() {
1559  if ((fMat[kMTransX] != 0) | (fMat[kMTransY] != 0)) {
1560  fTypeMask |= kTranslate_Mask;
1561  } else {
1562  fTypeMask &= ~kTranslate_Mask;
1563  }
1564  }
1565 
1566  typedef void (*MapXYProc)(const Matrix& mat, float x, float y, Point* result);
1567 
1568  static MapXYProc GetMapXYProc(TypeMask mask) {
1569  MNN_ASSERT((mask & ~kAllMasks) == 0);
1570  return gMapXYProcs[mask & kAllMasks];
1571  }
1572 
1573  MapXYProc getMapXYProc() const {
1574  return GetMapXYProc(this->getType());
1575  }
1576 
1577  typedef void (*MapPtsProc)(const Matrix& mat, Point dst[], const Point src[], int count);
1578 
1579  static MapPtsProc GetMapPtsProc(TypeMask mask) {
1580  MNN_ASSERT((mask & ~kAllMasks) == 0);
1581  return gMapPtsProcs[mask & kAllMasks];
1582  }
1583 
1584  MapPtsProc getMapPtsProc() const {
1585  return GetMapPtsProc(this->getType());
1586  }
1587 
1588  bool invertNonIdentity(Matrix* inverse) const;
1589 
1590  static void Identity_xy(const Matrix&, float, float, Point*);
1591  static void Trans_xy(const Matrix&, float, float, Point*);
1592  static void Scale_xy(const Matrix&, float, float, Point*);
1593  static void ScaleTrans_xy(const Matrix&, float, float, Point*);
1594  static void Rot_xy(const Matrix&, float, float, Point*);
1595  static void RotTrans_xy(const Matrix&, float, float, Point*);
1596  static void Persp_xy(const Matrix&, float, float, Point*);
1597 
1598  static const MapXYProc gMapXYProcs[];
1599 
1600  static void Identity_pts(const Matrix&, Point[], const Point[], int);
1601  static void Trans_pts(const Matrix&, Point dst[], const Point[], int);
1602  static void Scale_pts(const Matrix&, Point dst[], const Point[], int);
1603  static void ScaleTrans_pts(const Matrix&, Point dst[], const Point[], int count);
1604  static void Persp_pts(const Matrix&, Point dst[], const Point[], int);
1605 
1606  static void Affine_vpts(const Matrix&, Point dst[], const Point[], int);
1607 
1608  static const MapPtsProc gMapPtsProcs[];
1609 };
1610 } // namespace CV
1611 } // namespace MNN
1612 #endif
float get(int index) const
Definition: Matrix.h:285
float & operator[](int index)
Definition: Matrix.h:372
void setIdentity()
Definition: Matrix.h:535
float getScaleX() const
Definition: Matrix.h:295
ScaleToFit
Definition: Matrix.h:1064
Point mapXY(float x, float y) const
Definition: Matrix.h:1279
static Matrix MakeTrans(float dx, float dy)
Definition: Matrix.h:95
float getTranslateX() const
Definition: Matrix.h:333
bool isIdentity() const
Definition: Matrix.h:161
void setScaleTranslate(float sx, float sy, float tx, float ty)
Definition: Matrix.h:1464
bool isScaleTranslate() const
Definition: Matrix.h:174
#define MNN_ASSERT(x)
Definition: MNNDefine.h:41
Definition: Rect.h:54
void setTranslateX(float v)
Definition: Matrix.h:427
float getSkewX() const
Definition: Matrix.h:324
void setScaleX(float v)
Definition: Matrix.h:395
void setSkewX(float v)
Definition: Matrix.h:419
void setPerspY(float v)
Definition: Matrix.h:453
float getSkewY() const
Definition: Matrix.h:314
TypeMask getType() const
Definition: Matrix.h:145
scales in x and y to fill destination Rect
Definition: Matrix.h:1065
static Matrix MakeRectToRect(const Rect &src, const Rect &dst, ScaleToFit stf)
Definition: Matrix.h:1103
void setTranslateY(float v)
Definition: Matrix.h:435
static Matrix MakeScale(float scale)
Definition: Matrix.h:79
void mapXY(float x, float y, Point *result) const
Definition: Matrix.h:1259
scales and aligns to center
Definition: Matrix.h:1067
void setSkewY(float v)
Definition: Matrix.h:411
bool isTranslate() const
Definition: Matrix.h:186
float getPerspX() const
Definition: Matrix.h:350
void dirtyMatrixTypeCache()
Definition: Matrix.h:1449
float operator[](int index) const
Definition: Matrix.h:273
scales and aligns to right and bottom
Definition: Matrix.h:1068
bool invert(Matrix *inverse) const
Definition: Matrix.h:1132
Matrix()
Definition: Matrix.h:50
bool mapRect(Rect *rect) const
Definition: Matrix.h:1304
void setTranslate(float dx, float dy)
#define MNN_PUBLIC
Definition: MNNDefine.h:53
void setPerspX(float v)
Definition: Matrix.h:444
static Matrix MakeScale(float sx, float sy)
Definition: Matrix.h:64
Rect mapRect(const Rect &src) const
Definition: Matrix.h:1313
Definition: Matrix.h:48
void set(int index, float value)
Definition: Matrix.h:385
static Matrix MakeAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float pers0, float pers1, float pers2)
Definition: Matrix.h:118
float getTranslateY() const
Definition: Matrix.h:342
Definition: AutoTime.hpp:16
bool setRectToRect(const Rect &src, const Rect &dst, ScaleToFit stf)
void setConcat(const Matrix &a, const Matrix &b)
float getScaleY() const
Definition: Matrix.h:304
void mapPoints(Point dst[], const Point src[], int count) const
Definition: Matrix.h:1209
void mapPoints(Point pts[], int count) const
Definition: Matrix.h:1239
Definition: Rect.h:37
void setAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float persp0, float persp1, float persp2)
Definition: Matrix.h:473
bool rectStaysRect() const
Definition: Matrix.h:211
void setScale(float sx, float sy, float px, float py)
bool preservesAxisAlignment() const
Definition: Matrix.h:239
scales and aligns to left and top
Definition: Matrix.h:1066
static Matrix Concat(const Matrix &a, const Matrix &b)
Definition: Matrix.h:1440
void setScaleY(float v)
Definition: Matrix.h:403
bool cheapEqualTo(const Matrix &m) const
Definition: Matrix.h:1341
void get9(float buffer[9]) const
Definition: Matrix.h:493
TypeMask
Definition: Matrix.h:129
friend MNN_PUBLIC bool operator!=(const Matrix &a, const Matrix &b)
Definition: Matrix.h:1363
float getPerspY() const
Definition: Matrix.h:358