[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] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 13 | class RectF; |
[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] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 37 | void SetRotate(double 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] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 40 | void SetRotateAbout(const Point3F& point, double 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] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 43 | void SetScaleX(double x); |
| 44 | void SetScaleY(double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 45 | void SetScaleZ(double z); |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 46 | void SetScale(double x, double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 47 | void SetScale3d(double x, double y, double z); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 48 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 49 | // Sets the translation parameters. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 50 | void SetTranslateX(double x); |
| 51 | void SetTranslateY(double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 52 | void SetTranslateZ(double z); |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 53 | void SetTranslate(double x, double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 54 | 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] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 59 | |
[email protected] | 9dc5f9d | 2012-05-14 19:28:01 | [diff] [blame] | 60 | // Creates a perspective matrix. |
| 61 | // Based on the 'perspective' operation from |
| 62 | // http://www.w3.org/TR/css3-3d-transforms/#transform-functions |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 63 | void SetPerspectiveDepth(double depth); |
[email protected] | 9dc5f9d | 2012-05-14 19:28:01 | [diff] [blame] | 64 | |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 65 | // Applies a rotation on the current transformation. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 66 | void ConcatRotate(double degree); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 67 | |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 68 | // Applies an axis-angle rotation on the current transformation. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 69 | void ConcatRotateAbout(const Point3F& point, double degree); |
[email protected] | 3c2196b2 | 2012-03-17 03:42:25 | [diff] [blame] | 70 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 71 | // Applies scaling on current transform. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 72 | void ConcatScale(double x, double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 73 | void ConcatScale3d(double x, double y, double z); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 74 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 75 | // Applies translation on current transform. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 76 | void ConcatTranslate(double x, double y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 77 | void ConcatTranslate3d(double x, double y, double z); |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 78 | |
[email protected] | 2b36aeb | 2012-10-30 04:53:09 | [diff] [blame] | 79 | // Applies a perspective on current transform. |
[email protected] | 70910413 | 2012-11-14 14:12:04 | [diff] [blame] | 80 | void ConcatPerspectiveDepth(double depth); |
[email protected] | 2b36aeb | 2012-10-30 04:53:09 | [diff] [blame] | 81 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 82 | // 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] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 113 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 114 | // (i.e. 'this = this * transform;'). |
| 115 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 116 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 117 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 118 | // (i.e. 'this = transform * this;'). |
| 119 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 120 | |
| 121 | // Does the transformation change anything? |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 122 | bool HasChange() const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 123 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 124 | // Inverts the transform which is passed in. Returns true if successful. |
| 125 | bool GetInverse(Transform* transform) const; |
| 126 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 127 | // Applies the transformation on the point. Returns true if the point is |
| 128 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 129 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 130 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 131 | // Applies the transformation on the point. Returns true if the point is |
| 132 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 133 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 134 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 135 | // Applies the reverse transformation on the point. Returns true if the |
| 136 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 137 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 138 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 139 | // 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] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 141 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 142 | |
| 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] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 145 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 146 | void TransformRect(RectF* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 147 | |
| 148 | // Applies the reverse transformation on the rectangle. Returns true if |
| 149 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 150 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 151 | // transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 152 | bool TransformRectReverse(RectF* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 153 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame^] | 154 | // 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] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 165 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 166 | const SkMatrix44& matrix() const { return matrix_; } |
| 167 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 168 | |
| 169 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 170 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 171 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 172 | |
| 173 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 174 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 175 | |
| 176 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 177 | |
| 178 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 179 | }; |
| 180 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 181 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 182 | |
| 183 | #endif // UI_GFX_TRANSFORM_H_ |