Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / cc / base / math_util.cc
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.
4
5 #include "cc/base/math_util.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <limits>
10
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"
18
19 namespace cc {
20
21 const double MathUtil::kPiDouble = 3.14159265358979323846;
22 const float MathUtil::kPiFloat = 3.14159265358979323846f;
23
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);
34
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;
42 }
43
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;
50 }
51
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
58   //
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
64   //
65   // Once paramter t is known, the rest of p can be computed via
66   //    p = (1-t) h1 + (t) h2.
67
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());
74
75   // ...or any positive non-zero small epsilon
76   SkMScalar w = 0.00001f;
77   SkMScalar t = (w - h1.w()) / (h2.w() - h1.w());
78
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();
82
83   return HomogeneousCoordinate(x, y, z, w);
84 }
85
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);
95 }
96
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)++;
102 }
103
104 gfx::Rect MathUtil::MapClippedRect(const gfx::Transform& transform,
105                                    const gfx::Rect& src_rect) {
106   return gfx::ToEnclosingRect(MapClippedRect(transform, gfx::RectF(src_rect)));
107 }
108
109 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform,
110                                     const gfx::RectF& src_rect) {
111   if (transform.IsIdentityOrTranslation()) {
112     return src_rect +
113            gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
114                           SkMScalarToFloat(transform.matrix().get(1, 3)));
115   }
116
117   // Apply the transform, but retain the result in homogeneous coordinates.
118
119   SkMScalar quad[4 * 2];  // input: 4 x 2D points
120   quad[0] = src_rect.x();
121   quad[1] = src_rect.y();
122   quad[2] = src_rect.right();
123   quad[3] = src_rect.y();
124   quad[4] = src_rect.right();
125   quad[5] = src_rect.bottom();
126   quad[6] = src_rect.x();
127   quad[7] = src_rect.bottom();
128
129   SkMScalar result[4 * 4];  // output: 4 x 4D homogeneous points
130   transform.matrix().map2(quad, 4, result);
131
132   HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]);
133   HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]);
134   HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]);
135   HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]);
136   return ComputeEnclosingClippedRect(hc0, hc1, hc2, hc3);
137 }
138
139 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform,
140                                         const gfx::RectF& src_rect) {
141   if (transform.IsIdentityOrTranslation()) {
142     return src_rect +
143            gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
144                           SkMScalarToFloat(transform.matrix().get(1, 3)));
145   }
146
147   // Perform the projection, but retain the result in homogeneous coordinates.
148   gfx::QuadF q = gfx::QuadF(src_rect);
149   HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1());
150   HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2());
151   HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3());
152   HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4());
153
154   return ComputeEnclosingClippedRect(h1, h2, h3, h4);
155 }
156
157 void MathUtil::MapClippedQuad(const gfx::Transform& transform,
158                               const gfx::QuadF& src_quad,
159                               gfx::PointF clipped_quad[8],
160                               int* num_vertices_in_clipped_quad) {
161   HomogeneousCoordinate h1 =
162       MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p1()));
163   HomogeneousCoordinate h2 =
164       MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p2()));
165   HomogeneousCoordinate h3 =
166       MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p3()));
167   HomogeneousCoordinate h4 =
168       MapHomogeneousPoint(transform, gfx::Point3F(src_quad.p4()));
169
170   // The order of adding the vertices to the array is chosen so that
171   // clockwise / counter-clockwise orientation is retained.
172
173   *num_vertices_in_clipped_quad = 0;
174
175   if (!h1.ShouldBeClipped()) {
176     AddVertexToClippedQuad(
177         h1.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
178   }
179
180   if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped()) {
181     AddVertexToClippedQuad(
182         ComputeClippedPointForEdge(h1, h2).CartesianPoint2d(),
183         clipped_quad,
184         num_vertices_in_clipped_quad);
185   }
186
187   if (!h2.ShouldBeClipped()) {
188     AddVertexToClippedQuad(
189         h2.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
190   }
191
192   if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped()) {
193     AddVertexToClippedQuad(
194         ComputeClippedPointForEdge(h2, h3).CartesianPoint2d(),
195         clipped_quad,
196         num_vertices_in_clipped_quad);
197   }
198
199   if (!h3.ShouldBeClipped()) {
200     AddVertexToClippedQuad(
201         h3.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
202   }
203
204   if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped()) {
205     AddVertexToClippedQuad(
206         ComputeClippedPointForEdge(h3, h4).CartesianPoint2d(),
207         clipped_quad,
208         num_vertices_in_clipped_quad);
209   }
210
211   if (!h4.ShouldBeClipped()) {
212     AddVertexToClippedQuad(
213         h4.CartesianPoint2d(), clipped_quad, num_vertices_in_clipped_quad);
214   }
215
216   if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped()) {
217     AddVertexToClippedQuad(
218         ComputeClippedPointForEdge(h4, h1).CartesianPoint2d(),
219         clipped_quad,
220         num_vertices_in_clipped_quad);
221   }
222
223   DCHECK_LE(*num_vertices_in_clipped_quad, 8);
224 }
225
226 gfx::RectF MathUtil::ComputeEnclosingRectOfVertices(
227     const gfx::PointF vertices[],
228     int num_vertices) {
229   if (num_vertices < 2)
230     return gfx::RectF();
231
232   float xmin = std::numeric_limits<float>::max();
233   float xmax = -std::numeric_limits<float>::max();
234   float ymin = std::numeric_limits<float>::max();
235   float ymax = -std::numeric_limits<float>::max();
236
237   for (int i = 0; i < num_vertices; ++i)
238     ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax, vertices[i]);
239
240   return gfx::RectF(gfx::PointF(xmin, ymin),
241                     gfx::SizeF(xmax - xmin, ymax - ymin));
242 }
243
244 gfx::RectF MathUtil::ComputeEnclosingClippedRect(
245     const HomogeneousCoordinate& h1,
246     const HomogeneousCoordinate& h2,
247     const HomogeneousCoordinate& h3,
248     const HomogeneousCoordinate& h4) {
249   // This function performs clipping as necessary and computes the enclosing 2d
250   // gfx::RectF of the vertices. Doing these two steps simultaneously allows us
251   // to avoid the overhead of storing an unknown number of clipped vertices.
252
253   // If no vertices on the quad are clipped, then we can simply return the
254   // enclosing rect directly.
255   bool something_clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() ||
256                            h3.ShouldBeClipped() || h4.ShouldBeClipped();
257   if (!something_clipped) {
258     gfx::QuadF mapped_quad = gfx::QuadF(h1.CartesianPoint2d(),
259                                         h2.CartesianPoint2d(),
260                                         h3.CartesianPoint2d(),
261                                         h4.CartesianPoint2d());
262     return mapped_quad.BoundingBox();
263   }
264
265   bool everything_clipped = h1.ShouldBeClipped() && h2.ShouldBeClipped() &&
266                             h3.ShouldBeClipped() && h4.ShouldBeClipped();
267   if (everything_clipped)
268     return gfx::RectF();
269
270   float xmin = std::numeric_limits<float>::max();
271   float xmax = -std::numeric_limits<float>::max();
272   float ymin = std::numeric_limits<float>::max();
273   float ymax = -std::numeric_limits<float>::max();
274
275   if (!h1.ShouldBeClipped())
276     ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
277                                h1.CartesianPoint2d());
278
279   if (h1.ShouldBeClipped() ^ h2.ShouldBeClipped())
280     ExpandBoundsToIncludePoint(&xmin,
281                                &xmax,
282                                &ymin,
283                                &ymax,
284                                ComputeClippedPointForEdge(h1, h2)
285                                    .CartesianPoint2d());
286
287   if (!h2.ShouldBeClipped())
288     ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
289                                h2.CartesianPoint2d());
290
291   if (h2.ShouldBeClipped() ^ h3.ShouldBeClipped())
292     ExpandBoundsToIncludePoint(&xmin,
293                                &xmax,
294                                &ymin,
295                                &ymax,
296                                ComputeClippedPointForEdge(h2, h3)
297                                    .CartesianPoint2d());
298
299   if (!h3.ShouldBeClipped())
300     ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
301                                h3.CartesianPoint2d());
302
303   if (h3.ShouldBeClipped() ^ h4.ShouldBeClipped())
304     ExpandBoundsToIncludePoint(&xmin,
305                                &xmax,
306                                &ymin,
307                                &ymax,
308                                ComputeClippedPointForEdge(h3, h4)
309                                    .CartesianPoint2d());
310
311   if (!h4.ShouldBeClipped())
312     ExpandBoundsToIncludePoint(&xmin, &xmax, &ymin, &ymax,
313                                h4.CartesianPoint2d());
314
315   if (h4.ShouldBeClipped() ^ h1.ShouldBeClipped())
316     ExpandBoundsToIncludePoint(&xmin,
317                                &xmax,
318                                &ymin,
319                                &ymax,
320                                ComputeClippedPointForEdge(h4, h1)
321                                    .CartesianPoint2d());
322
323   return gfx::RectF(gfx::PointF(xmin, ymin),
324                     gfx::SizeF(xmax - xmin, ymax - ymin));
325 }
326
327 gfx::QuadF MathUtil::MapQuad(const gfx::Transform& transform,
328                              const gfx::QuadF& q,
329                              bool* clipped) {
330   if (transform.IsIdentityOrTranslation()) {
331     gfx::QuadF mapped_quad(q);
332     mapped_quad +=
333         gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)),
334                        SkMScalarToFloat(transform.matrix().get(1, 3)));
335     *clipped = false;
336     return mapped_quad;
337   }
338
339   HomogeneousCoordinate h1 =
340       MapHomogeneousPoint(transform, gfx::Point3F(q.p1()));
341   HomogeneousCoordinate h2 =
342       MapHomogeneousPoint(transform, gfx::Point3F(q.p2()));
343   HomogeneousCoordinate h3 =
344       MapHomogeneousPoint(transform, gfx::Point3F(q.p3()));
345   HomogeneousCoordinate h4 =
346       MapHomogeneousPoint(transform, gfx::Point3F(q.p4()));
347
348   *clipped = h1.ShouldBeClipped() || h2.ShouldBeClipped() ||
349             h3.ShouldBeClipped() || h4.ShouldBeClipped();
350
351   // Result will be invalid if clipped == true. But, compute it anyway just in
352   // case, to emulate existing behavior.
353   return gfx::QuadF(h1.CartesianPoint2d(),
354                     h2.CartesianPoint2d(),
355                     h3.CartesianPoint2d(),
356                     h4.CartesianPoint2d());
357 }
358
359 gfx::PointF MathUtil::MapPoint(const gfx::Transform& transform,
360                                const gfx::PointF& p,
361                                bool* clipped) {
362   HomogeneousCoordinate h = MapHomogeneousPoint(transform, gfx::Point3F(p));
363
364   if (h.w() > 0) {
365     *clipped = false;
366     return h.CartesianPoint2d();
367   }
368
369   // The cartesian coordinates will be invalid after dividing by w.
370   *clipped = true;
371
372   // Avoid dividing by w if w == 0.
373   if (!h.w())
374     return gfx::PointF();
375
376   // This return value will be invalid because clipped == true, but (1) users of
377   // this code should be ignoring the return value when clipped == true anyway,
378   // and (2) this behavior is more consistent with existing behavior of WebKit
379   // transforms if the user really does not ignore the return value.
380   return h.CartesianPoint2d();
381 }
382
383 gfx::Point3F MathUtil::MapPoint(const gfx::Transform& transform,
384                                 const gfx::Point3F& p,
385                                 bool* clipped) {
386   HomogeneousCoordinate h = MapHomogeneousPoint(transform, p);
387
388   if (h.w() > 0) {
389     *clipped = false;
390     return h.CartesianPoint3d();
391   }
392
393   // The cartesian coordinates will be invalid after dividing by w.
394   *clipped = true;
395
396   // Avoid dividing by w if w == 0.
397   if (!h.w())
398     return gfx::Point3F();
399
400   // This return value will be invalid because clipped == true, but (1) users of
401   // this code should be ignoring the return value when clipped == true anyway,
402   // and (2) this behavior is more consistent with existing behavior of WebKit
403   // transforms if the user really does not ignore the return value.
404   return h.CartesianPoint3d();
405 }
406
407 gfx::QuadF MathUtil::ProjectQuad(const gfx::Transform& transform,
408                                  const gfx::QuadF& q,
409                                  bool* clipped) {
410   gfx::QuadF projected_quad;
411   bool clipped_point;
412   projected_quad.set_p1(ProjectPoint(transform, q.p1(), &clipped_point));
413   *clipped = clipped_point;
414   projected_quad.set_p2(ProjectPoint(transform, q.p2(), &clipped_point));
415   *clipped |= clipped_point;
416   projected_quad.set_p3(ProjectPoint(transform, q.p3(), &clipped_point));
417   *clipped |= clipped_point;
418   projected_quad.set_p4(ProjectPoint(transform, q.p4(), &clipped_point));
419   *clipped |= clipped_point;
420
421   return projected_quad;
422 }
423
424 gfx::PointF MathUtil::ProjectPoint(const gfx::Transform& transform,
425                                    const gfx::PointF& p,
426                                    bool* clipped) {
427   HomogeneousCoordinate h = ProjectHomogeneousPoint(transform, p);
428
429   if (h.w() > 0) {
430     // The cartesian coordinates will be valid in this case.
431     *clipped = false;
432     return h.CartesianPoint2d();
433   }
434
435   // The cartesian coordinates will be invalid after dividing by w.
436   *clipped = true;
437
438   // Avoid dividing by w if w == 0.
439   if (!h.w())
440     return gfx::PointF();
441
442   // This return value will be invalid because clipped == true, but (1) users of
443   // this code should be ignoring the return value when clipped == true anyway,
444   // and (2) this behavior is more consistent with existing behavior of WebKit
445   // transforms if the user really does not ignore the return value.
446   return h.CartesianPoint2d();
447 }
448
449 gfx::RectF MathUtil::ScaleRectProportional(const gfx::RectF& input_outer_rect,
450                                            const gfx::RectF& scale_outer_rect,
451                                            const gfx::RectF& scale_inner_rect) {
452   gfx::RectF output_inner_rect = input_outer_rect;
453   float scale_rect_to_input_scale_x =
454       scale_outer_rect.width() / input_outer_rect.width();
455   float scale_rect_to_input_scale_y =
456       scale_outer_rect.height() / input_outer_rect.height();
457
458   gfx::Vector2dF top_left_diff =
459       scale_inner_rect.origin() - scale_outer_rect.origin();
460   gfx::Vector2dF bottom_right_diff =
461       scale_inner_rect.bottom_right() - scale_outer_rect.bottom_right();
462   output_inner_rect.Inset(top_left_diff.x() / scale_rect_to_input_scale_x,
463                           top_left_diff.y() / scale_rect_to_input_scale_y,
464                           -bottom_right_diff.x() / scale_rect_to_input_scale_x,
465                           -bottom_right_diff.y() / scale_rect_to_input_scale_y);
466   return output_inner_rect;
467 }
468
469 static inline float ScaleOnAxis(double a, double b, double c) {
470   if (!b && !c)
471     return a;
472   if (!a && !c)
473     return b;
474   if (!a && !b)
475     return c;
476
477   // Do the sqrt as a double to not lose precision.
478   return static_cast<float>(std::sqrt(a * a + b * b + c * c));
479 }
480
481 gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents(
482     const gfx::Transform& transform,
483     float fallback_value) {
484   if (transform.HasPerspective())
485     return gfx::Vector2dF(fallback_value, fallback_value);
486   float x_scale = ScaleOnAxis(transform.matrix().getDouble(0, 0),
487                               transform.matrix().getDouble(1, 0),
488                               transform.matrix().getDouble(2, 0));
489   float y_scale = ScaleOnAxis(transform.matrix().getDouble(0, 1),
490                               transform.matrix().getDouble(1, 1),
491                               transform.matrix().getDouble(2, 1));
492   return gfx::Vector2dF(x_scale, y_scale);
493 }
494
495 float MathUtil::SmallestAngleBetweenVectors(const gfx::Vector2dF& v1,
496                                             const gfx::Vector2dF& v2) {
497   double dot_product = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length();
498   // Clamp to compensate for rounding errors.
499   dot_product = std::max(-1.0, std::min(1.0, dot_product));
500   return static_cast<float>(Rad2Deg(std::acos(dot_product)));
501 }
502
503 gfx::Vector2dF MathUtil::ProjectVector(const gfx::Vector2dF& source,
504                                        const gfx::Vector2dF& destination) {
505   float projected_length =
506       gfx::DotProduct(source, destination) / destination.LengthSquared();
507   return gfx::Vector2dF(projected_length * destination.x(),
508                         projected_length * destination.y());
509 }
510
511 scoped_ptr<base::Value> MathUtil::AsValue(gfx::Size s) {
512   scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
513   res->SetDouble("width", s.width());
514   res->SetDouble("height", s.height());
515   return res.PassAs<base::Value>();
516 }
517
518 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::SizeF& s) {
519   scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
520   res->SetDouble("width", s.width());
521   res->SetDouble("height", s.height());
522   return res.PassAs<base::Value>();
523 }
524
525 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Rect& r) {
526   scoped_ptr<base::ListValue> res(new base::ListValue());
527   res->AppendInteger(r.x());
528   res->AppendInteger(r.y());
529   res->AppendInteger(r.width());
530   res->AppendInteger(r.height());
531   return res.PassAs<base::Value>();
532 }
533
534 bool MathUtil::FromValue(const base::Value* raw_value, gfx::Rect* out_rect) {
535   const base::ListValue* value = NULL;
536   if (!raw_value->GetAsList(&value))
537     return false;
538
539   if (value->GetSize() != 4)
540     return false;
541
542   int x, y, w, h;
543   bool ok = true;
544   ok &= value->GetInteger(0, &x);
545   ok &= value->GetInteger(1, &y);
546   ok &= value->GetInteger(2, &w);
547   ok &= value->GetInteger(3, &h);
548   if (!ok)
549     return false;
550
551   *out_rect = gfx::Rect(x, y, w, h);
552   return true;
553 }
554
555 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::PointF& pt) {
556   scoped_ptr<base::ListValue> res(new base::ListValue());
557   res->AppendDouble(pt.x());
558   res->AppendDouble(pt.y());
559   return res.PassAs<base::Value>();
560 }
561
562 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::QuadF& q) {
563   scoped_ptr<base::ListValue> res(new base::ListValue());
564   res->AppendDouble(q.p1().x());
565   res->AppendDouble(q.p1().y());
566   res->AppendDouble(q.p2().x());
567   res->AppendDouble(q.p2().y());
568   res->AppendDouble(q.p3().x());
569   res->AppendDouble(q.p3().y());
570   res->AppendDouble(q.p4().x());
571   res->AppendDouble(q.p4().y());
572   return res.PassAs<base::Value>();
573 }
574
575 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::RectF& rect) {
576   scoped_ptr<base::ListValue> res(new base::ListValue());
577   res->AppendDouble(rect.x());
578   res->AppendDouble(rect.y());
579   res->AppendDouble(rect.width());
580   res->AppendDouble(rect.height());
581   return res.PassAs<base::Value>();
582 }
583
584 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::Transform& transform) {
585   scoped_ptr<base::ListValue> res(new base::ListValue());
586   const SkMatrix44& m = transform.matrix();
587   for (int row = 0; row < 4; ++row) {
588     for (int col = 0; col < 4; ++col)
589       res->AppendDouble(m.getDouble(row, col));
590   }
591   return res.PassAs<base::Value>();
592 }
593
594 scoped_ptr<base::Value> MathUtil::AsValue(const gfx::BoxF& box) {
595   scoped_ptr<base::ListValue> res(new base::ListValue());
596   res->AppendInteger(box.x());
597   res->AppendInteger(box.y());
598   res->AppendInteger(box.z());
599   res->AppendInteger(box.width());
600   res->AppendInteger(box.height());
601   res->AppendInteger(box.depth());
602   return res.PassAs<base::Value>();
603 }
604
605 scoped_ptr<base::Value> MathUtil::AsValueSafely(double value) {
606   return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
607       std::min(value, std::numeric_limits<double>::max())));
608 }
609
610 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) {
611   return scoped_ptr<base::Value>(base::Value::CreateDoubleValue(
612       std::min(value, std::numeric_limits<float>::max())));
613 }
614
615 }  // namespace cc