| 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 |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "ui/gfx/quad_f.h" | 12 #include "ui/gfx/quad_f.h" |
| 13 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
| 14 #include "ui/gfx/rect_conversions.h" | 14 #include "ui/gfx/rect_conversions.h" |
| 15 #include "ui/gfx/rect_f.h" | 15 #include "ui/gfx/rect_f.h" |
| 16 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
| 17 #include "ui/gfx/vector2d_f.h" | 17 #include "ui/gfx/vector2d_f.h" |
| 18 | 18 |
| 19 namespace cc { | 19 namespace cc { |
| 20 | 20 |
| 21 const double MathUtil::kPiDouble = 3.14159265358979323846; | 21 const double MathUtil::kPiDouble = 3.14159265358979323846; |
| 22 const float MathUtil::kPiFloat = 3.14159265358979323846f; | 22 const float MathUtil::kPiFloat = 3.14159265358979323846f; |
| 23 | 23 |
| 24 static HomogeneousCoordinate ProjectHomogeneousPoint( | 24 static HomogeneousCoordinate ProjectHomogeneousPoint( |
| 25 const gfx::Transform& transform, | 25 const gfx::Transform& transform, |
| 26 gfx::PointF p) { | 26 const gfx::PointF& p) { |
| 27 // In this case, the layer we are trying to project onto is perpendicular to | 27 // In this case, the layer we are trying to project onto is perpendicular to |
| 28 // ray (point p and z-axis direction) that we are trying to project. This | 28 // ray (point p and z-axis direction) that we are trying to project. This |
| 29 // happens when the layer is rotated so that it is infinitesimally thin, or | 29 // happens when the layer is rotated so that it is infinitesimally thin, or |
| 30 // when it is co-planar with the camera origin -- i.e. when the layer is | 30 // when it is co-planar with the camera origin -- i.e. when the layer is |
| 31 // invisible anyway. | 31 // invisible anyway. |
| 32 if (!transform.matrix().get(2, 2)) | 32 if (!transform.matrix().get(2, 2)) |
| 33 return HomogeneousCoordinate(0.0, 0.0, 0.0, 1.0); | 33 return HomogeneousCoordinate(0.0, 0.0, 0.0, 1.0); |
| 34 | 34 |
| 35 SkMScalar z = -(transform.matrix().get(2, 0) * p.x() + | 35 SkMScalar z = -(transform.matrix().get(2, 0) * p.x() + |
| 36 transform.matrix().get(2, 1) * p.y() + | 36 transform.matrix().get(2, 1) * p.y() + |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 SkMScalar y = (SK_MScalar1 - t) * h1.y() + t * h2.y(); | 80 SkMScalar y = (SK_MScalar1 - t) * h1.y() + t * h2.y(); |
| 81 SkMScalar z = (SK_MScalar1 - t) * h1.z() + t * h2.z(); | 81 SkMScalar z = (SK_MScalar1 - t) * h1.z() + t * h2.z(); |
| 82 | 82 |
| 83 return HomogeneousCoordinate(x, y, z, w); | 83 return HomogeneousCoordinate(x, y, z, w); |
| 84 } | 84 } |
| 85 | 85 |
| 86 static inline void ExpandBoundsToIncludePoint(float* xmin, | 86 static inline void ExpandBoundsToIncludePoint(float* xmin, |
| 87 float* xmax, | 87 float* xmax, |
| 88 float* ymin, | 88 float* ymin, |
| 89 float* ymax, | 89 float* ymax, |
| 90 gfx::PointF p) { | 90 const gfx::PointF& p) { |
| 91 *xmin = std::min(p.x(), *xmin); | 91 *xmin = std::min(p.x(), *xmin); |
| 92 *xmax = std::max(p.x(), *xmax); | 92 *xmax = std::max(p.x(), *xmax); |
| 93 *ymin = std::min(p.y(), *ymin); | 93 *ymin = std::min(p.y(), *ymin); |
| 94 *ymax = std::max(p.y(), *ymax); | 94 *ymax = std::max(p.y(), *ymax); |
| 95 } | 95 } |
| 96 | 96 |
| 97 static inline void AddVertexToClippedQuad(gfx::PointF new_vertex, | 97 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex, |
| 98 gfx::PointF clipped_quad[8], | 98 gfx::PointF clipped_quad[8], |
| 99 int* num_vertices_in_clipped_quad) { | 99 int* num_vertices_in_clipped_quad) { |
| 100 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 100 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
| 101 (*num_vertices_in_clipped_quad)++; | 101 (*num_vertices_in_clipped_quad)++; |
| 102 } | 102 } |
| 103 | 103 |
| 104 gfx::Rect MathUtil::MapClippedRect(const gfx::Transform& transform, | 104 gfx::Rect MathUtil::MapClippedRect(const gfx::Transform& transform, |
| 105 const gfx::Rect& src_rect) { | 105 const gfx::Rect& src_rect) { |
| 106 return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect))); | 106 return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect))); |
| 107 } | 107 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { | 216 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) { |
| 217 AddVertexToClippedQuad( | 217 AddVertexToClippedQuad( |
| 218 ComputeClippedPointForEdge(h4, h1).CartesianPoint2d(), | 218 ComputeClippedPointForEdge(h4, h1).CartesianPoint2d(), |
| 219 clipped_quad, | 219 clipped_quad, |
| 220 num_vertices_in_clipped_quad); | 220 num_vertices_in_clipped_quad); |
| 221 } | 221 } |
| 222 | 222 |
| 223 DCHECK_LE(*num_vertices_in_clipped_quad, 8); | 223 DCHECK_LE(*num_vertices_in_clipped_quad, 8); |
| 224 } | 224 } |
| 225 | 225 |
| 226 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices(gfx::PointF vertices[], | 226 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices( |
| 227 int num_vertices) { | 227 const gfx::PointF vertices[], |
| 228 int num_vertices) { |
| 228 if (num_vertices < 2) | 229 if (num_vertices < 2) |
| 229 return gfx::RectF(); | 230 return gfx::RectF(); |
| 230 | 231 |
| 231 float xmin = std::numeric_limits<float>::max(); | 232 float xmin = std::numeric_limits<float>::max(); |
| 232 float xmax = -std::numeric_limits<float>::max(); | 233 float xmax = -std::numeric_limits<float>::max(); |
| 233 float ymin = std::numeric_limits<float>::max(); | 234 float ymin = std::numeric_limits<float>::max(); |
| 234 float ymax = -std::numeric_limits<float>::max(); | 235 float ymax = -std::numeric_limits<float>::max(); |
| 235 | 236 |
| 236 for (int i = 0; i < num_vertices; ++i) | 237 for (int i = 0; i < num_vertices; ++i) |
| 237 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, vertices[i]); | 238 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, vertices[i]); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 350 |
| 350 // Result will be invalid if clipped == true. But, compute it anyway just in | 351 // Result will be invalid if clipped == true. But, compute it anyway just in |
| 351 // case, to emulate existing behavior. | 352 // case, to emulate existing behavior. |
| 352 return gfx::QuadF(h1.CartesianPoint2d(), | 353 return gfx::QuadF(h1.CartesianPoint2d(), |
| 353 h2.CartesianPoint2d(), | 354 h2.CartesianPoint2d(), |
| 354 h3.CartesianPoint2d(), | 355 h3.CartesianPoint2d(), |
| 355 h4.CartesianPoint2d()); | 356 h4.CartesianPoint2d()); |
| 356 } | 357 } |
| 357 | 358 |
| 358 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform, | 359 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform, |
| 359 gfx::PointF p, | 360 const gfx::PointF& p, |
| 360 bool* clipped) { | 361 bool* clipped) { |
| 361 HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p)); | 362 HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p)); |
| 362 | 363 |
| 363 if (h.w() > 0) { | 364 if (h.w() > 0) { |
| 364 *clipped = false; | 365 *clipped = false; |
| 365 return h.CartesianPoint2d(); | 366 return h.CartesianPoint2d(); |
| 366 } | 367 } |
| 367 | 368 |
| 368 // The cartesian coordinates will be invalid after dividing by w. | 369 // The cartesian coordinates will be invalid after dividing by w. |
| 369 *clipped = true; | 370 *clipped = true; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 *clipped |= clipped_point; | 415 *clipped |= clipped_point; |
| 415 projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point)); | 416 projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point)); |
| 416 *clipped |= clipped_point; | 417 *clipped |= clipped_point; |
| 417 projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point)); | 418 projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point)); |
| 418 *clipped |= clipped_point; | 419 *clipped |= clipped_point; |
| 419 | 420 |
| 420 return projected_quad; | 421 return projected_quad; |
| 421 } | 422 } |
| 422 | 423 |
| 423 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform, | 424 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform, |
| 424 gfx::PointF p, | 425 const gfx::PointF& p, |
| 425 bool* clipped) { | 426 bool* clipped) { |
| 426 HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); | 427 HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); |
| 427 | 428 |
| 428 if (h.w() > 0) { | 429 if (h.w() > 0) { |
| 429 // The cartesian coordinates will be valid in this case. | 430 // The cartesian coordinates will be valid in this case. |
| 430 *clipped = false; | 431 *clipped = false; |
| 431 return h.CartesianPoint2d(); | 432 return h.CartesianPoint2d(); |
| 432 } | 433 } |
| 433 | 434 |
| 434 // The cartesian coordinates will be invalid after dividing by w. | 435 // The cartesian coordinates will be invalid after dividing by w. |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ok &= value->GetInteger(1, &y); | 545 ok &= value->GetInteger(1, &y); |
| 545 ok &= value->GetInteger(2, &w); | 546 ok &= value->GetInteger(2, &w); |
| 546 ok &= value->GetInteger(3, &h); | 547 ok &= value->GetInteger(3, &h); |
| 547 if (!ok) | 548 if (!ok) |
| 548 return false; | 549 return false; |
| 549 | 550 |
| 550 *out_rect = gfx::Rect(x, y, w, h); | 551 *out_rect = gfx::Rect(x, y, w, h); |
| 551 return true; | 552 return true; |
| 552 } | 553 } |
| 553 | 554 |
| 554 scoped_ptr<base::Value> MathUtil::AsValue(gfx::PointF pt) { | 555 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::PointF& pt) { |
| 555 scoped_ptr<base::ListValue> res(new base::ListValue()); | 556 scoped_ptr<base::ListValue> res(new base::ListValue()); |
| 556 res->AppendDouble(pt.x()); | 557 res->AppendDouble(pt.x()); |
| 557 res->AppendDouble(pt.y()); | 558 res->AppendDouble(pt.y()); |
| 558 return res.PassAs<base::Value>(); | 559 return res.PassAs<base::Value>(); |
| 559 } | 560 } |
| 560 | 561 |
| 561 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) { | 562 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) { |
| 562 scoped_ptr<base::ListValue> res(new base::ListValue()); | 563 scoped_ptr<base::ListValue> res(new base::ListValue()); |
| 563 res->AppendDouble(q.p1().x()); | 564 res->AppendDouble(q.p1().x()); |
| 564 res->AppendDouble(q.p1().y()); | 565 res->AppendDouble(q.p1().y()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 606 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
| 606 std::min(value, std::numeric_limits<double>::max()))); | 607 std::min(value, std::numeric_limits<double>::max()))); |
| 607 } | 608 } |
| 608 | 609 |
| 609 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { | 610 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { |
| 610 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 611 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
| 611 std::min(value, std::numeric_limits<float>::max()))); | 612 std::min(value, std::numeric_limits<float>::max()))); |
| 612 } | 613 } |
| 613 | 614 |
| 614 } // namespace cc | 615 } // namespace cc |
| OLD | NEW |