[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] | 6ab42d9 | 2012-12-20 20:36:53 | [diff] [blame] | 8 | #include <string> |
| 9 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 11 | #include "third_party/skia/include/utils/SkMatrix44.h" |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 12 | #include "ui/base/ui_export.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 13 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 14 | namespace gfx { |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 15 | |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 16 | class RectF; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 17 | class Point; |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 18 | class Point3F; |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 19 | class Vector3dF; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 20 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 21 | // 4x4 transformation matrix. Transform is cheap and explicitly allows |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 22 | // copy/assign. |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 23 | class UI_EXPORT Transform { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 24 | public: |
[email protected] | bda4196 | 2013-01-07 18:46:17 | [diff] [blame] | 25 | |
| 26 | enum SkipInitialization { |
| 27 | kSkipInitialization |
| 28 | }; |
| 29 | |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 30 | Transform() : matrix_(SkMatrix44::kIdentity_Constructor) {} |
[email protected] | bda4196 | 2013-01-07 18:46:17 | [diff] [blame] | 31 | |
| 32 | // Skips initializing this matrix to avoid overhead, when we know it will be |
| 33 | // initialized before use. |
| 34 | Transform(SkipInitialization) |
| 35 | : matrix_(SkMatrix44::kUninitialized_Constructor) {} |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 36 | Transform(const Transform& rhs) : matrix_(rhs.matrix_) {} |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 37 | // Initialize with the concatenation of lhs * rhs. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 38 | Transform(const Transform& lhs, const Transform& rhs) |
| 39 | : matrix_(lhs.matrix_, rhs.matrix_) {} |
| 40 | ~Transform() {} |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 41 | |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 42 | bool operator==(const Transform& rhs) const { return matrix_ == rhs.matrix_; } |
| 43 | bool operator!=(const Transform& rhs) const { return matrix_ != rhs.matrix_; } |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 44 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 45 | // Resets this transform to the identity transform. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 46 | void MakeIdentity() { matrix_.setIdentity(); } |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 47 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 48 | // Applies the current transformation on a 2d rotation and assigns the result |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 49 | // to |this|. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 50 | void Rotate(double degrees) { RotateAboutZAxis(degrees); } |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 51 | |
| 52 | // Applies the current transformation on an axis-angle rotation and assigns |
| 53 | // the result to |this|. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 54 | void RotateAboutXAxis(double degrees); |
| 55 | void RotateAboutYAxis(double degrees); |
| 56 | void RotateAboutZAxis(double degrees); |
| 57 | void RotateAbout(const Vector3dF& axis, double degrees); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 58 | |
| 59 | // Applies the current transformation on a scaling and assigns the result |
| 60 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 61 | void Scale(double x, double y); |
| 62 | void Scale3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 63 | |
| 64 | // Applies the current transformation on a translation and assigns the result |
| 65 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 66 | void Translate(double x, double y); |
| 67 | void Translate3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 68 | |
| 69 | // Applies the current transformation on a skew and assigns the result |
| 70 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 71 | void SkewX(double angle_x); |
| 72 | void SkewY(double angle_y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 73 | |
| 74 | // Applies the current transformation on a perspective transform and assigns |
| 75 | // the result to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 76 | void ApplyPerspectiveDepth(double depth); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 77 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 78 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 79 | // (i.e. 'this = this * transform;'). |
| 80 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 81 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 82 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 83 | // (i.e. 'this = transform * this;'). |
| 84 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 85 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 86 | // Returns true if this is the identity matrix. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 87 | bool IsIdentity() const { return matrix_.isIdentity(); } |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 88 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 89 | // Returns true if the matrix is either identity or pure translation. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 90 | bool IsIdentityOrTranslation() const { |
| 91 | return !(matrix_.getType() & ~SkMatrix44::kTranslate_Mask); |
| 92 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 93 | |
[email protected] | aedf4e5 | 2013-01-09 23:24:44 | [diff] [blame] | 94 | // Returns true if the matrix is either a positive scale and/or a translation. |
| 95 | bool IsPositiveScaleOrTranslation() const { |
| 96 | if (!IsScaleOrTranslation()) |
| 97 | return false; |
| 98 | return matrix_.getDouble(0, 0) > 0.0 && |
| 99 | matrix_.getDouble(1, 1) > 0.0 && |
| 100 | matrix_.getDouble(2, 2) > 0.0; |
| 101 | } |
| 102 | |
[email protected] | d1a56f0 | 2012-12-04 12:18:30 | [diff] [blame] | 103 | // Returns true if the matrix is either identity or pure, non-fractional |
| 104 | // translation. |
| 105 | bool IsIdentityOrIntegerTranslation() const; |
| 106 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 107 | // Returns true if the matrix is has only scaling and translation components. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 108 | bool IsScaleOrTranslation() const { |
| 109 | int mask = SkMatrix44::kScale_Mask | SkMatrix44::kTranslate_Mask; |
| 110 | return (matrix_.getType() & ~mask) == 0; |
| 111 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 112 | |
| 113 | // Returns true if the matrix has any perspective component that would |
| 114 | // change the w-component of a homogeneous point. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 115 | bool HasPerspective() const { |
| 116 | return (matrix_.getType() & SkMatrix44::kPerspective_Mask) != 0; |
| 117 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 118 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 119 | // Returns true if this transform is non-singular. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 120 | bool IsInvertible() const { return matrix_.invert(NULL); } |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 121 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 122 | // Returns true if a layer with a forward-facing normal of (0, 0, 1) would |
| 123 | // have its back side facing frontwards after applying the transform. |
| 124 | bool IsBackFaceVisible() const; |
| 125 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 126 | // Inverts the transform which is passed in. Returns true if successful. |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 127 | bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT; |
| 128 | |
| 129 | // Transposes this transform in place. |
| 130 | void Transpose(); |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 131 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 132 | // Applies the transformation on the point. Returns true if the point is |
| 133 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 134 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 135 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 136 | // Applies the transformation on the point. Returns true if the point is |
| 137 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 138 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 139 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 140 | // Applies the reverse transformation on the point. Returns true if the |
| 141 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 142 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 143 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 144 | // Applies the reverse transformation on the point. Returns true if the |
| 145 | // transformation can be inverted. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 146 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 147 | |
| 148 | // Applies transformation on the rectangle. Returns true if the transformed |
| 149 | // rectangle was axis aligned. If it returns false, rect will be the |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 150 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 151 | void TransformRect(RectF* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 152 | |
| 153 | // Applies the reverse transformation on the rectangle. Returns true if |
| 154 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 155 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 156 | // transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 157 | bool TransformRectReverse(RectF* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 158 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 159 | // Decomposes |this| and |from|, interpolates the decomposed values, and |
| 160 | // sets |this| to the reconstituted result. Returns false if either matrix |
| 161 | // can't be decomposed. Uses routines described in this spec: |
| 162 | // http://www.w3.org/TR/css3-3d-transforms/. |
| 163 | // |
| 164 | // Note: this call is expensive since we need to decompose the transform. If |
| 165 | // you're going to be calling this rapidly (e.g., in an animation) you should |
| 166 | // decompose once using gfx::DecomposeTransforms and reuse your |
| 167 | // DecomposedTransform. |
| 168 | bool Blend(const Transform& from, double progress); |
| 169 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 170 | // Returns |this| * |other|. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 171 | Transform operator*(const Transform& other) const { |
| 172 | return Transform(*this, other); |
| 173 | } |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 174 | |
| 175 | // Sets |this| = |this| * |other| |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 176 | Transform& operator*=(const Transform& other) { |
| 177 | PreconcatTransform(other); |
| 178 | return *this; |
| 179 | } |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 180 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 181 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 182 | const SkMatrix44& matrix() const { return matrix_; } |
| 183 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 184 | |
[email protected] | 6ab42d9 | 2012-12-20 20:36:53 | [diff] [blame] | 185 | std::string ToString() const; |
| 186 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 187 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 188 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 189 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 190 | |
| 191 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 192 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 193 | |
| 194 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 195 | |
| 196 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 197 | }; |
| 198 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 199 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 200 | |
| 201 | #endif // UI_GFX_TRANSFORM_H_ |