Changeset 11054 for code/branches/cpp11_v3/src/libraries/util/Math.h
- Timestamp:
- Jan 10, 2016, 1:54:11 PM (9 years ago)
- Location:
- code/branches/cpp11_v3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v3
- Property svn:mergeinfo changed
-
code/branches/cpp11_v3/src/libraries/util/Math.h
r11052 r11054 46 46 #include <cmath> 47 47 #include <cstdlib> 48 #include <random> 48 49 49 50 #include <OgreMath.h> … … 73 74 namespace math 74 75 { 75 const float twoPi = 6.283185482025146484375f; ///< PI * 276 const float pi = 3.1415927410125732421875f; ///< PI77 const float pi_2 = 1.57079637050628662109375f; ///< PI / 278 const float pi_4 = 0.785398185253143310546875f; ///< PI / 479 const float e = 2.718281269073486328125f; ///< e80 const float sqrt2 = 1.41421353816986083984375f; ///< sqrt(2)81 const float sqrt2_2 = 0.707106769084930419921875f; ///< sqrt(2) / 276 constexpr float twoPi = 6.283185482025146484375f; ///< PI * 2 77 constexpr float pi = 3.1415927410125732421875f; ///< PI 78 constexpr float pi_2 = 1.57079637050628662109375f; ///< PI / 2 79 constexpr float pi_4 = 0.785398185253143310546875f; ///< PI / 4 80 constexpr float e = 2.718281269073486328125f; ///< e 81 constexpr float sqrt2 = 1.41421353816986083984375f; ///< sqrt(2) 82 constexpr float sqrt2_2 = 0.707106769084930419921875f; ///< sqrt(2) / 2 82 83 } 83 84 … … 104 105 */ 105 106 template <typename T> 106 inline T sgn(T x)107 constexpr inline T sgn(T x) 107 108 { 108 109 return (x >= 0) ? (T)1 : (T)-1; … … 116 117 */ 117 118 template <typename T> 118 inline T clamp(T x, T min, T max) 119 { 120 if (x < min) 121 return min; 122 123 if (x > max) 124 return max; 125 126 return x; 119 constexpr inline T clamp(T x, T min, T max) 120 { 121 return x < min ? min : (x > max ? max : x); 127 122 } 128 123 … … 131 126 */ 132 127 template <typename T> 133 inline T square(T x)128 constexpr inline T square(T x) 134 129 { 135 130 return x*x; … … 140 135 */ 141 136 template <typename T> 142 inline T cube(T x)137 constexpr inline T cube(T x) 143 138 { 144 139 return x*x*x; … … 186 181 inline T zeroise() 187 182 { 188 // Default, raise a compiler error without including large boost header cascade. 189 T temp(); 190 *********temp; // If you reach this code, you abused zeroise()! 191 return temp; 183 // If you reach this code, you abused zeroise()! 184 static_assert(sizeof(T) != sizeof(T), "No template specialization available for T"); 192 185 } 193 186 … … 206 199 template <> inline long double zeroise<long double>() { return 0; } 207 200 template <> inline bool zeroise<bool>() { return 0; } 208 template <> inline void* zeroise<void*>() { return 0; }201 template <> inline void* zeroise<void*>() { return nullptr; } 209 202 template <> inline std::string zeroise<std::string>() { return std::string(); } 210 203 template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } … … 258 251 } 259 252 253 namespace detail 254 { 255 /** 256 Random number generator used for the functions below. Marked extern to only have one global instance. 257 */ 258 _UtilExport extern std::mt19937 rngen; 259 } 260 261 /** 262 @brief Seeds the random number generator used for the functions below. 263 */ 264 inline void rndseed(unsigned int seed) 265 { 266 detail::rngen.seed(seed); 267 } 268 269 /** 270 @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. 271 @param min The minimum 272 @param max The maximum 273 */ 274 inline float rnd(float min, float max) 275 { 276 std::uniform_real_distribution<float> dist(min, max); 277 return dist(detail::rngen); 278 } 279 260 280 /** 261 281 @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>. … … 263 283 inline float rnd() 264 284 { 265 return r and() / (RAND_MAX + 1.0f);285 return rnd(0, 1); 266 286 } 267 287 … … 272 292 inline float rnd(float max) 273 293 { 274 return rnd() * max; 275 } 276 277 /** 278 @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. 279 @param min The minimum 280 @param max The maximum 281 */ 282 inline float rnd(float min, float max) 283 { 284 return rnd(max - min) + min; 294 return rnd(0, max); 285 295 } 286 296 … … 290 300 inline float rndsgn() 291 301 { 292 return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 302 std::uniform_int_distribution<> dist; 303 return static_cast<float>((dist(detail::rngen) & 0x2) - 1); // dist(...) & 0x2 is either 2 or 0 293 304 } 294 305
Note: See TracChangeset
for help on using the changeset viewer.