Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

gmath.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: gmath.h,v 1.10 2006/02/08 12:25:16 jamu Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 
00023 /* this file contains some templated math functions and constants
00024  * visible in the salt namespace
00025  */
00026 #ifndef SALT_GMATH_H
00027 #define SALT_GMATH_H
00028 
00029 // #ifdef HAVE_CONFIG_H
00030 // #include <config.h>
00031 // #endif
00032 
00033 #include "defines.h"
00034 #include <cmath>
00035 
00036 namespace salt
00037 {
00038 #if 0
00039 }
00040 #endif
00041 
00042 // better directly use the cmath constants
00043 static const double gPI          = M_PI;   // was ((float)3.1415926535);
00044 static const double g2PI         = 2*M_PI; // was ((float)6.283185307);
00045 static const double gHalfPI      = M_PI_2; // was ((float)1.570796326794895);
00046 
00047 template <typename TYPE>
00048 f_inline TYPE gSqrt(const TYPE &v)
00049 {
00050     return (TYPE)sqrt(v);
00051 }
00052 
00053 template <class TYPE>
00054 f_inline TYPE gClamp(TYPE &val, TYPE min, TYPE max)
00055 {
00056     if (val<min) val=min; if (val>max) val=max; return val;
00057 }
00058 
00059 template <class TYPE>
00060 f_inline TYPE gMin(TYPE a, TYPE b)
00061 {
00062     if (a<b) return a; return b;
00063 }
00064 
00065 template <class TYPE>
00066 f_inline TYPE gMax(TYPE a, TYPE b)
00067 {
00068     if (a>b) return a; return b;
00069 }
00070 
00071 template <class TYPE>
00072 f_inline TYPE gFloor(TYPE a)
00073 {
00074     return (TYPE)floor(a);
00075 }
00076 
00077 template <class TYPE>
00078 f_inline TYPE   gCeil(TYPE a)
00079 {
00080     return ceil(a);
00081 }
00082 
00083 template <class TYPE>
00084 f_inline TYPE   gAbs(TYPE a)
00085 {
00086     return (TYPE)fabs(a);
00087 }
00088 
00089 template <class TYPE>
00090 f_inline TYPE   gNeg(TYPE a)
00091 {
00092     return -a;
00093 }
00094 
00095 template <class TYPE>
00096 f_inline TYPE gCos(TYPE a)
00097 {
00098     return (TYPE)cos(a);
00099 }
00100 
00101 template <class TYPE>
00102 f_inline TYPE   gSin(TYPE a)
00103 {
00104     return (TYPE)sin(a);
00105 }
00106 
00107 template <class TYPE>
00108 f_inline TYPE   gArcCos(TYPE a)
00109 {
00110     return acos(a);
00111 }
00112 
00113 template <class TYPE>
00114 f_inline TYPE   gArcSin(TYPE a)
00115 {
00116     return asin(a);
00117 }
00118 
00119 template <class TYPE>
00120 f_inline TYPE   gArcTan(TYPE a)
00121 {
00122     return atan(a);
00123 }
00124 
00125 template <class TYPE>
00126 f_inline TYPE   gArcTan2(TYPE a, TYPE b)
00127 {
00128     return atan2(a, b);
00129 }
00130 
00131 template <class TYPE>
00132 f_inline TYPE   gPow(TYPE a, TYPE b)
00133 {
00134     return pow(a, b);
00135 }
00136 
00137 template <class TYPE>
00138 f_inline void gSwap(TYPE &a, TYPE &b)
00139 {
00140     TYPE temp = a;
00141     a = b;
00142     b = temp;
00143 }
00144 
00145 template <class TYPE>
00146 f_inline int gSign(TYPE a)
00147 {
00148     return a > 0 ? 1 : a < 0 ? -1 : 0;
00149 }
00150 
00151 // some math conversion functions
00152 template <class TYPE>
00153 f_inline TYPE gDegToRad(TYPE deg)
00154 {
00155     return (static_cast<TYPE>(deg)) * (M_PI / 180.0);
00156 }
00157 
00158 template <class TYPE>
00159 f_inline TYPE gRadToDeg(TYPE rad)
00160 {
00161     return (static_cast<TYPE>(rad)) * (180.0 / M_PI);
00162 }
00163 
00164 template <class TYPE>
00165 f_inline bool   gInRange(const TYPE& val, const TYPE& low, const TYPE& high)
00166 {
00167     return ((val>=low) && (val<=high));
00168 }
00169 
00170 template <class TYPE>
00171 f_inline double gNormalizeDeg(TYPE angle)
00172 {
00173     while (angle > 180)
00174     {
00175         angle -= 360;
00176     }
00177 
00178     while (angle < -180)
00179     {
00180         angle += 360;
00181     }
00182 
00183     return angle;
00184 }
00185 
00186 template <class TYPE>
00187 f_inline double gNormalizeRad(TYPE angle)
00188 {
00189     while (angle > M_PI)
00190         {
00191             angle -= (2*M_PI);
00192         }
00193 
00194     while (angle < -M_PI)
00195         {
00196             angle += (2*M_PI);
00197         }
00198 
00199     return angle;
00200 }
00201 
00202 
00203 } //namespace salt
00204 
00205 #endif //SALT_GMATH_H

Generated on Thu Apr 6 15:25:38 2006 for rcssserver3d by  doxygen 1.4.4