Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkMath.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMath.h,v $
00005   Language:  C++
00006   Date:      $Date: 2010-04-07 17:25:21 $
00007   Version:   $Revision: 1.16 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012   Portions of this code are covered under the VTK copyright.
00013   See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details.
00014 
00015      This software is distributed WITHOUT ANY WARRANTY; without even 
00016      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00017      PURPOSE.  See the above copyright notices for more information.
00018 
00019 =========================================================================*/
00020 
00021 #ifndef __itkMath_h
00022 #define __itkMath_h
00023 
00024 
00025 #include "itkConfigure.h"
00026 #include "itkIntTypes.h"
00027 #include "itkMathDetail.h"
00028 #include "itkConceptChecking.h"
00029 
00030 
00031 namespace itk 
00032 {
00033 namespace Math 
00034 {
00035 
00036 // These constants originate from VXL's vnl_math.h. They have been
00037 // moved here to improve visibility, and to ensure that the constants
00038 // are available during compile time ( as opposed to static const
00039 // member vaiables ).
00040 
00042 static const double e                = 2.7182818284590452354;
00044 static const double log2e            = 1.4426950408889634074;
00046 static const double log10e           = 0.43429448190325182765;
00048 static const double ln2              = 0.69314718055994530942;
00050 static const double ln10             = 2.30258509299404568402;
00052 static const double pi               = 3.14159265358979323846;
00054 static const double pi_over_2        = 1.57079632679489661923;
00056 static const double pi_over_4        = 0.78539816339744830962;
00058 static const double one_over_pi      = 0.31830988618379067154;
00060 static const double two_over_pi      = 0.63661977236758134308;
00062 static const double two_over_sqrtpi  = 1.12837916709551257390;
00064 static const double one_over_sqrt2pi = 0.39894228040143267794;
00066 static const double sqrt2            = 1.41421356237309504880;
00068 static const double sqrt1_2          = 0.70710678118654752440;
00069 
00070 
00071 #ifdef ITK_HAS_INT_64
00072 
00075 #define itkTemplateFloatingToIntegerMacro(name)                         \
00076   template <typename TReturn,typename TInput>                           \
00077   inline TReturn name(TInput x)                                         \
00078   {                                                                     \
00079                                                                         \
00080     if (sizeof(TReturn) <= 4)                                           \
00081       {                                                                 \
00082       return static_cast<TReturn>(Detail::name##_32(x));                \
00083       }                                                                 \
00084     else if (sizeof(TReturn) <= 8)                                      \
00085       {                                                                 \
00086       return static_cast<TReturn>(Detail::name##_64(x));                \
00087       }                                                                 \
00088     else                                                                \
00089       {                                                                 \
00090       return static_cast<TReturn>(Detail::name##_base<TReturn,TInput>(x)); \
00091       }                                                                 \
00092    }
00093 #else
00094 #define itkTemplateFloatingToIntegerMacro(name)                         \
00095   template <typename TReturn,typename TInput>                           \
00096   inline TReturn name(TInput x)                                         \
00097   {                                                                     \
00098     if (sizeof(TReturn) <= 4)                                           \
00099       {                                                                 \
00100       return static_cast<TReturn>(Detail::name##_32(x));                \
00101       }                                                                 \
00102     else                                                                \
00103       {                                                                 \
00104       return static_cast<TReturn>(Detail::name##_base<TReturn,TInput>(x)); \
00105       }                                                                 \
00106    }
00107 #endif // end ITK_HAS_INT_64
00108 
00109 
00129 itkTemplateFloatingToIntegerMacro(RoundHalfIntegerToEven);
00130 
00153 itkTemplateFloatingToIntegerMacro(RoundHalfIntegerUp);
00154 
00162 template <typename TReturn, typename TInput>
00163 inline TReturn Round(TInput x) { return RoundHalfIntegerUp<TReturn,TInput>(x); }
00164 
00177 itkTemplateFloatingToIntegerMacro(Floor);
00178 
00189 itkTemplateFloatingToIntegerMacro(Ceil);
00190 
00191 
00192 #undef  itkTemplateFloatingToIntegerMacro
00193 
00194 
00195 #if !defined(ITK_LEGACY_REMOVE) &&  !VCL_TEMPLATE_MATCHES_TOO_OFTEN
00196 // VCL_TEMPLATE_MATCHES_TOO_OFTEN is used here because some compilers
00197 // can not handle function overloading with templated and
00198 // non-templated methods, ie the templated functions matches too often
00199 
00200 
00208 inline int RoundHalfIntegerToEven(double x) { return Detail::RoundHalfIntegerToEven_32(x); }
00209 inline int RoundHalfIntegerToEven(float  x) { return Detail::RoundHalfIntegerToEven_32(x); }
00211 
00212 inline int RoundHalfIntegerUp(double x) { return Detail::RoundHalfIntegerUp_32(x); }
00213 inline int RoundHalfIntegerUp(float  x) { return Detail::RoundHalfIntegerUp_32(x); }
00214 
00215 inline int Round(double x) { return Detail::RoundHalfIntegerUp_32(x); }
00216 inline int Round(float  x) { return Detail::RoundHalfIntegerUp_32(x); }
00217 
00218 inline int Floor(double x) { return Detail::Floor_32(x); }
00219 inline int Floor(float  x) { return Detail::Floor_32(x); }
00220 
00221 inline int Ceil(double x) { return Detail::Ceil_32(x); }
00222 inline int Ceil(float  x) { return Detail::Ceil_32(x); }
00225 #endif // end of ITK_LEGACY_REMOVE
00226 
00227 template <typename TReturn,typename TInput>
00228 inline TReturn CastWithRangeCheck(TInput x)
00229 {
00230 
00231 #ifdef ITK_USE_CONCEPT_CHECKING
00232   itkConceptMacro( OnlyDefinedForIntegerTypes1, (itk::Concept::IsInteger<TReturn>) );
00233   itkConceptMacro( OnlyDefinedForIntegerTypes2, (itk::Concept::IsInteger<TInput>) );
00234 #endif // ITK_USE_CONCEPT_CHECKING
00235 
00236   TReturn ret = static_cast<TReturn>(x);
00237   if ( sizeof (TReturn) > sizeof(TInput) && 
00238        !( !itk::NumericTraits<TReturn>::is_signed &&  itk::NumericTraits<TInput>::is_signed ) )
00239     { 
00240     // if the output type is bigger and we are not converting a signed
00241     // interger to an unsigned interger then we have no problems
00242     return ret;
00243     }
00244   else if ( sizeof (TReturn) >= sizeof(TInput) )
00245     {
00246     if ( itk::NumericTraits<TInput>::IsPositive(x) != itk::NumericTraits<TReturn>::IsPositive(ret) )
00247      {
00248      itk::RangeError _e(__FILE__, __LINE__);
00249      throw _e;
00250      }
00251     }
00252   else if ( static_cast<TInput>(ret) != x ||
00253             ( itk::NumericTraits<TInput>::IsPositive(x) != itk::NumericTraits<TReturn>::IsPositive(ret) ) )
00254     {
00255     itk::RangeError _e(__FILE__, __LINE__);
00256     throw _e;
00257     }
00258   return ret;
00259 }
00260 
00261 } // end namespace Math
00262 } // end namespace itk
00263 #endif // end of itkMath.h
00264 

Generated at Fri Apr 16 18:59:37 2010 for ITK by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2000