blob: 6d1ce1e1ad54e1f0ba78dcc60288f98d390b4689 [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();
24 ~Transform();
[email protected]36df22b2011-02-24 21:47:5625
[email protected]80248e32011-07-08 15:31:1126 bool operator==(const Transform& rhs) const;
27 bool operator!=(const Transform& rhs) const;
28
[email protected]f7c321eb2012-11-26 20:13:0829 // Resets this transform to the identity transform.
30 void MakeIdentity();
[email protected]2fcafa02012-11-15 01:12:5531
[email protected]2c7cd6d2012-11-28 23:49:2632 // Applies the current transformation on a 2d rotation and assigns the result
[email protected]2fcafa02012-11-15 01:12:5533 // to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2634 void Rotate(double degrees) { RotateAboutZAxis(degrees); }
[email protected]2fcafa02012-11-15 01:12:5535
36 // Applies the current transformation on an axis-angle rotation and assigns
37 // the result to |this|.
[email protected]2c7cd6d2012-11-28 23:49:2638 void RotateAboutXAxis(double degrees);
39 void RotateAboutYAxis(double degrees);
40 void RotateAboutZAxis(double degrees);
41 void RotateAbout(const Vector3dF& axis, double degrees);
[email protected]2fcafa02012-11-15 01:12:5542
43 // Applies the current transformation on a scaling and assigns the result
44 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0845 void Scale(double x, double y);
46 void Scale3d(double x, double y, double z);
[email protected]2fcafa02012-11-15 01:12:5547
48 // Applies the current transformation on a translation and assigns the result
49 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0850 void Translate(double x, double y);
51 void Translate3d(double x, double y, double z);
[email protected]2fcafa02012-11-15 01:12:5552
53 // Applies the current transformation on a skew and assigns the result
54 // to |this|.
[email protected]f7c321eb2012-11-26 20:13:0855 void SkewX(double angle_x);
56 void SkewY(double angle_y);
[email protected]2fcafa02012-11-15 01:12:5557
58 // Applies the current transformation on a perspective transform and assigns
59 // the result to |this|.
[email protected]f7c321eb2012-11-26 20:13:0860 void ApplyPerspectiveDepth(double depth);
[email protected]2fcafa02012-11-15 01:12:5561
[email protected]b9b1e7a42011-05-17 15:29:5162 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:1163 // (i.e. 'this = this * transform;').
64 void PreconcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:5665
[email protected]598080082011-04-14 19:36:3366 // Applies a transformation on the current transformation
[email protected]80248e32011-07-08 15:31:1167 // (i.e. 'this = transform * this;').
68 void ConcatTransform(const Transform& transform);
[email protected]36df22b2011-02-24 21:47:5669
[email protected]45127922012-11-17 12:24:4970 // Returns true if this is the identity matrix.
71 bool IsIdentity() const;
72
[email protected]2c7cd6d2012-11-28 23:49:2673 // Returns true if the matrix is either identity or pure translation.
74 bool IsIdentityOrTranslation() const;
75
[email protected]d1a56f02012-12-04 12:18:3076 // Returns true if the matrix is either identity or pure, non-fractional
77 // translation.
78 bool IsIdentityOrIntegerTranslation() const;
79
[email protected]2c7cd6d2012-11-28 23:49:2680 // 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]45127922012-11-17 12:24:4987 // Returns true if this transform is non-singular.
88 bool IsInvertible() const;
[email protected]36df22b2011-02-24 21:47:5689
[email protected]2c7cd6d2012-11-28 23:49:2690 // 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]38919392011-10-24 22:26:2394 // Inverts the transform which is passed in. Returns true if successful.
[email protected]45127922012-11-17 12:24:4995 bool GetInverse(Transform* transform) const WARN_UNUSED_RESULT;
96
97 // Transposes this transform in place.
98 void Transpose();
[email protected]38919392011-10-24 22:26:2399
[email protected]598080082011-04-14 19:36:33100 // Applies the transformation on the point. Returns true if the point is
101 // transformed successfully.
[email protected]2771b1c2012-10-31 05:15:43102 void TransformPoint(Point3F& point) const;
[email protected]36df22b2011-02-24 21:47:56103
[email protected]80248e32011-07-08 15:31:11104 // Applies the transformation on the point. Returns true if the point is
105 // transformed successfully. Rounds the result to the nearest point.
[email protected]0f0453e2012-10-14 18:15:35106 void TransformPoint(Point& point) const;
[email protected]36df22b2011-02-24 21:47:56107
[email protected]80248e32011-07-08 15:31:11108 // Applies the reverse transformation on the point. Returns true if the
109 // transformation can be inverted.
[email protected]2771b1c2012-10-31 05:15:43110 bool TransformPointReverse(Point3F& point) const;
[email protected]463eb0e2011-05-10 03:11:04111
[email protected]80248e32011-07-08 15:31:11112 // 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]0f0453e2012-10-14 18:15:35114 bool TransformPointReverse(Point& point) const;
[email protected]80248e32011-07-08 15:31:11115
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]91f4c792012-06-06 02:21:19118 // smallest axis aligned bounding box containing the transformed rect.
[email protected]79fbdab02012-11-14 07:28:11119 void TransformRect(RectF* rect) const;
[email protected]80248e32011-07-08 15:31:11120
121 // Applies the reverse transformation on the rectangle. Returns true if
122 // the transformed rectangle was axis aligned. If it returns false,
[email protected]91f4c792012-06-06 02:21:19123 // rect will be the smallest axis aligned bounding box containing the
[email protected]80248e32011-07-08 15:31:11124 // transformed rect.
[email protected]79fbdab02012-11-14 07:28:11125 bool TransformRectReverse(RectF* rect) const;
[email protected]277c7b72011-06-06 15:23:09126
[email protected]2fcafa02012-11-15 01:12:55127 // 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]f7c321eb2012-11-26 20:13:08138 // 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]b9b1e7a42011-05-17 15:29:51144 // Returns the underlying matrix.
[email protected]80248e32011-07-08 15:31:11145 const SkMatrix44& matrix() const { return matrix_; }
146 SkMatrix44& matrix() { return matrix_; }
[email protected]b9b1e7a42011-05-17 15:29:51147
148 private:
[email protected]80248e32011-07-08 15:31:11149 void TransformPointInternal(const SkMatrix44& xform,
[email protected]0f0453e2012-10-14 18:15:35150 Point& point) const;
[email protected]80248e32011-07-08 15:31:11151
152 void TransformPointInternal(const SkMatrix44& xform,
[email protected]2771b1c2012-10-31 05:15:43153 Point3F& point) const;
[email protected]80248e32011-07-08 15:31:11154
155 SkMatrix44 matrix_;
[email protected]b9b1e7a42011-05-17 15:29:51156
157 // copy/assign are allowed.
[email protected]36df22b2011-02-24 21:47:56158};
159
[email protected]0f0453e2012-10-14 18:15:35160} // namespace gfx
[email protected]36df22b2011-02-24 21:47:56161
162#endif // UI_GFX_TRANSFORM_H_