PF_API 0.52

Code/Libs/Math_LIB/MFun.h

Go to the documentation of this file.
00001 
00002 //    Copyright (C) 2004-2011 Dylan Blair
00003 //
00004 //    email: dblair@alumni.cs.utexas.edu
00005 //
00006 //    This library is free software; you can redistribute it and/or
00007 //    modify it under the terms of the GNU Lesser General Public
00008 //    License as published by the Free Software Foundation; either
00009 //    version 2.1 of the License, or (at your option) any later version.
00010 //
00011 //    This library is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 //    Lesser General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU Lesser General Public
00017 //    License along with this library; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #ifndef MFUN_H
00022 #define MFUN_H
00023 
00024 #include <math.h>
00025 #include <assert.h>
00026 #include <time.h>
00027 #ifndef _WIN32
00028 #include <unistd.h>
00029 extern long CLK_TCK;
00030 #else
00031 #ifndef NOMINMAX
00032 #define NOMINMAX
00033 #endif
00034 #endif
00035 
00036 #include "../Utils_LIB/UTypes.h"
00037 
00041 namespace OpenSkyNet {
00044     namespace Math {
00045         const float PI(3.14159265f);
00046         const float PSEUDO_INFINITY(1000000.0f);
00047         const float CLOSE_TO_ZERO(0.00001f);
00048 
00050         inline float getRadFromDeg(float deg_) { return deg_*PI/180.0f; }
00051 
00053         inline float rand01() { return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); }
00054 
00056         inline float randNeg1Pos1() { return ((static_cast<float>(rand()) / (static_cast<float>(RAND_MAX)/2.0f)) - 1.0f); }
00057 
00059         inline int roundToInt(double num_) { return static_cast<int>(num_+(num_>=0.0?0.5:-0.5)); }
00060 
00062         inline Utils::uint roundToUInt(float num_) { return static_cast<Utils::uint>(num_+0.5f); }
00063 
00065         inline Utils::uint timeStamp() { return static_cast<Utils::uint>(clock()/CLK_TCK); }
00066 
00072         template<class T>
00073         inline T clamp(T& num_, const T& min_=0, const T& max_=1) {
00074             if (num_ > max_) {
00075                 T diff = num_ - max_;
00076                 num_ = max_;
00077                 return diff;
00078             }
00079             if (num_ < min_) {
00080                 T diff = num_ - min_;
00081                 num_ = min_;
00082                 return diff;
00083             }
00084             return 0;
00085         }
00086     }
00087 }
00088 
00089 #endif //MFUN_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines