Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_MATH_RADIANDEGREE_H__
00009 #define __SP_MATH_RADIANDEGREE_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #include <cmath>
00015
00016
00017 namespace sp
00018 {
00019 namespace math
00020 {
00021
00022
00023 template <typename T> class Degree;
00024 template <typename T> class Radian;
00025
00026 template <typename T> class RadianDegreeAngle
00027 {
00028
00029 public:
00030
00031 virtual ~RadianDegreeAngle()
00032 {
00033 }
00034
00035 virtual T getDegree() const = 0;
00036 virtual T getRadian() const = 0;
00037
00038 protected:
00039
00040 RadianDegreeAngle()
00041 {
00042 }
00043
00044 };
00045
00047 template <typename T> class Degree : public RadianDegreeAngle<T>
00048 {
00049
00050 public:
00051
00052 Degree(const T &Deg = 0) :
00053 Degree_(Deg)
00054 {
00055 clamp();
00056 }
00057 Degree(const Degree<T> &Deg) :
00058 Degree_(Deg.Degree_)
00059 {
00060 }
00061 Degree(const Radian<T> &Rad);
00062 ~Degree()
00063 {
00064 }
00065
00066
00067
00068 T getDegree() const
00069 {
00070 return Degree_;
00071 }
00072 T getRadian() const
00073 {
00074 return Degree_ * static_cast<T>(M_PI / 180.0);
00075 }
00076
00077
00078
00079 inline Degree<T>& operator = (const Degree<T> &Other)
00080 {
00081 Degree_ = Other.Degree_;
00082 return *this;
00083 }
00084
00085 inline Degree<T>& operator += (const Degree<T> &Other)
00086 {
00087 Degree_ += Other.Degree_;
00088 clamp();
00089 return *this;
00090 }
00091 inline Degree<T>& operator -= (const Degree<T> &Other)
00092 {
00093 Degree_ -= Other.Degree_;
00094 clamp();
00095 return *this;
00096 }
00097 inline Degree<T>& operator *= (const Degree<T> &Other)
00098 {
00099 Degree_ *= Other.Degree_;
00100 clamp();
00101 return *this;
00102 }
00103 inline Degree<T>& operator /= (const Degree<T> &Other)
00104 {
00105 Degree_ /= Other.Degree_;
00106 clamp();
00107 return *this;
00108 }
00109
00110 inline Degree<T> operator + (const Degree<T> &Other) const
00111 {
00112 return Degree(Degree_ + Other.Degree_);
00113 }
00114 inline Degree<T> operator - (const Degree<T> &Other) const
00115 {
00116 return Degree(Degree_ - Other.Degree_);
00117 }
00118 inline Degree<T> operator * (const Degree<T> &Other) const
00119 {
00120 return Degree(Degree_ * Other.Degree_);
00121 }
00122 inline Degree<T> operator / (const Degree<T> &Other) const
00123 {
00124 return Degree(Degree_ / Other.Degree_);
00125 }
00126
00127 inline bool operator > (const Degree<T> &Other) const
00128 {
00129 return Degree_ > Other.Degree_;
00130 }
00131 inline bool operator >= (const Degree<T> &Other) const
00132 {
00133 return Degree_ >= Other.Degree_;
00134 }
00135 inline bool operator < (const Degree<T> &Other) const
00136 {
00137 return Degree_ < Other.Degree_;
00138 }
00139 inline bool operator <= (const Degree<T> &Other) const
00140 {
00141 return Degree_ <= Other.Degree_;
00142 }
00143
00144 private:
00145
00146
00147
00148 inline void clamp()
00149 {
00150 Degree_ -= static_cast<T>(std::floor(Degree_ / T(360)) * T(360));
00151 }
00152
00153
00154
00155 T Degree_;
00156
00157 };
00158
00159
00161 template <typename T> class Radian : public RadianDegreeAngle<T>
00162 {
00163
00164 public:
00165
00166 Radian(const T &Rad = T(0)) :
00167 Radian_(Rad)
00168 {
00169 clamp();
00170 }
00171 Radian(const Radian<T> &Rad) :
00172 Radian_(Rad.Radian_)
00173 {
00174 }
00175 Radian(const Degree<T> &Deg) :
00176 Radian_(Deg.getRadian())
00177 {
00178 }
00179 ~Radian()
00180 {
00181 }
00182
00183
00184
00185 T getDegree() const
00186 {
00187 return Radian_ * static_cast<T>(180.0 / M_PI);
00188 }
00189 T getRadian() const
00190 {
00191 return Radian_;
00192 }
00193
00194
00195
00196 inline Radian<T>& operator = (const Radian<T> &Other)
00197 {
00198 Radian_ = Other.Radian_;
00199 return *this;
00200 }
00201
00202 inline Radian<T>& operator += (const Radian<T> &Other)
00203 {
00204 Radian_ += Other.Radian_;
00205 clamp();
00206 return *this;
00207 }
00208 inline Radian<T>& operator -= (const Radian<T> &Other)
00209 {
00210 Radian_ -= Other.Radian_;
00211 clamp();
00212 return *this;
00213 }
00214 inline Radian<T>& operator *= (const Radian<T> &Other)
00215 {
00216 Radian_ *= Other.Radian_;
00217 clamp();
00218 return *this;
00219 }
00220 inline Radian<T>& operator /= (const Radian<T> &Other)
00221 {
00222 Radian_ /= Other.Radian_;
00223 clamp();
00224 return *this;
00225 }
00226
00227 inline Radian<T> operator + (const Radian<T> &Other) const
00228 {
00229 return Radian(Radian_ + Other.Radian_);
00230 }
00231 inline Radian<T> operator - (const Radian<T> &Other) const
00232 {
00233 return Radian(Radian_ - Other.Radian_);
00234 }
00235 inline Radian<T> operator * (const Radian<T> &Other) const
00236 {
00237 return Radian(Radian_ * Other.Radian_);
00238 }
00239 inline Radian<T> operator / (const Radian<T> &Other) const
00240 {
00241 return Radian(Radian_ / Other.Radian_);
00242 }
00243
00244 inline bool operator > (const Radian<T> &Other) const
00245 {
00246 return Radian_ > Other.Radian_;
00247 }
00248 inline bool operator >= (const Radian<T> &Other) const
00249 {
00250 return Radian_ >= Other.Radian_;
00251 }
00252 inline bool operator < (const Radian<T> &Other) const
00253 {
00254 return Radian_ < Other.Radian_;
00255 }
00256 inline bool operator <= (const Radian<T> &Other) const
00257 {
00258 return Radian_ <= Other.Radian_;
00259 }
00260
00261 private:
00262
00263
00264
00265 inline void clamp()
00266 {
00267 Radian_ -= static_cast<T>(std::floor(Radian_ / T(2.0*M_PI)) * T(2.0*M_PI));
00268 }
00269
00270
00271
00272 T Radian_;
00273
00274 };
00275
00276
00277 template <typename T> Degree<T>::Degree(const Radian<T> &Rad) :
00278 Degree_(Rad.getDegree())
00279 {
00280 }
00281
00282
00283 }
00284
00285 }
00286
00287
00288 #endif
00289
00290
00291
00292