00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __KARTO_MATH__
00019 #define __KARTO_MATH__
00020
00021 #include <assert.h>
00022 #include <math.h>
00023
00024 #include "Types.h"
00025
00026 namespace karto
00027 {
00031 const kt_double KT_PI = 3.14159265358979323846;
00032 const kt_double KT_2PI = 6.28318530717958647692;
00033 const kt_double KT_PI_2 = 1.57079632679489661923;
00034 const kt_double KT_PI_180 = 0.01745329251994329577;
00035 const kt_double KT_180_PI = 57.29577951308232087685;
00036
00040 const kt_double KT_TOLERANCE = 1e-06;
00041
00042 namespace math
00043 {
00049 inline kt_double DegreesToRadians(kt_double degrees)
00050 {
00051 return degrees * KT_PI_180;
00052 }
00053
00059 inline kt_double RadiansToDegrees(kt_double radians)
00060 {
00061 return radians * KT_180_PI;
00062 }
00063
00069 template<typename T>
00070 inline T Square(T value)
00071 {
00072 return (value * value);
00073 }
00074
00080 inline kt_double Round(kt_double value)
00081 {
00082 return value >= 0.0 ? floor(value + 0.5) : ceil(value - 0.5);
00083 }
00084
00091 template<typename T>
00092 inline const T& Minimum(const T& value1, const T& value2)
00093 {
00094 return value1 < value2 ? value1 : value2;
00095 }
00096
00103 template<typename T>
00104 inline const T& Maximum(const T& value1, const T& value2)
00105 {
00106 return value1 > value2 ? value1 : value2;
00107 }
00108
00116 template<typename T>
00117 inline const T& Clip(const T& n, const T& minValue, const T& maxValue)
00118 {
00119 return Minimum(Maximum(n, minValue), maxValue);
00120 }
00121
00128 inline kt_bool DoubleEqual(kt_double a, kt_double b)
00129 {
00130 double delta = a - b;
00131 return delta < 0.0 ? delta >= -KT_TOLERANCE : delta <= KT_TOLERANCE;
00132 }
00133
00139 template<typename T>
00140 inline kt_bool IsUpTo(const T& value, const T& maximum)
00141 {
00142 return (value >= 0 && value < maximum);
00143 }
00144
00151 template<>
00152 inline kt_bool IsUpTo<kt_int32u>(const kt_int32u& value, const kt_int32u& maximum)
00153 {
00154 return (value < maximum);
00155 }
00156
00157
00164 template<typename T>
00165 inline kt_bool InRange(const T& value, const T& a, const T& b)
00166 {
00167 return (value >= a && value <= b);
00168 }
00169
00175 inline kt_double NormalizeAngle(kt_double angle)
00176 {
00177 while (angle < -KT_PI)
00178 {
00179 if (angle < -KT_2PI)
00180 {
00181 angle += (kt_int32u)(angle / -KT_2PI) * KT_2PI;
00182 }
00183 else
00184 {
00185 angle += KT_2PI;
00186 }
00187 }
00188
00189 while (angle > KT_PI)
00190 {
00191 if (angle > KT_2PI)
00192 {
00193 angle -= (kt_int32u)(angle / KT_2PI) * KT_2PI;
00194 }
00195 else
00196 {
00197 angle -= KT_2PI;
00198 }
00199 }
00200
00201 assert(math::InRange(angle, -KT_PI, KT_PI));
00202
00203 return angle;
00204 }
00205
00214 inline kt_double NormalizeAngleDifference(kt_double minuend, kt_double subtrahend)
00215 {
00216 while (minuend - subtrahend < -KT_PI)
00217 {
00218 minuend += KT_2PI;
00219 }
00220
00221 while (minuend - subtrahend > KT_PI)
00222 {
00223 minuend -= KT_2PI;
00224 }
00225
00226 return minuend;
00227 }
00228
00236 template<class T>
00237 inline T AlignValue(size_t value, size_t alignValue = 8)
00238 {
00239 return static_cast<T> ((value + (alignValue - 1)) & ~(alignValue - 1));
00240 }
00241 }
00242
00243 }
00244
00245 #endif // __KARTO_MATH__