SkRSXform.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2015 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 SkRSXform_DEFINED
  8. #define SkRSXform_DEFINED
  9. #include "SkPoint.h"
  10. #include "SkSize.h"
  11. /**
  12. * A compressed form of a rotation+scale matrix.
  13. *
  14. * [ fSCos -fSSin fTx ]
  15. * [ fSSin fSCos fTy ]
  16. * [ 0 0 1 ]
  17. */
  18. struct SkRSXform {
  19. static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) {
  20. SkRSXform xform = { scos, ssin, tx, ty };
  21. return xform;
  22. }
  23. /*
  24. * Initialize a new xform based on the scale, rotation (in radians), final tx,ty location
  25. * and anchor-point ax,ay within the src quad.
  26. *
  27. * Note: the anchor point is not normalized (e.g. 0...1) but is in pixels of the src image.
  28. */
  29. static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty,
  30. SkScalar ax, SkScalar ay) {
  31. const SkScalar s = SkScalarSin(radians) * scale;
  32. const SkScalar c = SkScalarCos(radians) * scale;
  33. return Make(c, s, tx + -c * ax + s * ay, ty + -s * ax - c * ay);
  34. }
  35. SkScalar fSCos;
  36. SkScalar fSSin;
  37. SkScalar fTx;
  38. SkScalar fTy;
  39. bool rectStaysRect() const {
  40. return 0 == fSCos || 0 == fSSin;
  41. }
  42. void setIdentity() {
  43. fSCos = 1;
  44. fSSin = fTx = fTy = 0;
  45. }
  46. void set(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) {
  47. fSCos = scos;
  48. fSSin = ssin;
  49. fTx = tx;
  50. fTy = ty;
  51. }
  52. void toQuad(SkScalar width, SkScalar height, SkPoint quad[4]) const;
  53. void toQuad(const SkSize& size, SkPoint quad[4]) const {
  54. this->toQuad(size.width(), size.height(), quad);
  55. }
  56. void toTriStrip(SkScalar width, SkScalar height, SkPoint strip[4]) const;
  57. };
  58. #endif