Changeset 11071 for code/trunk/src/libraries/util/Math.h
- Timestamp:
- Jan 17, 2016, 10:29:21 PM (9 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/util/Math.h
r11052 r11071 46 46 #include <cmath> 47 47 #include <cstdlib> 48 #include <random> 49 #include <type_traits> 48 50 49 51 #include <OgreMath.h> … … 73 75 namespace math 74 76 { 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) / 277 constexpr float twoPi = 6.283185482025146484375f; ///< PI * 2 78 constexpr float pi = 3.1415927410125732421875f; ///< PI 79 constexpr float pi_2 = 1.57079637050628662109375f; ///< PI / 2 80 constexpr float pi_4 = 0.785398185253143310546875f; ///< PI / 4 81 constexpr float e = 2.718281269073486328125f; ///< e 82 constexpr float sqrt2 = 1.41421353816986083984375f; ///< sqrt(2) 83 constexpr float sqrt2_2 = 0.707106769084930419921875f; ///< sqrt(2) / 2 82 84 } 83 85 … … 104 106 */ 105 107 template <typename T> 106 inline T sgn(T x)108 constexpr inline T sgn(T x) 107 109 { 108 110 return (x >= 0) ? (T)1 : (T)-1; … … 116 118 */ 117 119 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; 120 constexpr inline T clamp(T x, T min, T max) 121 { 122 return x < min ? min : (x > max ? max : x); 127 123 } 128 124 … … 131 127 */ 132 128 template <typename T> 133 inline T square(T x)129 constexpr inline T square(T x) 134 130 { 135 131 return x*x; … … 140 136 */ 141 137 template <typename T> 142 inline T cube(T x)138 constexpr inline T cube(T x) 143 139 { 144 140 return x*x*x; … … 183 179 @c Vector3 you get <tt>Vector3(0, 0, 0)</tt>. 184 180 */ 185 template <typename T> 186 inline T zeroise() 187 { 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; 181 template <typename T> /* for normal classes */ typename std::enable_if<!std::is_enum<T>::value, T>::type 182 inline /*T*/ zeroise() 183 { 184 // If you reach this code, you abused zeroise()! 185 static_assert(sizeof(T) != sizeof(T), "No template specialization available for T"); 186 } 187 /// Implementation for enum classes: uses the underlying type to create a zero value. 188 template <typename T> /* for enum classes */ typename std::enable_if<std::is_enum<T>::value, T>::type 189 inline /*T*/ zeroise() 190 { 191 return static_cast<T>(zeroise<typename std::underlying_type<T>::type>()); 192 192 } 193 193 … … 206 206 template <> inline long double zeroise<long double>() { return 0; } 207 207 template <> inline bool zeroise<bool>() { return 0; } 208 template <> inline void* zeroise<void*>() { return 0; }208 template <> inline void* zeroise<void*>() { return nullptr; } 209 209 template <> inline std::string zeroise<std::string>() { return std::string(); } 210 210 template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } … … 258 258 } 259 259 260 namespace detail 261 { 262 /** 263 Random number generator used for the functions below. Marked extern to only have one global instance. 264 */ 265 _UtilExport extern std::mt19937 rngen; 266 } 267 268 /** 269 @brief Seeds the random number generator used for the functions below. 270 */ 271 inline void rndseed(unsigned int seed) 272 { 273 detail::rngen.seed(seed); 274 } 275 276 /** 277 @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. 278 @param min The minimum 279 @param max The maximum 280 */ 281 inline float rnd(float min, float max) 282 { 283 std::uniform_real_distribution<float> dist(min, max); 284 return dist(detail::rngen); 285 } 286 260 287 /** 261 288 @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>. … … 263 290 inline float rnd() 264 291 { 265 return r and() / (RAND_MAX + 1.0f);292 return rnd(0, 1); 266 293 } 267 294 … … 272 299 inline float rnd(float max) 273 300 { 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; 301 return rnd(0, max); 285 302 } 286 303 … … 290 307 inline float rndsgn() 291 308 { 292 return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 309 std::uniform_int_distribution<> dist; 310 return static_cast<float>((dist(detail::rngen) & 0x2) - 1); // dist(...) & 0x2 is either 2 or 0 293 311 } 294 312
Note: See TracChangeset
for help on using the changeset viewer.