Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef Math_H
00012 #define Math_H
00013
00014 #include <vector>
00015 #include "Point2D.h"
00016
00022 class Math
00023 {
00024 public:
00025
00026 struct WeightedValue
00027 {
00028 float value;
00029 float weight;
00030 };
00031
00032 static const double Pi = 3.14159265358979323846;
00033
00035 template<class ContainerT>
00036 static double mean ( const ContainerT& values );
00037
00039 template<class ContainerT>
00040 static double variance ( const ContainerT& values );
00041
00046 static float meanAngle ( const std::vector<float>& angles );
00047
00048 static float meanAngleWeighted ( const std::vector< WeightedValue >& weightedAngles );
00049
00051 static float angleVariance ( float meanAngle, const std::vector<float>& angles );
00052
00054 static float minTurnAngle ( float angle1, float angle2 );
00055
00056 static Point2D center ( std::vector<Point2D>& points );
00057
00058 static float deg2Rad ( float deg ) { return deg / 180.0*Pi; }
00059
00060 static float rad2Deg ( float rad ) { return rad / Pi*180.0; }
00061
00062 static double randomGauss ( float variance = 1.0 );
00063
00064 static double random01 ( unsigned long init = 0 );
00065
00067 static double angleToPercent ( double newAngle, double oldAngle ) { return tan ( ( Pi / 180.0 ) * newAngle / 2 ) / tan ( ( Pi / 180.0 ) * oldAngle / 2 ); };
00068
00070 static double percentToAngle ( double percent, double angle ) { return 2* atan ( tan ( ( Pi / 180.0 ) * angle / 2 ) * percent ) * ( 180 / Pi ); };
00071
00073 static double horizontalViewAngle ( double diagonalAngle, double aspectRatio ) { return verticalViewAngle ( diagonalAngle, 1.0 / aspectRatio ); };
00074
00076 static double verticalViewAngle ( double diagonalAngle, double aspectRatio )
00077 {
00078 return percentToAngle ( 1.0 / sqrt ( pow ( aspectRatio, 2 ) + 1.0 ), diagonalAngle );
00079 };
00080
00081 template<class ValueT>
00082 static inline ValueT min ( ValueT a, ValueT b ) { return a < b ? a : b; }
00083
00084 template<class ValueT>
00085 static inline ValueT max ( ValueT a, ValueT b ) { return a > b ? a : b; }
00086
00087 private:
00088
00090 Math();
00091
00093 ~Math();
00094
00095 };
00096
00097 template<class ContainerT>
00098 double Math::mean ( const ContainerT& values )
00099 {
00100 typename ContainerT::const_iterator it;
00101 it = values.begin();
00102 double sum = 0;
00103 while ( it != values.end() )
00104 {
00105 sum += *it;
00106 it++;
00107 }
00108 return sum / double ( values.size() );
00109 }
00110
00111
00112 template<class ContainerT>
00113 double Math::variance ( const ContainerT& values )
00114 {
00115 double mean = mean ( values );
00116 typename ContainerT::const_iterator it;
00117 it = values.begin();
00118 double sum = 0;
00119 while ( it != values.end() )
00120 {
00121 double diff = *it - mean;
00122 sum += diff * diff;
00123 it++;
00124 }
00125 return sum / double ( values.size() );
00126 }
00127
00128
00129 #endif