NativeViewHost should query its native view for the current cursor
[chromium-blink-merge.git] / cc / base / math_util.cc
blobb492404683a881045225b71876b8de17d7f7bc9b
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
3 // found in the LICENSE file.
5 #include "cc/base/math_util.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
11 #include "base/values.h"
12 #include "ui/gfx/quad_f.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/rect_conversions.h"
15 #include "ui/gfx/rect_f.h"
16 #include "ui/gfx/transform.h"
17 #include "ui/gfx/vector2d_f.h"
19 namespace cc {
21 const double MathUtil::kPiDouble = 3.14159265358979323846;
22 const float MathUtil::kPiFloat = 3.14159265358979323846f;
24 static HomogeneousCoordinate ProjectHomogeneousPoint(
25 const gfx::Transform& transform,
26 const gfx::PointF& p) {
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
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
31 // invisible anyway.
32 if (!transform.matrix().get(2, 2))
33 return HomogeneousCoordinate(0.0, 0.0, 0.0, 1.0);
35 SkMScalar z = -(transform.matrix().get(2, 0) * p.x() +
36 transform.matrix().get(2, 1) * p.y() +
37 transform.matrix().get(2, 3)) /
38 transform.matrix().get(2, 2);
39 HomogeneousCoordinate result(p.x(), p.y(), z, 1.0);
40 transform.matrix().mapMScalars(result.vec, result.vec);
41 return result;
44 static HomogeneousCoordinate MapHomogeneousPoint(
45 const gfx::Transform& transform,
46 const gfx::Point3F& p) {
47 HomogeneousCoordinate result(p.x(), p.y(), p.z(), 1.0);
48 transform.matrix().mapMScalars(result.vec, result.vec);
49 return result;
52 static HomogeneousCoordinate ComputeClippedPointForEdge(
53 const HomogeneousCoordinate& h1,
54 const HomogeneousCoordinate& h2) {
55 // Points h1 and h2 form a line in 4d, and any point on that line can be
56 // represented as an interpolation between h1 and h2:
57 // p = (1-t) h1 + (t) h2
59 // We want to compute point p such that p.w == epsilon, where epsilon is a
60 // small non-zero number. (but the smaller the number is, the higher the risk
61 // of overflow)
62 // To do this, we solve for t in the following equation:
63 // p.w = epsilon = (1-t) * h1.w + (t) * h2.w
65 // Once paramter t is known, the rest of p can be computed via
66 // p = (1-t) h1 + (t) h2.
68 // Technically this is a special case of the following assertion, but its a
69 // good idea to keep it an explicit sanity check here.
70 DCHECK_NE(h2.w(), h1.w());
71 // Exactly one of h1 or h2 (but not both) must be on the negative side of the
72 // w plane when this is called.
73 DCHECK(h1.ShouldBeClipped() ^ h2.ShouldBeClipped());
75 // ...or any positive non-zero small epsilon
76 SkMScalar w = 0.00001f;
77 SkMScalar t = (w - h1.w()) / (h2.w() - h1.w());
79 SkMScalar x = (SK_MScalar1 - t) * h1.x() + t * h2.x();
80 SkMScalar y = (SK_MScalar1 - t) * h1.y() + t * h2.y();
81 SkMScalar z = (SK_MScalar1 - t) * h1.z() + t * h2.z();
83 return HomogeneousCoordinate(x, y, z, w);
86 static inline void ExpandBoundsToIncludePoint(float* xmin,
87 float* xmax,
88 float* ymin,
89 float* ymax,
90 const gfx::PointF& p) {
91 *xmin = std::min(p.x(), *xmin);
92 *xmax = std::max(p.x(), *xmax);
93 *ymin = std::min(p.y(), *ymin);
94 *ymax = std::max(p.y(), *ymax);
97 static inline void AddVertexToClippedQuad(const gfx::PointF& new_vertex,
98 gfx::PointF clipped_quad[8],
99 int* num_vertices_in_clipped_quad) {
100 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex;
101 (*num_vertices_in_clipped_quad)++;
104 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform,
105 const gfx::Rect& src_rect) {
106 if (transform.IsIdentityOrIntegerTranslation()) {
107 return src_rect +
108 gfx::Vector2d(
109 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))),
110 static_cast<int>(
111 SkMScalarToFloat(transform.matrix().get(1, 3))));
113 return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect)));
116 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform,
117 const gfx::RectF& src_rect) {
118 if (transform.IsIdentityOrTranslation()) {
119 return src_rect +
120 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
121 SkMScalarToFloat(transform.matrix().get(1, 3)));
124 // Apply the transform, but retain the result in homogeneous coordinates.
126 SkMScalar quad[4 * 2]; // input: 4 x 2D points
127 quad[0] = src_rect.x();
128 quad[1] = src_rect.y();
129 quad[2] = src_rect.right();
130 quad[3] = src_rect.y();
131 quad[4] = src_rect.right();
132 quad[5] = src_rect.bottom();
133 quad[6] = src_rect.x();
134 quad[7] = src_rect.bottom();
136 SkMScalar result[4 * 4]; // output: 4 x 4D homogeneous points
137 transform.matrix().map2(quad, 4, result);
139 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]);
140 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]);
141 HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]);
142 HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]);
143 return ComputeEnclosingClippedRect(hc0, hc1, hc2, hc3);
146 gfx::Rect MathUtil::ProjectEnclosingClippedRect(const gfx::Transform& transform,
147 const gfx::Rect& src_rect) {
148 if (transform.IsIdentityOrIntegerTranslation()) {
149 return src_rect +
150 gfx::Vector2d(
151 static_cast<int>(SkMScalarToFloat(transform.matrix().get(0, 3))),
152 static_cast<int>(
153 SkMScalarToFloat(transform.matrix().get(1, 3))));
155 return gfx::ToEnclosingRect(
156 ProjectClippedRect(transform, gfx::RectF(src_rect)));
159 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform,
160 const gfx::RectF& src_rect) {
161 if (transform.IsIdentityOrTranslation()) {
162 return src_rect +
163 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
164 SkMScalarToFloat(transform.matrix().get(1, 3)));
167 // Perform the projection, but retain the result in homogeneous coordinates.
168 gfx::QuadF q = gfx::QuadF(src_rect);
169 HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1());
170 HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2());
171 HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3());
172 HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4());
174 return ComputeEnclosingClippedRect(h1, h2, h3, h4);
177 void MathUtil::MapClippedQuad(const gfx::Transform& transform,
178 const gfx::QuadF& src_quad,
179 gfx::PointF clipped_quad[8],
180 int* num_vertices_in_clipped_quad) {
181 HomogeneousCoordinate h1 =
182 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1()));
183 HomogeneousCoordinate h2 =
184 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2()));
185 HomogeneousCoordinate h3 =
186 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3()));
187 HomogeneousCoordinate h4 =
188 MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4()));
190 // The order of adding the vertices to the array is chosen so that
191 // clockwise / counter-clockwise orientation is retained.
193 *num_vertices_in_clipped_quad = 0;
195 if (!h1.ShouldBeClipped()) {
196 AddVertexToClippedQuad(
197 h1.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
200 if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped()) {
201 AddVertexToClippedQuad(
202 ComputeClippedPointForEdge(h1, h2).CartesianPoint2d(),
203 clipped_quad,
204 num_vertices_in_clipped_quad);
207 if (!h2.ShouldBeClipped()) {
208 AddVertexToClippedQuad(
209 h2.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
212 if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped()) {
213 AddVertexToClippedQuad(
214 ComputeClippedPointForEdge(h2, h3).CartesianPoint2d(),
215 clipped_quad,
216 num_vertices_in_clipped_quad);
219 if (!h3.ShouldBeClipped()) {
220 AddVertexToClippedQuad(
221 h3.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
224 if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped()) {
225 AddVertexToClippedQuad(
226 ComputeClippedPointForEdge(h3, h4).CartesianPoint2d(),
227 clipped_quad,
228 num_vertices_in_clipped_quad);
231 if (!h4.ShouldBeClipped()) {
232 AddVertexToClippedQuad(
233 h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
236 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) {
237 AddVertexToClippedQuad(
238 ComputeClippedPointForEdge(h4, h1).CartesianPoint2d(),
239 clipped_quad,
240 num_vertices_in_clipped_quad);
243 DCHECK_LE(*num_vertices_in_clipped_quad, 8);
246 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices(
247 const gfx::PointF vertices[],
248 int num_vertices) {
249 if (num_vertices < 2)
250 return gfx::RectF();
252 float xmin = std::numeric_limits<float>::max();
253 float xmax = -std::numeric_limits<float>::max();
254 float ymin = std::numeric_limits<float>::max();
255 float ymax = -std::numeric_limits<float>::max();
257 for (int i = 0; i < num_vertices; ++i)
258 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, vertices[i]);
260 return gfx::RectF(gfx::PointF(xmin, ymin),
261 gfx::SizeF(xmax - xmin, ymax - ymin));
264 gfx::RectF MathUtil::ComputeEnclosingClippedRect(
265 const HomogeneousCoordinate& h1,
266 const HomogeneousCoordinate& h2,
267 const HomogeneousCoordinate& h3,
268 const HomogeneousCoordinate& h4) {
269 // This function performs clipping as necessary and computes the enclosing 2d
270 // gfx::RectF of the vertices. Doing these two steps simultaneously allows us
271 // to avoid the overhead of storing an unknown number of clipped vertices.
273 // If no vertices on the quad are clipped, then we can simply return the
274 // enclosing rect directly.
275 bool something_clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() ||
276 h3.ShouldBeClipped() || h4.ShouldBeClipped();
277 if (!something_clipped) {
278 gfx::QuadF mapped_quad = gfx::QuadF(h1.CartesianPoint2d(),
279 h2.CartesianPoint2d(),
280 h3.CartesianPoint2d(),
281 h4.CartesianPoint2d());
282 return mapped_quad.BoundingBox();
285 bool everything_clipped = h1.ShouldBeClipped() && h2.ShouldBeClipped() &&
286 h3.ShouldBeClipped() && h4.ShouldBeClipped();
287 if (everything_clipped)
288 return gfx::RectF();
290 float xmin = std::numeric_limits<float>::max();
291 float xmax = -std::numeric_limits<float>::max();
292 float ymin = std::numeric_limits<float>::max();
293 float ymax = -std::numeric_limits<float>::max();
295 if (!h1.ShouldBeClipped())
296 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
297 h1.CartesianPoint2d());
299 if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped())
300 ExpandBoundsToIncludePoint(&xmin,
301 &xmax,
302 &ymin,
303 &ymax,
304 ComputeClippedPointForEdge(h1, h2)
305 .CartesianPoint2d());
307 if (!h2.ShouldBeClipped())
308 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
309 h2.CartesianPoint2d());
311 if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped())
312 ExpandBoundsToIncludePoint(&xmin,
313 &xmax,
314 &ymin,
315 &ymax,
316 ComputeClippedPointForEdge(h2, h3)
317 .CartesianPoint2d());
319 if (!h3.ShouldBeClipped())
320 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
321 h3.CartesianPoint2d());
323 if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped())
324 ExpandBoundsToIncludePoint(&xmin,
325 &xmax,
326 &ymin,
327 &ymax,
328 ComputeClippedPointForEdge(h3, h4)
329 .CartesianPoint2d());
331 if (!h4.ShouldBeClipped())
332 ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
333 h4.CartesianPoint2d());
335 if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped())
336 ExpandBoundsToIncludePoint(&xmin,
337 &xmax,
338 &ymin,
339 &ymax,
340 ComputeClippedPointForEdge(h4, h1)
341 .CartesianPoint2d());
343 return gfx::RectF(gfx::PointF(xmin, ymin),
344 gfx::SizeF(xmax - xmin, ymax - ymin));
347 gfx::QuadF MathUtil::MapQuad(const gfx::Transform& transform,
348 const gfx::QuadF& q,
349 bool* clipped) {
350 if (transform.IsIdentityOrTranslation()) {
351 gfx::QuadF mapped_quad(q);
352 mapped_quad +=
353 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
354 SkMScalarToFloat(transform.matrix().get(1, 3)));
355 *clipped = false;
356 return mapped_quad;
359 HomogeneousCoordinate h1 =
360 MapHomogeneousPoint(transform, gfx::Point3F(q.p1()));
361 HomogeneousCoordinate h2 =
362 MapHomogeneousPoint(transform, gfx::Point3F(q.p2()));
363 HomogeneousCoordinate h3 =
364 MapHomogeneousPoint(transform, gfx::Point3F(q.p3()));
365 HomogeneousCoordinate h4 =
366 MapHomogeneousPoint(transform, gfx::Point3F(q.p4()));
368 *clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() ||
369 h3.ShouldBeClipped() || h4.ShouldBeClipped();
371 // Result will be invalid if clipped == true. But, compute it anyway just in
372 // case, to emulate existing behavior.
373 return gfx::QuadF(h1.CartesianPoint2d(),
374 h2.CartesianPoint2d(),
375 h3.CartesianPoint2d(),
376 h4.CartesianPoint2d());
379 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform,
380 const gfx::PointF& p,
381 bool* clipped) {
382 HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p));
384 if (h.w() > 0) {
385 *clipped = false;
386 return h.CartesianPoint2d();
389 // The cartesian coordinates will be invalid after dividing by w.
390 *clipped = true;
392 // Avoid dividing by w if w == 0.
393 if (!h.w())
394 return gfx::PointF();
396 // This return value will be invalid because clipped == true, but (1) users of
397 // this code should be ignoring the return value when clipped == true anyway,
398 // and (2) this behavior is more consistent with existing behavior of WebKit
399 // transforms if the user really does not ignore the return value.
400 return h.CartesianPoint2d();
403 gfx::Point3F MathUtil::MapPoint(const gfx::Transform& transform,
404 const gfx::Point3F& p,
405 bool* clipped) {
406 HomogeneousCoordinate h = MapHomogeneousPoint(transform, p);
408 if (h.w() > 0) {
409 *clipped = false;
410 return h.CartesianPoint3d();
413 // The cartesian coordinates will be invalid after dividing by w.
414 *clipped = true;
416 // Avoid dividing by w if w == 0.
417 if (!h.w())
418 return gfx::Point3F();
420 // This return value will be invalid because clipped == true, but (1) users of
421 // this code should be ignoring the return value when clipped == true anyway,
422 // and (2) this behavior is more consistent with existing behavior of WebKit
423 // transforms if the user really does not ignore the return value.
424 return h.CartesianPoint3d();
427 gfx::QuadF MathUtil::ProjectQuad(const gfx::Transform& transform,
428 const gfx::QuadF& q,
429 bool* clipped) {
430 gfx::QuadF projected_quad;
431 bool clipped_point;
432 projected_quad.set_p1(ProjectPoint(transform, q.p1(), &clipped_point));
433 *clipped = clipped_point;
434 projected_quad.set_p2(ProjectPoint(transform, q.p2(), &clipped_point));
435 *clipped |= clipped_point;
436 projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point));
437 *clipped |= clipped_point;
438 projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point));
439 *clipped |= clipped_point;
441 return projected_quad;
444 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform,
445 const gfx::PointF& p,
446 bool* clipped) {
447 HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p);
449 if (h.w() > 0) {
450 // The cartesian coordinates will be valid in this case.
451 *clipped = false;
452 return h.CartesianPoint2d();
455 // The cartesian coordinates will be invalid after dividing by w.
456 *clipped = true;
458 // Avoid dividing by w if w == 0.
459 if (!h.w())
460 return gfx::PointF();
462 // This return value will be invalid because clipped == true, but (1) users of
463 // this code should be ignoring the return value when clipped == true anyway,
464 // and (2) this behavior is more consistent with existing behavior of WebKit
465 // transforms if the user really does not ignore the return value.
466 return h.CartesianPoint2d();
469 gfx::RectF MathUtil::ScaleRectProportional(const gfx::RectF& input_outer_rect,
470 const gfx::RectF& scale_outer_rect,
471 const gfx::RectF& scale_inner_rect) {
472 gfx::RectF output_inner_rect = input_outer_rect;
473 float scale_rect_to_input_scale_x =
474 scale_outer_rect.width() / input_outer_rect.width();
475 float scale_rect_to_input_scale_y =
476 scale_outer_rect.height() / input_outer_rect.height();
478 gfx::Vector2dF top_left_diff =
479 scale_inner_rect.origin() - scale_outer_rect.origin();
480 gfx::Vector2dF bottom_right_diff =
481 scale_inner_rect.bottom_right() - scale_outer_rect.bottom_right();
482 output_inner_rect.Inset(top_left_diff.x() / scale_rect_to_input_scale_x,
483 top_left_diff.y() / scale_rect_to_input_scale_y,
484 -bottom_right_diff.x() / scale_rect_to_input_scale_x,
485 -bottom_right_diff.y() / scale_rect_to_input_scale_y);
486 return output_inner_rect;
489 static inline float ScaleOnAxis(double a, double b, double c) {
490 if (!b && !c)
491 return a;
492 if (!a && !c)
493 return b;
494 if (!a && !b)
495 return c;
497 // Do the sqrt as a double to not lose precision.
498 return static_cast<float>(std::sqrt(a * a + b * b + c * c));
501 gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents(
502 const gfx::Transform& transform,
503 float fallback_value) {
504 if (transform.HasPerspective())
505 return gfx::Vector2dF(fallback_value, fallback_value);
506 float x_scale = ScaleOnAxis(transform.matrix().getDouble(0, 0),
507 transform.matrix().getDouble(1, 0),
508 transform.matrix().getDouble(2, 0));
509 float y_scale = ScaleOnAxis(transform.matrix().getDouble(0, 1),
510 transform.matrix().getDouble(1, 1),
511 transform.matrix().getDouble(2, 1));
512 return gfx::Vector2dF(x_scale, y_scale);
515 float MathUtil::SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
516 const gfx::Vector2dF& v2) {
517 double dot_product = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length();
518 // Clamp to compensate for rounding errors.
519 dot_product = std::max(-1.0, std::min(1.0, dot_product));
520 return static_cast<float>(Rad2Deg(std::acos(dot_product)));
523 gfx::Vector2dF MathUtil::ProjectVector(const gfx::Vector2dF& source,
524 const gfx::Vector2dF& destination) {
525 float projected_length =
526 gfx::DotProduct(source, destination) / destination.LengthSquared();
527 return gfx::Vector2dF(projected_length * destination.x(),
528 projected_length * destination.y());
531 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Size& s) {
532 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
533 res->SetDouble("width", s.width());
534 res->SetDouble("height", s.height());
535 return res.PassAs<base::Value>();
538 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::SizeF& s) {
539 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
540 res->SetDouble("width", s.width());
541 res->SetDouble("height", s.height());
542 return res.PassAs<base::Value>();
545 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Rect& r) {
546 scoped_ptr<base::ListValue> res(new base::ListValue());
547 res->AppendInteger(r.x());
548 res->AppendInteger(r.y());
549 res->AppendInteger(r.width());
550 res->AppendInteger(r.height());
551 return res.PassAs<base::Value>();
554 bool MathUtil::FromValue(const base::Value* raw_value, gfx::Rect* out_rect) {
555 const base::ListValue* value = NULL;
556 if (!raw_value->GetAsList(&value))
557 return false;
559 if (value->GetSize() != 4)
560 return false;
562 int x, y, w, h;
563 bool ok = true;
564 ok &= value->GetInteger(0, &x);
565 ok &= value->GetInteger(1, &y);
566 ok &= value->GetInteger(2, &w);
567 ok &= value->GetInteger(3, &h);
568 if (!ok)
569 return false;
571 *out_rect = gfx::Rect(x, y, w, h);
572 return true;
575 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::PointF& pt) {
576 scoped_ptr<base::ListValue> res(new base::ListValue());
577 res->AppendDouble(pt.x());
578 res->AppendDouble(pt.y());
579 return res.PassAs<base::Value>();
582 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Vector2d& v) {
583 scoped_ptr<base::ListValue> res(new base::ListValue());
584 res->AppendInteger(v.x());
585 res->AppendInteger(v.y());
586 return res.PassAs<base::Value>();
589 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) {
590 scoped_ptr<base::ListValue> res(new base::ListValue());
591 res->AppendDouble(q.p1().x());
592 res->AppendDouble(q.p1().y());
593 res->AppendDouble(q.p2().x());
594 res->AppendDouble(q.p2().y());
595 res->AppendDouble(q.p3().x());
596 res->AppendDouble(q.p3().y());
597 res->AppendDouble(q.p4().x());
598 res->AppendDouble(q.p4().y());
599 return res.PassAs<base::Value>();
602 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::RectF& rect) {
603 scoped_ptr<base::ListValue> res(new base::ListValue());
604 res->AppendDouble(rect.x());
605 res->AppendDouble(rect.y());
606 res->AppendDouble(rect.width());
607 res->AppendDouble(rect.height());
608 return res.PassAs<base::Value>();
611 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Transform& transform) {
612 scoped_ptr<base::ListValue> res(new base::ListValue());
613 const SkMatrix44& m = transform.matrix();
614 for (int row = 0; row < 4; ++row) {
615 for (int col = 0; col < 4; ++col)
616 res->AppendDouble(m.getDouble(row, col));
618 return res.PassAs<base::Value>();
621 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::BoxF& box) {
622 scoped_ptr<base::ListValue> res(new base::ListValue());
623 res->AppendInteger(box.x());
624 res->AppendInteger(box.y());
625 res->AppendInteger(box.z());
626 res->AppendInteger(box.width());
627 res->AppendInteger(box.height());
628 res->AppendInteger(box.depth());
629 return res.PassAs<base::Value>();
632 scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) {
633 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
634 std::min(value, std::numeric_limits<double>::max())));
637 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) {
638 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
639 std::min(value, std::numeric_limits<float>::max())));
642 } // namespace cc