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 | |
vmpstr
2015/08/04 18:17:23
"a ? b : false" is better written as "a && b"
| |
103 ? n <= (std::numeric_limits<T>::max() - | |
104 (std::numeric_limits<T>::max() % mul)) | |
105 : false; | |
106 } | |
107 | |
108 // Rounds up a given |n| to be a multiple of |mul|, but may overflow. | |
100 // Examples: | 109 // Examples: |
101 // - RoundUp(123, 50) returns 150. | 110 // - RoundUp(123, 50) returns 150. |
102 // - RoundUp(-123, 50) returns -100. | 111 // - RoundUp(-123, 50) returns -100. |
103 template <typename T> | 112 template <typename T> |
104 static T RoundUp(T n, T mul) { | 113 static T UncheckedRoundUp(T n, T mul) { |
105 static_assert(std::numeric_limits<T>::is_integer, | 114 static_assert(std::numeric_limits<T>::is_integer, |
106 "T must be an integer type"); | 115 "T must be an integer type"); |
107 return (n > 0) ? ((n + mul - 1) / mul) * mul : (n / mul) * mul; | 116 DCHECK(VerifyRoundup(n, mul)); |
117 return RoundUpInternal(n, mul); | |
108 } | 118 } |
109 | 119 |
110 // RoundDown rounds down a given |n| to be a multiple of |mul|. | 120 // Dies with a CRASH(), if rounding up a given |n| overflows T. |
vmpstr
2015/08/04 18:17:23
nit: You should say something like "Similar to Unc
| |
121 template <typename T> | |
122 static T CheckedRoundUp(T n, T mul) { | |
123 static_assert(std::numeric_limits<T>::is_integer, | |
124 "T must be an integer type"); | |
125 CHECK(VerifyRoundup(n, mul)); | |
126 return RoundUpInternal(n, mul); | |
127 } | |
128 | |
129 // Returns true if rounded down value does not underflow, false otherwise. | |
130 template <typename T> | |
131 static bool VerifyRoundDown(T n, T mul) { | |
132 return mul | |
vmpstr
2015/08/04 18:17:23
Same here
| |
133 ? n >= (std::numeric_limits<T>::min() - | |
134 (std::numeric_limits<T>::min() % mul)) | |
135 : false; | |
136 } | |
137 | |
138 // Rounds down a given |n| to be a multiple of |mul|, but may underflow. | |
111 // Examples: | 139 // Examples: |
112 // - RoundDown(123, 50) returns 100. | 140 // - RoundDown(123, 50) returns 100. |
113 // - RoundDown(-123, 50) returns -150. | 141 // - RoundDown(-123, 50) returns -150. |
114 template <typename T> | 142 template <typename T> |
115 static T RoundDown(T n, T mul) { | 143 static T UncheckedRoundDown(T n, T mul) { |
116 static_assert(std::numeric_limits<T>::is_integer, | 144 static_assert(std::numeric_limits<T>::is_integer, |
117 "T must be an integer type"); | 145 "T must be an integer type"); |
118 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 | 146 DCHECK(VerifyRoundDown(n, mul)); |
119 : ((n - mul + 1) / mul) * mul; | 147 return RoundDownInternal(n, mul); |
148 } | |
149 | |
150 // Dies with a CRASH(), if rounding down a given |n| underflows T. | |
151 template <typename T> | |
152 static T CheckedRoundDown(T n, T mul) { | |
153 static_assert(std::numeric_limits<T>::is_integer, | |
154 "T must be an integer type"); | |
155 CHECK(VerifyRoundDown(n, mul)); | |
156 return RoundDownInternal(n, mul); | |
120 } | 157 } |
121 | 158 |
122 template <typename T> static T ClampToRange(T value, T min, T max) { | 159 template <typename T> static T ClampToRange(T value, T min, T max) { |
123 return std::min(std::max(value, min), max); | 160 return std::min(std::max(value, min), max); |
124 } | 161 } |
125 | 162 |
126 // Background: Existing transform code does not do the right thing in | 163 // Background: Existing transform code does not do the right thing in |
127 // MapRect / MapQuad / ProjectQuad when there is a perspective projection that | 164 // 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 | 165 // 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 | 166 // 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. | 300 // Returns a base::Value representation of the floating point value. |
264 // If the value is inf, returns max double/float representation. | 301 // If the value is inf, returns max double/float representation. |
265 static double AsDoubleSafely(double value); | 302 static double AsDoubleSafely(double value); |
266 static float AsFloatSafely(float value); | 303 static float AsFloatSafely(float value); |
267 | 304 |
268 // Returns vector that x axis (1,0,0) transforms to under given transform. | 305 // Returns vector that x axis (1,0,0) transforms to under given transform. |
269 static gfx::Vector3dF GetXAxis(const gfx::Transform& transform); | 306 static gfx::Vector3dF GetXAxis(const gfx::Transform& transform); |
270 | 307 |
271 // Returns vector that y axis (0,1,0) transforms to under given transform. | 308 // Returns vector that y axis (0,1,0) transforms to under given transform. |
272 static gfx::Vector3dF GetYAxis(const gfx::Transform& transform); | 309 static gfx::Vector3dF GetYAxis(const gfx::Transform& transform); |
310 | |
311 private: | |
312 template <typename T> | |
313 static T RoundUpInternal(T n, T mul) { | |
314 return (n > 0) ? ((n + mul - 1) / mul) * mul : (n / mul) * mul; | |
315 } | |
316 | |
317 template <typename T> | |
318 static T RoundDownInternal(T n, T mul) { | |
319 return (n > 0) ? (n / mul) * mul : (n == 0) ? 0 | |
320 : ((n - mul + 1) / mul) * mul; | |
321 } | |
273 }; | 322 }; |
274 | 323 |
275 } // namespace cc | 324 } // namespace cc |
276 | 325 |
277 #endif // CC_BASE_MATH_UTIL_H_ | 326 #endif // CC_BASE_MATH_UTIL_H_ |
OLD | NEW |