Bromeon
Math/Trigonometry.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2012 Jan Haller
00005 // 
00006 // This software is provided 'as-is', without any express or implied
00007 // warranty. In no event will the authors be held liable for any damages
00008 // arising from the use of this software.
00009 // 
00010 // Permission is granted to anyone to use this software for any purpose,
00011 // including commercial applications, and to alter it and redistribute it
00012 // freely, subject to the following restrictions:
00013 // 
00014 // 1. The origin of this software must not be misrepresented; you must not
00015 //    claim that you wrote the original software. If you use this software
00016 //    in a product, an acknowledgment in the product documentation would be
00017 //    appreciated but is not required.
00018 // 
00019 // 2. Altered source versions must be plainly marked as such, and must not be
00020 //    misrepresented as being the original software.
00021 // 
00022 // 3. This notice may not be removed or altered from any source distribution.
00023 //
00025 
00028 
00029 #ifndef THOR_TRIGONOMETRY_HPP
00030 #define THOR_TRIGONOMETRY_HPP
00031 
00032 #include <Thor/Config.hpp>
00033 
00034 #include <cmath>
00035 
00036 
00037 namespace thor
00038 {
00039 
00042 
00063 template <typename T>
00064 struct TrigonometricTraits;
00065 
00068 template <>
00069 struct TrigonometricTraits<float>
00070 {
00071     typedef float Type;
00072 
00073     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00074     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00075     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00076     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00077     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00078     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00079     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00080     
00081     static Type Pi()                            { return 3.141592653589793238462643383f;    }
00082     static Type RadToDeg(Type rad)              { return 180 / Pi() * rad;                  }
00083     static Type DegToRad(Type deg)              { return Pi() / 180 * deg;                  }
00084 };
00085 
00088 template <>
00089 struct TrigonometricTraits<double>
00090 {
00091     typedef double Type;
00092     
00093     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00094     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00095     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00096     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00097     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00098     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00099     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00100     
00101     static Type Pi()                            { return 3.141592653589793238462643383;     }
00102     static Type RadToDeg(Type rad)              { return 180 / Pi() * rad;                  }
00103     static Type DegToRad(Type deg)              { return Pi() / 180 * deg;                  }
00104 };
00105 
00108 template <>
00109 struct TrigonometricTraits<long double>
00110 {
00111     typedef long double Type;
00112     
00113     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00114     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00115     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00116     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00117     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00118     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00119     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00120     
00121     static Type Pi()                            { return 3.141592653589793238462643383l;    }
00122     static Type RadToDeg(Type rad)              { return 180 / Pi() * rad;                  }
00123     static Type DegToRad(Type deg)              { return Pi() / 180 * deg;                  }
00124 };
00125 
00128 template <typename T>
00129 T ToDegree(T radian)
00130 {
00131     return TrigonometricTraits<T>::RadToDeg(radian);
00132 }
00133 
00136 template <typename T>
00137 T ToRadian(T degree)
00138 {
00139     return TrigonometricTraits<T>::DegToRad(degree);
00140 }
00141 
00144 extern const float THOR_API Pi;
00145 
00147 
00148 } // namespace thor
00149 
00150 #endif // THOR_TRIGONOMETRY_HPP