Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: cc/base/math_util.cc

Issue 139233002: [#4] Pass gfx structs by const ref (gfx::PointF) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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],
danakj 2014/01/15 16:54:46 This change seems unrelated?
r.kasibhatla 2014/01/16 05:05:39 Agree. No need for the change. Reverted the change
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 }
108 108
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1()); 149 HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1());
150 HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2()); 150 HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2());
151 HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3()); 151 HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3());
152 HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4()); 152 HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4());
153 153
154 return ComputeEnclosingClippedRect(h1, h2, h3, h4); 154 return ComputeEnclosingClippedRect(h1, h2, h3, h4);
155 } 155 }
156 156
157 void MathUtil::MapClippedQuad(const gfx::Transform& transform, 157 void MathUtil::MapClippedQuad(const gfx::Transform& transform,
158 const gfx::QuadF& src_quad, 158 const gfx::QuadF& src_quad,
159 gfx::PointF clipped_quad[8], 159 gfx::PointF (&clipped_quad)[8],
danakj 2014/01/15 16:54:46 This change seems unrelated?
r.kasibhatla 2014/01/16 05:05:39 Agree. No need for the change. Reverted the change
160 int* num_vertices_in_clipped_quad) { 160 int* num_vertices_in_clipped_quad) {
161 HomogeneousCoordinate h1 = 161 HomogeneousCoordinate h1 =
162 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1())); 162 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1()));
163 HomogeneousCoordinate h2 = 163 HomogeneousCoordinate h2 =
164 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2())); 164 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2()));
165 HomogeneousCoordinate h3 = 165 HomogeneousCoordinate h3 =
166 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3())); 166 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3()));
167 HomogeneousCoordinate h4 = 167 HomogeneousCoordinate h4 =
168 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4())); 168 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4()));
169 169
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(const gfx::PointF* vertices,
danakj 2014/01/15 16:54:46 This change seems unrelated?
r.kasibhatla 2014/01/16 05:05:39 As we are not modifying the 'vertices' array, we s
danakj 2014/01/16 17:46:12 Sure.. won't hurt.
227 int num_vertices) { 227 int num_vertices) {
228 if (num_vertices < 2) 228 if (num_vertices < 2)
229 return gfx::RectF(); 229 return gfx::RectF();
230 230
231 float xmin = std::numeric_limits<float>::max(); 231 float xmin = std::numeric_limits<float>::max();
232 float xmax = -std::numeric_limits<float>::max(); 232 float xmax = -std::numeric_limits<float>::max();
233 float ymin = std::numeric_limits<float>::max(); 233 float ymin = std::numeric_limits<float>::max();
234 float ymax = -std::numeric_limits<float>::max(); 234 float ymax = -std::numeric_limits<float>::max();
235 235
236 for (int i = 0; i < num_vertices; ++i) 236 for (int i = 0; i < num_vertices; ++i)
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 349
350 // Result will be invalid if clipped == true. But, compute it anyway just in 350 // Result will be invalid if clipped == true. But, compute it anyway just in
351 // case, to emulate existing behavior. 351 // case, to emulate existing behavior.
352 return gfx::QuadF(h1.CartesianPoint2d(), 352 return gfx::QuadF(h1.CartesianPoint2d(),
353 h2.CartesianPoint2d(), 353 h2.CartesianPoint2d(),
354 h3.CartesianPoint2d(), 354 h3.CartesianPoint2d(),
355 h4.CartesianPoint2d()); 355 h4.CartesianPoint2d());
356 } 356 }
357 357
358 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform, 358 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform,
359 gfx::PointF p, 359 const gfx::PointF& p,
360 bool* clipped) { 360 bool* clipped) {
361 HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p)); 361 HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p));
362 362
363 if (h.w() > 0) { 363 if (h.w() > 0) {
364 *clipped = false; 364 *clipped = false;
365 return h.CartesianPoint2d(); 365 return h.CartesianPoint2d();
366 } 366 }
367 367
368 // The cartesian coordinates will be invalid after dividing by w. 368 // The cartesian coordinates will be invalid after dividing by w.
369 *clipped = true; 369 *clipped = true;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 *clipped |= clipped_point; 414 *clipped |= clipped_point;
415 projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point)); 415 projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point));
416 *clipped |= clipped_point; 416 *clipped |= clipped_point;
417 projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point)); 417 projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point));
418 *clipped |= clipped_point; 418 *clipped |= clipped_point;
419 419
420 return projected_quad; 420 return projected_quad;
421 } 421 }
422 422
423 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform, 423 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform,
424 gfx::PointF p, 424 const gfx::PointF& p,
425 bool* clipped) { 425 bool* clipped) {
426 HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p); 426 HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p);
427 427
428 if (h.w() > 0) { 428 if (h.w() > 0) {
429 // The cartesian coordinates will be valid in this case. 429 // The cartesian coordinates will be valid in this case.
430 *clipped = false; 430 *clipped = false;
431 return h.CartesianPoint2d(); 431 return h.CartesianPoint2d();
432 } 432 }
433 433
434 // The cartesian coordinates will be invalid after dividing by w. 434 // The cartesian coordinates will be invalid after dividing by w.
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 ok &= value->GetInteger(1, &y); 544 ok &= value->GetInteger(1, &y);
545 ok &= value->GetInteger(2, &w); 545 ok &= value->GetInteger(2, &w);
546 ok &= value->GetInteger(3, &h); 546 ok &= value->GetInteger(3, &h);
547 if (!ok) 547 if (!ok)
548 return false; 548 return false;
549 549
550 *out_rect = gfx::Rect(x, y, w, h); 550 *out_rect = gfx::Rect(x, y, w, h);
551 return true; 551 return true;
552 } 552 }
553 553
554 scoped_ptr<base::Value> MathUtil::AsValue(gfx::PointF pt) { 554 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::PointF& pt) {
555 scoped_ptr<base::ListValue> res(new base::ListValue()); 555 scoped_ptr<base::ListValue> res(new base::ListValue());
556 res->AppendDouble(pt.x()); 556 res->AppendDouble(pt.x());
557 res->AppendDouble(pt.y()); 557 res->AppendDouble(pt.y());
558 return res.PassAs<base::Value>(); 558 return res.PassAs<base::Value>();
559 } 559 }
560 560
561 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) { 561 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) {
562 scoped_ptr<base::ListValue> res(new base::ListValue()); 562 scoped_ptr<base::ListValue> res(new base::ListValue());
563 res->AppendDouble(q.p1().x()); 563 res->AppendDouble(q.p1().x());
564 res->AppendDouble(q.p1().y()); 564 res->AppendDouble(q.p1().y());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( 605 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
606 std::min(value, std::numeric_limits<double>::max()))); 606 std::min(value, std::numeric_limits<double>::max())));
607 } 607 }
608 608
609 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { 609 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) {
610 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( 610 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
611 std::min(value, std::numeric_limits<float>::max()))); 611 std::min(value, std::numeric_limits<float>::max())));
612 } 612 }
613 613
614 } // namespace cc 614 } // namespace cc
OLDNEW
« cc/base/math_util.h ('K') | « cc/base/math_util.h ('k') | cc/debug/overdraw_metrics.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698