[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] | bea4c87 | 2013-08-14 18:10:00 | [diff] [blame^] | 13 | #include "ui/gfx/vector2d_f.h" |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 14 | |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 15 | namespace gfx { |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 16 | |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 17 | class RectF; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 18 | class Point; |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 19 | class Point3F; |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 20 | class Vector3dF; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 21 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 22 | // 4x4 transformation matrix. Transform is cheap and explicitly allows |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 23 | // copy/assign. |
[email protected] | 507bad5 | 2011-08-06 04:51:07 | [diff] [blame] | 24 | class UI_EXPORT Transform { |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 25 | public: |
[email protected] | bda4196 | 2013-01-07 18:46:17 | [diff] [blame] | 26 | |
| 27 | enum SkipInitialization { |
| 28 | kSkipInitialization |
| 29 | }; |
| 30 | |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 31 | Transform() : matrix_(SkMatrix44::kIdentity_Constructor) {} |
[email protected] | bda4196 | 2013-01-07 18:46:17 | [diff] [blame] | 32 | |
| 33 | // Skips initializing this matrix to avoid overhead, when we know it will be |
| 34 | // initialized before use. |
| 35 | Transform(SkipInitialization) |
| 36 | : matrix_(SkMatrix44::kUninitialized_Constructor) {} |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 37 | Transform(const Transform& rhs) : matrix_(rhs.matrix_) {} |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 38 | // Initialize with the concatenation of lhs * rhs. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 39 | Transform(const Transform& lhs, const Transform& rhs) |
| 40 | : matrix_(lhs.matrix_, rhs.matrix_) {} |
[email protected] | 78634b0c | 2013-01-15 07:49:40 | [diff] [blame] | 41 | // Constructs a transform from explicit 16 matrix elements. Elements |
| 42 | // should be given in row-major order. |
| 43 | Transform(double col1row1, double col2row1, double col3row1, double col4row1, |
| 44 | double col1row2, double col2row2, double col3row2, double col4row2, |
| 45 | double col1row3, double col2row3, double col3row3, double col4row3, |
| 46 | double col1row4, double col2row4, double col3row4, double col4row4); |
| 47 | // Constructs a transform from explicit 2d elements. All other matrix |
| 48 | // elements remain the same as the corresponding elements of an identity |
| 49 | // matrix. |
| 50 | Transform(double col1row1, double col2row1, |
| 51 | double col1row2, double col2row2, |
| 52 | double x_translation, double y_translation); |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 53 | ~Transform() {} |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 54 | |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 55 | bool operator==(const Transform& rhs) const { return matrix_ == rhs.matrix_; } |
| 56 | bool operator!=(const Transform& rhs) const { return matrix_ != rhs.matrix_; } |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 57 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 58 | // Resets this transform to the identity transform. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 59 | void MakeIdentity() { matrix_.setIdentity(); } |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 60 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 61 | // Applies the current transformation on a 2d rotation and assigns the result |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 62 | // to |this|. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 63 | void Rotate(double degrees) { RotateAboutZAxis(degrees); } |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 64 | |
| 65 | // Applies the current transformation on an axis-angle rotation and assigns |
| 66 | // the result to |this|. |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 67 | void RotateAboutXAxis(double degrees); |
| 68 | void RotateAboutYAxis(double degrees); |
| 69 | void RotateAboutZAxis(double degrees); |
| 70 | void RotateAbout(const Vector3dF& axis, double degrees); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 71 | |
| 72 | // Applies the current transformation on a scaling and assigns the result |
| 73 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 74 | void Scale(double x, double y); |
| 75 | void Scale3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 76 | |
| 77 | // Applies the current transformation on a translation and assigns the result |
| 78 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 79 | void Translate(double x, double y); |
| 80 | void Translate3d(double x, double y, double z); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 81 | |
| 82 | // Applies the current transformation on a skew and assigns the result |
| 83 | // to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 84 | void SkewX(double angle_x); |
| 85 | void SkewY(double angle_y); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 86 | |
| 87 | // Applies the current transformation on a perspective transform and assigns |
| 88 | // the result to |this|. |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 89 | void ApplyPerspectiveDepth(double depth); |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 90 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 91 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 92 | // (i.e. 'this = this * transform;'). |
| 93 | void PreconcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 94 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 95 | // Applies a transformation on the current transformation |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 96 | // (i.e. 'this = transform * this;'). |
| 97 | void ConcatTransform(const Transform& transform); |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 98 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 99 | // Returns true if this is the identity matrix. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 100 | bool IsIdentity() const { return matrix_.isIdentity(); } |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 101 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 102 | // Returns true if the matrix is either identity or pure translation. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 103 | bool IsIdentityOrTranslation() const { |
| 104 | return !(matrix_.getType() & ~SkMatrix44::kTranslate_Mask); |
| 105 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 106 | |
[email protected] | aedf4e5 | 2013-01-09 23:24:44 | [diff] [blame] | 107 | // Returns true if the matrix is either a positive scale and/or a translation. |
| 108 | bool IsPositiveScaleOrTranslation() const { |
| 109 | if (!IsScaleOrTranslation()) |
| 110 | return false; |
| 111 | return matrix_.getDouble(0, 0) > 0.0 && |
| 112 | matrix_.getDouble(1, 1) > 0.0 && |
| 113 | matrix_.getDouble(2, 2) > 0.0; |
| 114 | } |
| 115 | |
[email protected] | d1a56f0 | 2012-12-04 12:18:30 | [diff] [blame] | 116 | // Returns true if the matrix is either identity or pure, non-fractional |
| 117 | // translation. |
| 118 | bool IsIdentityOrIntegerTranslation() const; |
| 119 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 120 | // Returns true if the matrix is has only scaling and translation components. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 121 | bool IsScaleOrTranslation() const { |
| 122 | int mask = SkMatrix44::kScale_Mask | SkMatrix44::kTranslate_Mask; |
| 123 | return (matrix_.getType() & ~mask) == 0; |
| 124 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 125 | |
[email protected] | 3a9a92d | 2013-07-11 04:37:00 | [diff] [blame] | 126 | // Returns true if axis-aligned 2d rects will remain axis-aligned after being |
| 127 | // transformed by this matrix. |
| 128 | bool Preserves2dAxisAlignment() const; |
| 129 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 130 | // Returns true if the matrix has any perspective component that would |
| 131 | // change the w-component of a homogeneous point. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 132 | bool HasPerspective() const { |
| 133 | return (matrix_.getType() & SkMatrix44::kPerspective_Mask) != 0; |
| 134 | } |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 135 | |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 136 | // Returns true if this transform is non-singular. |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 137 | bool IsInvertible() const { return matrix_.invert(NULL); } |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 138 | |
[email protected] | 2c7cd6d | 2012-11-28 23:49:26 | [diff] [blame] | 139 | // Returns true if a layer with a forward-facing normal of (0, 0, 1) would |
| 140 | // have its back side facing frontwards after applying the transform. |
| 141 | bool IsBackFaceVisible() const; |
| 142 | |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 143 | // Inverts the transform which is passed in. Returns true if successful. |
[email protected] | 4512792 | 2012-11-17 12:24:49 | [diff] [blame] | 144 | bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT; |
| 145 | |
| 146 | // Transposes this transform in place. |
| 147 | void Transpose(); |
[email protected] | 3891939 | 2011-10-24 22:26:23 | [diff] [blame] | 148 | |
[email protected] | 78634b0c | 2013-01-15 07:49:40 | [diff] [blame] | 149 | // Set 3rd row and 3rd colum to (0, 0, 1, 0). Note that this flattening |
| 150 | // operation is not quite the same as an orthographic projection and is |
| 151 | // technically not a linear operation. |
| 152 | // |
| 153 | // One useful interpretation of doing this operation: |
| 154 | // - For x and y values, the new transform behaves effectively like an |
| 155 | // orthographic projection was added to the matrix sequence. |
| 156 | // - For z values, the new transform overrides any effect that the transform |
| 157 | // had on z, and instead it preserves the z value for any points that are |
| 158 | // transformed. |
| 159 | // - Because of linearity of transforms, this flattened transform also |
| 160 | // preserves the effect that any subsequent (multiplied from the right) |
| 161 | // transforms would have on z values. |
| 162 | // |
| 163 | void FlattenTo2d(); |
| 164 | |
[email protected] | bea4c87 | 2013-08-14 18:10:00 | [diff] [blame^] | 165 | // Returns the translation components of the matrix. It is an error to call |
| 166 | // this function if the transform does not represent only a 2d translation. |
| 167 | Vector2dF To2dTranslation() const; |
| 168 | |
[email protected] | 59808008 | 2011-04-14 19:36:33 | [diff] [blame] | 169 | // Applies the transformation on the point. Returns true if the point is |
| 170 | // transformed successfully. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 171 | void TransformPoint(Point3F& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 172 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 173 | // Applies the transformation on the point. Returns true if the point is |
| 174 | // transformed successfully. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 175 | void TransformPoint(Point& point) const; |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 176 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 177 | // Applies the reverse transformation on the point. Returns true if the |
| 178 | // transformation can be inverted. |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 179 | bool TransformPointReverse(Point3F& point) const; |
[email protected] | 463eb0e | 2011-05-10 03:11:04 | [diff] [blame] | 180 | |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 181 | // Applies the reverse transformation on the point. Returns true if the |
| 182 | // transformation can be inverted. Rounds the result to the nearest point. |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 183 | bool TransformPointReverse(Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 184 | |
| 185 | // Applies transformation on the rectangle. Returns true if the transformed |
| 186 | // rectangle was axis aligned. If it returns false, rect will be the |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 187 | // smallest axis aligned bounding box containing the transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 188 | void TransformRect(RectF* rect) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 189 | |
| 190 | // Applies the reverse transformation on the rectangle. Returns true if |
| 191 | // the transformed rectangle was axis aligned. If it returns false, |
[email protected] | 91f4c79 | 2012-06-06 02:21:19 | [diff] [blame] | 192 | // rect will be the smallest axis aligned bounding box containing the |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 193 | // transformed rect. |
[email protected] | 79fbdab0 | 2012-11-14 07:28:11 | [diff] [blame] | 194 | bool TransformRectReverse(RectF* rect) const; |
[email protected] | 277c7b7 | 2011-06-06 15:23:09 | [diff] [blame] | 195 | |
[email protected] | 2fcafa0 | 2012-11-15 01:12:55 | [diff] [blame] | 196 | // Decomposes |this| and |from|, interpolates the decomposed values, and |
| 197 | // sets |this| to the reconstituted result. Returns false if either matrix |
| 198 | // can't be decomposed. Uses routines described in this spec: |
| 199 | // http://www.w3.org/TR/css3-3d-transforms/. |
| 200 | // |
| 201 | // Note: this call is expensive since we need to decompose the transform. If |
| 202 | // you're going to be calling this rapidly (e.g., in an animation) you should |
| 203 | // decompose once using gfx::DecomposeTransforms and reuse your |
| 204 | // DecomposedTransform. |
| 205 | bool Blend(const Transform& from, double progress); |
| 206 | |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 207 | // Returns |this| * |other|. |
[email protected] | d9a6f330 | 2012-12-07 19:17:54 | [diff] [blame] | 208 | Transform operator*(const Transform& other) const { |
| 209 | return Transform(*this, other); |
| 210 | } |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 211 | |
| 212 | // Sets |this| = |this| * |other| |
[email protected] | 0db1322 | 2012-12-13 21:27:54 | [diff] [blame] | 213 | Transform& operator*=(const Transform& other) { |
| 214 | PreconcatTransform(other); |
| 215 | return *this; |
| 216 | } |
[email protected] | f7c321eb | 2012-11-26 20:13:08 | [diff] [blame] | 217 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 218 | // Returns the underlying matrix. |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 219 | const SkMatrix44& matrix() const { return matrix_; } |
| 220 | SkMatrix44& matrix() { return matrix_; } |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 221 | |
[email protected] | 6ab42d9 | 2012-12-20 20:36:53 | [diff] [blame] | 222 | std::string ToString() const; |
| 223 | |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 224 | private: |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 225 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 226 | Point& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 227 | |
| 228 | void TransformPointInternal(const SkMatrix44& xform, |
[email protected] | 2771b1c | 2012-10-31 05:15:43 | [diff] [blame] | 229 | Point3F& point) const; |
[email protected] | 80248e3 | 2011-07-08 15:31:11 | [diff] [blame] | 230 | |
| 231 | SkMatrix44 matrix_; |
[email protected] | b9b1e7a4 | 2011-05-17 15:29:51 | [diff] [blame] | 232 | |
| 233 | // copy/assign are allowed. |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 234 | }; |
| 235 | |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 236 | } // namespace gfx |
[email protected] | 36df22b | 2011-02-24 21:47:56 | [diff] [blame] | 237 | |
| 238 | #endif // UI_GFX_TRANSFORM_H_ |