OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "cc/base/math_util.h" | 5 #include "cc/base/math_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 if (transform.IsIdentityOrIntegerTranslation()) { | 123 if (transform.IsIdentityOrIntegerTranslation()) { |
124 return src_rect + | 124 return src_rect + |
125 gfx::Vector2d( | 125 gfx::Vector2d( |
126 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | 126 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), |
127 static_cast<int>( | 127 static_cast<int>( |
128 SkMScalarToFloat(transform.matrix().get(1, 3)))); | 128 SkMScalarToFloat(transform.matrix().get(1, 3)))); |
129 } | 129 } |
130 return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect))); | 130 return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect))); |
131 } | 131 } |
132 | 132 |
133 gfx::Rect MathUtil::MapEnclosedClippedRect(const gfx::Transform& transform, | |
134 const gfx::Rect& src_rect) { | |
135 if (transform.IsIdentityOrIntegerTranslation()) { | |
136 return src_rect + | |
137 gfx::Vector2d( | |
138 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | |
139 static_cast<int>( | |
140 SkMScalarToFloat(transform.matrix().get(1, 3)))); | |
141 } | |
142 return gfx::ToEnclosedRect(MapClippedRect(transform, gfx::RectF(src_rect))); | |
143 } | |
144 | |
145 gfx::Rect MathUtil::MapEnclosedNonClippedRect(const gfx::Transform& transform, | |
146 const gfx::Rect& src_rect) { | |
147 if (transform.IsIdentityOrIntegerTranslation()) { | |
148 return src_rect + | |
149 gfx::Vector2d( | |
150 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | |
151 static_cast<int>( | |
152 SkMScalarToFloat(transform.matrix().get(1, 3)))); | |
153 } | |
154 | |
155 SkMScalar quad[4 * 2]; // input: 4 x 2D points | |
156 quad[0] = src_rect.x(); | |
157 quad[1] = src_rect.y(); | |
158 quad[2] = src_rect.right(); | |
159 quad[3] = src_rect.y(); | |
160 quad[4] = src_rect.right(); | |
161 quad[5] = src_rect.bottom(); | |
162 quad[6] = src_rect.x(); | |
163 quad[7] = src_rect.bottom(); | |
164 | |
165 SkMScalar result[4 * 4]; // output: 4 x 4D homogeneous points | |
166 transform.matrix().map2(quad, 4, result); | |
167 | |
168 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); | |
169 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); | |
170 HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]); | |
171 HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]); | |
172 DCHECK(!hc0.ShouldBeClipped()); | |
173 DCHECK(!hc1.ShouldBeClipped()); | |
174 DCHECK(!hc2.ShouldBeClipped()); | |
175 DCHECK(!hc3.ShouldBeClipped()); | |
176 | |
177 gfx::QuadF mapped_quad = gfx::QuadF(hc0.CartesianPoint2d(), | |
178 hc1.CartesianPoint2d(), | |
179 hc2.CartesianPoint2d(), | |
180 hc3.CartesianPoint2d()); | |
181 return gfx::ToEnclosedRect(mapped_quad.BoundingBox()); | |
182 } | |
183 | |
133 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform, | 184 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform, |
134 const gfx::RectF& src_rect) { | 185 const gfx::RectF& src_rect) { |
135 if (transform.IsIdentityOrTranslation()) { | 186 if (transform.IsIdentityOrTranslation()) { |
136 return src_rect + | 187 return src_rect + |
137 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), | 188 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), |
138 SkMScalarToFloat(transform.matrix().get(1, 3))); | 189 SkMScalarToFloat(transform.matrix().get(1, 3))); |
139 } | 190 } |
140 | 191 |
141 // Apply the transform, but retain the result in homogeneous coordinates. | 192 // Apply the transform, but retain the result in homogeneous coordinates. |
142 | 193 |
(...skipping 23 matching lines...) Expand all Loading... | |
166 return src_rect + | 217 return src_rect + |
167 gfx::Vector2d( | 218 gfx::Vector2d( |
168 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | 219 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), |
169 static_cast<int>( | 220 static_cast<int>( |
170 SkMScalarToFloat(transform.matrix().get(1, 3)))); | 221 SkMScalarToFloat(transform.matrix().get(1, 3)))); |
171 } | 222 } |
172 return gfx::ToEnclosingRect( | 223 return gfx::ToEnclosingRect( |
173 ProjectClippedRect(transform, gfx::RectF(src_rect))); | 224 ProjectClippedRect(transform, gfx::RectF(src_rect))); |
174 } | 225 } |
175 | 226 |
227 gfx::Rect MathUtil::ProjectEnclosedClippedRect(const gfx::Transform& transform, | |
228 const gfx::Rect& src_rect) { | |
229 if (transform.IsIdentityOrIntegerTranslation()) { | |
230 return src_rect + | |
231 gfx::Vector2d( | |
232 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | |
233 static_cast<int>( | |
234 SkMScalarToFloat(transform.matrix().get(1, 3)))); | |
235 } | |
236 return gfx::ToEnclosedRect( | |
237 ProjectClippedRect(transform, gfx::RectF(src_rect))); | |
238 } | |
239 | |
240 gfx::Rect MathUtil::ProjectEnclosedNonClippedRect( | |
241 const gfx::Transform& transform, | |
242 const gfx::Rect& src_rect) { | |
243 if (transform.IsIdentityOrIntegerTranslation()) { | |
244 return src_rect + | |
245 gfx::Vector2d( | |
246 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))), | |
247 static_cast<int>( | |
248 SkMScalarToFloat(transform.matrix().get(1, 3)))); | |
249 } | |
250 | |
251 // Perform the projection, but retain the result in homogeneous coordinates. | |
252 gfx::QuadF q = gfx::QuadF(src_rect); | |
253 HomogeneousCoordinate hc0 = ProjectHomogeneousPoint(transform, q.p1()); | |
254 HomogeneousCoordinate hc1 = ProjectHomogeneousPoint(transform, q.p2()); | |
255 HomogeneousCoordinate hc2 = ProjectHomogeneousPoint(transform, q.p3()); | |
256 HomogeneousCoordinate hc3 = ProjectHomogeneousPoint(transform, q.p4()); | |
257 DCHECK(!hc0.ShouldBeClipped()); | |
258 DCHECK(!hc1.ShouldBeClipped()); | |
259 DCHECK(!hc2.ShouldBeClipped()); | |
260 DCHECK(!hc3.ShouldBeClipped()); | |
261 | |
262 gfx::QuadF projected_quad = gfx::QuadF(hc0.CartesianPoint2d(), | |
263 hc1.CartesianPoint2d(), | |
264 hc2.CartesianPoint2d(), | |
265 hc3.CartesianPoint2d()); | |
266 return gfx::ToEnclosedRect(projected_quad.BoundingBox()); | |
enne (OOO)
2014/08/21 17:54:05
This code was here before, but is this actually ri
danakj
2014/08/21 17:58:08
Ya, good point. I had this thought about everythin
| |
267 } | |
268 | |
176 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform, | 269 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform, |
177 const gfx::RectF& src_rect) { | 270 const gfx::RectF& src_rect) { |
178 if (transform.IsIdentityOrTranslation()) { | 271 if (transform.IsIdentityOrTranslation()) { |
179 return src_rect + | 272 return src_rect + |
180 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), | 273 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), |
181 SkMScalarToFloat(transform.matrix().get(1, 3))); | 274 SkMScalarToFloat(transform.matrix().get(1, 3))); |
182 } | 275 } |
183 | 276 |
184 // Perform the projection, but retain the result in homogeneous coordinates. | 277 // Perform the projection, but retain the result in homogeneous coordinates. |
185 gfx::QuadF q = gfx::QuadF(src_rect); | 278 gfx::QuadF q = gfx::QuadF(src_rect); |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
790 | 883 |
791 double MathUtil::AsDoubleSafely(double value) { | 884 double MathUtil::AsDoubleSafely(double value) { |
792 return std::min(value, std::numeric_limits<double>::max()); | 885 return std::min(value, std::numeric_limits<double>::max()); |
793 } | 886 } |
794 | 887 |
795 float MathUtil::AsFloatSafely(float value) { | 888 float MathUtil::AsFloatSafely(float value) { |
796 return std::min(value, std::numeric_limits<float>::max()); | 889 return std::min(value, std::numeric_limits<float>::max()); |
797 } | 890 } |
798 | 891 |
799 } // namespace cc | 892 } // namespace cc |
OLD | NEW |