Matrix.h
浏览该文件的文档.
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Generated by tools/bookmaker from include/core/Matrix.h and docs/SkMatrix_Reference.bmh
on 2018-07-13 08:15:11. Additional documentation and examples can be found at:
https://skia.org/user/api/SkMatrix_Reference
You may edit either file directly. Structural changes to public interfaces require
editing both files. After editing docs/SkMatrix_Reference.bmh, run:
bookmaker -b docs -i include/core/Matrix.h -p
to create an updated version of this file.
*/
//
// Modified by jiangxiaotang on 2018/09/19.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef SkMatrix_DEFINED
#define SkMatrix_DEFINED
#include <string.h>
#include <cstdint>
#include "Rect.h"
namespace MNN {
namespace CV {
class MNN_PUBLIC Matrix {
public:
Matrix() {
setIdentity();
}
static Matrix MakeScale(float sx, float sy) {
Matrix m;
m.setScale(sx, sy);
return m;
}
static Matrix MakeScale(float scale) {
Matrix m;
m.setScale(scale, scale);
return m;
}
static Matrix MakeTrans(float dx, float dy) {
Matrix m;
m.setTranslate(dx, dy);
return m;
}
static Matrix MakeAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float pers0,
float pers1, float pers2) {
Matrix m;
m.setAll(scaleX, skewX, transX, skewY, scaleY, transY, pers0, pers1, pers2);
return m;
}
enum TypeMask {
kIdentity_Mask = 0,
kTranslate_Mask = 0x01,
kScale_Mask = 0x02,
kAffine_Mask = 0x04,
kPerspective_Mask = 0x08,
};
TypeMask getType() const {
if (fTypeMask & kUnknown_Mask) {
fTypeMask = this->computeTypeMask();
}
// only return the public masks
return (TypeMask)(fTypeMask & 0xF);
}
bool isIdentity() const {
return this->getType() == 0;
}
bool isScaleTranslate() const {
return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
}
bool isTranslate() const {
return !(this->getType() & ~(kTranslate_Mask));
}
bool rectStaysRect() const {
if (fTypeMask & kUnknown_Mask) {
fTypeMask = this->computeTypeMask();
}
return (fTypeMask & kRectStaysRect_Mask) != 0;
}
bool preservesAxisAlignment() const {
return this->rectStaysRect();
}
static constexpr int kMScaleX = 0;
static constexpr int kMSkewX = 1;
static constexpr int kMTransX = 2;
static constexpr int kMSkewY = 3;
static constexpr int kMScaleY = 4;
static constexpr int kMTransY = 5;
static constexpr int kMPersp0 = 6;
static constexpr int kMPersp1 = 7;
static constexpr int kMPersp2 = 8;
static constexpr int kAScaleX = 0;
static constexpr int kASkewY = 1;
static constexpr int kASkewX = 2;
static constexpr int kAScaleY = 3;
static constexpr int kATransX = 4;
static constexpr int kATransY = 5;
float operator[](int index) const {
MNN_ASSERT((unsigned)index < 9);
return fMat[index];
}
float get(int index) const {
MNN_ASSERT((unsigned)index < 9);
return fMat[index];
}
float getScaleX() const {
return fMat[kMScaleX];
}
float getScaleY() const {
return fMat[kMScaleY];
}
float getSkewY() const {
return fMat[kMSkewY];
}
float getSkewX() const {
return fMat[kMSkewX];
}
float getTranslateX() const {
return fMat[kMTransX];
}
float getTranslateY() const {
return fMat[kMTransY];
}
float getPerspX() const {
return fMat[kMPersp0];
}
float getPerspY() const {
return fMat[kMPersp1];
}
float& operator[](int index) {
MNN_ASSERT((unsigned)index < 9);
this->setTypeMask(kUnknown_Mask);
return fMat[index];
}
void set(int index, float value) {
MNN_ASSERT((unsigned)index < 9);
fMat[index] = value;
this->setTypeMask(kUnknown_Mask);
}
void setScaleX(float v) {
this->set(kMScaleX, v);
}
void setScaleY(float v) {
this->set(kMScaleY, v);
}
void setSkewY(float v) {
this->set(kMSkewY, v);
}
void setSkewX(float v) {
this->set(kMSkewX, v);
}
void setTranslateX(float v) {
this->set(kMTransX, v);
}
void setTranslateY(float v) {
this->set(kMTransY, v);
}
void setPerspX(float v) {
this->set(kMPersp0, v);
}
void setPerspY(float v) {
this->set(kMPersp1, v);
}
void setAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float persp0,
float persp1, float persp2) {
fMat[kMScaleX] = scaleX;
fMat[kMSkewX] = skewX;
fMat[kMTransX] = transX;
fMat[kMSkewY] = skewY;
fMat[kMScaleY] = scaleY;
fMat[kMTransY] = transY;
fMat[kMPersp0] = persp0;
fMat[kMPersp1] = persp1;
fMat[kMPersp2] = persp2;
this->setTypeMask(kUnknown_Mask);
}
void get9(float buffer[9]) const {
memcpy(buffer, fMat, 9 * sizeof(float));
}
void set9(const float buffer[9]);
void reset();
void setIdentity() {
this->reset();
}
void setTranslate(float dx, float dy);
void setScale(float sx, float sy, float px, float py);
void setScale(float sx, float sy);
void setRotate(float degrees, float px, float py);
void setRotate(float degrees);
void setSinCos(float sinValue, float cosValue, float px, float py);
void setSinCos(float sinValue, float cosValue);
void setSkew(float kx, float ky, float px, float py);
void setSkew(float kx, float ky);
void setConcat(const Matrix& a, const Matrix& b);
void preTranslate(float dx, float dy);
void preScale(float sx, float sy, float px, float py);
void preScale(float sx, float sy);
void preRotate(float degrees, float px, float py);
void preRotate(float degrees);
void preSkew(float kx, float ky, float px, float py);
void preSkew(float kx, float ky);
void preConcat(const Matrix& other);
void postTranslate(float dx, float dy);
void postScale(float sx, float sy, float px, float py);
void postScale(float sx, float sy);
bool postIDiv(int divx, int divy);
void postRotate(float degrees, float px, float py);
void postRotate(float degrees);
void postSkew(float kx, float ky, float px, float py);
void postSkew(float kx, float ky);
void postConcat(const Matrix& other);
enum ScaleToFit {
kFill_ScaleToFit,
kStart_ScaleToFit,
kCenter_ScaleToFit,
kEnd_ScaleToFit,
};
bool setRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf);
static Matrix MakeRectToRect(const Rect& src, const Rect& dst, ScaleToFit stf) {
Matrix m;
m.setRectToRect(src, dst, stf);
return m;
}
bool setPolyToPoly(const Point src[], const Point dst[], int count);
bool invert(Matrix* inverse) const {
// Allow the trivial case to be inlined.
if (this->isIdentity()) {
if (inverse) {
inverse->reset();
}
return true;
}
return this->invertNonIdentity(inverse);
}
static void SetAffineIdentity(float affine[6]);
bool asAffine(float affine[6]) const;
void setAffine(const float affine[6]);
void mapPoints(Point dst[], const Point src[], int count) const {
MNN_ASSERT((dst && src && count > 0) || 0 == count);
// no partial overlap
MNN_ASSERT(src == dst || &dst[count] <= &src[0] || &src[count] <= &dst[0]);
this->getMapPtsProc()(*this, dst, src, count);
}
void mapPoints(Point pts[], int count) const {
this->mapPoints(pts, pts, count);
}
void mapXY(float x, float y, Point* result) const {
this->getMapXYProc()(*this, x, y, result);
}
Point mapXY(float x, float y) const {
Point result;
this->getMapXYProc()(*this, x, y, &result);
return result;
}
bool mapRect(Rect* dst, const Rect& src) const;
bool mapRect(Rect* rect) const {
return this->mapRect(rect, *rect);
}
Rect mapRect(const Rect& src) const {
Rect dst;
(void)this->mapRect(&dst, src);
return dst;
}
void mapRectScaleTranslate(Rect* dst, const Rect& src) const;
bool cheapEqualTo(const Matrix& m) const {
return 0 == memcmp(fMat, m.fMat, sizeof(fMat));
}
friend MNN_PUBLIC bool operator==(const Matrix& a, const Matrix& b);
friend MNN_PUBLIC bool operator!=(const Matrix& a, const Matrix& b) {
return !(a == b);
}
void dump() const;
float getMinScale() const;
float getMaxScale() const;
bool getMinMaxScales(float scaleFactors[2]) const;
static const Matrix& I();
static const Matrix& InvalidMatrix();
static Matrix Concat(const Matrix& a, const Matrix& b) {
Matrix result;
result.setConcat(a, b);
return result;
}
void dirtyMatrixTypeCache() {
this->setTypeMask(kUnknown_Mask);
}
void setScaleTranslate(float sx, float sy, float tx, float ty) {
fMat[kMScaleX] = sx;
fMat[kMSkewX] = 0;
fMat[kMTransX] = tx;
fMat[kMSkewY] = 0;
fMat[kMScaleY] = sy;
fMat[kMTransY] = ty;
fMat[kMPersp0] = 0;
fMat[kMPersp1] = 0;
fMat[kMPersp2] = 1;
unsigned mask = 0;
if (sx != 1 || sy != 1) {
mask |= kScale_Mask;
}
if (tx || ty) {
mask |= kTranslate_Mask;
}
this->setTypeMask(mask | kRectStaysRect_Mask);
}
private:
static constexpr int kRectStaysRect_Mask = 0x10;
static constexpr int kOnlyPerspectiveValid_Mask = 0x40;
static constexpr int kUnknown_Mask = 0x80;
static constexpr int kORableMasks = kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask;
static constexpr int kAllMasks =
kTranslate_Mask | kScale_Mask | kAffine_Mask | kPerspective_Mask | kRectStaysRect_Mask;
float fMat[9];
mutable uint32_t fTypeMask;
static void ComputeInv(float dst[9], const float src[9], double invDet, bool isPersp);
uint8_t computeTypeMask() const;
uint8_t computePerspectiveTypeMask() const;
void setTypeMask(int mask) {
// allow kUnknown or a valid mask
MNN_ASSERT(kUnknown_Mask == mask || (mask & kAllMasks) == mask ||
((kUnknown_Mask | kOnlyPerspectiveValid_Mask) & mask) ==
(kUnknown_Mask | kOnlyPerspectiveValid_Mask));
fTypeMask = (uint8_t)(mask);
}
void orTypeMask(int mask) {
MNN_ASSERT((mask & kORableMasks) == mask);
fTypeMask = (uint8_t)(fTypeMask | mask);
}
void clearTypeMask(int mask) {
// only allow a valid mask
MNN_ASSERT((mask & kAllMasks) == mask);
fTypeMask = fTypeMask & ~mask;
}
TypeMask getPerspectiveTypeMaskOnly() const {
if ((fTypeMask & kUnknown_Mask) && !(fTypeMask & kOnlyPerspectiveValid_Mask)) {
fTypeMask = this->computePerspectiveTypeMask();
}
return (TypeMask)(fTypeMask & 0xF);
}
bool isTriviallyIdentity() const {
if (fTypeMask & kUnknown_Mask) {
return false;
}
return ((fTypeMask & 0xF) == 0);
}
inline void updateTranslateMask() {
if ((fMat[kMTransX] != 0) | (fMat[kMTransY] != 0)) {
fTypeMask |= kTranslate_Mask;
} else {
fTypeMask &= ~kTranslate_Mask;
}
}
typedef void (*MapXYProc)(const Matrix& mat, float x, float y, Point* result);
static MapXYProc GetMapXYProc(TypeMask mask) {
MNN_ASSERT((mask & ~kAllMasks) == 0);
return gMapXYProcs[mask & kAllMasks];
}
MapXYProc getMapXYProc() const {
return GetMapXYProc(this->getType());
}
typedef void (*MapPtsProc)(const Matrix& mat, Point dst[], const Point src[], int count);
static MapPtsProc GetMapPtsProc(TypeMask mask) {
MNN_ASSERT((mask & ~kAllMasks) == 0);
return gMapPtsProcs[mask & kAllMasks];
}
MapPtsProc getMapPtsProc() const {
return GetMapPtsProc(this->getType());
}
bool invertNonIdentity(Matrix* inverse) const;
static void Identity_xy(const Matrix&, float, float, Point*);
static void Trans_xy(const Matrix&, float, float, Point*);
static void Scale_xy(const Matrix&, float, float, Point*);
static void ScaleTrans_xy(const Matrix&, float, float, Point*);
static void Rot_xy(const Matrix&, float, float, Point*);
static void RotTrans_xy(const Matrix&, float, float, Point*);
static void Persp_xy(const Matrix&, float, float, Point*);
static const MapXYProc gMapXYProcs[];
static void Identity_pts(const Matrix&, Point[], const Point[], int);
static void Trans_pts(const Matrix&, Point dst[], const Point[], int);
static void Scale_pts(const Matrix&, Point dst[], const Point[], int);
static void ScaleTrans_pts(const Matrix&, Point dst[], const Point[], int count);
static void Persp_pts(const Matrix&, Point dst[], const Point[], int);
static void Affine_vpts(const Matrix&, Point dst[], const Point[], int);
static const MapPtsProc gMapPtsProcs[];
};
} // namespace CV
} // namespace MNN
#endif
MNN::CV::Matrix::get
float get(int index) const
Definition: Matrix.h:285
MNN::CV::Matrix::operator[]
float & operator[](int index)
Definition: Matrix.h:372
MNN::CV::Matrix::setIdentity
void setIdentity()
Definition: Matrix.h:535
MNN::CV::Matrix::getScaleX
float getScaleX() const
Definition: Matrix.h:295
MNN::CV::Matrix::ScaleToFit
ScaleToFit
Definition: Matrix.h:1064
MNN::CV::Matrix::mapXY
Point mapXY(float x, float y) const
Definition: Matrix.h:1279
MNN::CV::Matrix::MakeTrans
static Matrix MakeTrans(float dx, float dy)
Definition: Matrix.h:95
MNN::CV::Matrix::getTranslateX
float getTranslateX() const
Definition: Matrix.h:333
MNN::CV::Matrix::isIdentity
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
MNN_ASSERT
#define MNN_ASSERT(x)
Definition: MNNDefine.h:41
MNN::CV::Rect
Definition: Rect.h:54
MNN::CV::Matrix::setTranslateX
void setTranslateX(float v)
Definition: Matrix.h:427
MNN::CV::Matrix::getSkewX
float getSkewX() const
Definition: Matrix.h:324
MNN::CV::Matrix::setScaleX
void setScaleX(float v)
Definition: Matrix.h:395
MNN::CV::Matrix::setSkewX
void setSkewX(float v)
Definition: Matrix.h:419
MNN::CV::Matrix::setPerspY
void setPerspY(float v)
Definition: Matrix.h:453
MNN::CV::Matrix::getSkewY
float getSkewY() const
Definition: Matrix.h:314
MNN::CV::Matrix::getType
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
MNN::CV::Matrix::MakeScale
static Matrix MakeScale(float scale)
Definition: Matrix.h:79
MNN::CV::Matrix::mapXY
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
MNN::CV::Matrix::setSkewY
void setSkewY(float v)
Definition: Matrix.h:411
MNN::CV::Matrix::isTranslate
bool isTranslate() const
Definition: Matrix.h:186
MNN::CV::Matrix::getPerspX
float getPerspX() const
Definition: Matrix.h:350
MNN::CV::Matrix::dirtyMatrixTypeCache
void dirtyMatrixTypeCache()
Definition: Matrix.h:1449
MNN::CV::Matrix::operator[]
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
MNN::CV::Matrix::invert
bool invert(Matrix *inverse) const
Definition: Matrix.h:1132
MNN::CV::Matrix::Matrix
Matrix()
Definition: Matrix.h:50
MNN::CV::Matrix::mapRect
bool mapRect(Rect *rect) const
Definition: Matrix.h:1304
MNN::CV::Matrix::setTranslate
void setTranslate(float dx, float dy)
MNN_PUBLIC
#define MNN_PUBLIC
Definition: MNNDefine.h:53
MNN::CV::Matrix::setPerspX
void setPerspX(float v)
Definition: Matrix.h:444
MNN::CV::Matrix::MakeScale
static Matrix MakeScale(float sx, float sy)
Definition: Matrix.h:64
MNN::CV::Matrix::mapRect
Rect mapRect(const Rect &src) const
Definition: Matrix.h:1313
MNN::CV::Matrix
Definition: Matrix.h:48
MNN::CV::Matrix::set
void set(int index, float value)
Definition: Matrix.h:385
MNN::CV::Matrix::MakeAll
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
MNN
Definition: AutoTime.hpp:16
MNN::CV::Matrix::setRectToRect
bool setRectToRect(const Rect &src, const Rect &dst, ScaleToFit stf)
MNN::CV::Matrix::setConcat
void setConcat(const Matrix &a, const Matrix &b)
MNN::CV::Matrix::getScaleY
float getScaleY() const
Definition: Matrix.h:304
MNN::CV::Matrix::mapPoints
void mapPoints(Point dst[], const Point src[], int count) const
Definition: Matrix.h:1209
MNN::CV::Matrix::mapPoints
void mapPoints(Point pts[], int count) const
Definition: Matrix.h:1239
MNN::CV::Point
Definition: Rect.h:37
MNN::CV::Matrix::setAll
void setAll(float scaleX, float skewX, float transX, float skewY, float scaleY, float transY, float persp0, float persp1, float persp2)
Definition: Matrix.h:473
MNN::CV::Matrix::reset
void reset()
MNN::CV::Matrix::rectStaysRect
bool rectStaysRect() const
Definition: Matrix.h:211
MNN::CV::Matrix::setScale
void setScale(float sx, float sy, float px, float py)
Rect.h
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
MNN::CV::Matrix::Concat
static Matrix Concat(const Matrix &a, const Matrix &b)
Definition: Matrix.h:1440
MNN::CV::Matrix::setScaleY
void setScaleY(float v)
Definition: Matrix.h:403
MNN::CV::Matrix::cheapEqualTo
bool cheapEqualTo(const Matrix &m) const
Definition: Matrix.h:1341
MNN::CV::Matrix::get9
void get9(float buffer[9]) const
Definition: Matrix.h:493
MNN::CV::Matrix::TypeMask
TypeMask
Definition: Matrix.h:129
MNN::CV::Matrix::operator!=
friend MNN_PUBLIC bool operator!=(const Matrix &a, const Matrix &b)
Definition: Matrix.h:1363
MNN::CV::Matrix::getPerspY
float getPerspY() const
Definition: Matrix.h:358