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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 (*num_vertices_in_clipped_quad)++; | 113 (*num_vertices_in_clipped_quad)++; |
114 } | 114 } |
115 | 115 |
116 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, | 116 static inline void AddVertexToClippedQuad3d(const gfx::Point3F& new_vertex, |
117 gfx::Point3F clipped_quad[8], | 117 gfx::Point3F clipped_quad[8], |
118 int* num_vertices_in_clipped_quad) { | 118 int* num_vertices_in_clipped_quad) { |
119 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; | 119 clipped_quad[*num_vertices_in_clipped_quad] = new_vertex; |
120 (*num_vertices_in_clipped_quad)++; | 120 (*num_vertices_in_clipped_quad)++; |
121 } | 121 } |
122 | 122 |
| 123 float MathUtil::RoundToFixedPrecision(float value, int precision) { |
| 124 DCHECK_GE(precision, 0); |
| 125 |
| 126 // The value of std::numeric_limits<float>::digits10 is the number of base-10 |
| 127 // digits that can be represented by the type float without change. |
| 128 int max_digits10 = std::numeric_limits<float>::digits10; |
| 129 |
| 130 float integral_part; |
| 131 float fractional_part = std::modf(value, &integral_part); |
| 132 |
| 133 // Return if |value| can not be represented by float without change. |
| 134 int digits10_in_integral_part = |
| 135 integral_part > 0 ? static_cast<int>(std::log10(integral_part)) + 1 : 1; |
| 136 if (digits10_in_integral_part > max_digits10) |
| 137 return value; |
| 138 |
| 139 // Get maximum possible precision. |
| 140 int possible_precision = |
| 141 std::min((max_digits10 - digits10_in_integral_part), precision); |
| 142 DCHECK(possible_precision < max_digits10); |
| 143 |
| 144 float factor = std::pow(10, possible_precision); |
| 145 fractional_part = std::round(fractional_part * factor) / factor; |
| 146 return integral_part + fractional_part; |
| 147 } |
| 148 |
123 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, | 149 gfx::Rect MathUtil::MapEnclosingClippedRect(const gfx::Transform& transform, |
124 const gfx::Rect& src_rect) { | 150 const gfx::Rect& src_rect) { |
125 if (transform.IsIdentityOrIntegerTranslation()) { | 151 if (transform.IsIdentityOrIntegerTranslation()) { |
126 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), | 152 gfx::Vector2d offset(static_cast<int>(transform.matrix().getFloat(0, 3)), |
127 static_cast<int>(transform.matrix().getFloat(1, 3))); | 153 static_cast<int>(transform.matrix().getFloat(1, 3))); |
128 return src_rect + offset; | 154 return src_rect + offset; |
129 } | 155 } |
130 gfx::RectF mapped_rect = MapClippedRect(transform, gfx::RectF(src_rect)); | 156 gfx::RectF mapped_rect = MapClippedRect(transform, gfx::RectF(src_rect)); |
131 | 157 |
132 // gfx::ToEnclosingRect crashes if called on a RectF with any NaN coordinate. | 158 // gfx::ToEnclosingRect crashes if called on a RectF with any NaN coordinate. |
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 transform.matrix().getFloat(2, 0)); | 915 transform.matrix().getFloat(2, 0)); |
890 } | 916 } |
891 | 917 |
892 gfx::Vector3dF MathUtil::GetYAxis(const gfx::Transform& transform) { | 918 gfx::Vector3dF MathUtil::GetYAxis(const gfx::Transform& transform) { |
893 return gfx::Vector3dF(transform.matrix().getFloat(0, 1), | 919 return gfx::Vector3dF(transform.matrix().getFloat(0, 1), |
894 transform.matrix().getFloat(1, 1), | 920 transform.matrix().getFloat(1, 1), |
895 transform.matrix().getFloat(2, 1)); | 921 transform.matrix().getFloat(2, 1)); |
896 } | 922 } |
897 | 923 |
898 } // namespace cc | 924 } // namespace cc |
OLD | NEW |