blob: 96104fee4a6ebc3a25a0a33787bb82e9970cd737 [file] [log] [blame]
[email protected]3c2196b22012-03-17 03:42:251// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]36df22b2011-02-24 21:47:562// 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]36df22b2011-02-24 21:47:567
[email protected]a109fd02014-07-10 07:41:438#include <iosfwd>
[email protected]6ab42d92012-12-20 20:36:539#include <string>
10
[email protected]45127922012-11-17 12:24:4911#include "base/compiler_specific.h"
[email protected]80248e32011-07-08 15:31:1112#include "third_party/skia/include/utils/SkMatrix44.h"
[email protected]d34e4072013-09-05 20:28:3013#include "ui/gfx/gfx_export.h"
[email protected]bea4c872013-08-14 18:10:0014#include "ui/gfx/vector2d_f.h"
[email protected]b9b1e7a42011-05-17 15:29:5115
[email protected]36df22b2011-02-24 21:47:5616namespace gfx {
[email protected]0f0453e2012-10-14 18:15:3517
[email protected]f4d2b9012013-10-11 19:10:5018class BoxF;
[email protected]79fbdab02012-11-14 07:28:1119class RectF;
[email protected]80248e32011-07-08 15:31:1120class Point;
[email protected]2771b1c2012-10-31 05:15:4321class Point3F;
[email protected]f7c321eb2012-11-26 20:13:0822class Vector3dF;
[email protected]36df22b2011-02-24 21:47:5623
[email protected]80248e32011-07-08 15:31:1124// 4x4 transformation matrix. Transform is cheap and explicitly allows
[email protected]b9b1e7a42011-05-17 15:29:5125// copy/assign.
[email protected]4ffa7892013-09-27 16:56:0626class GFX_EXPORT Transform {
[email protected]36df22b2011-02-24 21:47:5627 public:
[email protected]bda41962013-01-07 18:46:1728
29 enum SkipInitialization {
30 kSkipInitialization
31 };
32
[email protected]0db13222012-12-13 21:27:5433 Transform() : matrix_(SkMatrix44::kIdentity_Constructor) {}
[email protected]bda41962013-01-07 18:46:1734
35 // Skips initializing this matrix to avoid overhead, when we know it will be
36 // initialized before use.
37 Transform(SkipInitialization)
38 : matrix_(SkMatrix44::kUninitialized_Constructor) {}
[email protected]0db13222012-12-13 21:27:5439 Transform(const Transform& rhs) : matrix_(rhs.matrix_) {}
[email protected]d9a6f3302012-12-07 19:17:5440 // Initialize with the concatenation of lhs * rhs.
[email protected]0db13222012-12-13 21:27:5441 Transform(const Transform& lhs, const Transform& rhs)
42 : matrix_(lhs.matrix_, rhs.matrix_) {}
[email protected]78634b0c2013-01-15 07:49:4043 // Constructs a transform from explicit 16 matrix elements. Elements
44 // should be given in row-major order.
[email protected]803f6b52013-09-12 00:51:2645 Transform(SkMScalar col1row1,
46 SkMScalar col2row1,
47 SkMScalar col3row1,
48 SkMScalar col4row1,
49 SkMScalar col1row2,
50 SkMScalar col2row2,
51 SkMScalar col3row2,
52 SkMScalar col4row2,
53 SkMScalar col1row3,
54 SkMScalar col2row3,
55 SkMScalar col3row3,
56 SkMScalar col4row3,
57 SkMScalar col1row4,
58 SkMScalar col2row4,
59 SkMScalar col3row4,
60 SkMScalar col4row4);
[email protected]78634b0c2013-01-15 07:49:4061 // Constructs a transform from explicit 2d elements. All other matrix
62 // elements remain the same as the corresponding elements of an identity
63 // matrix.
[email protected]803f6b52013-09-12 00:51:2664 Transform(SkMScalar col1row1,
65 SkMScalar col2row1,
66 SkMScalar col1row2,
67 SkMScalar col2row2,
68 SkMScalar x_translation,
69 SkMScalar y_translation);
[email protected]0db13222012-12-13 21:27:5470 ~Transform() {}
[email protected]36df22b2011-02-24 21:47:5671
[email protected]0db13222012-12-13 21:27:5472 bool operator==(const Transform& rhs) const { return matrix_ == rhs.matrix_; }
73 bool operator!=(const Transform& rhs) const { return matrix_ != rhs.matrix_; }
[email protected]80248e32011-07-08 15:31:1174
[email protected]f7c321eb2012-11-26 20:13:0875 // Resets this transform to the identity transform.
[email protected]0db13222012-12-13 21:27:5476 void MakeIdentity() { matrix_.setIdentity(); }
[email protected]2fcafa02012-11-15 01:12:5577
[email protected]2c7cd6d2012-11-28 23:49:2678 // Applies the current transformation on a 2d rotation and assigns the result
[email protected]2fcafa02012-11-15 01:12:5579 // to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2680 void Rotate(double degrees) { RotateAboutZAxis(degrees); }
[email protected]2fcafa02012-11-15 01:12:5581
82 // Applies the current transformation on an axis-angle rotation and assigns
83 // the result to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2684 void RotateAboutXAxis(double degrees);
85 void RotateAboutYAxis(double degrees);
86 void RotateAboutZAxis(double degrees);
87 void RotateAbout(const Vector3dF& axis, double degrees);
[email protected]2fcafa02012-11-15 01:12:5588
89 // Applies the current transformation on a scaling and assigns the result
90 // to |this|.
[email protected]803f6b52013-09-12 00:51:2691 void Scale(SkMScalar x, SkMScalar y);
92 void Scale3d(SkMScalar x, SkMScalar y, SkMScalar z);
[email protected]adeda572014-01-31 00:49:4793 gfx::Vector2dF Scale2d() const {
94 return gfx::Vector2dF(matrix_.get(0, 0), matrix_.get(1, 1));
95 }
[email protected]2fcafa02012-11-15 01:12:5596
97 // Applies the current transformation on a translation and assigns the result
98 // to |this|.
[email protected]803f6b52013-09-12 00:51:2699 void Translate(SkMScalar x, SkMScalar y);
100 void Translate3d(SkMScalar x, SkMScalar y, SkMScalar z);
[email protected]2fcafa02012-11-15 01:12:55101
102 // Applies the current transformation on a skew and assigns the result
103 // to |this|.
[email protected]6138db702013-09-25 03:25:05104 void SkewX(double angle_x);
105 void SkewY(double angle_y);
[email protected]2fcafa02012-11-15 01:12:55106
107 // Applies the current transformation on a perspective transform and assigns
108 // the result to |this|.
[email protected]803f6b52013-09-12 00:51:26109 void ApplyPerspectiveDepth(SkMScalar depth);
[email protected]2fcafa02012-11-15 01:12:55110
[email protected]b9b1e7a42011-05-17 15:29:51111 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:11112 // (i.e. 'this = this * transform;').
113 void PreconcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:56114
[email protected]598080082011-04-14 19:36:33115 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:11116 // (i.e. 'this = transform * this;').
117 void ConcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:56118
[email protected]45127922012-11-17 12:24:49119 // Returns true if this is the identity matrix.
[email protected]d9a6f3302012-12-07 19:17:54120 bool IsIdentity() const { return matrix_.isIdentity(); }
[email protected]45127922012-11-17 12:24:49121
[email protected]2c7cd6d2012-11-28 23:49:26122 // Returns true if the matrix is either identity or pure translation.
[email protected]d9a6f3302012-12-07 19:17:54123 bool IsIdentityOrTranslation() const {
124 return !(matrix_.getType() & ~SkMatrix44::kTranslate_Mask);
125 }
[email protected]2c7cd6d2012-11-28 23:49:26126
[email protected]19f90542013-10-19 18:26:07127 // Returns true if the matrix is either identity or pure translation,
128 // allowing for an amount of inaccuracy as specified by the parameter.
129 bool IsApproximatelyIdentityOrTranslation(SkMScalar tolerance) const;
130
[email protected]aedf4e52013-01-09 23:24:44131 // Returns true if the matrix is either a positive scale and/or a translation.
132 bool IsPositiveScaleOrTranslation() const {
133 if (!IsScaleOrTranslation())
134 return false;
[email protected]803f6b52013-09-12 00:51:26135 return matrix_.get(0, 0) > 0.0 && matrix_.get(1, 1) > 0.0 &&
136 matrix_.get(2, 2) > 0.0;
[email protected]aedf4e52013-01-09 23:24:44137 }
138
[email protected]d1a56f02012-12-04 12:18:30139 // Returns true if the matrix is either identity or pure, non-fractional
140 // translation.
141 bool IsIdentityOrIntegerTranslation() const;
142
[email protected]adeda572014-01-31 00:49:47143 // Returns true if the matrix had only scaling components.
144 bool IsScale2d() const {
145 return !(matrix_.getType() & ~SkMatrix44::kScale_Mask);
146 }
147
[email protected]2c7cd6d2012-11-28 23:49:26148 // Returns true if the matrix is has only scaling and translation components.
[email protected]0db13222012-12-13 21:27:54149 bool IsScaleOrTranslation() const {
150 int mask = SkMatrix44::kScale_Mask | SkMatrix44::kTranslate_Mask;
151 return (matrix_.getType() & ~mask) == 0;
152 }
[email protected]2c7cd6d2012-11-28 23:49:26153
[email protected]3a9a92d2013-07-11 04:37:00154 // Returns true if axis-aligned 2d rects will remain axis-aligned after being
155 // transformed by this matrix.
156 bool Preserves2dAxisAlignment() const;
157
[email protected]2c7cd6d2012-11-28 23:49:26158 // Returns true if the matrix has any perspective component that would
159 // change the w-component of a homogeneous point.
[email protected]0db13222012-12-13 21:27:54160 bool HasPerspective() const {
161 return (matrix_.getType() & SkMatrix44::kPerspective_Mask) != 0;
162 }
[email protected]2c7cd6d2012-11-28 23:49:26163
[email protected]45127922012-11-17 12:24:49164 // Returns true if this transform is non-singular.
[email protected]0db13222012-12-13 21:27:54165 bool IsInvertible() const { return matrix_.invert(NULL); }
[email protected]36df22b2011-02-24 21:47:56166
[email protected]2c7cd6d2012-11-28 23:49:26167 // Returns true if a layer with a forward-facing normal of (0, 0, 1) would
168 // have its back side facing frontwards after applying the transform.
169 bool IsBackFaceVisible() const;
170
[email protected]38919392011-10-24 22:26:23171 // Inverts the transform which is passed in. Returns true if successful.
[email protected]45127922012-11-17 12:24:49172 bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT;
173
174 // Transposes this transform in place.
175 void Transpose();
[email protected]38919392011-10-24 22:26:23176
[email protected]78634b0c2013-01-15 07:49:40177 // Set 3rd row and 3rd colum to (0, 0, 1, 0). Note that this flattening
178 // operation is not quite the same as an orthographic projection and is
179 // technically not a linear operation.
180 //
181 // One useful interpretation of doing this operation:
182 // - For x and y values, the new transform behaves effectively like an
183 // orthographic projection was added to the matrix sequence.
184 // - For z values, the new transform overrides any effect that the transform
185 // had on z, and instead it preserves the z value for any points that are
186 // transformed.
187 // - Because of linearity of transforms, this flattened transform also
188 // preserves the effect that any subsequent (multiplied from the right)
189 // transforms would have on z values.
190 //
191 void FlattenTo2d();
192
[email protected]d81752b2013-10-25 08:32:23193 // Returns the x and y translation components of the matrix.
[email protected]bea4c872013-08-14 18:10:00194 Vector2dF To2dTranslation() const;
195
[email protected]908af422013-10-10 22:22:42196 // Applies the transformation to the point.
[email protected]26d7ece2013-09-12 20:59:47197 void TransformPoint(Point3F* point) const;
[email protected]36df22b2011-02-24 21:47:56198
[email protected]908af422013-10-10 22:22:42199 // Applies the transformation to the point.
[email protected]26d7ece2013-09-12 20:59:47200 void TransformPoint(Point* point) const;
[email protected]36df22b2011-02-24 21:47:56201
[email protected]80248e32011-07-08 15:31:11202 // Applies the reverse transformation on the point. Returns true if the
203 // transformation can be inverted.
[email protected]26d7ece2013-09-12 20:59:47204 bool TransformPointReverse(Point3F* point) const;
[email protected]463eb0e2011-05-10 03:11:04205
[email protected]80248e32011-07-08 15:31:11206 // Applies the reverse transformation on the point. Returns true if the
207 // transformation can be inverted. Rounds the result to the nearest point.
[email protected]26d7ece2013-09-12 20:59:47208 bool TransformPointReverse(Point* point) const;
[email protected]80248e32011-07-08 15:31:11209
[email protected]f4d2b9012013-10-11 19:10:50210 // Applies transformation on the given rect. After the function completes,
211 // |rect| will be the smallest axis aligned bounding rect containing the
212 // transformed rect.
[email protected]79fbdab02012-11-14 07:28:11213 void TransformRect(RectF* rect) const;
[email protected]80248e32011-07-08 15:31:11214
[email protected]f4d2b9012013-10-11 19:10:50215 // Applies the reverse transformation on the given rect. After the function
216 // completes, |rect| will be the smallest axis aligned bounding rect
217 // containing the transformed rect. Returns false if the matrix cannot be
218 // inverted.
[email protected]79fbdab02012-11-14 07:28:11219 bool TransformRectReverse(RectF* rect) const;
[email protected]277c7b72011-06-06 15:23:09220
[email protected]f4d2b9012013-10-11 19:10:50221 // Applies transformation on the given box. After the function completes,
222 // |box| will be the smallest axis aligned bounding box containing the
223 // transformed box.
224 void TransformBox(BoxF* box) const;
225
226 // Applies the reverse transformation on the given box. After the function
227 // completes, |box| will be the smallest axis aligned bounding box
228 // containing the transformed box. Returns false if the matrix cannot be
229 // inverted.
230 bool TransformBoxReverse(BoxF* box) const;
231
[email protected]2fcafa02012-11-15 01:12:55232 // Decomposes |this| and |from|, interpolates the decomposed values, and
233 // sets |this| to the reconstituted result. Returns false if either matrix
234 // can't be decomposed. Uses routines described in this spec:
235 // http://www.w3.org/TR/css3-3d-transforms/.
236 //
237 // Note: this call is expensive since we need to decompose the transform. If
238 // you're going to be calling this rapidly (e.g., in an animation) you should
239 // decompose once using gfx::DecomposeTransforms and reuse your
240 // DecomposedTransform.
[email protected]6138db702013-09-25 03:25:05241 bool Blend(const Transform& from, double progress);
[email protected]2fcafa02012-11-15 01:12:55242
[email protected]f7c321eb2012-11-26 20:13:08243 // Returns |this| * |other|.
[email protected]d9a6f3302012-12-07 19:17:54244 Transform operator*(const Transform& other) const {
245 return Transform(*this, other);
246 }
[email protected]f7c321eb2012-11-26 20:13:08247
248 // Sets |this| = |this| * |other|
[email protected]0db13222012-12-13 21:27:54249 Transform& operator*=(const Transform& other) {
250 PreconcatTransform(other);
251 return *this;
252 }
[email protected]f7c321eb2012-11-26 20:13:08253
[email protected]b9b1e7a42011-05-17 15:29:51254 // Returns the underlying matrix.
[email protected]80248e32011-07-08 15:31:11255 const SkMatrix44& matrix() const { return matrix_; }
256 SkMatrix44& matrix() { return matrix_; }
[email protected]b9b1e7a42011-05-17 15:29:51257
[email protected]6ab42d92012-12-20 20:36:53258 std::string ToString() const;
259
[email protected]b9b1e7a42011-05-17 15:29:51260 private:
[email protected]80248e32011-07-08 15:31:11261 void TransformPointInternal(const SkMatrix44& xform,
[email protected]26d7ece2013-09-12 20:59:47262 Point* point) const;
[email protected]80248e32011-07-08 15:31:11263
264 void TransformPointInternal(const SkMatrix44& xform,
[email protected]26d7ece2013-09-12 20:59:47265 Point3F* point) const;
[email protected]80248e32011-07-08 15:31:11266
267 SkMatrix44 matrix_;
[email protected]b9b1e7a42011-05-17 15:29:51268
269 // copy/assign are allowed.
[email protected]36df22b2011-02-24 21:47:56270};
271
[email protected]a109fd02014-07-10 07:41:43272// This is declared here for use in gtest-based unit tests but is defined in
273// the gfx_test_support target. Depend on that to use this in your unit test.
274// This should not be used in production code - call ToString() instead.
275void PrintTo(const Transform& transform, ::std::ostream* os);
276
[email protected]0f0453e2012-10-14 18:15:35277} // namespace gfx
[email protected]36df22b2011-02-24 21:47:56278
279#endif // UI_GFX_TRANSFORM_H_