SkMatrix44.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #ifndef SkMatrix44_DEFINED
  8. #define SkMatrix44_DEFINED
  9. #include "SkMatrix.h"
  10. #include "SkScalar.h"
  11. #include <atomic>
  12. #include <cstring>
  13. #ifdef SK_MSCALAR_IS_DOUBLE
  14. #ifdef SK_MSCALAR_IS_FLOAT
  15. #error "can't define MSCALAR both as DOUBLE and FLOAT"
  16. #endif
  17. typedef double SkMScalar;
  18. static inline double SkFloatToMScalar(float x) {
  19. return static_cast<double>(x);
  20. }
  21. static inline float SkMScalarToFloat(double x) {
  22. return static_cast<float>(x);
  23. }
  24. static inline double SkDoubleToMScalar(double x) {
  25. return x;
  26. }
  27. static inline double SkMScalarToDouble(double x) {
  28. return x;
  29. }
  30. static inline double SkMScalarAbs(double x) {
  31. return fabs(x);
  32. }
  33. static const SkMScalar SK_MScalarPI = 3.141592653589793;
  34. #define SkMScalarFloor(x) sk_double_floor(x)
  35. #define SkMScalarCeil(x) sk_double_ceil(x)
  36. #define SkMScalarRound(x) sk_double_round(x)
  37. #define SkMScalarFloorToInt(x) sk_double_floor2int(x)
  38. #define SkMScalarCeilToInt(x) sk_double_ceil2int(x)
  39. #define SkMScalarRoundToInt(x) sk_double_round2int(x)
  40. #elif defined SK_MSCALAR_IS_FLOAT
  41. #ifdef SK_MSCALAR_IS_DOUBLE
  42. #error "can't define MSCALAR both as DOUBLE and FLOAT"
  43. #endif
  44. typedef float SkMScalar;
  45. static inline float SkFloatToMScalar(float x) {
  46. return x;
  47. }
  48. static inline float SkMScalarToFloat(float x) {
  49. return x;
  50. }
  51. static inline float SkDoubleToMScalar(double x) {
  52. return sk_double_to_float(x);
  53. }
  54. static inline double SkMScalarToDouble(float x) {
  55. return static_cast<double>(x);
  56. }
  57. static inline float SkMScalarAbs(float x) {
  58. return sk_float_abs(x);
  59. }
  60. static const SkMScalar SK_MScalarPI = 3.14159265f;
  61. #define SkMScalarFloor(x) sk_float_floor(x)
  62. #define SkMScalarCeil(x) sk_float_ceil(x)
  63. #define SkMScalarRound(x) sk_float_round(x)
  64. #define SkMScalarFloorToInt(x) sk_float_floor2int(x)
  65. #define SkMScalarCeilToInt(x) sk_float_ceil2int(x)
  66. #define SkMScalarRoundToInt(x) sk_float_round2int(x)
  67. #endif
  68. #define SkIntToMScalar(n) static_cast<SkMScalar>(n)
  69. #define SkMScalarToScalar(x) SkMScalarToFloat(x)
  70. #define SkScalarToMScalar(x) SkFloatToMScalar(x)
  71. static const SkMScalar SK_MScalar1 = 1;
  72. ///////////////////////////////////////////////////////////////////////////////
  73. struct SkVector4 {
  74. SkScalar fData[4];
  75. SkVector4() {
  76. this->set(0, 0, 0, 1);
  77. }
  78. SkVector4(const SkVector4& src) {
  79. memcpy(fData, src.fData, sizeof(fData));
  80. }
  81. SkVector4(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
  82. fData[0] = x;
  83. fData[1] = y;
  84. fData[2] = z;
  85. fData[3] = w;
  86. }
  87. SkVector4& operator=(const SkVector4& src) {
  88. memcpy(fData, src.fData, sizeof(fData));
  89. return *this;
  90. }
  91. bool operator==(const SkVector4& v) {
  92. return fData[0] == v.fData[0] && fData[1] == v.fData[1] &&
  93. fData[2] == v.fData[2] && fData[3] == v.fData[3];
  94. }
  95. bool operator!=(const SkVector4& v) {
  96. return !(*this == v);
  97. }
  98. bool equals(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
  99. return fData[0] == x && fData[1] == y &&
  100. fData[2] == z && fData[3] == w;
  101. }
  102. void set(SkScalar x, SkScalar y, SkScalar z, SkScalar w = SK_Scalar1) {
  103. fData[0] = x;
  104. fData[1] = y;
  105. fData[2] = z;
  106. fData[3] = w;
  107. }
  108. };
  109. /** \class SkMatrix44
  110. The SkMatrix44 class holds a 4x4 matrix.
  111. SkMatrix44 is not thread safe unless you've first called SkMatrix44::getType().
  112. */
  113. class SK_API SkMatrix44 {
  114. public:
  115. enum Uninitialized_Constructor {
  116. kUninitialized_Constructor
  117. };
  118. enum Identity_Constructor {
  119. kIdentity_Constructor
  120. };
  121. SkMatrix44(Uninitialized_Constructor) {} // ironically, cannot be constexpr
  122. constexpr SkMatrix44(Identity_Constructor)
  123. : fMat{{ 1, 0, 0, 0, },
  124. { 0, 1, 0, 0, },
  125. { 0, 0, 1, 0, },
  126. { 0, 0, 0, 1, }}
  127. , fTypeMask(kIdentity_Mask)
  128. {}
  129. constexpr SkMatrix44() : SkMatrix44{kIdentity_Constructor} {}
  130. SkMatrix44(const SkMatrix44& src) {
  131. memcpy(fMat, src.fMat, sizeof(fMat));
  132. fTypeMask.store(src.fTypeMask, std::memory_order_relaxed);
  133. }
  134. SkMatrix44(const SkMatrix44& a, const SkMatrix44& b) {
  135. this->setConcat(a, b);
  136. }
  137. SkMatrix44& operator=(const SkMatrix44& src) {
  138. if (&src != this) {
  139. memcpy(fMat, src.fMat, sizeof(fMat));
  140. fTypeMask.store(src.fTypeMask, std::memory_order_relaxed);
  141. }
  142. return *this;
  143. }
  144. bool operator==(const SkMatrix44& other) const;
  145. bool operator!=(const SkMatrix44& other) const {
  146. return !(other == *this);
  147. }
  148. /* When converting from SkMatrix44 to SkMatrix, the third row and
  149. * column is dropped. When converting from SkMatrix to SkMatrix44
  150. * the third row and column remain as identity:
  151. * [ a b c ] [ a b 0 c ]
  152. * [ d e f ] -> [ d e 0 f ]
  153. * [ g h i ] [ 0 0 1 0 ]
  154. * [ g h 0 i ]
  155. */
  156. SkMatrix44(const SkMatrix&);
  157. SkMatrix44& operator=(const SkMatrix& src);
  158. operator SkMatrix() const;
  159. /**
  160. * Return a reference to a const identity matrix
  161. */
  162. static const SkMatrix44& I();
  163. enum TypeMask {
  164. kIdentity_Mask = 0,
  165. kTranslate_Mask = 0x01, //!< set if the matrix has translation
  166. kScale_Mask = 0x02, //!< set if the matrix has any scale != 1
  167. kAffine_Mask = 0x04, //!< set if the matrix skews or rotates
  168. kPerspective_Mask = 0x08 //!< set if the matrix is in perspective
  169. };
  170. /**
  171. * Returns a bitfield describing the transformations the matrix may
  172. * perform. The bitfield is computed conservatively, so it may include
  173. * false positives. For example, when kPerspective_Mask is true, all
  174. * other bits may be set to true even in the case of a pure perspective
  175. * transform.
  176. */
  177. inline TypeMask getType() const {
  178. if (fTypeMask.load(std::memory_order_relaxed) & kUnknown_Mask) {
  179. fTypeMask.store(this->computeTypeMask(), std::memory_order_relaxed);
  180. }
  181. SkASSERT(!(fTypeMask & kUnknown_Mask));
  182. return (TypeMask)fTypeMask.load(std::memory_order_relaxed);
  183. }
  184. /**
  185. * Return true if the matrix is identity.
  186. */
  187. inline bool isIdentity() const {
  188. return kIdentity_Mask == this->getType();
  189. }
  190. /**
  191. * Return true if the matrix contains translate or is identity.
  192. */
  193. inline bool isTranslate() const {
  194. return !(this->getType() & ~kTranslate_Mask);
  195. }
  196. /**
  197. * Return true if the matrix only contains scale or translate or is identity.
  198. */
  199. inline bool isScaleTranslate() const {
  200. return !(this->getType() & ~(kScale_Mask | kTranslate_Mask));
  201. }
  202. /**
  203. * Returns true if the matrix only contains scale or is identity.
  204. */
  205. inline bool isScale() const {
  206. return !(this->getType() & ~kScale_Mask);
  207. }
  208. inline bool hasPerspective() const {
  209. return SkToBool(this->getType() & kPerspective_Mask);
  210. }
  211. void setIdentity();
  212. inline void reset() { this->setIdentity();}
  213. /**
  214. * get a value from the matrix. The row,col parameters work as follows:
  215. * (0, 0) scale-x
  216. * (0, 3) translate-x
  217. * (3, 0) perspective-x
  218. */
  219. inline SkMScalar get(int row, int col) const {
  220. SkASSERT((unsigned)row <= 3);
  221. SkASSERT((unsigned)col <= 3);
  222. return fMat[col][row];
  223. }
  224. /**
  225. * set a value in the matrix. The row,col parameters work as follows:
  226. * (0, 0) scale-x
  227. * (0, 3) translate-x
  228. * (3, 0) perspective-x
  229. */
  230. inline void set(int row, int col, SkMScalar value) {
  231. SkASSERT((unsigned)row <= 3);
  232. SkASSERT((unsigned)col <= 3);
  233. fMat[col][row] = value;
  234. this->dirtyTypeMask();
  235. }
  236. inline double getDouble(int row, int col) const {
  237. return SkMScalarToDouble(this->get(row, col));
  238. }
  239. inline void setDouble(int row, int col, double value) {
  240. this->set(row, col, SkDoubleToMScalar(value));
  241. }
  242. inline float getFloat(int row, int col) const {
  243. return SkMScalarToFloat(this->get(row, col));
  244. }
  245. inline void setFloat(int row, int col, float value) {
  246. this->set(row, col, SkFloatToMScalar(value));
  247. }
  248. /** These methods allow one to efficiently read matrix entries into an
  249. * array. The given array must have room for exactly 16 entries. Whenever
  250. * possible, they will try to use memcpy rather than an entry-by-entry
  251. * copy.
  252. *
  253. * Col major indicates that consecutive elements of columns will be stored
  254. * contiguously in memory. Row major indicates that consecutive elements
  255. * of rows will be stored contiguously in memory.
  256. */
  257. void asColMajorf(float[]) const;
  258. void asColMajord(double[]) const;
  259. void asRowMajorf(float[]) const;
  260. void asRowMajord(double[]) const;
  261. /** These methods allow one to efficiently set all matrix entries from an
  262. * array. The given array must have room for exactly 16 entries. Whenever
  263. * possible, they will try to use memcpy rather than an entry-by-entry
  264. * copy.
  265. *
  266. * Col major indicates that input memory will be treated as if consecutive
  267. * elements of columns are stored contiguously in memory. Row major
  268. * indicates that input memory will be treated as if consecutive elements
  269. * of rows are stored contiguously in memory.
  270. */
  271. void setColMajorf(const float[]);
  272. void setColMajord(const double[]);
  273. void setRowMajorf(const float[]);
  274. void setRowMajord(const double[]);
  275. #ifdef SK_MSCALAR_IS_FLOAT
  276. void setColMajor(const SkMScalar data[]) { this->setColMajorf(data); }
  277. void setRowMajor(const SkMScalar data[]) { this->setRowMajorf(data); }
  278. #else
  279. void setColMajor(const SkMScalar data[]) { this->setColMajord(data); }
  280. void setRowMajor(const SkMScalar data[]) { this->setRowMajord(data); }
  281. #endif
  282. /* This sets the top-left of the matrix and clears the translation and
  283. * perspective components (with [3][3] set to 1). m_ij is interpreted
  284. * as the matrix entry at row = i, col = j. */
  285. void set3x3(SkMScalar m_00, SkMScalar m_10, SkMScalar m_20,
  286. SkMScalar m_01, SkMScalar m_11, SkMScalar m_21,
  287. SkMScalar m_02, SkMScalar m_12, SkMScalar m_22);
  288. void set3x3RowMajorf(const float[]);
  289. void setTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
  290. void preTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
  291. void postTranslate(SkMScalar dx, SkMScalar dy, SkMScalar dz);
  292. void setScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
  293. void preScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
  294. void postScale(SkMScalar sx, SkMScalar sy, SkMScalar sz);
  295. inline void setScale(SkMScalar scale) {
  296. this->setScale(scale, scale, scale);
  297. }
  298. inline void preScale(SkMScalar scale) {
  299. this->preScale(scale, scale, scale);
  300. }
  301. inline void postScale(SkMScalar scale) {
  302. this->postScale(scale, scale, scale);
  303. }
  304. void setRotateDegreesAbout(SkMScalar x, SkMScalar y, SkMScalar z,
  305. SkMScalar degrees) {
  306. this->setRotateAbout(x, y, z, degrees * SK_MScalarPI / 180);
  307. }
  308. /** Rotate about the vector [x,y,z]. If that vector is not unit-length,
  309. it will be automatically resized.
  310. */
  311. void setRotateAbout(SkMScalar x, SkMScalar y, SkMScalar z,
  312. SkMScalar radians);
  313. /** Rotate about the vector [x,y,z]. Does not check the length of the
  314. vector, assuming it is unit-length.
  315. */
  316. void setRotateAboutUnit(SkMScalar x, SkMScalar y, SkMScalar z,
  317. SkMScalar radians);
  318. void setConcat(const SkMatrix44& a, const SkMatrix44& b);
  319. inline void preConcat(const SkMatrix44& m) {
  320. this->setConcat(*this, m);
  321. }
  322. inline void postConcat(const SkMatrix44& m) {
  323. this->setConcat(m, *this);
  324. }
  325. friend SkMatrix44 operator*(const SkMatrix44& a, const SkMatrix44& b) {
  326. return SkMatrix44(a, b);
  327. }
  328. /** If this is invertible, return that in inverse and return true. If it is
  329. not invertible, return false and leave the inverse parameter in an
  330. unspecified state.
  331. */
  332. bool invert(SkMatrix44* inverse) const;
  333. /** Transpose this matrix in place. */
  334. void transpose();
  335. /** Apply the matrix to the src vector, returning the new vector in dst.
  336. It is legal for src and dst to point to the same memory.
  337. */
  338. void mapScalars(const SkScalar src[4], SkScalar dst[4]) const;
  339. inline void mapScalars(SkScalar vec[4]) const {
  340. this->mapScalars(vec, vec);
  341. }
  342. #ifdef SK_MSCALAR_IS_DOUBLE
  343. void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const;
  344. #elif defined SK_MSCALAR_IS_FLOAT
  345. inline void mapMScalars(const SkMScalar src[4], SkMScalar dst[4]) const {
  346. this->mapScalars(src, dst);
  347. }
  348. #endif
  349. inline void mapMScalars(SkMScalar vec[4]) const {
  350. this->mapMScalars(vec, vec);
  351. }
  352. friend SkVector4 operator*(const SkMatrix44& m, const SkVector4& src) {
  353. SkVector4 dst;
  354. m.mapScalars(src.fData, dst.fData);
  355. return dst;
  356. }
  357. /**
  358. * map an array of [x, y, 0, 1] through the matrix, returning an array
  359. * of [x', y', z', w'].
  360. *
  361. * @param src2 array of [x, y] pairs, with implied z=0 and w=1
  362. * @param count number of [x, y] pairs in src2
  363. * @param dst4 array of [x', y', z', w'] quads as the output.
  364. */
  365. void map2(const float src2[], int count, float dst4[]) const;
  366. void map2(const double src2[], int count, double dst4[]) const;
  367. /** Returns true if transformating an axis-aligned square in 2d by this matrix
  368. will produce another 2d axis-aligned square; typically means the matrix
  369. is a scale with perhaps a 90-degree rotation. A 3d rotation through 90
  370. degrees into a perpendicular plane collapses a square to a line, but
  371. is still considered to be axis-aligned.
  372. By default, tolerates very slight error due to float imprecisions;
  373. a 90-degree rotation can still end up with 10^-17 of
  374. "non-axis-aligned" result.
  375. */
  376. bool preserves2dAxisAlignment(SkMScalar epsilon = SK_ScalarNearlyZero) const;
  377. void dump() const;
  378. double determinant() const;
  379. private:
  380. /* This is indexed by [col][row]. */
  381. SkMScalar fMat[4][4];
  382. mutable std::atomic<unsigned> fTypeMask;
  383. static constexpr int kUnknown_Mask = 0x80;
  384. static constexpr int kAllPublic_Masks = 0xF;
  385. void as3x4RowMajorf(float[]) const;
  386. void set3x4RowMajorf(const float[]);
  387. SkMScalar transX() const { return fMat[3][0]; }
  388. SkMScalar transY() const { return fMat[3][1]; }
  389. SkMScalar transZ() const { return fMat[3][2]; }
  390. SkMScalar scaleX() const { return fMat[0][0]; }
  391. SkMScalar scaleY() const { return fMat[1][1]; }
  392. SkMScalar scaleZ() const { return fMat[2][2]; }
  393. SkMScalar perspX() const { return fMat[0][3]; }
  394. SkMScalar perspY() const { return fMat[1][3]; }
  395. SkMScalar perspZ() const { return fMat[2][3]; }
  396. int computeTypeMask() const;
  397. inline void dirtyTypeMask() {
  398. fTypeMask.store(kUnknown_Mask, std::memory_order_relaxed);
  399. }
  400. inline void setTypeMask(int mask) {
  401. SkASSERT(0 == (~(kAllPublic_Masks | kUnknown_Mask) & mask));
  402. fTypeMask.store(mask, std::memory_order_relaxed);
  403. }
  404. /**
  405. * Does not take the time to 'compute' the typemask. Only returns true if
  406. * we already know that this matrix is identity.
  407. */
  408. inline bool isTriviallyIdentity() const {
  409. return 0 == fTypeMask.load(std::memory_order_relaxed);
  410. }
  411. inline const SkMScalar* values() const { return &fMat[0][0]; }
  412. friend class SkColorSpace;
  413. };
  414. #endif