TCMathUtil.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCMathUtil.h 848 2008-05-30 08:59:15Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TC_MATH_UTIL_H_
00037 #define _TC_MATH_UTIL_H_
00038 
00039 #include "TCTypes.h"
00040 #include "TCMathApi.h"
00041 
00042 #include <cmath>
00043 #include <limits>
00044 
00045 #undef PI
00046 
00047 namespace TC
00048 {
00057 namespace Math
00058 {
00071    const double PI = 3.1415926535897932385E0;
00073    const double MEGA_BYTES = 1024. * 1024.;
00074 
00076    template <class T>
00077    inline T Abs(const T& x)
00078    {
00079       return (x < 0) ? -x : x;
00080    }
00081 
00083    template<class T>
00084    inline T Pow(T x, uint32 y)
00085    {
00086       if (y == 0)
00087       {
00088          return 1;
00089       }
00090 
00091       T ret = x;
00092       while (--y)
00093       {
00094          ret*=x;
00095       }
00096       return ret;
00097    }
00098 
00104    inline uint32 Align32(uint32 size)
00105    {
00106       return ((size + 31) & (~31));
00107    }
00108 
00110    inline bool Compare(double val1, double val2)
00111    {
00112       return Abs(val1 - val2) < std::numeric_limits<double>::epsilon();
00113    }
00114 
00120    inline uint32 Align16(uint32 size)
00121    {
00122       return ((size + 15) & (~15));
00123    }
00124 
00130    template <class T>
00131    inline double Deg2Rad(T val)
00132    {
00133       return static_cast<double>(val) * PI / 180.0;
00134    }
00135 
00141    template <class T>
00142    inline double Rad2Deg(T val)
00143    {
00144       return static_cast<double>(val) * 180.0 / PI;
00145    }
00146 
00155    template <class T>
00156    inline T Normalize(T minValue, T maxValue, T value)
00157    {
00158       // check if min and max was exchanged
00159       T startSize   = (T)0.;
00160       T multiplier  = (T)1.;
00161       if (maxValue < minValue)
00162       {
00163          Swap(minValue, maxValue);
00164          startSize  = (T)1.;
00165          multiplier = (T)-1.;
00166       }
00167 
00168       value = Max(minValue, value);
00169       value = Min(maxValue, value);
00170 
00171       if (maxValue - minValue)
00172          return (T)(startSize + multiplier * ((value -  minValue) / (maxValue - minValue)));
00173       else
00174          return (T)1.;
00175    }
00176 
00182    template <class T>
00183    inline double SinXOverX(T x)
00184    {
00185       if (1.0 + x*x > 1.0)
00186          return std::sin(x)/x;
00187       else
00188          return 1;
00189    }
00190 
00201    template <class T>
00202    inline T RoundToFirstSignificantValue(T val)
00203    {
00204       // on 0 we do nothing
00205       if (val == (T)0) return (T)0;
00206 
00207       double logRange   = std::log10(Abs(val));
00208       sint32 logRangeFloor = (sint32)std::floor(logRange);
00209       double normalized = (sint32)std::pow(10.0, logRange - logRangeFloor);
00210 
00211       sint32 i;
00212       if (logRangeFloor > 0)
00213          for (i=0; i<logRangeFloor; i++) normalized *= 10.;
00214       else {
00215          sint32 absLogRangeFloor = Abs(logRangeFloor);
00216          for (i=0; i<absLogRangeFloor; i++) normalized *= 0.1;
00217       }
00218 
00219       if (val < (T)0) normalized = -normalized;
00220 
00221       return (T)normalized;
00222    }
00223 
00225    template <class T>
00226    bool Compare(const T& val1, const T& val2)
00227    {
00228       return Abs(val1 - val2) > std::numeric_limits<T>::epsilon();
00229    }
00230 
00235    TCMATH_API bool IsNaN(double val);
00236 
00241    TCMATH_API bool IsFinite(double val);
00242 
00247    TCMATH_API bool IsInf(double val);
00248 
00253 } // namespace Math
00254 } // namespace TC
00255 
00256 #endif // _TC_MATH_UTIL_H_

Copyright (c) Thomas Goessler 2003 - 2008