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 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 gfx::Rect src_rect) { | 105 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 |
109 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform, | 109 gfx::RectF MathUtil::MapClippedRect(const gfx::Transform& transform, |
110 const gfx::RectF& src_rect) { | 110 const gfx::RectF& src_rect) { |
111 if (transform.IsIdentityOrTranslation()) | 111 if (transform.IsIdentityOrTranslation()) { |
112 return src_rect + | 112 return src_rect + |
113 gfx::Vector2dF( | 113 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), |
danakj
2013/09/11 18:53:46
can probably use getFloat() now? (thanks for doing
enne (OOO)
2013/09/11 19:58:30
For some future version of now? I'll land this as-
danakj
2013/09/11 19:59:40
Ya sorry I figured the skia folks would have commi
| |
114 static_cast<float>(transform.matrix().getDouble(0, 3)), | 114 SkMScalarToFloat(transform.matrix().get(1, 3))); |
115 static_cast<float>(transform.matrix().getDouble(1, 3))); | 115 } |
116 | 116 |
117 // Apply the transform, but retain the result in homogeneous coordinates. | 117 // Apply the transform, but retain the result in homogeneous coordinates. |
118 | 118 |
119 double quad[4 * 2]; // input: 4 x 2D points | 119 SkMScalar quad[4 * 2]; // input: 4 x 2D points |
120 quad[0] = src_rect.x(); | 120 quad[0] = src_rect.x(); |
121 quad[1] = src_rect.y(); | 121 quad[1] = src_rect.y(); |
122 quad[2] = src_rect.right(); | 122 quad[2] = src_rect.right(); |
123 quad[3] = src_rect.y(); | 123 quad[3] = src_rect.y(); |
124 quad[4] = src_rect.right(); | 124 quad[4] = src_rect.right(); |
125 quad[5] = src_rect.bottom(); | 125 quad[5] = src_rect.bottom(); |
126 quad[6] = src_rect.x(); | 126 quad[6] = src_rect.x(); |
127 quad[7] = src_rect.bottom(); | 127 quad[7] = src_rect.bottom(); |
128 | 128 |
129 double result[4 * 4]; // output: 4 x 4D homogeneous points | 129 SkMScalar result[4 * 4]; // output: 4 x 4D homogeneous points |
130 transform.matrix().map2(quad, 4, result); | 130 transform.matrix().map2(quad, 4, result); |
131 | 131 |
132 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); | 132 HomogeneousCoordinate hc0(result[0], result[1], result[2], result[3]); |
133 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); | 133 HomogeneousCoordinate hc1(result[4], result[5], result[6], result[7]); |
134 HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]); | 134 HomogeneousCoordinate hc2(result[8], result[9], result[10], result[11]); |
135 HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]); | 135 HomogeneousCoordinate hc3(result[12], result[13], result[14], result[15]); |
136 return ComputeEnclosingClippedRect(hc0, hc1, hc2, hc3); | 136 return ComputeEnclosingClippedRect(hc0, hc1, hc2, hc3); |
137 } | 137 } |
138 | 138 |
139 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform, | 139 gfx::RectF MathUtil::ProjectClippedRect(const gfx::Transform& transform, |
140 const gfx::RectF& src_rect) { | 140 const gfx::RectF& src_rect) { |
141 if (transform.IsIdentityOrTranslation()) { | 141 if (transform.IsIdentityOrTranslation()) { |
142 return src_rect + | 142 return src_rect + |
143 gfx::Vector2dF( | 143 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), |
144 static_cast<float>(transform.matrix().getDouble(0, 3)), | 144 SkMScalarToFloat(transform.matrix().get(1, 3))); |
145 static_cast<float>(transform.matrix().getDouble(1, 3))); | |
146 } | 145 } |
147 | 146 |
148 // Perform the projection, but retain the result in homogeneous coordinates. | 147 // Perform the projection, but retain the result in homogeneous coordinates. |
149 gfx::QuadF q = gfx::QuadF(src_rect); | 148 gfx::QuadF q = gfx::QuadF(src_rect); |
150 HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1()); | 149 HomogeneousCoordinate h1 = ProjectHomogeneousPoint(transform, q.p1()); |
151 HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2()); | 150 HomogeneousCoordinate h2 = ProjectHomogeneousPoint(transform, q.p2()); |
152 HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3()); | 151 HomogeneousCoordinate h3 = ProjectHomogeneousPoint(transform, q.p3()); |
153 HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4()); | 152 HomogeneousCoordinate h4 = ProjectHomogeneousPoint(transform, q.p4()); |
154 | 153 |
155 return ComputeEnclosingClippedRect(h1, h2, h3, h4); | 154 return ComputeEnclosingClippedRect(h1, h2, h3, h4); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 return gfx::RectF(gfx::PointF(xmin, ymin), | 322 return gfx::RectF(gfx::PointF(xmin, ymin), |
324 gfx::SizeF(xmax - xmin, ymax - ymin)); | 323 gfx::SizeF(xmax - xmin, ymax - ymin)); |
325 } | 324 } |
326 | 325 |
327 gfx::QuadF MathUtil::MapQuad(const gfx::Transform& transform, | 326 gfx::QuadF MathUtil::MapQuad(const gfx::Transform& transform, |
328 const gfx::QuadF& q, | 327 const gfx::QuadF& q, |
329 bool* clipped) { | 328 bool* clipped) { |
330 if (transform.IsIdentityOrTranslation()) { | 329 if (transform.IsIdentityOrTranslation()) { |
331 gfx::QuadF mapped_quad(q); | 330 gfx::QuadF mapped_quad(q); |
332 mapped_quad += | 331 mapped_quad += |
333 gfx::Vector2dF(static_cast<float>(transform.matrix().getDouble(0, 3)), | 332 gfx::Vector2dF(SkMScalarToFloat(transform.matrix().get(0, 3)), |
334 static_cast<float>(transform.matrix().getDouble(1, 3))); | 333 SkMScalarToFloat(transform.matrix().get(1, 3))); |
335 *clipped = false; | 334 *clipped = false; |
336 return mapped_quad; | 335 return mapped_quad; |
337 } | 336 } |
338 | 337 |
339 HomogeneousCoordinate h1 = | 338 HomogeneousCoordinate h1 = |
340 MapHomogeneousPoint(transform, gfx::Point3F(q.p1())); | 339 MapHomogeneousPoint(transform, gfx::Point3F(q.p1())); |
341 HomogeneousCoordinate h2 = | 340 HomogeneousCoordinate h2 = |
342 MapHomogeneousPoint(transform, gfx::Point3F(q.p2())); | 341 MapHomogeneousPoint(transform, gfx::Point3F(q.p2())); |
343 HomogeneousCoordinate h3 = | 342 HomogeneousCoordinate h3 = |
344 MapHomogeneousPoint(transform, gfx::Point3F(q.p3())); | 343 MapHomogeneousPoint(transform, gfx::Point3F(q.p3())); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 scale_inner_rect.origin() - scale_outer_rect.origin(); | 458 scale_inner_rect.origin() - scale_outer_rect.origin(); |
460 gfx::Vector2dF bottom_right_diff = | 459 gfx::Vector2dF bottom_right_diff = |
461 scale_inner_rect.bottom_right() - scale_outer_rect.bottom_right(); | 460 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, | 461 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, | 462 top_left_diff.y() / scale_rect_to_input_scale_y, |
464 -bottom_right_diff.x() / scale_rect_to_input_scale_x, | 463 -bottom_right_diff.x() / scale_rect_to_input_scale_x, |
465 -bottom_right_diff.y() / scale_rect_to_input_scale_y); | 464 -bottom_right_diff.y() / scale_rect_to_input_scale_y); |
466 return output_inner_rect; | 465 return output_inner_rect; |
467 } | 466 } |
468 | 467 |
469 static inline float ScaleOnAxis(double a, double b, double c) { | 468 static inline SkMScalar ScaleOnAxis(SkMScalar a, SkMScalar b, SkMScalar c) { |
danakj
2013/09/11 18:53:46
here we're multiplying stuff just to take the sqrt
enne (OOO)
2013/09/11 19:58:30
Ok, sure. I'll buy your argument that the sqrt sh
| |
470 return std::sqrt(a * a + b * b + c * c); | 469 return std::sqrt(a * a + b * b + c * c); |
471 } | 470 } |
472 | 471 |
473 gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents( | 472 gfx::Vector2dF MathUtil::ComputeTransform2dScaleComponents( |
474 const gfx::Transform& transform, | 473 const gfx::Transform& transform, |
475 float fallback_value) { | 474 float fallback_value) { |
476 if (transform.HasPerspective()) | 475 if (transform.HasPerspective()) |
477 return gfx::Vector2dF(fallback_value, fallback_value); | 476 return gfx::Vector2dF(fallback_value, fallback_value); |
478 float x_scale = ScaleOnAxis(transform.matrix().getDouble(0, 0), | 477 SkMScalar x_scale = ScaleOnAxis(transform.matrix().get(0, 0), |
479 transform.matrix().getDouble(1, 0), | 478 transform.matrix().get(1, 0), |
480 transform.matrix().getDouble(2, 0)); | 479 transform.matrix().get(2, 0)); |
481 float y_scale = ScaleOnAxis(transform.matrix().getDouble(0, 1), | 480 SkMScalar y_scale = ScaleOnAxis(transform.matrix().get(0, 1), |
482 transform.matrix().getDouble(1, 1), | 481 transform.matrix().get(1, 1), |
483 transform.matrix().getDouble(2, 1)); | 482 transform.matrix().get(2, 1)); |
484 return gfx::Vector2dF(x_scale, y_scale); | 483 return gfx::Vector2dF(SkMScalarToFloat(x_scale), SkMScalarToFloat(y_scale)); |
485 } | 484 } |
486 | 485 |
487 float MathUtil::SmallestAngleBetweenVectors(gfx::Vector2dF v1, | 486 float MathUtil::SmallestAngleBetweenVectors(gfx::Vector2dF v1, |
488 gfx::Vector2dF v2) { | 487 gfx::Vector2dF v2) { |
489 double dot_product = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length(); | 488 double dot_product = gfx::DotProduct(v1, v2) / v1.Length() / v2.Length(); |
490 // Clamp to compensate for rounding errors. | 489 // Clamp to compensate for rounding errors. |
491 dot_product = std::max(-1.0, std::min(1.0, dot_product)); | 490 dot_product = std::max(-1.0, std::min(1.0, dot_product)); |
492 return static_cast<float>(Rad2Deg(std::acos(dot_product))); | 491 return static_cast<float>(Rad2Deg(std::acos(dot_product))); |
493 } | 492 } |
494 | 493 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
587 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 586 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
588 std::min(value, std::numeric_limits<double>::max()))); | 587 std::min(value, std::numeric_limits<double>::max()))); |
589 } | 588 } |
590 | 589 |
591 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { | 590 scoped_ptr<base::Value> MathUtil::AsValueSafely(float value) { |
592 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( | 591 return scoped_ptr<base::Value>(base::Value::CreateDoubleValue( |
593 std::min(value, std::numeric_limits<float>::max()))); | 592 std::min(value, std::numeric_limits<float>::max()))); |
594 } | 593 } |
595 | 594 |
596 } // namespace cc | 595 } // namespace cc |
OLD | NEW |