blob: 2be72efd0d7bf91c51409f574ac37f52a9820297 [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]45127922012-11-17 12:24:498#include "base/compiler_specific.h"
[email protected]80248e32011-07-08 15:31:119#include "third_party/skia/include/utils/SkMatrix44.h"
[email protected]507bad52011-08-06 04:51:0710#include "ui/base/ui_export.h"
[email protected]b9b1e7a42011-05-17 15:29:5111
[email protected]36df22b2011-02-24 21:47:5612namespace gfx {
[email protected]0f0453e2012-10-14 18:15:3513
[email protected]79fbdab02012-11-14 07:28:1114class RectF;
[email protected]80248e32011-07-08 15:31:1115class Point;
[email protected]2771b1c2012-10-31 05:15:4316class Point3F;
[email protected]f7c321eb2012-11-26 20:13:0817class Vector3dF;
[email protected]36df22b2011-02-24 21:47:5618
[email protected]80248e32011-07-08 15:31:1119// 4x4 transformation matrix. Transform is cheap and explicitly allows
[email protected]b9b1e7a42011-05-17 15:29:5120// copy/assign.
[email protected]507bad52011-08-06 04:51:0721class UI_EXPORT Transform {
[email protected]36df22b2011-02-24 21:47:5622 public:
[email protected]b9b1e7a42011-05-17 15:29:5123 Transform();
[email protected]d9a6f3302012-12-07 19:17:5424 Transform(const Transform& rhs);
25 // Initialize with the concatenation of lhs * rhs.
26 Transform(const Transform& lhs, const Transform& rhs);
[email protected]b9b1e7a42011-05-17 15:29:5127 ~Transform();
[email protected]36df22b2011-02-24 21:47:5628
[email protected]80248e32011-07-08 15:31:1129 bool operator==(const Transform& rhs) const;
30 bool operator!=(const Transform& rhs) const;
31
[email protected]f7c321eb2012-11-26 20:13:0832 // Resets this transform to the identity transform.
33 void MakeIdentity();
[email protected]2fcafa02012-11-15 01:12:5534
[email protected]2c7cd6d2012-11-28 23:49:2635 // Applies the current transformation on a 2d rotation and assigns the result
[email protected]2fcafa02012-11-15 01:12:5536 // to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2637 void Rotate(double degrees) { RotateAboutZAxis(degrees); }
[email protected]2fcafa02012-11-15 01:12:5538
39 // Applies the current transformation on an axis-angle rotation and assigns
40 // the result to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2641 void RotateAboutXAxis(double degrees);
42 void RotateAboutYAxis(double degrees);
43 void RotateAboutZAxis(double degrees);
44 void RotateAbout(const Vector3dF& axis, double degrees);
[email protected]2fcafa02012-11-15 01:12:5545
46 // Applies the current transformation on a scaling and assigns the result
47 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0848 void Scale(double x, double y);
49 void Scale3d(double x, double y, double z);
[email protected]2fcafa02012-11-15 01:12:5550
51 // Applies the current transformation on a translation and assigns the result
52 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0853 void Translate(double x, double y);
54 void Translate3d(double x, double y, double z);
[email protected]2fcafa02012-11-15 01:12:5555
56 // Applies the current transformation on a skew and assigns the result
57 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0858 void SkewX(double angle_x);
59 void SkewY(double angle_y);
[email protected]2fcafa02012-11-15 01:12:5560
61 // Applies the current transformation on a perspective transform and assigns
62 // the result to |this|.
[email protected]f7c321eb2012-11-26 20:13:0863 void ApplyPerspectiveDepth(double depth);
[email protected]2fcafa02012-11-15 01:12:5564
[email protected]b9b1e7a42011-05-17 15:29:5165 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:1166 // (i.e. 'this = this * transform;').
67 void PreconcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:5668
[email protected]598080082011-04-14 19:36:3369 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:1170 // (i.e. 'this = transform * this;').
71 void ConcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:5672
[email protected]45127922012-11-17 12:24:4973 // Returns true if this is the identity matrix.
[email protected]d9a6f3302012-12-07 19:17:5474 bool IsIdentity() const { return matrix_.isIdentity(); }
[email protected]45127922012-11-17 12:24:4975
[email protected]2c7cd6d2012-11-28 23:49:2676 // Returns true if the matrix is either identity or pure translation.
[email protected]d9a6f3302012-12-07 19:17:5477 bool IsIdentityOrTranslation() const {
78 return !(matrix_.getType() & ~SkMatrix44::kTranslate_Mask);
79 }
[email protected]2c7cd6d2012-11-28 23:49:2680
[email protected]d1a56f02012-12-04 12:18:3081 // Returns true if the matrix is either identity or pure, non-fractional
82 // translation.
83 bool IsIdentityOrIntegerTranslation() const;
84
[email protected]2c7cd6d2012-11-28 23:49:2685 // Returns true if the matrix is has only scaling and translation components.
86 bool IsScaleOrTranslation() const;
87
88 // Returns true if the matrix has any perspective component that would
89 // change the w-component of a homogeneous point.
90 bool HasPerspective() const;
91
[email protected]45127922012-11-17 12:24:4992 // Returns true if this transform is non-singular.
93 bool IsInvertible() const;
[email protected]36df22b2011-02-24 21:47:5694
[email protected]2c7cd6d2012-11-28 23:49:2695 // Returns true if a layer with a forward-facing normal of (0, 0, 1) would
96 // have its back side facing frontwards after applying the transform.
97 bool IsBackFaceVisible() const;
98
[email protected]38919392011-10-24 22:26:2399 // Inverts the transform which is passed in. Returns true if successful.
[email protected]45127922012-11-17 12:24:49100 bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT;
101
102 // Transposes this transform in place.
103 void Transpose();
[email protected]38919392011-10-24 22:26:23104
[email protected]598080082011-04-14 19:36:33105 // Applies the transformation on the point. Returns true if the point is
106 // transformed successfully.
[email protected]2771b1c2012-10-31 05:15:43107 void TransformPoint(Point3F& point) const;
[email protected]36df22b2011-02-24 21:47:56108
[email protected]80248e32011-07-08 15:31:11109 // Applies the transformation on the point. Returns true if the point is
110 // transformed successfully. Rounds the result to the nearest point.
[email protected]0f0453e2012-10-14 18:15:35111 void TransformPoint(Point& point) const;
[email protected]36df22b2011-02-24 21:47:56112
[email protected]80248e32011-07-08 15:31:11113 // Applies the reverse transformation on the point. Returns true if the
114 // transformation can be inverted.
[email protected]2771b1c2012-10-31 05:15:43115 bool TransformPointReverse(Point3F& point) const;
[email protected]463eb0e2011-05-10 03:11:04116
[email protected]80248e32011-07-08 15:31:11117 // Applies the reverse transformation on the point. Returns true if the
118 // transformation can be inverted. Rounds the result to the nearest point.
[email protected]0f0453e2012-10-14 18:15:35119 bool TransformPointReverse(Point& point) const;
[email protected]80248e32011-07-08 15:31:11120
121 // Applies transformation on the rectangle. Returns true if the transformed
122 // rectangle was axis aligned. If it returns false, rect will be the
[email protected]91f4c792012-06-06 02:21:19123 // smallest axis aligned bounding box containing the transformed rect.
[email protected]79fbdab02012-11-14 07:28:11124 void TransformRect(RectF* rect) const;
[email protected]80248e32011-07-08 15:31:11125
126 // Applies the reverse transformation on the rectangle. Returns true if
127 // the transformed rectangle was axis aligned. If it returns false,
[email protected]91f4c792012-06-06 02:21:19128 // rect will be the smallest axis aligned bounding box containing the
[email protected]80248e32011-07-08 15:31:11129 // transformed rect.
[email protected]79fbdab02012-11-14 07:28:11130 bool TransformRectReverse(RectF* rect) const;
[email protected]277c7b72011-06-06 15:23:09131
[email protected]2fcafa02012-11-15 01:12:55132 // Decomposes |this| and |from|, interpolates the decomposed values, and
133 // sets |this| to the reconstituted result. Returns false if either matrix
134 // can't be decomposed. Uses routines described in this spec:
135 // http://www.w3.org/TR/css3-3d-transforms/.
136 //
137 // Note: this call is expensive since we need to decompose the transform. If
138 // you're going to be calling this rapidly (e.g., in an animation) you should
139 // decompose once using gfx::DecomposeTransforms and reuse your
140 // DecomposedTransform.
141 bool Blend(const Transform& from, double progress);
142
[email protected]f7c321eb2012-11-26 20:13:08143 // Returns |this| * |other|.
[email protected]d9a6f3302012-12-07 19:17:54144 Transform operator*(const Transform& other) const {
145 return Transform(*this, other);
146 }
[email protected]f7c321eb2012-11-26 20:13:08147
148 // Sets |this| = |this| * |other|
149 Transform& operator*=(const Transform& other);
150
[email protected]b9b1e7a42011-05-17 15:29:51151 // Returns the underlying matrix.
[email protected]80248e32011-07-08 15:31:11152 const SkMatrix44& matrix() const { return matrix_; }
153 SkMatrix44& matrix() { return matrix_; }
[email protected]b9b1e7a42011-05-17 15:29:51154
155 private:
[email protected]80248e32011-07-08 15:31:11156 void TransformPointInternal(const SkMatrix44& xform,
[email protected]0f0453e2012-10-14 18:15:35157 Point& point) const;
[email protected]80248e32011-07-08 15:31:11158
159 void TransformPointInternal(const SkMatrix44& xform,
[email protected]2771b1c2012-10-31 05:15:43160 Point3F& point) const;
[email protected]80248e32011-07-08 15:31:11161
162 SkMatrix44 matrix_;
[email protected]b9b1e7a42011-05-17 15:29:51163
164 // copy/assign are allowed.
[email protected]36df22b2011-02-24 21:47:56165};
166
[email protected]0f0453e2012-10-14 18:15:35167} // namespace gfx
[email protected]36df22b2011-02-24 21:47:56168
169#endif // UI_GFX_TRANSFORM_H_