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"
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"
21 const double MathUtil::kPiDouble
= 3.14159265358979323846;
22 const float MathUtil::kPiFloat
= 3.14159265358979323846f
;
24 static HomogeneousCoordinate
ProjectHomogeneousPoint(
25 const gfx::Transform
& transform
,
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
32 if (!transform
.matrix().getDouble(2, 2))
33 return HomogeneousCoordinate(0.0, 0.0, 0.0, 1.0);
37 double z
= -(transform
.matrix().getDouble(2, 0) * x
+
38 transform
.matrix().getDouble(2, 1) * y
+
39 transform
.matrix().getDouble(2, 3)) /
40 transform
.matrix().getDouble(2, 2);
41 // implicit definition of w = 1;
43 double out_x
= x
* transform
.matrix().getDouble(0, 0) +
44 y
* transform
.matrix().getDouble(0, 1) +
45 z
* transform
.matrix().getDouble(0, 2) +
46 transform
.matrix().getDouble(0, 3);
47 double out_y
= x
* transform
.matrix().getDouble(1, 0) +
48 y
* transform
.matrix().getDouble(1, 1) +
49 z
* transform
.matrix().getDouble(1, 2) +
50 transform
.matrix().getDouble(1, 3);
51 double out_z
= x
* transform
.matrix().getDouble(2, 0) +
52 y
* transform
.matrix().getDouble(2, 1) +
53 z
* transform
.matrix().getDouble(2, 2) +
54 transform
.matrix().getDouble(2, 3);
55 double out_w
= x
* transform
.matrix().getDouble(3, 0) +
56 y
* transform
.matrix().getDouble(3, 1) +
57 z
* transform
.matrix().getDouble(3, 2) +
58 transform
.matrix().getDouble(3, 3);
60 return HomogeneousCoordinate(out_x
, out_y
, out_z
, out_w
);
63 static HomogeneousCoordinate
MapHomogeneousPoint(
64 const gfx::Transform
& transform
,
65 const gfx::Point3F
& p
) {
69 // implicit definition of w = 1;
71 double out_x
= x
* transform
.matrix().getDouble(0, 0) +
72 y
* transform
.matrix().getDouble(0, 1) +
73 z
* transform
.matrix().getDouble(0, 2) +
74 transform
.matrix().getDouble(0, 3);
75 double out_y
= x
* transform
.matrix().getDouble(1, 0) +
76 y
* transform
.matrix().getDouble(1, 1) +
77 z
* transform
.matrix().getDouble(1, 2) +
78 transform
.matrix().getDouble(1, 3);
79 double out_z
= x
* transform
.matrix().getDouble(2, 0) +
80 y
* transform
.matrix().getDouble(2, 1) +
81 z
* transform
.matrix().getDouble(2, 2) +
82 transform
.matrix().getDouble(2, 3);
83 double out_w
= x
* transform
.matrix().getDouble(3, 0) +
84 y
* transform
.matrix().getDouble(3, 1) +
85 z
* transform
.matrix().getDouble(3, 2) +
86 transform
.matrix().getDouble(3, 3);
88 return HomogeneousCoordinate(out_x
, out_y
, out_z
, out_w
);
91 static HomogeneousCoordinate
ComputeClippedPointForEdge(
92 const HomogeneousCoordinate
& h1
,
93 const HomogeneousCoordinate
& h2
) {
94 // Points h1 and h2 form a line in 4d, and any point on that line can be
95 // represented as an interpolation between h1 and h2:
96 // p = (1-t) h1 + (t) h2
98 // We want to compute point p such that p.w == epsilon, where epsilon is a
99 // small non-zero number. (but the smaller the number is, the higher the risk
101 // To do this, we solve for t in the following equation:
102 // p.w = epsilon = (1-t) * h1.w + (t) * h2.w
104 // Once paramter t is known, the rest of p can be computed via
105 // p = (1-t) h1 + (t) h2.
107 // Technically this is a special case of the following assertion, but its a
108 // good idea to keep it an explicit sanity check here.
109 DCHECK_NE(h2
.w
, h1
.w
);
110 // Exactly one of h1 or h2 (but not both) must be on the negative side of the
111 // w plane when this is called.
112 DCHECK(h1
.ShouldBeClipped() ^ h2
.ShouldBeClipped());
114 double w
= 0.00001; // or any positive non-zero small epsilon
116 double t
= (w
- h1
.w
) / (h2
.w
- h1
.w
);
118 double x
= (1 - t
) * h1
.x
+ t
* h2
.x
;
119 double y
= (1 - t
) * h1
.y
+ t
* h2
.y
;
120 double z
= (1 - t
) * h1
.z
+ t
* h2
.z
;
122 return HomogeneousCoordinate(x
, y
, z
, w
);
125 static inline void ExpandBoundsToIncludePoint(float* xmin
,
130 *xmin
= std::min(p
.x(), *xmin
);
131 *xmax
= std::max(p
.x(), *xmax
);
132 *ymin
= std::min(p
.y(), *ymin
);
133 *ymax
= std::max(p
.y(), *ymax
);
136 static inline void AddVertexToClippedQuad(gfx::PointF new_vertex
,
137 gfx::PointF clipped_quad
[8],
138 int* num_vertices_in_clipped_quad
) {
139 clipped_quad
[*num_vertices_in_clipped_quad
] = new_vertex
;
140 (*num_vertices_in_clipped_quad
)++;
143 gfx::Rect
MathUtil::MapClippedRect(const gfx::Transform
& transform
,
144 gfx::Rect src_rect
) {
145 return gfx::ToEnclosingRect(MapClippedRect(transform
, gfx::RectF(src_rect
)));
148 gfx::RectF
MathUtil::MapClippedRect(const gfx::Transform
& transform
,
149 const gfx::RectF
& src_rect
) {
150 if (transform
.IsIdentityOrTranslation())
153 static_cast<float>(transform
.matrix().getDouble(0, 3)),
154 static_cast<float>(transform
.matrix().getDouble(1, 3)));
156 // Apply the transform, but retain the result in homogeneous coordinates.
158 double quad
[4 * 2]; // input: 4 x 2D points
159 quad
[0] = src_rect
.x();
160 quad
[1] = src_rect
.y();
161 quad
[2] = src_rect
.right();
162 quad
[3] = src_rect
.y();
163 quad
[4] = src_rect
.right();
164 quad
[5] = src_rect
.bottom();
165 quad
[6] = src_rect
.x();
166 quad
[7] = src_rect
.bottom();
168 double result
[4 * 4]; // output: 4 x 4D homogeneous points
169 transform
.matrix().map2(quad
, 4, result
);
171 HomogeneousCoordinate
hc0(result
[0], result
[1], result
[2], result
[3]);
172 HomogeneousCoordinate
hc1(result
[4], result
[5], result
[6], result
[7]);
173 HomogeneousCoordinate
hc2(result
[8], result
[9], result
[10], result
[11]);
174 HomogeneousCoordinate
hc3(result
[12], result
[13], result
[14], result
[15]);
175 return ComputeEnclosingClippedRect(hc0
, hc1
, hc2
, hc3
);
178 gfx::RectF
MathUtil::ProjectClippedRect(const gfx::Transform
& transform
,
179 const gfx::RectF
& src_rect
) {
180 if (transform
.IsIdentityOrTranslation()) {
183 static_cast<float>(transform
.matrix().getDouble(0, 3)),
184 static_cast<float>(transform
.matrix().getDouble(1, 3)));
187 // Perform the projection, but retain the result in homogeneous coordinates.
188 gfx::QuadF q
= gfx::QuadF(src_rect
);
189 HomogeneousCoordinate h1
= ProjectHomogeneousPoint(transform
, q
.p1());
190 HomogeneousCoordinate h2
= ProjectHomogeneousPoint(transform
, q
.p2());
191 HomogeneousCoordinate h3
= ProjectHomogeneousPoint(transform
, q
.p3());
192 HomogeneousCoordinate h4
= ProjectHomogeneousPoint(transform
, q
.p4());
194 return ComputeEnclosingClippedRect(h1
, h2
, h3
, h4
);
197 void MathUtil::MapClippedQuad(const gfx::Transform
& transform
,
198 const gfx::QuadF
& src_quad
,
199 gfx::PointF clipped_quad
[8],
200 int* num_vertices_in_clipped_quad
) {
201 HomogeneousCoordinate h1
=
202 MapHomogeneousPoint(transform
, gfx::Point3F(src_quad
.p1()));
203 HomogeneousCoordinate h2
=
204 MapHomogeneousPoint(transform
, gfx::Point3F(src_quad
.p2()));
205 HomogeneousCoordinate h3
=
206 MapHomogeneousPoint(transform
, gfx::Point3F(src_quad
.p3()));
207 HomogeneousCoordinate h4
=
208 MapHomogeneousPoint(transform
, gfx::Point3F(src_quad
.p4()));
210 // The order of adding the vertices to the array is chosen so that
211 // clockwise / counter-clockwise orientation is retained.
213 *num_vertices_in_clipped_quad
= 0;
215 if (!h1
.ShouldBeClipped()) {
216 AddVertexToClippedQuad(
217 h1
.CartesianPoint2d(), clipped_quad
, num_vertices_in_clipped_quad
);
220 if (h1
.ShouldBeClipped() ^ h2
.ShouldBeClipped()) {
221 AddVertexToClippedQuad(
222 ComputeClippedPointForEdge(h1
, h2
).CartesianPoint2d(),
224 num_vertices_in_clipped_quad
);
227 if (!h2
.ShouldBeClipped()) {
228 AddVertexToClippedQuad(
229 h2
.CartesianPoint2d(), clipped_quad
, num_vertices_in_clipped_quad
);
232 if (h2
.ShouldBeClipped() ^ h3
.ShouldBeClipped()) {
233 AddVertexToClippedQuad(
234 ComputeClippedPointForEdge(h2
, h3
).CartesianPoint2d(),
236 num_vertices_in_clipped_quad
);
239 if (!h3
.ShouldBeClipped()) {
240 AddVertexToClippedQuad(
241 h3
.CartesianPoint2d(), clipped_quad
, num_vertices_in_clipped_quad
);
244 if (h3
.ShouldBeClipped() ^ h4
.ShouldBeClipped()) {
245 AddVertexToClippedQuad(
246 ComputeClippedPointForEdge(h3
, h4
).CartesianPoint2d(),
248 num_vertices_in_clipped_quad
);
251 if (!h4
.ShouldBeClipped()) {
252 AddVertexToClippedQuad(
253 h4
.CartesianPoint2d(), clipped_quad
, num_vertices_in_clipped_quad
);
256 if (h4
.ShouldBeClipped() ^ h1
.ShouldBeClipped()) {
257 AddVertexToClippedQuad(
258 ComputeClippedPointForEdge(h4
, h1
).CartesianPoint2d(),
260 num_vertices_in_clipped_quad
);
263 DCHECK_LE(*num_vertices_in_clipped_quad
, 8);
266 gfx::RectF
MathUtil::ComputeEnclosingRectOfVertices(gfx::PointF vertices
[],
268 if (num_vertices
< 2)
271 float xmin
= std::numeric_limits
<float>::max();
272 float xmax
= -std::numeric_limits
<float>::max();
273 float ymin
= std::numeric_limits
<float>::max();
274 float ymax
= -std::numeric_limits
<float>::max();
276 for (int i
= 0; i
< num_vertices
; ++i
)
277 ExpandBoundsToIncludePoint(&xmin
, &xmax
, &ymin
, &ymax
, vertices
[i
]);
279 return gfx::RectF(gfx::PointF(xmin
, ymin
),
280 gfx::SizeF(xmax
- xmin
, ymax
- ymin
));
283 gfx::RectF
MathUtil::ComputeEnclosingClippedRect(
284 const HomogeneousCoordinate
& h1
,
285 const HomogeneousCoordinate
& h2
,
286 const HomogeneousCoordinate
& h3
,
287 const HomogeneousCoordinate
& h4
) {
288 // This function performs clipping as necessary and computes the enclosing 2d
289 // gfx::RectF of the vertices. Doing these two steps simultaneously allows us
290 // to avoid the overhead of storing an unknown number of clipped vertices.
292 // If no vertices on the quad are clipped, then we can simply return the
293 // enclosing rect directly.
294 bool something_clipped
= h1
.ShouldBeClipped() || h2
.ShouldBeClipped() ||
295 h3
.ShouldBeClipped() || h4
.ShouldBeClipped();
296 if (!something_clipped
) {
297 gfx::QuadF mapped_quad
= gfx::QuadF(h1
.CartesianPoint2d(),
298 h2
.CartesianPoint2d(),
299 h3
.CartesianPoint2d(),
300 h4
.CartesianPoint2d());
301 return mapped_quad
.BoundingBox();
304 bool everything_clipped
= h1
.ShouldBeClipped() && h2
.ShouldBeClipped() &&
305 h3
.ShouldBeClipped() && h4
.ShouldBeClipped();
306 if (everything_clipped
)
309 float xmin
= std::numeric_limits
<float>::max();
310 float xmax
= -std::numeric_limits
<float>::max();
311 float ymin
= std::numeric_limits
<float>::max();
312 float ymax
= -std::numeric_limits
<float>::max();
314 if (!h1
.ShouldBeClipped())
315 ExpandBoundsToIncludePoint(&xmin
, &xmax
, &ymin
, &ymax
,
316 h1
.CartesianPoint2d());
318 if (h1
.ShouldBeClipped() ^ h2
.ShouldBeClipped())
319 ExpandBoundsToIncludePoint(&xmin
,
323 ComputeClippedPointForEdge(h1
, h2
)
324 .CartesianPoint2d());
326 if (!h2
.ShouldBeClipped())
327 ExpandBoundsToIncludePoint(&xmin
, &xmax
, &ymin
, &ymax
,
328 h2
.CartesianPoint2d());
330 if (h2
.ShouldBeClipped() ^ h3
.ShouldBeClipped())
331 ExpandBoundsToIncludePoint(&xmin
,
335 ComputeClippedPointForEdge(h2
, h3
)
336 .CartesianPoint2d());
338 if (!h3
.ShouldBeClipped())
339 ExpandBoundsToIncludePoint(&xmin
, &xmax
, &ymin
, &ymax
,
340 h3
.CartesianPoint2d());
342 if (h3
.ShouldBeClipped() ^ h4
.ShouldBeClipped())
343 ExpandBoundsToIncludePoint(&xmin
,
347 ComputeClippedPointForEdge(h3
, h4
)
348 .CartesianPoint2d());
350 if (!h4
.ShouldBeClipped())
351 ExpandBoundsToIncludePoint(&xmin
, &xmax
, &ymin
, &ymax
,
352 h4
.CartesianPoint2d());
354 if (h4
.ShouldBeClipped() ^ h1
.ShouldBeClipped())
355 ExpandBoundsToIncludePoint(&xmin
,
359 ComputeClippedPointForEdge(h4
, h1
)
360 .CartesianPoint2d());
362 return gfx::RectF(gfx::PointF(xmin
, ymin
),
363 gfx::SizeF(xmax
- xmin
, ymax
- ymin
));
366 gfx::QuadF
MathUtil::MapQuad(const gfx::Transform
& transform
,
369 if (transform
.IsIdentityOrTranslation()) {
370 gfx::QuadF
mapped_quad(q
);
372 gfx::Vector2dF(static_cast<float>(transform
.matrix().getDouble(0, 3)),
373 static_cast<float>(transform
.matrix().getDouble(1, 3)));
378 HomogeneousCoordinate h1
=
379 MapHomogeneousPoint(transform
, gfx::Point3F(q
.p1()));
380 HomogeneousCoordinate h2
=
381 MapHomogeneousPoint(transform
, gfx::Point3F(q
.p2()));
382 HomogeneousCoordinate h3
=
383 MapHomogeneousPoint(transform
, gfx::Point3F(q
.p3()));
384 HomogeneousCoordinate h4
=
385 MapHomogeneousPoint(transform
, gfx::Point3F(q
.p4()));
387 *clipped
= h1
.ShouldBeClipped() || h2
.ShouldBeClipped() ||
388 h3
.ShouldBeClipped() || h4
.ShouldBeClipped();
390 // Result will be invalid if clipped == true. But, compute it anyway just in
391 // case, to emulate existing behavior.
392 return gfx::QuadF(h1
.CartesianPoint2d(),
393 h2
.CartesianPoint2d(),
394 h3
.CartesianPoint2d(),
395 h4
.CartesianPoint2d());
398 gfx::PointF
MathUtil::MapPoint(const gfx::Transform
& transform
,
401 HomogeneousCoordinate h
= MapHomogeneousPoint(transform
, gfx::Point3F(p
));
405 return h
.CartesianPoint2d();
408 // The cartesian coordinates will be invalid after dividing by w.
411 // Avoid dividing by w if w == 0.
413 return gfx::PointF();
415 // This return value will be invalid because clipped == true, but (1) users of
416 // this code should be ignoring the return value when clipped == true anyway,
417 // and (2) this behavior is more consistent with existing behavior of WebKit
418 // transforms if the user really does not ignore the return value.
419 return h
.CartesianPoint2d();
422 gfx::Point3F
MathUtil::MapPoint(const gfx::Transform
& transform
,
423 const gfx::Point3F
& p
,
425 HomogeneousCoordinate h
= MapHomogeneousPoint(transform
, p
);
429 return h
.CartesianPoint3d();
432 // The cartesian coordinates will be invalid after dividing by w.
435 // Avoid dividing by w if w == 0.
437 return gfx::Point3F();
439 // This return value will be invalid because clipped == true, but (1) users of
440 // this code should be ignoring the return value when clipped == true anyway,
441 // and (2) this behavior is more consistent with existing behavior of WebKit
442 // transforms if the user really does not ignore the return value.
443 return h
.CartesianPoint3d();
446 gfx::QuadF
MathUtil::ProjectQuad(const gfx::Transform
& transform
,
449 gfx::QuadF projected_quad
;
451 projected_quad
.set_p1(ProjectPoint(transform
, q
.p1(), &clipped_point
));
452 *clipped
= clipped_point
;
453 projected_quad
.set_p2(ProjectPoint(transform
, q
.p2(), &clipped_point
));
454 *clipped
|= clipped_point
;
455 projected_quad
.set_p3(ProjectPoint(transform
, q
.p3(), &clipped_point
));
456 *clipped
|= clipped_point
;
457 projected_quad
.set_p4(ProjectPoint(transform
, q
.p4(), &clipped_point
));
458 *clipped
|= clipped_point
;
460 return projected_quad
;
463 gfx::PointF
MathUtil::ProjectPoint(const gfx::Transform
& transform
,
466 HomogeneousCoordinate h
= ProjectHomogeneousPoint(transform
, p
);
469 // The cartesian coordinates will be valid in this case.
471 return h
.CartesianPoint2d();
474 // The cartesian coordinates will be invalid after dividing by w.
477 // Avoid dividing by w if w == 0.
479 return gfx::PointF();
481 // This return value will be invalid because clipped == true, but (1) users of
482 // this code should be ignoring the return value when clipped == true anyway,
483 // and (2) this behavior is more consistent with existing behavior of WebKit
484 // transforms if the user really does not ignore the return value.
485 return h
.CartesianPoint2d();
488 static inline float ScaleOnAxis(double a
, double b
, double c
) {
489 return std::sqrt(a
* a
+ b
* b
+ c
* c
);
492 gfx::Vector2dF
MathUtil::ComputeTransform2dScaleComponents(
493 const gfx::Transform
& transform
,
494 float fallback_value
) {
495 if (transform
.HasPerspective())
496 return gfx::Vector2dF(fallback_value
, fallback_value
);
497 float x_scale
= ScaleOnAxis(transform
.matrix().getDouble(0, 0),
498 transform
.matrix().getDouble(1, 0),
499 transform
.matrix().getDouble(2, 0));
500 float y_scale
= ScaleOnAxis(transform
.matrix().getDouble(0, 1),
501 transform
.matrix().getDouble(1, 1),
502 transform
.matrix().getDouble(2, 1));
503 return gfx::Vector2dF(x_scale
, y_scale
);
506 float MathUtil::SmallestAngleBetweenVectors(gfx::Vector2dF v1
,
508 double dot_product
= gfx::DotProduct(v1
, v2
) / v1
.Length() / v2
.Length();
509 // Clamp to compensate for rounding errors.
510 dot_product
= std::max(-1.0, std::min(1.0, dot_product
));
511 return static_cast<float>(Rad2Deg(std::acos(dot_product
)));
514 gfx::Vector2dF
MathUtil::ProjectVector(gfx::Vector2dF source
,
515 gfx::Vector2dF destination
) {
516 float projected_length
=
517 gfx::DotProduct(source
, destination
) / destination
.LengthSquared();
518 return gfx::Vector2dF(projected_length
* destination
.x(),
519 projected_length
* destination
.y());
522 scoped_ptr
<base::Value
> MathUtil::AsValue(gfx::Size s
) {
523 scoped_ptr
<base::DictionaryValue
> res(new base::DictionaryValue());
524 res
->SetDouble("width", s
.width());
525 res
->SetDouble("height", s
.height());
526 return res
.PassAs
<base::Value
>();
529 scoped_ptr
<base::Value
> MathUtil::AsValue(gfx::PointF pt
) {
530 scoped_ptr
<base::DictionaryValue
> res(new base::DictionaryValue());
531 res
->SetDouble("x", pt
.x());
532 res
->SetDouble("y", pt
.y());
533 return res
.PassAs
<base::Value
>();
536 scoped_ptr
<base::Value
> MathUtil::AsValue(gfx::QuadF q
) {
537 scoped_ptr
<base::DictionaryValue
> res(new base::DictionaryValue());
538 res
->Set("p1", AsValue(q
.p1()).release());
539 res
->Set("p2", AsValue(q
.p2()).release());
540 res
->Set("p3", AsValue(q
.p3()).release());
541 res
->Set("p4", AsValue(q
.p4()).release());
542 return res
.PassAs
<base::Value
>();
545 scoped_ptr
<base::Value
> MathUtil::AsValueSafely(double value
) {
546 return scoped_ptr
<base::Value
>(base::Value::CreateDoubleValue(
547 std::min(value
, std::numeric_limits
<double>::max())));
550 scoped_ptr
<base::Value
> MathUtil::AsValueSafely(float value
) {
551 return scoped_ptr
<base::Value
>(base::Value::CreateDoubleValue(
552 std::min(value
, std::numeric_limits
<float>::max())));