CrystalSpace

Public API Reference

csgeom/math.h

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 2005 by Marten Svanfeldt
00003   
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public
00015   License along with this library; if not, write to the Free
00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_MATH_H__
00020 #define __CS_MATH_H__
00021 
00022 #include "csutil/algorithms.h"
00023 
00034 template<class T>
00035 const T& csMax (const T& a, const T& b)
00036 {
00037   if (b < a) return a;
00038   return b;
00039 }
00040 
00044 template<class T>
00045 const T& csMin (const T& a, const T& b)
00046 {
00047   if (a < b) return a;
00048   return b;
00049 }
00050 
00054 template<class T>
00055 void csSort (T& a, T& b)
00056 {
00057   if (b < a)
00058     CS::Swap (a, b);
00059 }
00060 
00065 template<class T, class U>
00066 void csSort (T& a, T& b, U& x, U& y)
00067 {
00068   if (b < a)
00069   {
00070     CS::Swap (a, b);
00071     CS::Swap (x, y);
00072   }
00073 }
00074 
00075 
00079 template<class T>
00080 T csClamp (const T& a, T max, T min)
00081 {
00082   return csMin (csMax (a, min), max);
00083 }
00084 
00090 template<class T>
00091 T csSmoothStep (const T& a, T max, T min)
00092 {
00093   T tmp, tmp2;
00094   if (a <= min)
00095     tmp = 0.0f;
00096   else if (a >= max)
00097     tmp = 1.0f;
00098   else
00099   {
00100     tmp2 = (a - min) / (max-min);
00101     tmp = tmp2*tmp2 * (3.0 - 2.0*tmp2);
00102   }
00103   return tmp;
00104 }
00105 
00110 template<class T, class Tfactor>
00111 T csLerp (const T& a, const T& b, const Tfactor& f)
00112 {
00113   return (a + (b - a) * f);
00114 }
00115 
00119 template<class T>
00120 T csSquare (const T& x)
00121 {
00122   return x * x;
00123 }
00124 
00126 
00127 CS_FORCEINLINE bool csFinite (float f)
00128 {
00129 #if defined (CS_HAVE_FINITEF)
00130   return finitef (f);
00131 #elif defined (CS_HAVE_STD__ISFINITE)
00132   return std::isfinite (f);
00133 #elif defined(CS_HAVE_ISFINITE)
00134   return isfinite (f);
00135 #elif defined (CS_HAVE_FINITE)
00136   return finite (f);
00137 #elif defined (CS_HAVE__FINITE)
00138   return _finite (f) != 0;
00139 #else
00140 #error Your platform has no isfinite()-alike function!
00141 #endif
00142 }
00144 CS_FORCEINLINE bool csFinite (double d)
00145 {
00146 #if defined (CS_HAVE_STD__ISFINITE)
00147   return std::isfinite (d);
00148 #elif defined(CS_HAVE_ISFINITE)
00149   return isfinite (d);
00150 #elif defined (CS_HAVE_FINITE)
00151   return finite (d);
00152 #elif defined (CS_HAVE__FINITE)
00153   return _finite (d) != 0;
00154 #else
00155 #error Your platform has no isfinite()-alike function!
00156 #endif
00157 }
00158 
00160 CS_FORCEINLINE bool csNaN (float f)
00161 {
00162 #if defined (CS_HAVE_NANF)
00163   return isnanf (f);
00164 #elif defined (CS_HAVE_STD__ISNAN)
00165   return std::isnan (f);
00166 #elif defined(CS_HAVE_ISNAN)
00167   return isnan (f);
00168 #elif defined (CS_HAVE__ISNAN)
00169   return _isnan (f) != 0;
00170 #else
00171 #error Your platform has no isnan()-alike function!
00172 #endif
00173 }
00175 CS_FORCEINLINE bool csNaN (double d)
00176 {
00177 #if defined (CS_HAVE_STD__ISNAN)
00178   return std::isnan (d);
00179 #elif defined(CS_HAVE_ISNAN)
00180   return isnan (d);
00181 #elif defined (CS_HAVE__ISNAN)
00182   return _isnan (d) != 0;
00183 #else
00184 #error Your platform has no isnan()-alike function!
00185 #endif
00186 }
00187 
00189 CS_FORCEINLINE bool csNormal (float f)
00190 {
00191 #if defined (CS_HAVE_NORMALF)
00192   return normalf (f);
00193 #elif defined (CS_HAVE_STD__ISNORMAL)
00194   return std::isnormal (f);
00195 #elif defined(CS_HAVE_ISNORMAL)
00196   return isnormal (f);
00197 #else
00198   return csFinite(f) && !csNaN(f);
00199 #endif
00200 }
00202 CS_FORCEINLINE bool csNormal (double d)
00203 {
00204 #if defined (CS_HAVE_STD__ISNORMAL)
00205   return std::isnormal (d);
00206 #elif defined(CS_HAVE_ISNORMAL)
00207   return isnormal (d);
00208 #else
00209   return csFinite(d) && !csNaN(d);
00210 #endif
00211 }
00213 
00216 #endif //__CS_MATH_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3