doc/API/html/_matrix_8h_source.html
| 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
48 class MNN_PUBLIC Matrix {
49 public:
51 setIdentity();
52 }
53
64static Matrix MakeScale(float sx, float sy) {
65Matrix m;
66 m.setScale(sx, sy);
67return m;
68 }
69
79static Matrix MakeScale(float scale) {
80Matrix m;
81 m.setScale(scale, scale);
82return m;
83 }
84
95static Matrix MakeTrans(float dx, float dy) {
96Matrix m;
97 m.setTranslate(dx, dy);
98return m;
99 }
100
118static Matrix MakeAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float pers0,
119float pers1, float pers2) {
120Matrix m;
121 m.setAll(scaleX, skewX, transX, skewY, scaleY, transY, pers0, pers1, pers2);
122return m;
123 }
124
130 kIdentity_Mask = 0,
131 kTranslate_Mask = 0x01,
132 kScale_Mask = 0x02,
133 kAffine_Mask = 0x04,
134 kPerspective_Mask = 0x08,
135 };
136
146if (fTypeMask & kUnknown_Mask) {
147 fTypeMask = this->computeTypeMask();
148 }
149// only return the public masks
150return (TypeMask)(fTypeMask & 0xF);
151 }
152
161bool isIdentity() const {
162return this->getType() == 0;
163 }
164
174bool isScaleTranslate() const {
175return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
176 }
177
186bool isTranslate() const {
187return !(this->getType() & ~(kTranslate_Mask));
188 }
189
211bool rectStaysRect() const {
212if (fTypeMask & kUnknown_Mask) {
213 fTypeMask = this->computeTypeMask();
214 }
215return (fTypeMask & kRectStaysRect_Mask) != 0;
216 }
217
239bool preservesAxisAlignment() const {
240return this->rectStaysRect();
241 }
242
246static constexpr int kMScaleX = 0;
247static constexpr int kMSkewX = 1;
248static constexpr int kMTransX = 2;
249static constexpr int kMSkewY = 3;
250static constexpr int kMScaleY = 4;
251static constexpr int kMTransY = 5;
252static constexpr int kMPersp0 = 6;
253static constexpr int kMPersp1 = 7;
254static constexpr int kMPersp2 = 8;
255
259static constexpr int kAScaleX = 0;
260static constexpr int kASkewY = 1;
261static constexpr int kASkewX = 2;
262static constexpr int kAScaleY = 3;
263static constexpr int kATransX = 4;
264static constexpr int kATransY = 5;
265
273float operator[](int index) const {
274MNN_ASSERT((unsigned)index < 9);
275return fMat[index];
276 }
277
285float get(int index) const {
286MNN_ASSERT((unsigned)index < 9);
287return fMat[index];
288 }
289
296return fMat[kMScaleX];
297 }
298
305return fMat[kMScaleY];
306 }
307
315return fMat[kMSkewY];
316 }
317
325return fMat[kMSkewX];
326 }
327
333float getTranslateX() const {
334return fMat[kMTransX];
335 }
336
342float getTranslateY() const {
343return fMat[kMTransY];
344 }
345
351return fMat[kMPersp0];
352 }
353
359return fMat[kMPersp1];
360 }
361
372float& operator[](int index) {
373MNN_ASSERT((unsigned)index < 9);
374 this->setTypeMask(kUnknown_Mask);
375return fMat[index];
376 }
377
385void set(int index, float value) {
386MNN_ASSERT((unsigned)index < 9);
387 fMat[index] = value;
388 this->setTypeMask(kUnknown_Mask);
389 }
390
396 this->set(kMScaleX, v);
397 }
398
404 this->set(kMScaleY, v);
405 }
406
412 this->set(kMSkewY, v);
413 }
414
420 this->set(kMSkewX, v);
421 }
422
427void setTranslateX(float v) {
428 this->set(kMTransX, v);
429 }
430
435void setTranslateY(float v) {
436 this->set(kMTransY, v);
437 }
438
445 this->set(kMPersp0, v);
446 }
447
454 this->set(kMPersp1, v);
455 }
456
473void setAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float persp0,
474float 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
493void get9(float buffer[9]) const {
494 memcpy(buffer, fMat, 9 * sizeof(float));
495 }
496
513void set9(const float buffer[9]);
514
524void reset();
525
535void setIdentity() {
536 this->reset();
537 }
538
544void setTranslate(float dx, float dy);
545
554void setScale(float sx, float sy, float px, float py);
555
561void setScale(float sx, float sy);
562
572void setRotate(float degrees, float px, float py);
573
579void setRotate(float degrees);
580
592void setSinCos(float sinValue, float cosValue, float px, float py);
593
602void setSinCos(float sinValue, float cosValue);
603
612void setSkew(float kx, float ky, float px, float py);
613
619void setSkew(float kx, float ky);
620
638void setConcat(const Matrix& a, const Matrix& b);
639
658void preTranslate(float dx, float dy);
659
686void preScale(float sx, float sy, float px, float py);
687
707void preScale(float sx, float sy);
708
738void preRotate(float degrees, float px, float py);
739
765void preRotate(float degrees);
766
793void preSkew(float kx, float ky, float px, float py);
794
814void preSkew(float kx, float ky);
815
833void preConcat(const Matrix& other);
834
853void postTranslate(float dx, float dy);
854
881void postScale(float sx, float sy, float px, float py);
882
902void postScale(float sx, float sy);
903
930bool postIDiv(int divx, int divy);
931
961void postRotate(float degrees, float px, float py);
962
988void postRotate(float degrees);
989
1016void postSkew(float kx, float ky, float px, float py);
1017
1037void postSkew(float kx, float ky);
1038
1056void postConcat(const Matrix& other);
1057
1064enum ScaleToFit {
1069 };
1070
1086bool setRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf);
1087
1103static Matrix MakeRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf) {
1104Matrix m;
1105 m.setRectToRect(src, dst, stf);
1106return m;
1107 }
1108
1122bool setPolyToPoly(const Point src[], const Point dst[], int count);
1123
1132bool invert(Matrix* inverse) const {
1133// Allow the trivial case to be inlined.
1134if (this->isIdentity()) {
1135if (inverse) {
1136 inverse->reset();
1137 }
1138return true;
1139 }
1140return this->invertNonIdentity(inverse);
1141 }
1142
1153static void SetAffineIdentity(float affine[6]);
1154
1165bool asAffine(float affine[6]) const;
1166
1181void setAffine(const float affine[6]);
1182
1209void mapPoints(Point dst[], const Point src[], int count) const {
1210MNN_ASSERT((dst && src && count > 0) || 0 == count);
1211// no partial overlap
1212MNN_ASSERT(src == dst || &dst[count] <= &src[0] || &src[count] <= &dst[0]);
1213 this->getMapPtsProc()(*this, dst, src, count);
1214 }
1215
1239void mapPoints(Point pts[], int count) const {
1240 this->mapPoints(pts, pts, count);
1241 }
1242
1259void mapXY(float x, float y, Point* result) const {
1260 this->getMapXYProc()(*this, x, y, result);
1261 }
1262
1279Point mapXY(float x, float y) const {
1280Point result;
1281 this->getMapXYProc()(*this, x, y, &result);
1282return result;
1283 }
1284
1294bool mapRect(Rect* dst, const Rect& src) const;
1295
1304bool mapRect(Rect* rect) const {
1305return this->mapRect(rect, *rect);
1306 }
1307
1313Rect mapRect(const Rect& src) const {
1314Rect dst;
1315 (void)this->mapRect(&dst, src);
1316return dst;
1317 }
1318
1326void mapRectScaleTranslate(Rect* dst, const Rect& src) const;
1327
1341bool cheapEqualTo(const Matrix& m) const {
1342return 0 == memcmp(fMat, m.fMat, sizeof(fMat));
1343 }
1344
1353friend MNN_PUBLIC bool operator==(const Matrix& a, const Matrix& b);
1354
1363friend MNN_PUBLIC bool operator!=(const Matrix& a, const Matrix& b) {
1364return !(a == b);
1365 }
1366
1371void dump() const;
1372
1379float getMinScale() const;
1380
1387float getMaxScale() const;
1388
1399bool getMinMaxScales(float scaleFactors[2]) const;
1400
1409static const Matrix& I();
1410
1420static const Matrix& InvalidMatrix();
1421
1440static Matrix Concat(const Matrix& a, const Matrix& b) {
1441Matrix result;
1442 result.setConcat(a, b);
1443return result;
1444 }
1445
1449void dirtyMatrixTypeCache() {
1450 this->setTypeMask(kUnknown_Mask);
1451 }
1452
1464void 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
1477unsigned mask = 0;
1478if (sx != 1 || sy != 1) {
1479 mask |= kScale_Mask;
1480 }
1481if (tx || ty) {
1482 mask |= kTranslate_Mask;
1483 }
1484 this->setTypeMask(mask | kRectStaysRect_Mask);
1485 }
1486
1493 private:
1500static constexpr int kRectStaysRect_Mask = 0x10;
1501
1505static constexpr int kOnlyPerspectiveValid_Mask = 0x40;
1506
1507static constexpr int kUnknown_Mask = 0x80;
1508
1509static constexpr int kORableMasks = kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask;
1510
1511static constexpr int kAllMasks =
1512 kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask | kRectStaysRect_Mask;
1513
1514float fMat[9];
1515mutable uint32_t fTypeMask;
1516
1517static 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
1522void setTypeMask(int mask) {
1523// allow kUnknown or a valid mask
1524MNN_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
1530void orTypeMask(int mask) {
1531MNN_ASSERT((mask & kORableMasks) == mask);
1532 fTypeMask = (uint8_t)(fTypeMask | mask);
1533 }
1534
1535void clearTypeMask(int mask) {
1536// only allow a valid mask
1537MNN_ASSERT((mask & kAllMasks) == mask);
1538 fTypeMask = fTypeMask & ~mask;
1539 }
1540
1541 TypeMask getPerspectiveTypeMaskOnly() const {
1542if ((fTypeMask & kUnknown_Mask) && !(fTypeMask & kOnlyPerspectiveValid_Mask)) {
1543 fTypeMask = this->computePerspectiveTypeMask();
1544 }
1545return (TypeMask)(fTypeMask & 0xF);
1546 }
1547
1551bool isTriviallyIdentity() const {
1552if (fTypeMask & kUnknown_Mask) {
1553return false;
1554 }
1555return ((fTypeMask & 0xF) == 0);
1556 }
1557
1558inline void updateTranslateMask() {
1559if ((fMat[kMTransX] != 0) | (fMat[kMTransY] != 0)) {
1560 fTypeMask |= kTranslate_Mask;
1561 } else {
1562 fTypeMask &= ~kTranslate_Mask;
1563 }
1564 }
1565
1566typedef void (*MapXYProc)(const Matrix& mat, float x, float y, Point* result);
1567
1568static MapXYProc GetMapXYProc(TypeMask mask) {
1569MNN_ASSERT((mask & ~kAllMasks) == 0);
1570return gMapXYProcs[mask & kAllMasks];
1571 }
1572
1573 MapXYProc getMapXYProc() const {
1574return GetMapXYProc(this->getType());
1575 }
1576
1577typedef void (*MapPtsProc)(const Matrix& mat, Point dst[], const Point src[], int count);
1578
1579static MapPtsProc GetMapPtsProc(TypeMask mask) {
1580MNN_ASSERT((mask & ~kAllMasks) == 0);
1581return gMapPtsProcs[mask & kAllMasks];
1582 }
1583
1584 MapPtsProc getMapPtsProc() const {
1585return GetMapPtsProc(this->getType());
1586 }
1587
1588bool invertNonIdentity(Matrix* inverse) const;
1589
1590static void Identity_xy(const Matrix&, float, float, Point*);
1591static void Trans_xy(const Matrix&, float, float, Point*);
1592static void Scale_xy(const Matrix&, float, float, Point*);
1593static void ScaleTrans_xy(const Matrix&, float, float, Point*);
1594static void Rot_xy(const Matrix&, float, float, Point*);
1595static void RotTrans_xy(const Matrix&, float, float, Point*);
1596static void Persp_xy(const Matrix&, float, float, Point*);
1597
1598static const MapXYProc gMapXYProcs[];
1599
1600static void Identity_pts(const Matrix&, Point[], const Point[], int);
1601static void Trans_pts(const Matrix&, Point dst[], const Point[], int);
1602static void Scale_pts(const Matrix&, Point dst[], const Point[], int);
1603static void ScaleTrans_pts(const Matrix&, Point dst[], const Point[], int count);
1604static void Persp_pts(const Matrix&, Point dst[], const Point[], int);
1605
1606static void Affine_vpts(const Matrix&, Point dst[], const Point[], int);
1607
1608static 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
MNN::CV::Matrix::getTranslateX
float getTranslateX() const
Definition: Matrix.h:333
bool isIdentity() const
Definition: Matrix.h:161
MNN::CV::Matrix::setScaleTranslate
void setScaleTranslate(float sx, float sy, float tx, float ty)
Definition: Matrix.h:1464
MNN::CV::Matrix::isScaleTranslate
bool isScaleTranslate() const
Definition: Matrix.h:174
#define MNN_ASSERT(x)
Definition: MNNDefine.h:41
Definition: Rect.h:54
MNN::CV::Matrix::setTranslateX
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
MNN::CV::Matrix::kFill_ScaleToFit
scales in x and y to fill destination Rect
Definition: Matrix.h:1065
MNN::CV::Matrix::MakeRectToRect
static Matrix MakeRectToRect(const Rect &src, const Rect &dst, ScaleToFit stf)
Definition: Matrix.h:1103
MNN::CV::Matrix::setTranslateY
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
MNN::CV::Matrix::kCenter_ScaleToFit
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
MNN::CV::Matrix::dirtyMatrixTypeCache
void dirtyMatrixTypeCache()
Definition: Matrix.h:1449
float operator[](int index) const
Definition: Matrix.h:273
MNN::CV::Matrix::kEnd_ScaleToFit
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
MNN::CV::Matrix::getTranslateY
float getTranslateY() const
Definition: Matrix.h:342
Definition: AutoTime.hpp:16
MNN::CV::Matrix::setRectToRect
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
void reset()
MNN::CV::Matrix::rectStaysRect
bool rectStaysRect() const
Definition: Matrix.h:211
void setScale(float sx, float sy, float px, float py)
MNN::CV::Matrix::preservesAxisAlignment
bool preservesAxisAlignment() const
Definition: Matrix.h:239
MNN::CV::Matrix::kStart_ScaleToFit
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