Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <limits.h>
00012 #include "Math.h"
00013 #include <math.h>
00014
00015 #include "vec2.h"
00016
00017 #define THIS Math
00018
00019 THIS::THIS()
00020 {
00021 }
00022
00023 THIS::~THIS()
00024 {
00025 }
00026
00027 float THIS::meanAngle( const std::vector<float>& angles )
00028 {
00029
00030 CVec2 vectorSum(0,0);
00031 for ( unsigned i=0; i<angles.size(); i++ )
00032 {
00033 vectorSum = vectorSum + CVec2( cos( angles[i] ), sin ( angles[i] ) );
00034 }
00035
00036 if ( vectorSum.magnitude() == 0 ) { return 0; }
00037 return atan2( vectorSum.y(), vectorSum.x() );
00038 }
00039
00040
00041 float THIS::meanAngleWeighted( const std::vector< WeightedValue >& weightedAngles )
00042 {
00043
00044 CVec2 vectorSum(0,0);
00045 for ( unsigned i=0; i<weightedAngles.size(); i++ )
00046 {
00047 vectorSum = vectorSum + weightedAngles[i].weight * CVec2( cos( weightedAngles[i].value ), sin ( weightedAngles[i].value ) );
00048 }
00049
00050 if ( vectorSum.magnitude() == 0 ) { return 0; }
00051 return atan2( vectorSum.y(), vectorSum.x() );
00052 }
00053
00054
00055 float THIS::angleVariance( float meanAngle, const std::vector<float>& angles )
00056 {
00057 float quadSum=0;
00058 for( unsigned i=0; i < angles.size(); i++ )
00059 {
00060 float turnAngle=minTurnAngle( angles[i], meanAngle );
00061 quadSum += turnAngle*turnAngle;
00062 }
00063 return quadSum / float ( angles.size() );
00064 }
00065
00066
00067 float THIS::minTurnAngle( float angle1, float angle2 )
00068 {
00069
00070
00071
00072
00073 float diff= angle2 - angle1;
00074 float sign=1;
00075 if ( diff < 0 ) { sign=-1; }
00076
00077
00078 if ( fabs(diff) > M_PI ) {
00079 diff = 2*M_PI - fabs(diff);
00080 diff *= sign;
00081 }
00082 return diff;
00083 }
00084
00085 Point2D THIS::center( std::vector<Point2D>& points )
00086 {
00087 double numPoints = double( points.size() );
00088 double sumX=0, sumY=0;
00089 for( unsigned i=0; i < points.size(); i++ )
00090 {
00091 sumX += points[i].x();
00092 sumY += points[i].y();
00093 }
00094 return Point2D( sumX / numPoints, sumY / numPoints );
00095 }
00096
00097
00098 double THIS::randomGauss(float variance)
00099 {
00100 if (variance < 0) {
00101 variance = -variance;
00102 }
00103 double x1, x2, w, y1;
00104 do {
00105 x1 = 2.0 * random01() - 1.0;
00106 x2 = 2.0 * random01() - 1.0;
00107 w = x1 * x1 + x2 * x2;
00108 } while ( w >= 1.0 );
00109
00110 w = sqrt((-2.0 * log(w)) / w);
00111 y1 = x1 * w;
00112
00113 return sqrt(variance) * y1;
00114 }
00115
00116 double THIS::random01(unsigned long init)
00117 {
00118 static unsigned long n;
00119 if (init > 0) {
00120 n = init;
00121 }
00122 n = 1664525 * n + 1013904223;
00123
00124 return (double)(n/2) / (double)LONG_MAX;
00125 }
00126
00127 #undef THIS