[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 2 | // 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] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 7 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 8 | #include "third_party/skia/include/utils/SkMatrix44.h" |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 9 | #include "ui/base/ui_export.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 10 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 11 | namespace gfx { |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 12 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 13 | class Rect; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 14 | class Point; |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 15 | class Point3F; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 16 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 17 | // 4x4 transformation matrix. Transform is cheap and explicitly allows |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 18 | // copy/assign. |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 19 | class UI_EXPORT Transform { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 20 | public: |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 21 | Transform(); |
| 22 | ~Transform(); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 23 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 24 | bool operator==(const Transform& rhs) const; |
| 25 | bool operator!=(const Transform& rhs) const; |
| 26 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 27 | // 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] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 37 | void SetRotate(float degree); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 38 | |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 39 | // Sets the rotation of the transform (about a vector). |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 40 | void SetRotateAbout(const Point3F& point, float degree); |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 41 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 42 | // Sets the scaling parameters. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 43 | void SetScaleX(float x); |
| 44 | void SetScaleY(float y); |
| 45 | void SetScale(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 46 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 47 | // Sets the translation parameters. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 48 | void SetTranslateX(float x); |
| 49 | void SetTranslateY(float y); |
| 50 | void SetTranslate(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 51 | |
[email protected] | 9dc5f9d | 2012-05-14 19:28:01 | [diff] [blame] | 52 | // Creates a perspective matrix. |
| 53 | // Based on the 'perspective' operation from |
| 54 | // http://www.w3.org/TR/css3-3d-transforms/#transform-functions |
| 55 | void SetPerspectiveDepth(float depth); |
| 56 | |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 57 | // Applies a rotation on the current transformation. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 58 | void ConcatRotate(float degree); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 59 | |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 60 | // Applies an axis-angle rotation on the current transformation. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 61 | void ConcatRotateAbout(const Point3F& point, float degree); |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 62 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 63 | // Applies scaling on current transform. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 64 | void ConcatScale(float x, float y); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 65 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 66 | // Applies translation on current transform. |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 67 | void ConcatTranslate(float x, float y); |
| 68 | |
[email protected] | 2b36aeb | 2012-10-30 04:53:09 | [diff] [blame] | 69 | // Applies a perspective on current transform. |
| 70 | void ConcatPerspectiveDepth(float depth); |
| 71 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 72 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 73 | // (i.e. 'this = this * transform;'). |
| 74 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 75 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 76 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 77 | // (i.e. 'this = transform * this;'). |
| 78 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 79 | |
| 80 | // Does the transformation change anything? |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 81 | bool HasChange() const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 82 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 83 | // Inverts the transform which is passed in. Returns true if successful. |
| 84 | bool GetInverse(Transform* transform) const; |
| 85 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 86 | // Applies the transformation on the point. Returns true if the point is |
| 87 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 88 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 89 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 90 | // Applies the transformation on the point. Returns true if the point is |
| 91 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 92 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 93 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 94 | // Applies the reverse transformation on the point. Returns true if the |
| 95 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 96 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 97 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 98 | // Applies the reverse transformation on the point. Returns true if the |
| 99 | // transformation can be inverted. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 100 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 101 | |
| 102 | // Applies transformation on the rectangle. Returns true if the transformed |
| 103 | // rectangle was axis aligned. If it returns false, rect will be the |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 104 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 105 | void TransformRect(Rect* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 106 | |
| 107 | // Applies the reverse transformation on the rectangle. Returns true if |
| 108 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 109 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 110 | // transformed rect. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 111 | bool TransformRectReverse(Rect* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 112 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 113 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 114 | const SkMatrix44& matrix() const { return matrix_; } |
| 115 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 116 | |
| 117 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 118 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 119 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 120 | |
| 121 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame^] | 122 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 123 | |
| 124 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 125 | |
| 126 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 127 | }; |
| 128 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 129 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 130 | |
| 131 | #endif // UI_GFX_TRANSFORM_H_ |