• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

/home/hauberg/Dokumenter/Capture/humim-tracker-0.1/src/OpenTissue/OpenTissue/core/math/math_functions.h

Go to the documentation of this file.
00001 #ifndef OPENTISSUE_CORE_MATH_MATH_FUNCTIONS_H
00002 #define OPENTISSUE_CORE_MATH_MATH_FUNCTIONS_H
00003 //
00004 // OpenTissue Template Library
00005 // - A generic toolbox for physics-based modeling and simulation.
00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen.
00007 //
00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php
00009 //
00010 #include <OpenTissue/configuration.h>
00011 
00012 #include <OpenTissue/core/math/math_constants.h>
00013 
00014 #include <boost/numeric/conversion/bounds.hpp>
00015 #include <boost/cast.hpp>
00016 #include <cmath>
00017 
00018 
00019 namespace OpenTissue
00020 {
00021 
00029   namespace math
00030   {
00031 
00040     template<typename T>
00041     inline T clamp(T const & value, T const & min_value, T const & max_value)
00042     {
00043       assert(min_value <= max_value || !"max_value cannot be less than min_value");
00044       using std::min;
00045       using std::max;
00046 
00047       return T(min(max_value, max(min_value, value)));
00048     }
00049 
00050 
00058     template<typename T>
00059     inline T clamp_min(T const & value, T const & min_value)
00060     {
00061         using std::max;
00062       return clamp(value, min_value, max(value, min_value));
00063     }
00064 
00065 
00073     template<typename T>
00074     inline T clamp_max(T const & value, T const & max_value)
00075     {
00076         using std::min;
00077       return clamp(value, min(value, max_value), max_value);
00078     }
00079 
00080 
00088     template<typename T>
00089     inline T clamp_zero_one(T const & value)
00090     {
00091       return clamp(value, detail::zero<T>(), detail::one<T>());
00092     }
00093 
00094 
00095     template<typename T>
00096     inline T fac(unsigned long n)
00097     {
00098       // TODO what about implicit type conversions? This could have been done more elegangtly using partial specialization  
00099       unsigned long val = 1;
00100       for(; n > 0; val *= n--);
00101       return T(val);
00102     }
00103 
00104 
00105     template<typename T>
00106     inline T sgn(T const & val)
00107     {
00108       static T const zero = detail::zero<T>();
00109       static T const one  = detail::one<T>();
00110       return val > zero ? one : val < zero ? -one : zero;
00111     }
00112 
00113 
00125     template<typename T>
00126     inline T sinc(T & x)
00127     {
00128       using std::fabs;
00129       using std::sin;
00130 
00131       static T const tiny   = boost::numeric_cast<T>(1.0e-4);
00132       static T const factor = boost::numeric_cast<T>(0.166666666666666666667);
00133 
00134       //--- if |x| < 1e-4 then use a taylor series expansion. this two term expansion
00135       //--- is actually accurate to one LS bit within this range if double precision
00136       //--- is being used - so don't worry!
00137       return (fabs(x) < tiny) ? (detail::one<T>() - x*x*factor) : (sin(x)/x);
00138     }
00139 
00140 
00141     // use this to convert radians into degrees
00142     template<typename T>
00143     inline T to_degrees(T const & radians)
00144     {
00145       return radians*detail::radian<T>();
00146     }
00147 
00148 
00149     // use this to convert degrees into radians
00150     template<typename T>
00151     inline T to_radians(T const & degrees)
00152     {
00153       return degrees*detail::degree<T>();
00154     }
00155 
00156   }  // namespace math
00157   
00158 }  // namespace OpenTissue
00159 
00160 // #define OPENTISSUE_CORE_MATH_MATH_FUNCTIONS_H
00161 #endif

Generated on Thu Dec 1 2011 12:52:07 for HUMIM Tracker by  doxygen 1.7.1