[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] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 9 | #include "third_party/skia/include/utils/SkMatrix44.h" |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 10 | #include "ui/base/ui_export.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 11 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 12 | namespace gfx { |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 13 | |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 14 | class RectF; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 15 | class Point; |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 16 | class Point3F; |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 17 | class Vector3dF; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 18 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 19 | // 4x4 transformation matrix. Transform is cheap and explicitly allows |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 20 | // copy/assign. |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 21 | class UI_EXPORT Transform { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 22 | public: |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 23 | Transform(); |
| 24 | ~Transform(); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 25 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 26 | bool operator==(const Transform& rhs) const; |
| 27 | bool operator!=(const Transform& rhs) const; |
| 28 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 29 | // Resets this transform to the identity transform. |
| 30 | void MakeIdentity(); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 31 | |
| 32 | // Applies the current transformation on a rotation and assigns the result |
| 33 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 34 | void Rotate(double degree); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 35 | |
| 36 | // Applies the current transformation on an axis-angle rotation and assigns |
| 37 | // the result to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 38 | void RotateAbout(const Vector3dF& point, double degree); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 39 | |
| 40 | // Applies the current transformation on a scaling and assigns the result |
| 41 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 42 | void Scale(double x, double y); |
| 43 | void Scale3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 44 | |
| 45 | // Applies the current transformation on a translation and assigns the result |
| 46 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 47 | void Translate(double x, double y); |
| 48 | void Translate3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 49 | |
| 50 | // Applies the current transformation on a skew and assigns the result |
| 51 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 52 | void SkewX(double angle_x); |
| 53 | void SkewY(double angle_y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 54 | |
| 55 | // Applies the current transformation on a perspective transform and assigns |
| 56 | // the result to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 57 | void ApplyPerspectiveDepth(double depth); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 58 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 59 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 60 | // (i.e. 'this = this * transform;'). |
| 61 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 62 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 63 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 64 | // (i.e. 'this = transform * this;'). |
| 65 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 66 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 67 | // Returns true if this is the identity matrix. |
| 68 | bool IsIdentity() const; |
| 69 | |
| 70 | // Returns true if this transform is non-singular. |
| 71 | bool IsInvertible() const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 72 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 73 | // Inverts the transform which is passed in. Returns true if successful. |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 74 | bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT; |
| 75 | |
| 76 | // Transposes this transform in place. |
| 77 | void Transpose(); |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 78 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 79 | // Applies the transformation on the point. Returns true if the point is |
| 80 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 81 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 82 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 83 | // Applies the transformation on the point. Returns true if the point is |
| 84 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 85 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 86 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 87 | // Applies the reverse transformation on the point. Returns true if the |
| 88 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 89 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 90 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 91 | // Applies the reverse transformation on the point. Returns true if the |
| 92 | // transformation can be inverted. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 93 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 94 | |
| 95 | // Applies transformation on the rectangle. Returns true if the transformed |
| 96 | // rectangle was axis aligned. If it returns false, rect will be the |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 97 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 98 | void TransformRect(RectF* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 99 | |
| 100 | // Applies the reverse transformation on the rectangle. Returns true if |
| 101 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 102 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 103 | // transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 104 | bool TransformRectReverse(RectF* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 105 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 106 | // Decomposes |this| and |from|, interpolates the decomposed values, and |
| 107 | // sets |this| to the reconstituted result. Returns false if either matrix |
| 108 | // can't be decomposed. Uses routines described in this spec: |
| 109 | // http://www.w3.org/TR/css3-3d-transforms/. |
| 110 | // |
| 111 | // Note: this call is expensive since we need to decompose the transform. If |
| 112 | // you're going to be calling this rapidly (e.g., in an animation) you should |
| 113 | // decompose once using gfx::DecomposeTransforms and reuse your |
| 114 | // DecomposedTransform. |
| 115 | bool Blend(const Transform& from, double progress); |
| 116 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame^] | 117 | // Returns |this| * |other|. |
| 118 | Transform operator*(const Transform& other) const; |
| 119 | |
| 120 | // Sets |this| = |this| * |other| |
| 121 | Transform& operator*=(const Transform& other); |
| 122 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 123 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 124 | const SkMatrix44& matrix() const { return matrix_; } |
| 125 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 126 | |
| 127 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 128 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 129 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 130 | |
| 131 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 132 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 133 | |
| 134 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 135 | |
| 136 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 137 | }; |
| 138 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 139 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 140 | |
| 141 | #endif // UI_GFX_TRANSFORM_H_ |