Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 10, 2016, 1:54:11 PM (8 years ago)
Author:
landauf
Message:

merged branch cpp11_v2 into cpp11_v3

Location:
code/branches/cpp11_v3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v3

  • code/branches/cpp11_v3/src/libraries/util/Math.h

    r11052 r11054  
    4646#include <cmath>
    4747#include <cstdlib>
     48#include <random>
    4849
    4950#include <OgreMath.h>
     
    7374    namespace math
    7475    {
    75         const float twoPi   = 6.283185482025146484375f;     ///< PI * 2
    76         const float pi      = 3.1415927410125732421875f;    ///< PI
    77         const float pi_2    = 1.57079637050628662109375f;   ///< PI / 2
    78         const float pi_4    = 0.785398185253143310546875f;  ///< PI / 4
    79         const float e       = 2.718281269073486328125f;     ///< e
    80         const float sqrt2   = 1.41421353816986083984375f;   ///< sqrt(2)
    81         const float sqrt2_2 = 0.707106769084930419921875f;  ///< sqrt(2) / 2
     76        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
    8283    }
    8384
     
    104105    */
    105106    template <typename T>
    106     inline T sgn(T x)
     107    constexpr inline T sgn(T x)
    107108    {
    108109        return (x >= 0) ? (T)1 : (T)-1;
     
    116117    */
    117118    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);
    127122    }
    128123
     
    131126    */
    132127    template <typename T>
    133     inline T square(T x)
     128    constexpr inline T square(T x)
    134129    {
    135130        return x*x;
     
    140135    */
    141136    template <typename T>
    142     inline T cube(T x)
     137    constexpr inline T cube(T x)
    143138    {
    144139        return x*x*x;
     
    186181    inline T zeroise()
    187182    {
    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");
    192185    }
    193186
     
    206199    template <> inline long double          zeroise<long double>()          { return 0; }
    207200    template <> inline bool                 zeroise<bool>()                 { return 0; }
    208     template <> inline void*                zeroise<void*>()                { return 0; }
     201    template <> inline void*                zeroise<void*>()                { return nullptr; }
    209202    template <> inline std::string          zeroise<std::string>()          { return std::string(); }
    210203    template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
     
    258251    }
    259252
     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
    260280    /**
    261281        @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
     
    263283    inline float rnd()
    264284    {
    265         return rand() / (RAND_MAX + 1.0f);
     285        return rnd(0, 1);
    266286    }
    267287
     
    272292    inline float rnd(float max)
    273293    {
    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);
    285295    }
    286296
     
    290300    inline float rndsgn()
    291301    {
    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
    293304    }
    294305
Note: See TracChangeset for help on using the changeset viewer.