Math/Trigonometry.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011-2015 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 
00064 template <typename T>
00065 struct TrigonometricTraits
00066 {
00067 };
00068 
00069 // Trigonometric traits: Specialization for float
00070 template <>
00071 struct TrigonometricTraits<float>
00072 {
00073     typedef float Type;
00074 
00075     static Type sin(Type deg)                   { return std::sin(degToRad(deg));           }
00076     static Type cos(Type deg)                   { return std::cos(degToRad(deg));           }
00077     static Type tan(Type deg)                   { return std::tan(degToRad(deg));           }
00078     static Type arcSin(Type value)              { return radToDeg(std::asin(value));        }
00079     static Type arcCos(Type value)              { return radToDeg(std::acos(value));        }
00080     static Type arcTan2(Type valY, Type valX)   { return radToDeg(std::atan2(valY, valX));  }
00081     static Type sqrt(Type value)                { return std::sqrt(value);                  }
00082 
00083     static Type pi()                            { return 3.141592653589793238462643383f;    }
00084     static Type radToDeg(Type rad)              { return 180 / pi() * rad;                  }
00085     static Type degToRad(Type deg)              { return pi() / 180 * deg;                  }
00086 };
00087 
00088 // Trigonometric traits: Specialization for double
00089 template <>
00090 struct TrigonometricTraits<double>
00091 {
00092     typedef double Type;
00093 
00094     static Type sin(Type deg)                   { return std::sin(degToRad(deg));           }
00095     static Type cos(Type deg)                   { return std::cos(degToRad(deg));           }
00096     static Type tan(Type deg)                   { return std::tan(degToRad(deg));           }
00097     static Type arcSin(Type value)              { return radToDeg(std::asin(value));        }
00098     static Type arcCos(Type value)              { return radToDeg(std::acos(value));        }
00099     static Type arcTan2(Type valY, Type valX)   { return radToDeg(std::atan2(valY, valX));  }
00100     static Type sqrt(Type value)                { return std::sqrt(value);                  }
00101 
00102     static Type pi()                            { return 3.141592653589793238462643383;     }
00103     static Type radToDeg(Type rad)              { return 180 / pi() * rad;                  }
00104     static Type degToRad(Type deg)              { return pi() / 180 * deg;                  }
00105 };
00106 
00107 // Trigonometric traits: Specialization for long double
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 
00126 // ---------------------------------------------------------------------------------------------------------------------------
00127 
00128 
00131 template <typename T>
00132 T toDegree(T radian)
00133 {
00134     return TrigonometricTraits<T>::radToDeg(radian);
00135 }
00136 
00139 template <typename T>
00140 T toRadian(T degree)
00141 {
00142     return TrigonometricTraits<T>::degToRad(degree);
00143 }
00144 
00147 extern const float THOR_API Pi;
00148 
00150 
00151 } // namespace thor
00152 
00153 #endif // THOR_TRIGONOMETRY_HPP