transform_util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_GEOMETRY_TRANSFORM_UTIL_H_
  5. #define UI_GFX_GEOMETRY_TRANSFORM_UTIL_H_
  6. #include "third_party/abseil-cpp/absl/types/optional.h"
  7. #include "ui/gfx/geometry/geometry_skia_export.h"
  8. #include "ui/gfx/geometry/point.h"
  9. #include "ui/gfx/geometry/quaternion.h"
  10. #include "ui/gfx/geometry/transform.h"
  11. namespace gfx {
  12. class Rect;
  13. class RectF;
  14. // Returns a scale transform at |anchor| point.
  15. GEOMETRY_SKIA_EXPORT Transform GetScaleTransform(const Point& anchor,
  16. float scale);
  17. // Contains the components of a factored transform. These components may be
  18. // blended and recomposed.
  19. struct GEOMETRY_SKIA_EXPORT DecomposedTransform {
  20. // The default constructor initializes the components in such a way that
  21. // if used with Compose below, will produce the identity transform.
  22. DecomposedTransform();
  23. SkScalar translate[3];
  24. SkScalar scale[3];
  25. SkScalar skew[3];
  26. SkScalar perspective[4];
  27. Quaternion quaternion;
  28. std::string ToString() const;
  29. // Copy and assign are allowed.
  30. };
  31. // Interpolates the decomposed components |to| with |from| using the
  32. // routines described in http://www.w3.org/TR/css3-3d-transform/.
  33. // |progress| is in the range [0, 1]. If 0 we will return |from|, if 1, we will
  34. // return |to|.
  35. GEOMETRY_SKIA_EXPORT DecomposedTransform
  36. BlendDecomposedTransforms(const DecomposedTransform& to,
  37. const DecomposedTransform& from,
  38. double progress);
  39. // Decomposes this transform into its translation, scale, skew, perspective,
  40. // and rotation components following the routines detailed in this spec:
  41. // http://www.w3.org/TR/css3-3d-transforms/.
  42. GEOMETRY_SKIA_EXPORT bool DecomposeTransform(DecomposedTransform* out,
  43. const Transform& transform);
  44. // Composes a transform from the given translation, scale, skew, perspective,
  45. // and rotation components following the routines detailed in this spec:
  46. // http://www.w3.org/TR/css3-3d-transforms/.
  47. GEOMETRY_SKIA_EXPORT Transform
  48. ComposeTransform(const DecomposedTransform& decomp);
  49. GEOMETRY_SKIA_EXPORT bool SnapTransform(Transform* out,
  50. const Transform& transform,
  51. const Rect& viewport);
  52. // Calculates a transform with a transformed origin. The resulting transform is
  53. // created by composing P * T * P^-1 where P is a constant transform to the new
  54. // origin.
  55. GEOMETRY_SKIA_EXPORT Transform TransformAboutPivot(const Point& pivot,
  56. const Transform& transform);
  57. // Calculates a transform which would transform |src| to |dst|.
  58. GEOMETRY_SKIA_EXPORT Transform TransformBetweenRects(const RectF& src,
  59. const RectF& dst);
  60. // Generates projection matrix and returns it as a Transform.
  61. GEOMETRY_SKIA_EXPORT Transform OrthoProjectionMatrix(float left,
  62. float right,
  63. float bottom,
  64. float top);
  65. // Generates window matrix and returns it as a Transform.
  66. GEOMETRY_SKIA_EXPORT Transform WindowMatrix(int x,
  67. int y,
  68. int width,
  69. int height);
  70. // Compute 2D scale if possible; return whether it was set.
  71. GEOMETRY_SKIA_EXPORT absl::optional<Vector2dF>
  72. TryComputeTransform2dScaleComponents(const Transform& transform);
  73. // Compute 2D scale, and fall back to fallback_value if not possible.
  74. GEOMETRY_SKIA_EXPORT Vector2dF
  75. ComputeTransform2dScaleComponents(const Transform& transform,
  76. float fallback_value);
  77. // Returns an approximate max scale value of the transform even if it has
  78. // perspective. Prefer to use ComputeTransform2dScaleComponents if there is no
  79. // perspective, since it can produce more accurate results.
  80. GEOMETRY_SKIA_EXPORT
  81. float ComputeApproximateMaxScale(const Transform& transform);
  82. } // namespace gfx
  83. #endif // UI_GFX_GEOMETRY_TRANSFORM_UTIL_H_