OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "ui/gfx/transform.h" | 5 #include "ui/gfx/transform.h" |
6 | 6 |
7 #include "ui/gfx/point3.h" | 7 #include "ui/gfx/point3.h" |
8 #include "ui/gfx/rect.h" | 8 #include "ui/gfx/rect.h" |
9 #include "ui/gfx/skia_util.h" | 9 #include "ui/gfx/skia_util.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 static int SymmetricRound(float x) { | 13 static int SymmetricRound(float x) { |
14 return static_cast<int>( | 14 return static_cast<int>( |
15 x > 0 | 15 x > 0 |
16 ? std::floor(x + 0.5f) | 16 ? std::floor(x + 0.5f) |
17 : std::ceil(x - 0.5f)); | 17 : std::ceil(x - 0.5f)); |
18 } | 18 } |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 namespace ui { | 22 namespace gfx { |
23 | 23 |
24 Transform::Transform() { | 24 Transform::Transform() { |
25 matrix_.reset(); | 25 matrix_.reset(); |
26 } | 26 } |
27 | 27 |
28 Transform::~Transform() {} | 28 Transform::~Transform() {} |
29 | 29 |
30 bool Transform::operator==(const Transform& rhs) const { | 30 bool Transform::operator==(const Transform& rhs) const { |
31 return matrix_ == rhs.matrix_; | 31 return matrix_ == rhs.matrix_; |
32 } | 32 } |
33 | 33 |
34 bool Transform::operator!=(const Transform& rhs) const { | 34 bool Transform::operator!=(const Transform& rhs) const { |
35 return !(*this == rhs); | 35 return !(*this == rhs); |
36 } | 36 } |
37 | 37 |
38 void Transform::SetRotate(float degree) { | 38 void Transform::SetRotate(float degree) { |
39 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); | 39 matrix_.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
40 } | 40 } |
41 | 41 |
42 void Transform::SetRotateAbout(const gfx::Point3f& axis, float degree) { | 42 void Transform::SetRotateAbout(const Point3f& axis, float degree) { |
43 matrix_.setRotateDegreesAbout(axis.x(), | 43 matrix_.setRotateDegreesAbout(axis.x(), |
44 axis.y(), | 44 axis.y(), |
45 axis.z(), | 45 axis.z(), |
46 SkFloatToScalar(degree)); | 46 SkFloatToScalar(degree)); |
47 } | 47 } |
48 | 48 |
49 void Transform::SetScaleX(float x) { | 49 void Transform::SetScaleX(float x) { |
50 matrix_.set(0, 0, SkFloatToScalar(x)); | 50 matrix_.set(0, 0, SkFloatToScalar(x)); |
51 } | 51 } |
52 | 52 |
(...skipping 26 matching lines...) Expand all Loading... |
79 m.set(3, 2, -1 / depth); | 79 m.set(3, 2, -1 / depth); |
80 matrix_ = m; | 80 matrix_ = m; |
81 } | 81 } |
82 | 82 |
83 void Transform::ConcatRotate(float degree) { | 83 void Transform::ConcatRotate(float degree) { |
84 SkMatrix44 rot; | 84 SkMatrix44 rot; |
85 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); | 85 rot.setRotateDegreesAbout(0, 0, 1, SkFloatToScalar(degree)); |
86 matrix_.postConcat(rot); | 86 matrix_.postConcat(rot); |
87 } | 87 } |
88 | 88 |
89 void Transform::ConcatRotateAbout(const gfx::Point3f& axis, float degree) { | 89 void Transform::ConcatRotateAbout(const Point3f& axis, float degree) { |
90 SkMatrix44 rot; | 90 SkMatrix44 rot; |
91 rot.setRotateDegreesAbout(axis.x(), | 91 rot.setRotateDegreesAbout(axis.x(), |
92 axis.y(), | 92 axis.y(), |
93 axis.z(), | 93 axis.z(), |
94 SkFloatToScalar(degree)); | 94 SkFloatToScalar(degree)); |
95 matrix_.postConcat(rot); | 95 matrix_.postConcat(rot); |
96 } | 96 } |
97 | 97 |
98 void Transform::ConcatScale(float x, float y) { | 98 void Transform::ConcatScale(float x, float y) { |
99 SkMatrix44 scale; | 99 SkMatrix44 scale; |
(...skipping 20 matching lines...) Expand all Loading... |
120 } | 120 } |
121 | 121 |
122 bool Transform::HasChange() const { | 122 bool Transform::HasChange() const { |
123 return !matrix_.isIdentity(); | 123 return !matrix_.isIdentity(); |
124 } | 124 } |
125 | 125 |
126 bool Transform::GetInverse(Transform* transform) const { | 126 bool Transform::GetInverse(Transform* transform) const { |
127 return matrix_.invert(&transform->matrix_); | 127 return matrix_.invert(&transform->matrix_); |
128 } | 128 } |
129 | 129 |
130 void Transform::TransformPoint(gfx::Point& point) const { | 130 void Transform::TransformPoint(Point& point) const { |
131 TransformPointInternal(matrix_, point); | 131 TransformPointInternal(matrix_, point); |
132 } | 132 } |
133 | 133 |
134 void Transform::TransformPoint(gfx::Point3f& point) const { | 134 void Transform::TransformPoint(Point3f& point) const { |
135 TransformPointInternal(matrix_, point); | 135 TransformPointInternal(matrix_, point); |
136 } | 136 } |
137 | 137 |
138 bool Transform::TransformPointReverse(gfx::Point& point) const { | 138 bool Transform::TransformPointReverse(Point& point) const { |
139 // TODO(sad): Try to avoid trying to invert the matrix. | 139 // TODO(sad): Try to avoid trying to invert the matrix. |
140 SkMatrix44 inverse; | 140 SkMatrix44 inverse; |
141 if (!matrix_.invert(&inverse)) | 141 if (!matrix_.invert(&inverse)) |
142 return false; | 142 return false; |
143 | 143 |
144 TransformPointInternal(inverse, point); | 144 TransformPointInternal(inverse, point); |
145 return true; | 145 return true; |
146 } | 146 } |
147 | 147 |
148 bool Transform::TransformPointReverse(gfx::Point3f& point) const { | 148 bool Transform::TransformPointReverse(Point3f& point) const { |
149 // TODO(sad): Try to avoid trying to invert the matrix. | 149 // TODO(sad): Try to avoid trying to invert the matrix. |
150 SkMatrix44 inverse; | 150 SkMatrix44 inverse; |
151 if (!matrix_.invert(&inverse)) | 151 if (!matrix_.invert(&inverse)) |
152 return false; | 152 return false; |
153 | 153 |
154 TransformPointInternal(inverse, point); | 154 TransformPointInternal(inverse, point); |
155 return true; | 155 return true; |
156 } | 156 } |
157 | 157 |
158 void Transform::TransformRect(gfx::Rect* rect) const { | 158 void Transform::TransformRect(Rect* rect) const { |
159 SkRect src = gfx::RectToSkRect(*rect); | 159 SkRect src = RectToSkRect(*rect); |
160 const SkMatrix& matrix = matrix_; | 160 const SkMatrix& matrix = matrix_; |
161 matrix.mapRect(&src); | 161 matrix.mapRect(&src); |
162 *rect = gfx::SkRectToRect(src); | 162 *rect = SkRectToRect(src); |
163 } | 163 } |
164 | 164 |
165 bool Transform::TransformRectReverse(gfx::Rect* rect) const { | 165 bool Transform::TransformRectReverse(Rect* rect) const { |
166 SkMatrix44 inverse; | 166 SkMatrix44 inverse; |
167 if (!matrix_.invert(&inverse)) | 167 if (!matrix_.invert(&inverse)) |
168 return false; | 168 return false; |
169 const SkMatrix& matrix = inverse; | 169 const SkMatrix& matrix = inverse; |
170 SkRect src = gfx::RectToSkRect(*rect); | 170 SkRect src = RectToSkRect(*rect); |
171 matrix.mapRect(&src); | 171 matrix.mapRect(&src); |
172 *rect = gfx::SkRectToRect(src); | 172 *rect = SkRectToRect(src); |
173 return true; | 173 return true; |
174 } | 174 } |
175 | 175 |
176 void Transform::TransformPointInternal(const SkMatrix44& xform, | 176 void Transform::TransformPointInternal(const SkMatrix44& xform, |
177 gfx::Point3f& point) const { | 177 Point3f& point) const { |
178 SkScalar p[4] = { | 178 SkScalar p[4] = { |
179 SkFloatToScalar(point.x()), | 179 SkFloatToScalar(point.x()), |
180 SkFloatToScalar(point.y()), | 180 SkFloatToScalar(point.y()), |
181 SkFloatToScalar(point.z()), | 181 SkFloatToScalar(point.z()), |
182 1 }; | 182 1 }; |
183 | 183 |
184 xform.map(p); | 184 xform.map(p); |
185 | 185 |
186 if (p[3] != 1 && abs(p[3]) > 0) { | 186 if (p[3] != 1 && abs(p[3]) > 0) { |
187 point.SetPoint(p[0] / p[3], p[1] / p[3], p[2]/ p[3]); | 187 point.SetPoint(p[0] / p[3], p[1] / p[3], p[2]/ p[3]); |
188 } else { | 188 } else { |
189 point.SetPoint(p[0], p[1], p[2]); | 189 point.SetPoint(p[0], p[1], p[2]); |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 void Transform::TransformPointInternal(const SkMatrix44& xform, | 193 void Transform::TransformPointInternal(const SkMatrix44& xform, |
194 gfx::Point& point) const { | 194 Point& point) const { |
195 SkScalar p[4] = { | 195 SkScalar p[4] = { |
196 SkIntToScalar(point.x()), | 196 SkIntToScalar(point.x()), |
197 SkIntToScalar(point.y()), | 197 SkIntToScalar(point.y()), |
198 0, | 198 0, |
199 1 }; | 199 1 }; |
200 | 200 |
201 xform.map(p); | 201 xform.map(p); |
202 | 202 |
203 point.SetPoint(SymmetricRound(p[0]), | 203 point.SetPoint(SymmetricRound(p[0]), |
204 SymmetricRound(p[1])); | 204 SymmetricRound(p[1])); |
205 } | 205 } |
206 | 206 |
207 } // namespace ui | 207 } // namespace gfx |
OLD | NEW |