Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 17, 2016, 10:29:21 PM (8 years ago)
Author:
landauf
Message:

merged branch cpp11_v3 back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/util/Math.h

    r11052 r11071  
    4646#include <cmath>
    4747#include <cstdlib>
     48#include <random>
     49#include <type_traits>
    4850
    4951#include <OgreMath.h>
     
    7375    namespace math
    7476    {
    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
     77        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
    8284    }
    8385
     
    104106    */
    105107    template <typename T>
    106     inline T sgn(T x)
     108    constexpr inline T sgn(T x)
    107109    {
    108110        return (x >= 0) ? (T)1 : (T)-1;
     
    116118    */
    117119    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);
    127123    }
    128124
     
    131127    */
    132128    template <typename T>
    133     inline T square(T x)
     129    constexpr inline T square(T x)
    134130    {
    135131        return x*x;
     
    140136    */
    141137    template <typename T>
    142     inline T cube(T x)
     138    constexpr inline T cube(T x)
    143139    {
    144140        return x*x*x;
     
    183179        @c Vector3 you get <tt>Vector3(0, 0, 0)</tt>.
    184180    */
    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>());
    192192    }
    193193
     
    206206    template <> inline long double          zeroise<long double>()          { return 0; }
    207207    template <> inline bool                 zeroise<bool>()                 { return 0; }
    208     template <> inline void*                zeroise<void*>()                { return 0; }
     208    template <> inline void*                zeroise<void*>()                { return nullptr; }
    209209    template <> inline std::string          zeroise<std::string>()          { return std::string(); }
    210210    template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
     
    258258    }
    259259
     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
    260287    /**
    261288        @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
     
    263290    inline float rnd()
    264291    {
    265         return rand() / (RAND_MAX + 1.0f);
     292        return rnd(0, 1);
    266293    }
    267294
     
    272299    inline float rnd(float max)
    273300    {
    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);
    285302    }
    286303
     
    290307    inline float rndsgn()
    291308    {
    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
    293311    }
    294312
Note: See TracChangeset for help on using the changeset viewer.