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 #ifdef __SSE__ | 10 #ifdef __SSE__ |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 float* xmax, | 148 float* xmax, |
149 float* ymin, | 149 float* ymin, |
150 float* ymax, | 150 float* ymax, |
151 const gfx::PointF& p) { | 151 const gfx::PointF& p) { |
152 *xmin = std::min(p.x(), *xmin); | 152 *xmin = std::min(p.x(), *xmin); |
153 *xmax = std::max(p.x(), *xmax); | 153 *xmax = std::max(p.x(), *xmax); |
154 *ymin = std::min(p.y(), *ymin); | 154 *ymin = std::min(p.y(), *ymin); |
155 *ymax = std::max(p.y(), *ymax); | 155 *ymax = std::max(p.y(), *ymax); |
156 } | 156 } |
157 | 157 |
| 158 static inline bool IsNearlyTheSame(const float f, const float g) { |
| 159 // The idea behind this is to use this fraction of the larger of the |
| 160 // two numbers as the limit of the difference. This breaks down near |
| 161 // zero, so we reuse this as the minimum absolute size we will use |
| 162 // for the base of the scale too. |
| 163 static const float epsilon_scale = 0.00001f; |
| 164 return std::abs(f - g) < |
| 165 epsilon_scale * |
| 166 std::max(std::max(std::abs(f), std::abs(g)), epsilon_scale); |
| 167 } |
| 168 |
| 169 static inline bool IsNearlyTheSame(const gfx::PointF& lhs, |
| 170 const gfx::PointF& rhs) { |
| 171 return IsNearlyTheSame(lhs.x(), rhs.x()) && IsNearlyTheSame(lhs.y(), rhs.y()); |
| 172 } |
| 173 |
| 174 static inline bool IsNearlyTheSame(const gfx::Point3F& lhs, |
| 175 const gfx::Point3F& rhs) { |
| 176 return IsNearlyTheSame(lhs.x(), rhs.x()) && |
| 177 IsNearlyTheSame(lhs.y(), rhs.y()) && IsNearlyTheSame(lhs.z(), rhs.z()); |
| 178 } |
| 179 |
158 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, | 180 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, |
159 gfx::PointF clipped_quad[8], | 181 gfx::PointF clipped_quad[8], |
160 int* num_vertices_in_clipped_quad) { | 182 int* num_vertices_in_clipped_quad) { |
| 183 if (*num_vertices_in_clipped_quad > 0 && |
| 184 IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], |
| 185 new_vertex)) |
| 186 return; |
| 187 |
161 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 188 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
162 (*num_vertices_in_clipped_quad)++; | 189 (*num_vertices_in_clipped_quad)++; |
163 } | 190 } |
164 | 191 |
165 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, | 192 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, |
166 gfx::Point3F clipped_quad[8], | 193 gfx::Point3F clipped_quad[8], |
167 int* num_vertices_in_clipped_quad) { | 194 int* num_vertices_in_clipped_quad) { |
| 195 if (*num_vertices_in_clipped_quad > 0 && |
| 196 IsNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], |
| 197 new_vertex)) |
| 198 return; |
| 199 |
168 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 200 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
169 (*num_vertices_in_clipped_quad)++; | 201 (*num_vertices_in_clipped_quad)++; |
170 } | 202 } |
171 | 203 |
172 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, | 204 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, |
173 const gfx::Rect& src_rect) { | 205 const gfx::Rect& src_rect) { |
174 if (transform.IsIdentityOrIntegerTranslation()) { | 206 if (transform.IsIdentityOrIntegerTranslation()) { |
175 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), | 207 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), |
176 static_cast<int>(transform.matrix().getFloat(1, 3))); | 208 static_cast<int>(transform.matrix().getFloat(1, 3))); |
177 return src_rect + offset; | 209 return src_rect + offset; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 if (!h4.ShouldBeClipped()) { | 370 if (!h4.ShouldBeClipped()) { |
339 AddVertexToClippedQuad( | 371 AddVertexToClippedQuad( |
340 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); | 372 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad); |
341 } | 373 } |
342 | 374 |
343 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { | 375 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { |
344 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h4, h1), | 376 AddVertexToClippedQuad(ComputeClippedCartesianPoint2dForEdge(h4, h1), |
345 clipped_quad, num_vertices_in_clipped_quad); | 377 clipped_quad, num_vertices_in_clipped_quad); |
346 } | 378 } |
347 | 379 |
| 380 if (*num_vertices_in_clipped_quad > 2 && |
| 381 IsNearlyTheSame(clipped_quad[0], |
| 382 clipped_quad[*num_vertices_in_clipped_quad - 1])) |
| 383 *num_vertices_in_clipped_quad -= 1; |
| 384 |
348 DCHECK_LE(*num_vertices_in_clipped_quad, 8); | 385 DCHECK_LE(*num_vertices_in_clipped_quad, 8); |
349 } | 386 } |
350 | 387 |
351 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform, | 388 bool MathUtil::MapClippedQuad3d(const gfx::Transform& transform, |
352 const gfx::QuadF& src_quad, | 389 const gfx::QuadF& src_quad, |
353 gfx::Point3F clipped_quad[8], | 390 gfx::Point3F clipped_quad[8], |
354 int* num_vertices_in_clipped_quad) { | 391 int* num_vertices_in_clipped_quad) { |
355 HomogeneousCoordinate h1 = | 392 HomogeneousCoordinate h1 = |
356 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); | 393 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); |
357 HomogeneousCoordinate h2 = | 394 HomogeneousCoordinate h2 = |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 if (!h4.ShouldBeClipped()) { | 436 if (!h4.ShouldBeClipped()) { |
400 AddVertexToClippedQuad3d( | 437 AddVertexToClippedQuad3d( |
401 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad); | 438 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad); |
402 } | 439 } |
403 | 440 |
404 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { | 441 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { |
405 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1), | 442 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1), |
406 clipped_quad, num_vertices_in_clipped_quad); | 443 clipped_quad, num_vertices_in_clipped_quad); |
407 } | 444 } |
408 | 445 |
| 446 if (*num_vertices_in_clipped_quad > 2 && |
| 447 IsNearlyTheSame(clipped_quad[0], |
| 448 clipped_quad[*num_vertices_in_clipped_quad - 1])) |
| 449 *num_vertices_in_clipped_quad -= 1; |
| 450 |
409 DCHECK_LE(*num_vertices_in_clipped_quad, 8); | 451 DCHECK_LE(*num_vertices_in_clipped_quad, 8); |
410 return (*num_vertices_in_clipped_quad >= 4); | 452 return (*num_vertices_in_clipped_quad >= 4); |
411 } | 453 } |
412 | 454 |
413 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( | 455 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( |
414 const gfx::PointF vertices[], | 456 const gfx::PointF vertices[], |
415 int num_vertices) { | 457 int num_vertices) { |
416 if (num_vertices < 2) | 458 if (num_vertices < 2) |
417 return gfx::RectF(); | 459 return gfx::RectF(); |
418 | 460 |
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
928 _mm_setcsr(orig_state_ | 0x8040); | 970 _mm_setcsr(orig_state_ | 0x8040); |
929 #endif | 971 #endif |
930 } | 972 } |
931 | 973 |
932 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { | 974 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { |
933 #ifdef __SSE__ | 975 #ifdef __SSE__ |
934 _mm_setcsr(orig_state_); | 976 _mm_setcsr(orig_state_); |
935 #endif | 977 #endif |
936 } | 978 } |
937 | 979 |
| 980 bool IsNearlyTheSameForTesting(float left, float right) { |
| 981 return IsNearlyTheSame(left, right); |
| 982 } |
| 983 |
| 984 bool IsNearlyTheSameForTesting(const gfx::PointF& left, |
| 985 const gfx::PointF& right) { |
| 986 return IsNearlyTheSame(left, right); |
| 987 } |
| 988 |
| 989 bool IsNearlyTheSameForTesting(const gfx::Point3F& left, |
| 990 const gfx::Point3F& right) { |
| 991 return IsNearlyTheSame(left, right); |
| 992 } |
| 993 |
938 } // namespace cc | 994 } // namespace cc |
OLD | NEW |