## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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
    00109 template<class T>
    00110 T csSquare (const T& x)
    00111 {
    00112   return x * x;
    00113 }
    00114
    00116
    00119 CS_FORCEINLINE bool csFinite (float f)
    00120 {
    00121 #if defined (CS_HAVE_FINITEF)
    00122   return finitef (f);
    00123 #elif defined (CS_HAVE_STD__ISFINITE)
    00124   return std::isfinite (f);
    00125 #elif defined(CS_HAVE_ISFINITE)
    00126   return isfinite (f);
    00127 #elif defined (CS_HAVE_FINITE)
    00128   return finite (f);
    00129 #elif defined (CS_HAVE__FINITE)
    00130   return _finite (f) != 0;
    00131 #else
    00132 #error Your platform has no isfinite()-alike function!
    00133 #endif
    00134 }
    00135 CS_FORCEINLINE bool csFinite (double d)
    00136 {
    00137 #if defined (CS_HAVE_STD__ISFINITE)
    00138   return std::isfinite (d);
    00139 #elif defined(CS_HAVE_ISFINITE)
    00140   return isfinite (d);
    00141 #elif defined (CS_HAVE_FINITE)
    00142   return finite (d);
    00143 #elif defined (CS_HAVE__FINITE)
    00144   return _finite (d) != 0;
    00145 #else
    00146 #error Your platform has no isfinite()-alike function!
    00147 #endif
    00148 }
    00150
    00153 #endif //__CS_MATH_H__