OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_TRANSFORM_H_ | 5 #ifndef UI_GFX_TRANSFORM_H_ |
6 #define UI_GFX_TRANSFORM_H_ | 6 #define UI_GFX_TRANSFORM_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include "base/template_util.h" |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
11 #include "third_party/skia/include/core/SkMatrix.h" | 12 #include "third_party/skia/include/utils/SkMatrix44.h" |
| 13 #include "ui/gfx/point.h" |
| 14 |
| 15 #include <cmath> |
12 | 16 |
13 namespace gfx { | 17 namespace gfx { |
14 class Point; | |
15 class Rect; | 18 class Rect; |
16 } | 19 } |
17 | 20 |
18 namespace ui { | 21 namespace ui { |
19 | 22 |
20 // 3x3 transformation matrix. Transform is cheap and explicitly allows | 23 // 4x4 transformation matrix. Transform is cheap and explicitly allows |
21 // copy/assign. | 24 // copy/assign. |
22 // TODO: make this a 4x4. | |
23 class Transform { | 25 class Transform { |
24 public: | 26 public: |
25 Transform(); | 27 Transform(); |
26 ~Transform(); | 28 ~Transform(); |
27 | 29 |
28 // NOTE: The 'Set' functions overwrite the previously set transformation | 30 // NOTE: The 'Set' functions overwrite the previously set transformation |
29 // parameters. The 'Concat' functions apply a transformation (e.g. rotation, | 31 // parameters. The 'Concat' functions apply a transformation (e.g. rotation, |
30 // scale, translate) on top of the existing transforms, instead of overwriting | 32 // scale, translate) on top of the existing transforms, instead of overwriting |
31 // them. | 33 // them. |
32 | 34 |
(...skipping 17 matching lines...) Expand all Loading... |
50 // Applies rotation on the current transformation. | 52 // Applies rotation on the current transformation. |
51 void ConcatRotate(float degree); | 53 void ConcatRotate(float degree); |
52 | 54 |
53 // Applies scaling on current transform. | 55 // Applies scaling on current transform. |
54 void ConcatScale(float x, float y); | 56 void ConcatScale(float x, float y); |
55 | 57 |
56 // Applies translation on current transform. | 58 // Applies translation on current transform. |
57 void ConcatTranslate(float x, float y); | 59 void ConcatTranslate(float x, float y); |
58 | 60 |
59 // Applies a transformation on the current transformation | 61 // Applies a transformation on the current transformation |
60 // (i.e. 'this = this * transform;'). Returns true if the result can be | 62 // (i.e. 'this = this * transform;'). |
61 // represented. | 63 void PreconcatTransform(const Transform& transform); |
62 bool PreconcatTransform(const Transform& transform); | |
63 | 64 |
64 // Applies a transformation on the current transformation | 65 // Applies a transformation on the current transformation |
65 // (i.e. 'this = transform * this;'). Returns true if the result can be | 66 // (i.e. 'this = transform * this;'). |
66 // represented. | 67 void ConcatTransform(const Transform& transform); |
67 bool ConcatTransform(const Transform& transform); | |
68 | 68 |
69 // Does the transformation change anything? | 69 // Does the transformation change anything? |
70 bool HasChange() const; | 70 bool HasChange() const; |
71 | 71 |
72 // Applies the transformation on the point. Returns true if the point is | 72 // Applies the transformation on the point. Returns true if the point is |
73 // transformed successfully. | 73 // transformed successfully. |
74 bool TransformPoint(gfx::Point* point); | 74 template <class T> |
| 75 bool TransformPoint(T& point) const; |
75 | 76 |
76 // Applies the reverse transformation on the point. Returns true if the point | 77 // Applies the reverse transformation on the point. Returns true if the |
77 // is transformed successfully. | 78 // transformation can be inverted |
78 bool TransformPointReverse(gfx::Point* point); | 79 template <class T> |
| 80 bool TransformPointReverse(T& point) const; |
79 | 81 |
80 // Applies transformation on the rectangle. Returns true if the rectangle is | 82 // Applies transformation on the rectangle. Returns true if the rectangle is |
81 // transformed successfully. | 83 // transformed successfully. |
82 bool TransformRect(gfx::Rect* rect); | 84 bool TransformRect(gfx::Rect* rect) const; |
83 | 85 |
84 // Applies the reverse transformation on the rectangle. Returns true if the | 86 // Applies the reverse transformation on the rectangle. Returns true if the |
85 // rectangle is transformed successfully. | 87 // rectangle is transformed successfully. |
86 bool TransformRectReverse(gfx::Rect* rect); | 88 bool TransformRectReverse(gfx::Rect* rect) const; |
87 | 89 |
88 // Returns the underlying matrix. | 90 // Returns the underlying matrix. |
89 const SkMatrix& matrix() const { return matrix_; } | 91 const SkMatrix44& matrix() const { return matrix_; } |
90 | 92 |
91 private: | 93 private: |
92 SkMatrix matrix_; | 94 template <typename T> |
| 95 void TransformPointInternal(const SkMatrix44& xform, T& point) const; |
| 96 |
| 97 template <typename T> |
| 98 static int SymmetricRound(T value); |
| 99 |
| 100 SkMatrix44 matrix_; |
93 | 101 |
94 // copy/assign are allowed. | 102 // copy/assign are allowed. |
95 }; | 103 }; |
96 | 104 |
97 } // namespace ui | 105 template <class T> |
| 106 bool Transform::TransformPoint(T& point) const { |
| 107 TransformPointInternal(matrix_, point); |
| 108 return true; |
| 109 } |
| 110 |
| 111 template <class T> |
| 112 bool Transform::TransformPointReverse(T& point) const { |
| 113 // TODO(sad): Try to avoid trying to invert the matrix. |
| 114 SkMatrix44 inverse; |
| 115 if (!matrix_.invert(&inverse)) |
| 116 return false; |
| 117 |
| 118 TransformPointInternal(inverse, point); |
| 119 return true; |
| 120 } |
| 121 |
| 122 template <typename T> |
| 123 void Transform::TransformPointInternal(const SkMatrix44& xform, |
| 124 T& point) const { |
| 125 SkScalar p[4] = { |
| 126 point[0], |
| 127 point[1], |
| 128 point[2], |
| 129 1 }; |
| 130 |
| 131 xform.map(p); |
| 132 |
| 133 if (p[3] != 1 && abs(p[3]) > 0) { |
| 134 point[0] = p[0] / p[3]; |
| 135 point[1] = p[1] / p[3]; |
| 136 point[2] = p[2] / p[3]; |
| 137 } else { |
| 138 point[0] = p[0]; |
| 139 point[1] = p[1]; |
| 140 point[2] = p[2]; |
| 141 } |
| 142 } |
| 143 |
| 144 template <> |
| 145 inline void Transform::TransformPointInternal<gfx::Point>( |
| 146 const SkMatrix44& xform, |
| 147 gfx::Point& point) const { |
| 148 |
| 149 SkScalar p[4] = { |
| 150 SkIntToScalar(point.x()), |
| 151 SkIntToScalar(point.y()), |
| 152 0, |
| 153 1 }; |
| 154 |
| 155 xform.map(p); |
| 156 point.SetPoint(SymmetricRound(p[0]), |
| 157 SymmetricRound(p[1])); |
| 158 } |
| 159 |
| 160 template <typename T> |
| 161 int Transform::SymmetricRound(T x) { |
| 162 return static_cast<int>( |
| 163 x > 0.0f |
| 164 ? std::floor(x + 0.5f) |
| 165 : std::ceil(x - 0.5f)); |
| 166 } |
| 167 |
| 168 }// namespace ui |
98 | 169 |
99 #endif // UI_GFX_TRANSFORM_H_ | 170 #endif // UI_GFX_TRANSFORM_H_ |
OLD | NEW |