Math.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *  Math.cpp
00003  *
00004  *  (C) 2007 AG Aktives Sehen <agas@uni-koblenz.de>
00005  *           Universitaet Koblenz-Landau
00006  *
00007  *  Additional information:
00008  *  $Id: $ 
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   //calculate vectors from angles
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   //return vectorSum.getAngle( CVec2(1,0) );
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   //calculate vectors from angles
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   //return vectorSum.getAngle( CVec2(1,0) );
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 /*  CVec2 vector1( cos( angle1 ), sin ( angle1 ) );
00070   CVec2 vector2( cos( angle2 ), sin ( angle2 ) );
00071   return vector1.getAngle( vector2 );
00072   */
00073   float diff= angle2 - angle1;
00074   float sign=1;
00075   if ( diff < 0 ) { sign=-1; }
00076   //minimal turn angle:
00077   //if the absolute difference is above 180°, calculate the difference in other direction
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   // now y1 is uniformly distributed
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   // create double from unsigned long
00124   return (double)(n/2) / (double)LONG_MAX;
00125 }
00126 
00127 #undef THIS


robbie_architecture
Author(s): Viktor Seib
autogenerated on Mon Oct 6 2014 02:53:09