fs/mth/fsFunc.h

Go to the documentation of this file.
00001 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00002 /*                          _______      ______    ______          __      */
00003 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~    / ____(_)___ / ___ /)  / ____/___  ____/ /__    */
00004 /*      [fsFunc]          / /_  / / __ \\__ \|/  / /   / __ \/ __  / _ \   */
00005 /*       rev. 2          / __/ / / /_/ /__/ /   / /___/ /_/ / /_/ /  __/   */
00006 /*    20th Jan 2007     /_/   /_/ ,___/____/    \____/\____/\__,_/\___/    */
00007 /*     [x] stable              /_/ (c) 2006-7 Filip STOKLAS (FipS)         */
00008 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~       http://HOLE.4FipS.com/fips_code.php          */
00009 /*                                                                         */
00010 /* This code is free for personal and commercial use. You may redistribute */
00011 /* it by any means. If you use the code for your own projects please give  */
00012 /* me credit. Please send a bug report. Don't alter or remove this header! */
00013 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00014 #ifndef FS_MTH_FSFUNC_H
00015 #define FS_MTH_FSFUNC_H
00016 //---------------------------------------------------------------------------
00017 #if defined(FS_INCLUDE_USERDEFS) // inject user definition file on request
00018 #   include <fs/fsUserDefs.h>
00019 #endif
00020 
00021 #include "fsConst.h"
00022 
00023 #include <cmath>
00024 //---------------------------------------------------------------------------
00025 namespace fs { namespace mth {
00026 //---------------------------------------------------------------------------
00027 template<typename T>
00028 inline T Min(T tVal1, T tVal2)
00029 {
00030     return tVal1 < tVal2 ? tVal1 : tVal2;
00031 }
00032 //---------------------------------------------------------------------------
00033 template<typename T>
00034 inline T Max(T tVal1, T tVal2)
00035 {
00036     return tVal1 > tVal2 ? tVal1 : tVal2;
00037 }
00038 //---------------------------------------------------------------------------
00039 template<typename T>
00040 inline T Clamp(T tVal, T tMinVal, T tMaxVal)
00041 {
00042     return tVal < tMinVal ? tMinVal : tVal > tMaxVal ? tMaxVal : tVal;
00043 }
00044 //---------------------------------------------------------------------------
00045 template<typename T>
00046 inline T Abs(T tVal)
00047 {
00048     return tVal >= static_cast<T>(0) ? tVal : -tVal;
00049 }
00050 //---------------------------------------------------------------------------
00051 template <typename T>
00052 inline T Sgn(T tVal)
00053 {
00054     return tVal > static_cast<T>(0) ? static_cast<T>(1) :
00055      (tVal < static_cast<T>(0) ? static_cast<T>(-1) : static_cast<T>(0));
00056 }
00057 //---------------------------------------------------------------------------
00058 template <typename T>
00059 inline T Floor(T tVal)
00060 {
00061     return static_cast<T>(::floor(tVal));
00062 }
00063 //---------------------------------------------------------------------------
00064 inline float Sin(float fAngle)
00065 {
00066     return ::sin(fAngle);
00067 }
00068 //---------------------------------------------------------------------------
00069 inline float Cos(float fAngle)
00070 {
00071     return ::cos(fAngle);
00072 }
00073 //---------------------------------------------------------------------------
00074 inline void SinCos(float tAngle, float &tSin, float &tCos)
00075 {
00076     tSin = ::sin(tAngle);
00077     tCos = ::cos(tAngle);
00078 }
00079 //---------------------------------------------------------------------------
00080 template<typename T>
00081 inline T Sqrt(T tVal)
00082 {
00083     return static_cast<T>(::sqrt(tVal));
00084 }
00085 //---------------------------------------------------------------------------
00086 template<typename T>
00087 inline T SqrtInv(T tVal)
00088 {
00089     return static_cast<T>(1) / Sqrt(tVal);
00090 }
00091 //---------------------------------------------------------------------------
00092 template<typename T>
00093 inline T Norm(T tVal, T tMin, T tMax)
00094 {
00095     T tRange = tMax - tMin;
00096     return tVal - tRange * Floor((tVal - tMin) / tRange);
00097 }
00098 //---------------------------------------------------------------------------
00099 inline int Norm0_360(int nVal)
00100 {
00101     return (nVal % 360 + 360) % 360;
00102 }
00103 //---------------------------------------------------------------------------
00104 inline float Norm0_360(float fVal)
00105 {
00106     return fVal - 360.f * ::floor(fVal / 360.f);
00107 }
00108 //---------------------------------------------------------------------------
00109 }} // namespace fs::mth
00110 //---------------------------------------------------------------------------
00111 #endif // FS_MTH_FSFIXED_H

FipS' Code (Thu Feb 15 22:43:35 2007) - (c) Filip STOKLAS (FipS) - [ www ] [ Guest Book ]