Bromeon
Math/Trigonometry.hpp
Go to the documentation of this file.
00001 
00002 //
00003 // Thor C++ Library
00004 // Copyright (c) 2011 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 <cmath>
00033 
00034 
00035 namespace thor
00036 {
00037 
00040 
00061 template <typename T>
00062 struct TrigonometricTraits;
00063 
00066 template <>
00067 struct TrigonometricTraits<float>
00068 {
00069     typedef float Type;
00070 
00071     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00072     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00073     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00074     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00075     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00076     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00077     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00078     
00079     static Type Pi()                            { return 3.14159265f;                       }
00080     static Type RadToDeg(Type rad)              { return 180 / 3.14159265f * rad;           }
00081     static Type DegToRad(Type deg)              { return 3.14159265f / 180 * deg;           }
00082 };
00083 
00086 template <>
00087 struct TrigonometricTraits<double>
00088 {
00089     typedef double Type;
00090     
00091     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00092     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00093     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00094     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00095     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00096     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00097     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00098     
00099     static Type Pi()                            { return 3.14159265;                        }
00100     static Type RadToDeg(Type rad)              { return 180 / 3.14159265 * rad;            }
00101     static Type DegToRad(Type deg)              { return 3.14159265 / 180 * deg;            }
00102 };
00103 
00106 template <>
00107 struct TrigonometricTraits<long double>
00108 {
00109     typedef long double Type;
00110     
00111     static Type Sin(Type deg)                   { return std::sin(DegToRad(deg));           }
00112     static Type Cos(Type deg)                   { return std::cos(DegToRad(deg));           }
00113     static Type Tan(Type deg)                   { return std::tan(DegToRad(deg));           }
00114     static Type ArcSin(Type value)              { return RadToDeg(std::asin(value));        }
00115     static Type ArcCos(Type value)              { return RadToDeg(std::acos(value));        }
00116     static Type ArcTan2(Type valY, Type valX)   { return RadToDeg(std::atan2(valY, valX));  }
00117     static Type Sqrt(Type value)                { return std::sqrt(value);                  }
00118     
00119     static Type Pi()                            { return 3.14159265l;                       }
00120     static Type RadToDeg(Type rad)              { return 180 / 3.14159265l * rad;           }
00121     static Type DegToRad(Type deg)              { return 3.14159265l / 180 * deg;           }
00122 };
00123 
00126 template <typename T>
00127 T ToDegree(T radian)
00128 {
00129     return TrigonometricTraits<T>::RadToDeg(radian);
00130 }
00131 
00134 template <typename T>
00135 T ToRadian(T degree)
00136 {
00137     return TrigonometricTraits<T>::DegToRad(degree);
00138 }
00139 
00142 extern const float Pi;
00143 
00145 
00146 } // namespace thor
00147 
00148 #endif // THOR_TRIGONOMETRY_HPP