blob: 45bc4dab2cf30c3a2ff6e760f5d0dd1938d7bfce [file] [log] [blame]
[email protected]3c2196b22012-03-17 03:42:251// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]36df22b2011-02-24 21:47:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_GFX_TRANSFORM_H_
6#define UI_GFX_TRANSFORM_H_
[email protected]36df22b2011-02-24 21:47:567
[email protected]80248e32011-07-08 15:31:118#include "third_party/skia/include/utils/SkMatrix44.h"
[email protected]507bad52011-08-06 04:51:079#include "ui/base/ui_export.h"
[email protected]b9b1e7a42011-05-17 15:29:5110
[email protected]36df22b2011-02-24 21:47:5611namespace gfx {
[email protected]0f0453e2012-10-14 18:15:3512
[email protected]79fbdab02012-11-14 07:28:1113class RectF;
[email protected]80248e32011-07-08 15:31:1114class Point;
[email protected]2771b1c2012-10-31 05:15:4315class Point3F;
[email protected]36df22b2011-02-24 21:47:5616
[email protected]80248e32011-07-08 15:31:1117// 4x4 transformation matrix. Transform is cheap and explicitly allows
[email protected]b9b1e7a42011-05-17 15:29:5118// copy/assign.
[email protected]507bad52011-08-06 04:51:0719class UI_EXPORT Transform {
[email protected]36df22b2011-02-24 21:47:5620 public:
[email protected]b9b1e7a42011-05-17 15:29:5121 Transform();
22 ~Transform();
[email protected]36df22b2011-02-24 21:47:5623
[email protected]80248e32011-07-08 15:31:1124 bool operator==(const Transform& rhs) const;
25 bool operator!=(const Transform& rhs) const;
26
[email protected]598080082011-04-14 19:36:3327 // NOTE: The 'Set' functions overwrite the previously set transformation
28 // parameters. The 'Concat' functions apply a transformation (e.g. rotation,
29 // scale, translate) on top of the existing transforms, instead of overwriting
30 // them.
31
32 // NOTE: The order of the 'Set' function calls do not matter. However, the
33 // order of the 'Concat' function calls do matter, especially when combined
34 // with the 'Set' functions.
35
36 // Sets the rotation of the transformation.
[email protected]709104132012-11-14 14:12:0437 void SetRotate(double degree);
[email protected]36df22b2011-02-24 21:47:5638
[email protected]3c2196b22012-03-17 03:42:2539 // Sets the rotation of the transform (about a vector).
[email protected]709104132012-11-14 14:12:0440 void SetRotateAbout(const Point3F& point, double degree);
[email protected]3c2196b22012-03-17 03:42:2541
[email protected]598080082011-04-14 19:36:3342 // Sets the scaling parameters.
[email protected]709104132012-11-14 14:12:0443 void SetScaleX(double x);
44 void SetScaleY(double y);
[email protected]2fcafa02012-11-15 01:12:5545 void SetScaleZ(double z);
[email protected]709104132012-11-14 14:12:0446 void SetScale(double x, double y);
[email protected]2fcafa02012-11-15 01:12:5547 void SetScale3d(double x, double y, double z);
[email protected]36df22b2011-02-24 21:47:5648
[email protected]598080082011-04-14 19:36:3349 // Sets the translation parameters.
[email protected]709104132012-11-14 14:12:0450 void SetTranslateX(double x);
51 void SetTranslateY(double y);
[email protected]2fcafa02012-11-15 01:12:5552 void SetTranslateZ(double z);
[email protected]709104132012-11-14 14:12:0453 void SetTranslate(double x, double y);
[email protected]2fcafa02012-11-15 01:12:5554 void SetTranslate3d(double x, double y, double z);
55
56 // Sets the skew parameters.
57 void SetSkewX(double angle);
58 void SetSkewY(double angle);
[email protected]36df22b2011-02-24 21:47:5659
[email protected]9dc5f9d2012-05-14 19:28:0160 // Creates a perspective matrix.
61 // Based on the 'perspective' operation from
62 // http://www.w3.org/TR/css3-3d-transforms/#transform-functions
[email protected]709104132012-11-14 14:12:0463 void SetPerspectiveDepth(double depth);
[email protected]9dc5f9d2012-05-14 19:28:0164
[email protected]3c2196b22012-03-17 03:42:2565 // Applies a rotation on the current transformation.
[email protected]709104132012-11-14 14:12:0466 void ConcatRotate(double degree);
[email protected]36df22b2011-02-24 21:47:5667
[email protected]3c2196b22012-03-17 03:42:2568 // Applies an axis-angle rotation on the current transformation.
[email protected]709104132012-11-14 14:12:0469 void ConcatRotateAbout(const Point3F& point, double degree);
[email protected]3c2196b22012-03-17 03:42:2570
[email protected]598080082011-04-14 19:36:3371 // Applies scaling on current transform.
[email protected]709104132012-11-14 14:12:0472 void ConcatScale(double x, double y);
[email protected]2fcafa02012-11-15 01:12:5573 void ConcatScale3d(double x, double y, double z);
[email protected]36df22b2011-02-24 21:47:5674
[email protected]598080082011-04-14 19:36:3375 // Applies translation on current transform.
[email protected]709104132012-11-14 14:12:0476 void ConcatTranslate(double x, double y);
[email protected]2fcafa02012-11-15 01:12:5577 void ConcatTranslate3d(double x, double y, double z);
[email protected]b9b1e7a42011-05-17 15:29:5178
[email protected]2b36aeb2012-10-30 04:53:0979 // Applies a perspective on current transform.
[email protected]709104132012-11-14 14:12:0480 void ConcatPerspectiveDepth(double depth);
[email protected]2b36aeb2012-10-30 04:53:0981
[email protected]2fcafa02012-11-15 01:12:5582 // Applies a skew on the current transform.
83 void ConcatSkewX(double angle_x);
84 void ConcatSkewY(double angle_y);
85
86 // Applies the current transformation on a rotation and assigns the result
87 // to |this|.
88 void PreconcatRotate(double degree);
89
90 // Applies the current transformation on an axis-angle rotation and assigns
91 // the result to |this|.
92 void PreconcatRotateAbout(const Point3F& point, double degree);
93
94 // Applies the current transformation on a scaling and assigns the result
95 // to |this|.
96 void PreconcatScale(double x, double y);
97 void PreconcatScale3d(double x, double y, double z);
98
99 // Applies the current transformation on a translation and assigns the result
100 // to |this|.
101 void PreconcatTranslate(double x, double y);
102 void PreconcatTranslate3d(double x, double y, double z);
103
104 // Applies the current transformation on a skew and assigns the result
105 // to |this|.
106 void PreconcatSkewX(double angle_x);
107 void PreconcatSkewY(double angle_y);
108
109 // Applies the current transformation on a perspective transform and assigns
110 // the result to |this|.
111 void PreconcatPerspectiveDepth(double depth);
112
[email protected]b9b1e7a42011-05-17 15:29:51113 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:11114 // (i.e. 'this = this * transform;').
115 void PreconcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:56116
[email protected]598080082011-04-14 19:36:33117 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:11118 // (i.e. 'this = transform * this;').
119 void ConcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:56120
121 // Does the transformation change anything?
[email protected]b9b1e7a42011-05-17 15:29:51122 bool HasChange() const;
[email protected]36df22b2011-02-24 21:47:56123
[email protected]38919392011-10-24 22:26:23124 // Inverts the transform which is passed in. Returns true if successful.
125 bool GetInverse(Transform* transform) const;
126
[email protected]598080082011-04-14 19:36:33127 // Applies the transformation on the point. Returns true if the point is
128 // transformed successfully.
[email protected]2771b1c2012-10-31 05:15:43129 void TransformPoint(Point3F& point) const;
[email protected]36df22b2011-02-24 21:47:56130
[email protected]80248e32011-07-08 15:31:11131 // Applies the transformation on the point. Returns true if the point is
132 // transformed successfully. Rounds the result to the nearest point.
[email protected]0f0453e2012-10-14 18:15:35133 void TransformPoint(Point& point) const;
[email protected]36df22b2011-02-24 21:47:56134
[email protected]80248e32011-07-08 15:31:11135 // Applies the reverse transformation on the point. Returns true if the
136 // transformation can be inverted.
[email protected]2771b1c2012-10-31 05:15:43137 bool TransformPointReverse(Point3F& point) const;
[email protected]463eb0e2011-05-10 03:11:04138
[email protected]80248e32011-07-08 15:31:11139 // Applies the reverse transformation on the point. Returns true if the
140 // transformation can be inverted. Rounds the result to the nearest point.
[email protected]0f0453e2012-10-14 18:15:35141 bool TransformPointReverse(Point& point) const;
[email protected]80248e32011-07-08 15:31:11142
143 // Applies transformation on the rectangle. Returns true if the transformed
144 // rectangle was axis aligned. If it returns false, rect will be the
[email protected]91f4c792012-06-06 02:21:19145 // smallest axis aligned bounding box containing the transformed rect.
[email protected]79fbdab02012-11-14 07:28:11146 void TransformRect(RectF* rect) const;
[email protected]80248e32011-07-08 15:31:11147
148 // Applies the reverse transformation on the rectangle. Returns true if
149 // the transformed rectangle was axis aligned. If it returns false,
[email protected]91f4c792012-06-06 02:21:19150 // rect will be the smallest axis aligned bounding box containing the
[email protected]80248e32011-07-08 15:31:11151 // transformed rect.
[email protected]79fbdab02012-11-14 07:28:11152 bool TransformRectReverse(RectF* rect) const;
[email protected]277c7b72011-06-06 15:23:09153
[email protected]2fcafa02012-11-15 01:12:55154 // Decomposes |this| and |from|, interpolates the decomposed values, and
155 // sets |this| to the reconstituted result. Returns false if either matrix
156 // can't be decomposed. Uses routines described in this spec:
157 // http://www.w3.org/TR/css3-3d-transforms/.
158 //
159 // Note: this call is expensive since we need to decompose the transform. If
160 // you're going to be calling this rapidly (e.g., in an animation) you should
161 // decompose once using gfx::DecomposeTransforms and reuse your
162 // DecomposedTransform.
163 bool Blend(const Transform& from, double progress);
164
[email protected]b9b1e7a42011-05-17 15:29:51165 // Returns the underlying matrix.
[email protected]80248e32011-07-08 15:31:11166 const SkMatrix44& matrix() const { return matrix_; }
167 SkMatrix44& matrix() { return matrix_; }
[email protected]b9b1e7a42011-05-17 15:29:51168
169 private:
[email protected]80248e32011-07-08 15:31:11170 void TransformPointInternal(const SkMatrix44& xform,
[email protected]0f0453e2012-10-14 18:15:35171 Point& point) const;
[email protected]80248e32011-07-08 15:31:11172
173 void TransformPointInternal(const SkMatrix44& xform,
[email protected]2771b1c2012-10-31 05:15:43174 Point3F& point) const;
[email protected]80248e32011-07-08 15:31:11175
176 SkMatrix44 matrix_;
[email protected]b9b1e7a42011-05-17 15:29:51177
178 // copy/assign are allowed.
[email protected]36df22b2011-02-24 21:47:56179};
180
[email protected]0f0453e2012-10-14 18:15:35181} // namespace gfx
[email protected]36df22b2011-02-24 21:47:56182
183#endif // UI_GFX_TRANSFORM_H_