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 #ifndef CC_BASE_MATH_UTIL_H_ | 5 #ifndef CC_BASE_MATH_UTIL_H_ |
6 #define CC_BASE_MATH_UTIL_H_ | 6 #define CC_BASE_MATH_UTIL_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <cmath> | 9 #include <cmath> |
10 #include <vector> | 10 #include <vector> |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } | 89 static float Deg2Rad(float deg) { return deg * kPiFloat / 180.0f; } |
90 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } | 90 static float Rad2Deg(float rad) { return rad * 180.0f / kPiFloat; } |
91 | 91 |
92 static float Round(float f) { | 92 static float Round(float f) { |
93 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); | 93 return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f); |
94 } | 94 } |
95 static double Round(double d) { | 95 static double Round(double d) { |
96 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); | 96 return (d > 0.0) ? std::floor(d + 0.5) : std::ceil(d - 0.5); |
97 } | 97 } |
98 | 98 |
99 // RoundUp rounds up a given |n| to be a multiple of |mul|. | 99 // Returns true if rounded up value does not overflow, false otherwise. |
| 100 template <typename T> |
| 101 static bool VerifyRoundup(T n, T mul) { |
| 102 return mul && (n <= (std::numeric_limits<T>::max() - |
| 103 (std::numeric_limits<T>::max() % mul))); |
| 104 } |
| 105 |
| 106 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. |
100 // Examples: | 107 // Examples: |
101 // - RoundUp(123, 50) returns 150. | 108 // - RoundUp(123, 50) returns 150. |
102 // - RoundUp(-123, 50) returns -100. | 109 // - RoundUp(-123, 50) returns -100. |
103 template <typename T> | 110 template <typename T> |
104 static T RoundUp(T n, T mul) { | 111 static T UncheckedRoundUp(T n, T mul) { |
105 static_assert(std::numeric_limits<T>::is_integer, | 112 static_assert(std::numeric_limits<T>::is_integer, |
106 "T must be an integer type"); | 113 "T must be an integer type"); |
107 return (n > 0) ? ((n + mul - 1) / mul) * mul : (n / mul) * mul; | 114 DCHECK(VerifyRoundup(n, mul)); |
| 115 return RoundUpInternal(n, mul); |
108 } | 116 } |
109 | 117 |
110 // RoundDown rounds down a given |n| to be a multiple of |mul|. | 118 // Similar to UncheckedRoundUp(), but dies with a CRASH() if rounding up a |
| 119 // given |n| overflows T. |
| 120 template <typename T> |
| 121 static T CheckedRoundUp(T n, T mul) { |
| 122 static_assert(std::numeric_limits<T>::is_integer, |
| 123 "T must be an integer type"); |
| 124 CHECK(VerifyRoundup(n, mul)); |
| 125 return RoundUpInternal(n, mul); |
| 126 } |
| 127 |
| 128 // Returns true if rounded down value does not underflow, false otherwise. |
| 129 template <typename T> |
| 130 static bool VerifyRoundDown(T n, T mul) { |
| 131 return mul && (n >= (std::numeric_limits<T>::min() - |
| 132 (std::numeric_limits<T>::min() % mul))); |
| 133 } |
| 134 |
| 135 // Rounds down a given |n| to be a multiple of |mul|, but may underflow. |
111 // Examples: | 136 // Examples: |
112 // - RoundDown(123, 50) returns 100. | 137 // - RoundDown(123, 50) returns 100. |
113 // - RoundDown(-123, 50) returns -150. | 138 // - RoundDown(-123, 50) returns -150. |
114 template <typename T> | 139 template <typename T> |
115 static T RoundDown(T n, T mul) { | 140 static T UncheckedRoundDown(T n, T mul) { |
116 static_assert(std::numeric_limits<T>::is_integer, | 141 static_assert(std::numeric_limits<T>::is_integer, |
117 "T must be an integer type"); | 142 "T must be an integer type"); |
118 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 | 143 DCHECK(VerifyRoundDown(n, mul)); |
119 : ((n - mul + 1) / mul) * mul; | 144 return RoundDownInternal(n, mul); |
| 145 } |
| 146 |
| 147 // Similar to UncheckedRoundDown(), but dies with a CRASH() if rounding down a |
| 148 // given |n| underflows T. |
| 149 template <typename T> |
| 150 static T CheckedRoundDown(T n, T mul) { |
| 151 static_assert(std::numeric_limits<T>::is_integer, |
| 152 "T must be an integer type"); |
| 153 CHECK(VerifyRoundDown(n, mul)); |
| 154 return RoundDownInternal(n, mul); |
120 } | 155 } |
121 | 156 |
122 template <typename T> static T ClampToRange(T value, T min, T max) { | 157 template <typename T> static T ClampToRange(T value, T min, T max) { |
123 return std::min(std::max(value, min), max); | 158 return std::min(std::max(value, min), max); |
124 } | 159 } |
125 | 160 |
126 // Background: Existing transform code does not do the right thing in | 161 // Background: Existing transform code does not do the right thing in |
127 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that | 162 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that |
128 // causes one of the transformed vertices to go to w < 0. In those cases, it | 163 // causes one of the transformed vertices to go to w < 0. In those cases, it |
129 // is necessary to perform clipping in homogeneous coordinates, after applying | 164 // is necessary to perform clipping in homogeneous coordinates, after applying |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 // Returns a base::Value representation of the floating point value. | 298 // Returns a base::Value representation of the floating point value. |
264 // If the value is inf, returns max double/float representation. | 299 // If the value is inf, returns max double/float representation. |
265 static double AsDoubleSafely(double value); | 300 static double AsDoubleSafely(double value); |
266 static float AsFloatSafely(float value); | 301 static float AsFloatSafely(float value); |
267 | 302 |
268 // Returns vector that x axis (1,0,0) transforms to under given transform. | 303 // Returns vector that x axis (1,0,0) transforms to under given transform. |
269 static gfx::Vector3dF GetXAxis(const gfx::Transform& transform); | 304 static gfx::Vector3dF GetXAxis(const gfx::Transform& transform); |
270 | 305 |
271 // Returns vector that y axis (0,1,0) transforms to under given transform. | 306 // Returns vector that y axis (0,1,0) transforms to under given transform. |
272 static gfx::Vector3dF GetYAxis(const gfx::Transform& transform); | 307 static gfx::Vector3dF GetYAxis(const gfx::Transform& transform); |
| 308 |
| 309 private: |
| 310 template <typename T> |
| 311 static T RoundUpInternal(T n, T mul) { |
| 312 return (n > 0) ? ((n + mul - 1) / mul) * mul : (n / mul) * mul; |
| 313 } |
| 314 |
| 315 template <typename T> |
| 316 static T RoundDownInternal(T n, T mul) { |
| 317 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 |
| 318 : ((n - mul + 1) / mul) * mul; |
| 319 } |
273 }; | 320 }; |
274 | 321 |
275 } // namespace cc | 322 } // namespace cc |
276 | 323 |
277 #endif // CC_BASE_MATH_UTIL_H_ | 324 #endif // CC_BASE_MATH_UTIL_H_ |
OLD | NEW |