Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 10, 2008, 12:05:03 AM (16 years ago)
Author:
landauf
Message:

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

Location:
code/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/util

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

    r2087 r2171  
    2828
    2929/**
    30     @file Math.h
     30    @file
    3131    @brief Declaration and implementation of several math-functions, typedefs of some Ogre::Math classes to the orxonox namespace.
    3232*/
     
    3939#include <ostream>
    4040#include <string>
     41#include <cmath>
    4142#include <boost/static_assert.hpp>
    4243
     
    4950#include <OgreQuaternion.h>
    5051#include <OgreColourValue.h>
    51 
    52 namespace orxonox
    53 {
    54   typedef Ogre::Radian Radian;
    55   typedef Ogre::Degree Degree;
    56   typedef Ogre::Vector2 Vector2;
    57   typedef Ogre::Vector3 Vector3;
    58   typedef Ogre::Vector4 Vector4;
    59   typedef Ogre::Matrix3 Matrix3;
    60   typedef Ogre::Matrix4 Matrix4;
    61   typedef Ogre::Quaternion Quaternion;
    62   typedef Ogre::ColourValue ColourValue;
    63 }
    64 
    65 _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
    66 _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
    67 _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
    68 _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
    69 
    70 _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
    71 _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
    72 _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
    73 _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
    7452
    7553//Get around Windows hackery
     
    8361#endif
    8462
    85 /**
    86     @brief Returns the sign of the given value.
    87     @param x The value
    88     @return 1 if the value is positive or zero, -1 if the value is negative
    89 */
    90 template <typename T>
    91 inline T sgn(T x)
     63namespace orxonox
    9264{
    93     return (x >= 0) ? 1 : -1;
     65    typedef Ogre::Radian Radian;
     66    typedef Ogre::Degree Degree;
     67    typedef Ogre::Vector2 Vector2;
     68    typedef Ogre::Vector3 Vector3;
     69    typedef Ogre::Vector4 Vector4;
     70    typedef Ogre::Matrix3 Matrix3;
     71    typedef Ogre::Matrix4 Matrix4;
     72    typedef Ogre::Quaternion Quaternion;
     73    typedef Ogre::ColourValue ColourValue;
     74
     75    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
     76    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
     77    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
     78    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
     79
     80    _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
     81    _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
     82    _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
     83    _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
     84
     85    /**
     86        @brief Returns the sign of the given value.
     87        @param x The value
     88        @return 1 if the value is positive or zero, -1 if the value is negative
     89    */
     90    template <typename T>
     91    inline T sgn(T x)
     92    {
     93        return (x >= 0) ? 1 : -1;
     94    }
     95
     96    /**
     97        @brief Returns the smaller of two values.
     98    */
     99    template <typename T>
     100    inline T min(T a, T b)
     101    {
     102        return (a <= b) ? a : b;
     103    }
     104
     105    /**
     106        @brief Returns the greater of two values.
     107    */
     108    template <typename T>
     109    inline T max(T a, T b)
     110    {
     111        return (a >= b) ? a : b;
     112    }
     113
     114    /**
     115        @brief Keeps a value between a lower and an upper limit.
     116        @param x The value
     117        @param min The lower limit
     118        @param max The upper limit
     119    */
     120    template <typename T>
     121    inline T clamp(T x, T min, T max)
     122    {
     123        if (x < min)
     124            return min;
     125
     126        if (x > max)
     127            return max;
     128
     129        return x;
     130    }
     131
     132    /**
     133        @brief Returns the square value (x^2).
     134    */
     135    template <typename T>
     136    inline T square(T x)
     137    {
     138        return x*x;
     139    }
     140
     141    /**
     142        @brief Returns the cube value (x^3).
     143    */
     144    template <typename T>
     145    inline T cube(T x)
     146    {
     147        return x*x*x;
     148    }
     149
     150    /**
     151        @brief Rounds the value.
     152    */
     153    template <typename T>
     154    inline int round(T x)
     155    {
     156        return (int)(x + 0.5);
     157    }
     158
     159    /**
     160        @brief The modulo operation, enhanced to work properly with negative values.
     161        @param x The value
     162        @param max The operand
     163    */
     164    template <typename T>
     165    inline int mod(T x, int max)
     166    {
     167        if (x >= 0)
     168            return (x % max);
     169        else
     170            return ((x % max) + max);
     171    }
     172
     173    template <typename T>
     174    inline T zeroise()
     175    {
     176        BOOST_STATIC_ASSERT(sizeof(T) == 0);
     177        return T();
     178    }
     179
     180    template <> inline char                 zeroise<char>()                 { return 0; }
     181    template <> inline unsigned char        zeroise<unsigned char>()        { return 0; }
     182    template <> inline short                zeroise<short>()                { return 0; }
     183    template <> inline unsigned short       zeroise<unsigned short>()       { return 0; }
     184    template <> inline int                  zeroise<int>()                  { return 0; }
     185    template <> inline unsigned int         zeroise<unsigned int>()         { return 0; }
     186    template <> inline long                 zeroise<long>()                 { return 0; }
     187    template <> inline unsigned long        zeroise<unsigned long>()        { return 0; }
     188    template <> inline long long            zeroise<long long>()            { return 0; }
     189    template <> inline unsigned long long   zeroise<unsigned long long>()   { return 0; }
     190    template <> inline float                zeroise<float>()                { return 0; }
     191    template <> inline double               zeroise<double>()               { return 0; }
     192    template <> inline long double          zeroise<long double>()          { return 0; }
     193    template <> inline bool                 zeroise<bool>()                 { return 0; }
     194    template <> inline void*                zeroise<void*>()                { return 0; }
     195    template <> inline std::string          zeroise<std::string>()          { return ""; }
     196    template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
     197    template <> inline orxonox::Degree      zeroise<orxonox::Degree>()      { return orxonox::Degree(0.0f); }
     198    template <> inline orxonox::Vector2     zeroise<orxonox::Vector2>()     { return orxonox::Vector2    (0, 0)      ; }
     199    template <> inline orxonox::Vector3     zeroise<orxonox::Vector3>()     { return orxonox::Vector3    (0, 0, 0)   ; }
     200    template <> inline orxonox::Vector4     zeroise<orxonox::Vector4>()     { return orxonox::Vector4    (0, 0, 0, 0); }
     201    template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); }
     202    template <> inline orxonox::Quaternion  zeroise<orxonox::Quaternion>()  { return orxonox::Quaternion (0, 0, 0, 0); }
     203
     204    /**
     205        @brief Interpolates between two values for a time between 0 and 1.
     206        @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1.
     207        @param start The value at time = 0
     208        @param end The value at time = 1
     209        @return The interpolation at a given time
     210    */
     211    template <typename T>
     212    T interpolate(float time, const T& start, const T& end)
     213    {
     214        return time * (end - start) + start;
     215    }
     216
     217    /**
     218        @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again.
     219        @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1.
     220        @param start The value at time = 0
     221        @param end The value at time = 1
     222        @return The smoothed interpolation at a given time
     223    */
     224    template <typename T>
     225    T interpolateSmooth(float time, const T& start, const T& end)
     226    {
     227        return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
     228    }
     229
     230    /**
     231        @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1.
     232    */
     233    inline float rnd()
     234    {
     235        return rand() / (RAND_MAX + 1.0);
     236    }
     237
     238    /**
     239        @brief Returns a random number between 0 and almost max: 0 <= rnd < max.
     240        @param max The maximum
     241    */
     242    inline float rnd(float max)
     243    {
     244        return rnd() * max;
     245    }
     246
     247    /**
     248        @brief Returns a random number between min and almost max: min <= rnd < max.
     249        @param min The minimum
     250        @param max The maximum
     251    */
     252    inline float rnd(float min, float max)
     253    {
     254        return rnd(max - min) + min;
     255    }
     256
     257    _UtilExport unsigned long getUniqueNumber();
     258
     259    class IntVector2
     260    {
     261    public:
     262      IntVector2() : x(0), y(0) { }
     263      IntVector2(int _x, int _y) : x(_x), y(_y) { }
     264      int x;
     265      int y;
     266    };
     267
     268    class IntVector3
     269    {
     270    public:
     271      IntVector3() : x(0), y(0), z(0) { }
     272      IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
     273      int x;
     274      int y;
     275      int z;
     276    };
    94277}
    95278
    96 /**
    97     @brief Returns the smaller of two values.
    98 */
    99 template <typename T>
    100 inline T min(T a, T b)
    101 {
    102     return (a <= b) ? a : b;
    103 }
    104 
    105 /**
    106     @brief Returns the greater of two values.
    107 */
    108 template <typename T>
    109 inline T max(T a, T b)
    110 {
    111     return (a >= b) ? a : b;
    112 }
    113 
    114 /**
    115     @brief Keeps a value between a lower and an upper limit.
    116     @param x The value
    117     @param min The lower limit
    118     @param max The upper limit
    119 */
    120 template <typename T>
    121 inline T clamp(T x, T min, T max)
    122 {
    123     if (x < min)
    124         return min;
    125 
    126     if (x > max)
    127         return max;
    128 
    129     return x;
    130 }
    131 
    132 /**
    133     @brief Returns the square value (x^2).
    134 */
    135 template <typename T>
    136 inline T square(T x)
    137 {
    138     return x*x;
    139 }
    140 
    141 /**
    142     @brief Returns the cube value (x^3).
    143 */
    144 template <typename T>
    145 inline T cube(T x)
    146 {
    147     return x*x*x;
    148 }
    149 
    150 /**
    151     @brief Rounds the value down.
    152 */
    153 template <typename T>
    154 inline int floor(T x)
    155 {
    156     return (int)(x);
    157 }
    158 
    159 /**
    160     @brief Rounds the value up.
    161 */
    162 template <typename T>
    163 inline int ceil(T x)
    164 {
    165     int temp = floor(x);
    166     return (temp != x) ? (temp + 1) : temp;
    167 }
    168 
    169 /**
    170     @brief Rounds the value.
    171 */
    172 template <typename T>
    173 inline int round(T x)
    174 {
    175     return (int)(x + 0.5);
    176 }
    177 
    178 /**
    179     @brief The modulo operation, enhanced to work properly with negative values.
    180     @param x The value
    181     @param max The operand
    182 */
    183 template <typename T>
    184 inline int mod(T x, int max)
    185 {
    186     if (x >= 0)
    187         return (x % max);
    188     else
    189         return ((x % max) + max);
    190 }
    191 
    192 template <typename T>
    193 inline T zeroise()
    194 {
    195     BOOST_STATIC_ASSERT(sizeof(T) == 0);
    196     return T();
    197 }
    198 
    199 template <> inline char                 zeroise<char>()                 { return 0; }
    200 template <> inline unsigned char        zeroise<unsigned char>()        { return 0; }
    201 template <> inline short                zeroise<short>()                { return 0; }
    202 template <> inline unsigned short       zeroise<unsigned short>()       { return 0; }
    203 template <> inline int                  zeroise<int>()                  { return 0; }
    204 template <> inline unsigned int         zeroise<unsigned int>()         { return 0; }
    205 template <> inline long                 zeroise<long>()                 { return 0; }
    206 template <> inline unsigned long        zeroise<unsigned long>()        { return 0; }
    207 template <> inline long long            zeroise<long long>()            { return 0; }
    208 template <> inline unsigned long long   zeroise<unsigned long long>()   { return 0; }
    209 template <> inline float                zeroise<float>()                { return 0; }
    210 template <> inline double               zeroise<double>()               { return 0; }
    211 template <> inline long double          zeroise<long double>()          { return 0; }
    212 template <> inline bool                 zeroise<bool>()                 { return 0; }
    213 template <> inline void*                zeroise<void*>()                { return 0; }
    214 template <> inline std::string          zeroise<std::string>()          { return ""; }
    215 template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
    216 template <> inline orxonox::Degree      zeroise<orxonox::Degree>()      { return orxonox::Degree(0.0f); }
    217 template <> inline orxonox::Vector2     zeroise<orxonox::Vector2>()     { return orxonox::Vector2    (0, 0)      ; }
    218 template <> inline orxonox::Vector3     zeroise<orxonox::Vector3>()     { return orxonox::Vector3    (0, 0, 0)   ; }
    219 template <> inline orxonox::Vector4     zeroise<orxonox::Vector4>()     { return orxonox::Vector4    (0, 0, 0, 0); }
    220 template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); }
    221 template <> inline orxonox::Quaternion  zeroise<orxonox::Quaternion>()  { return orxonox::Quaternion (0, 0, 0, 0); }
    222 
    223 /**
    224     @brief Interpolates between two values for a time between 0 and 1.
    225     @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1.
    226     @param start The value at time = 0
    227     @param end The value at time = 1
    228     @return The interpolation at a given time
    229 */
    230 template <typename T>
    231 T interpolate(float time, const T& start, const T& end)
    232 {
    233     return time * (end - start) + start;
    234 }
    235 
    236 /**
    237     @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again.
    238     @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1.
    239     @param start The value at time = 0
    240     @param end The value at time = 1
    241     @return The smoothed interpolation at a given time
    242 */
    243 template <typename T>
    244 T interpolateSmooth(float time, const T& start, const T& end)
    245 {
    246     return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
    247 }
    248 
    249 /**
    250     @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1.
    251 */
    252 inline _UtilExport float rnd()
    253 {
    254     return rand() / (RAND_MAX + 1.0);
    255 }
    256 
    257 /**
    258     @brief Returns a random number between 0 and almost max: 0 <= rnd < max.
    259     @param max The maximum
    260 */
    261 inline _UtilExport float rnd(float max)
    262 {
    263     return rnd() * max;
    264 }
    265 
    266 /**
    267     @brief Returns a random number between min and almost max: min <= rnd < max.
    268     @param min The minimum
    269     @param max The maximum
    270 */
    271 inline _UtilExport float rnd(float min, float max)
    272 {
    273     return rnd(max - min) + min;
    274 }
    275 
    276 _UtilExport unsigned long getUniqueNumber();
    277 
    278 class _UtilExport IntVector2
    279 {
    280 public:
    281   IntVector2() : x(0), y(0) { }
    282   IntVector2(int _x, int _y) : x(_x), y(_y) { }
    283   int x;
    284   int y;
    285 };
    286 
    287 class _UtilExport IntVector3
    288 {
    289 public:
    290   IntVector3() : x(0), y(0), z(0) { }
    291   IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
    292   int x;
    293   int y;
    294   int z;
    295 };
    296 
    297279#endif /* _Util_Math_H__ */
Note: See TracChangeset for help on using the changeset viewer.