Chromium Code Reviews| 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 static const float epsilon_scale = 0.00001f; | |
|
flackr
2017/01/11 18:23:57
nit: Can you add a short comment as to why / how w
Peter Mayo
2017/01/11 22:23:31
Done.
| |
| 160 return std::abs(f - g) < | |
| 161 epsilon_scale * | |
| 162 std::max(std::max(std::abs(f), std::abs(g)), epsilon_scale); | |
| 163 } | |
| 164 | |
| 165 static inline bool isNearlyTheSame(const gfx::PointF& lhs, | |
| 166 const gfx::PointF& rhs) { | |
| 167 return isNearlyTheSame(lhs.x(), rhs.x()) && isNearlyTheSame(lhs.y(), rhs.y()); | |
| 168 } | |
| 169 | |
| 170 static inline bool isNearlyTheSame(const gfx::Point3F& lhs, | |
| 171 const gfx::Point3F& rhs) { | |
| 172 return isNearlyTheSame(lhs.x(), rhs.x()) && | |
| 173 isNearlyTheSame(lhs.y(), rhs.y()) && isNearlyTheSame(lhs.z(), rhs.z()); | |
| 174 } | |
| 175 | |
| 158 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, | 176 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, |
| 159 gfx::PointF clipped_quad[8], | 177 gfx::PointF clipped_quad[8], |
| 160 int* num_vertices_in_clipped_quad) { | 178 int* num_vertices_in_clipped_quad) { |
| 179 if (*num_vertices_in_clipped_quad > 0) { | |
| 180 if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], | |
| 181 new_vertex)) | |
| 182 return; | |
| 183 if (isNearlyTheSame(clipped_quad[0], new_vertex)) | |
|
flackr
2017/01/11 18:23:57
Instead of comparing every added vertex with verte
Peter Mayo
2017/01/11 22:23:31
This expands the scope in which the "isNearlyTheSa
| |
| 184 return; | |
| 185 } | |
| 161 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 186 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
| 162 (*num_vertices_in_clipped_quad)++; | 187 (*num_vertices_in_clipped_quad)++; |
| 163 } | 188 } |
| 164 | 189 |
| 165 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, | 190 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, |
| 166 gfx::Point3F clipped_quad[8], | 191 gfx::Point3F clipped_quad[8], |
| 167 int* num_vertices_in_clipped_quad) { | 192 int* num_vertices_in_clipped_quad) { |
| 193 if (*num_vertices_in_clipped_quad > 0) { | |
| 194 if (isNearlyTheSame(clipped_quad[*num_vertices_in_clipped_quad - 1], | |
| 195 new_vertex)) | |
| 196 return; | |
| 197 if (isNearlyTheSame(clipped_quad[0], 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); | 312 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); |
| 281 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); | 313 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); |
| 282 DCHECK(!hc0.ShouldBeClipped()); | 314 DCHECK(!hc0.ShouldBeClipped()); |
| 283 DCHECK(!hc1.ShouldBeClipped()); | 315 DCHECK(!hc1.ShouldBeClipped()); |
| 284 | 316 |
| 285 gfx::PointF top_left(hc0.CartesianPoint2d()); | 317 gfx::PointF top_left(hc0.CartesianPoint2d()); |
| 286 gfx::PointF bottom_right(hc1.CartesianPoint2d()); | 318 gfx::PointF bottom_right(hc1.CartesianPoint2d()); |
| 287 return gfx::ToEnclosedRect(gfx::BoundingRect(top_left, bottom_right)); | 319 return gfx::ToEnclosedRect(gfx::BoundingRect(top_left, bottom_right)); |
| 288 } | 320 } |
| 289 | 321 |
| 290 void MathUtil::MapClippedQuad(const gfx::Transform& transform, | 322 void MathUtil::MapClippedQuad(const gfx::Transform& transform, |
|
flackr
2017/01/11 18:23:57
It's interesting that there seem to be no calls to
Peter Mayo
2017/01/11 22:23:31
A fine idea for another CL. I have a few more for
| |
| 291 const gfx::QuadF& src_quad, | 323 const gfx::QuadF& src_quad, |
| 292 gfx::PointF clipped_quad[8], | 324 gfx::PointF clipped_quad[8], |
| 293 int* num_vertices_in_clipped_quad) { | 325 int* num_vertices_in_clipped_quad) { |
| 294 HomogeneousCoordinate h1 = | 326 HomogeneousCoordinate h1 = |
| 295 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); | 327 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); |
| 296 HomogeneousCoordinate h2 = | 328 HomogeneousCoordinate h2 = |
| 297 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); | 329 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); |
| 298 HomogeneousCoordinate h3 = | 330 HomogeneousCoordinate h3 = |
| 299 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); | 331 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); |
| 300 HomogeneousCoordinate h4 = | 332 HomogeneousCoordinate h4 = |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 AddVertexToClippedQuad3d( | 432 AddVertexToClippedQuad3d( |
| 401 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad); | 433 h4.CartesianPoint3d(), clipped_quad, num_vertices_in_clipped_quad); |
| 402 } | 434 } |
| 403 | 435 |
| 404 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { | 436 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { |
| 405 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1), | 437 AddVertexToClippedQuad3d(ComputeClippedCartesianPoint3dForEdge(h4, h1), |
| 406 clipped_quad, num_vertices_in_clipped_quad); | 438 clipped_quad, num_vertices_in_clipped_quad); |
| 407 } | 439 } |
| 408 | 440 |
| 409 DCHECK_LE(*num_vertices_in_clipped_quad, 8); | 441 DCHECK_LE(*num_vertices_in_clipped_quad, 8); |
| 410 return (*num_vertices_in_clipped_quad >= 4); | 442 return (*num_vertices_in_clipped_quad >= 4); |
|
flackr
2017/01/11 18:23:57
It seems like noone uses this return value and we
Peter Mayo
2017/01/11 22:23:31
A fine idea for another CL. I have a few more for
| |
| 411 } | 443 } |
| 412 | 444 |
| 413 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( | 445 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( |
| 414 const gfx::PointF vertices[], | 446 const gfx::PointF vertices[], |
| 415 int num_vertices) { | 447 int num_vertices) { |
| 416 if (num_vertices < 2) | 448 if (num_vertices < 2) |
| 417 return gfx::RectF(); | 449 return gfx::RectF(); |
| 418 | 450 |
| 419 float xmin = std::numeric_limits<float>::max(); | 451 float xmin = std::numeric_limits<float>::max(); |
| 420 float xmax = -std::numeric_limits<float>::max(); | 452 float xmax = -std::numeric_limits<float>::max(); |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 928 _mm_setcsr(orig_state_ | 0x8040); | 960 _mm_setcsr(orig_state_ | 0x8040); |
| 929 #endif | 961 #endif |
| 930 } | 962 } |
| 931 | 963 |
| 932 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { | 964 ScopedSubnormalFloatDisabler::~ScopedSubnormalFloatDisabler() { |
| 933 #ifdef __SSE__ | 965 #ifdef __SSE__ |
| 934 _mm_setcsr(orig_state_); | 966 _mm_setcsr(orig_state_); |
| 935 #endif | 967 #endif |
| 936 } | 968 } |
| 937 | 969 |
| 970 bool IsNearlyTheSameForUnitTesting(const float left, const float right) { | |
| 971 return isNearlyTheSame(left, right); | |
| 972 } | |
| 973 | |
| 974 bool IsNearlyTheSameForUnitTesting(const gfx::PointF& left, | |
| 975 const gfx::PointF& right) { | |
| 976 return isNearlyTheSame(left, right); | |
| 977 } | |
| 978 | |
| 979 bool IsNearlyTheSameForUnitTesting(const gfx::Point3F& left, | |
| 980 const gfx::Point3F& right) { | |
| 981 return isNearlyTheSame(left, right); | |
| 982 } | |
| 983 | |
| 938 } // namespace cc | 984 } // namespace cc |
| OLD | NEW |