[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 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 32 | // Applies the current transformation on a 2d rotation and assigns the result |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 33 | // to |this|. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 34 | void Rotate(double degrees) { RotateAboutZAxis(degrees); } |
[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] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 38 | void RotateAboutXAxis(double degrees); |
| 39 | void RotateAboutYAxis(double degrees); |
| 40 | void RotateAboutZAxis(double degrees); |
| 41 | void RotateAbout(const Vector3dF& axis, double degrees); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 42 | |
| 43 | // Applies the current transformation on a scaling and assigns the result |
| 44 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 45 | void Scale(double x, double y); |
| 46 | void Scale3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 47 | |
| 48 | // Applies the current transformation on a translation and assigns the result |
| 49 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 50 | void Translate(double x, double y); |
| 51 | void Translate3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 52 | |
| 53 | // Applies the current transformation on a skew and assigns the result |
| 54 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 55 | void SkewX(double angle_x); |
| 56 | void SkewY(double angle_y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 57 | |
| 58 | // Applies the current transformation on a perspective transform and assigns |
| 59 | // the result to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 60 | void ApplyPerspectiveDepth(double depth); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 61 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 62 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 63 | // (i.e. 'this = this * transform;'). |
| 64 | void PreconcatTransform(const Transform& transform); |
[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 a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 67 | // (i.e. 'this = transform * this;'). |
| 68 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 69 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 70 | // Returns true if this is the identity matrix. |
| 71 | bool IsIdentity() const; |
| 72 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 73 | // Returns true if the matrix is either identity or pure translation. |
| 74 | bool IsIdentityOrTranslation() const; |
| 75 | |
[email protected] | d1a56f0 | 2012-12-04 12:18:30 | [diff] [blame^] | 76 | // Returns true if the matrix is either identity or pure, non-fractional |
| 77 | // translation. |
| 78 | bool IsIdentityOrIntegerTranslation() const; |
| 79 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 80 | // Returns true if the matrix is has only scaling and translation components. |
| 81 | bool IsScaleOrTranslation() const; |
| 82 | |
| 83 | // Returns true if the matrix has any perspective component that would |
| 84 | // change the w-component of a homogeneous point. |
| 85 | bool HasPerspective() const; |
| 86 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 87 | // Returns true if this transform is non-singular. |
| 88 | bool IsInvertible() const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 89 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 90 | // Returns true if a layer with a forward-facing normal of (0, 0, 1) would |
| 91 | // have its back side facing frontwards after applying the transform. |
| 92 | bool IsBackFaceVisible() const; |
| 93 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 94 | // Inverts the transform which is passed in. Returns true if successful. |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 95 | bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT; |
| 96 | |
| 97 | // Transposes this transform in place. |
| 98 | void Transpose(); |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 99 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 100 | // Applies the transformation on the point. Returns true if the point is |
| 101 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 102 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 103 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 104 | // Applies the transformation on the point. Returns true if the point is |
| 105 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 106 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 107 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 108 | // Applies the reverse transformation on the point. Returns true if the |
| 109 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 110 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 111 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 112 | // Applies the reverse transformation on the point. Returns true if the |
| 113 | // transformation can be inverted. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 114 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 115 | |
| 116 | // Applies transformation on the rectangle. Returns true if the transformed |
| 117 | // rectangle was axis aligned. If it returns false, rect will be the |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 118 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 119 | void TransformRect(RectF* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 120 | |
| 121 | // Applies the reverse transformation on the rectangle. Returns true if |
| 122 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 123 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 124 | // transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 125 | bool TransformRectReverse(RectF* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 126 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 127 | // Decomposes |this| and |from|, interpolates the decomposed values, and |
| 128 | // sets |this| to the reconstituted result. Returns false if either matrix |
| 129 | // can't be decomposed. Uses routines described in this spec: |
| 130 | // http://www.w3.org/TR/css3-3d-transforms/. |
| 131 | // |
| 132 | // Note: this call is expensive since we need to decompose the transform. If |
| 133 | // you're going to be calling this rapidly (e.g., in an animation) you should |
| 134 | // decompose once using gfx::DecomposeTransforms and reuse your |
| 135 | // DecomposedTransform. |
| 136 | bool Blend(const Transform& from, double progress); |
| 137 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 138 | // Returns |this| * |other|. |
| 139 | Transform operator*(const Transform& other) const; |
| 140 | |
| 141 | // Sets |this| = |this| * |other| |
| 142 | Transform& operator*=(const Transform& other); |
| 143 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 144 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 145 | const SkMatrix44& matrix() const { return matrix_; } |
| 146 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 147 | |
| 148 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 149 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 150 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 151 | |
| 152 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 153 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 154 | |
| 155 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 156 | |
| 157 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 158 | }; |
| 159 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 160 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 161 | |
| 162 | #endif // UI_GFX_TRANSFORM_H_ |