linearApproximator.cpp
Go to the documentation of this file.
00001 
00018 #include <MathHelpers/linearApproximator.h>
00019 #ifndef Q_MOC_RUN
00020 #include <ros/ros.h>
00021 #endif
00022 
00023     ApproximationResult LinearApproximator::calculateApproximation(Eigen::Vector2d * functionPoints, int startIndex, int endIndex, Eigen::Vector2d centerOffset)
00024     {
00025         double X = 0, Y = 0, Z = 0;
00026         double alpha;
00027         for (int i = startIndex; i < endIndex; i++)
00028         {
00029             Eigen::Vector2d it = functionPoints[i];
00030             X += (it[0] - centerOffset[0]) * (it[0] - centerOffset[0]);
00031             Y += (it[1] - centerOffset[1]) * (it[1] - centerOffset[1]);
00032             Z += (it[0] - centerOffset[0]) * (it[1] - centerOffset[1]);
00033         }
00034         if (X == Y)
00035         {
00036             if (Z > 0)
00037             {
00038                 alpha = M_PI / 4.0;
00039             }
00040             else
00041             {
00042                 alpha = M_PI * 3.0 / 4.0;
00043             }
00044         }
00045         else
00046         {
00047             if (Y - X < 0)
00048             {
00049                 alpha = - atan(2.0 * Z / (Y - X)) * 0.5;
00050                 //ROS_INFO_STREAM("Y - X < 0");
00051             }
00052             else
00053             {
00054                 //ROS_INFO_STREAM("Y - X > 0");
00055                 //alpha = - atan(2.0 * Z / (Y - X)) * 0.5;
00056                 alpha = M_PI / 2.0 - atan(2.0 * Z / (Y - X)) * 0.5;
00057             }
00058             if (alpha < M_PI/2.0)
00059             {
00060                 alpha += M_PI;
00061             }
00062         }
00063 
00064         double variance = 0;
00065         double maxLength = 0;
00066         for (int i = startIndex; i < endIndex; i++)
00067         {
00068             Eigen::Vector2d it = functionPoints[i];
00069             variance += pow(sin(alpha) * (it[0] - centerOffset[0]) - cos(alpha) * (it[1] - centerOffset[1]), 2.0);
00070             maxLength = std::max(maxLength, std::abs(cos(alpha) * (it[0] - centerOffset[0]) + sin(alpha) * (it[1] - centerOffset[1])));
00071         }
00072         variance /= (double)functionPoints->size();
00073         ApproximationResult result;
00074         result.approximatedVector = Eigen::Vector2d(cos(alpha) * maxLength, sin(alpha) * maxLength);
00075         result.angle = alpha;
00076         result.variance = variance;
00077         return result;
00078     }
00079 /*
00080     double LinearApproximator::calculateApproximation(std::vector<Eigen::Vector2d> * functionPoints)
00081     {
00082         long X = 0, Y = 0, Z = 0;
00083         double alpha;
00084         for (std::vector<Eigen::Vector2d>::iterator it = functionPoints->begin(); it < functionPoints->end(); it++)
00085         {
00086             X += it[0] * it[0];
00087             Y += it[1] * it[1];
00088             Z += it[0] * it[1];
00089         }
00090 
00091         if (X == Y)
00092         {
00093             if (Z > 0)
00094             {
00095                 alpha = M_PI / 4.0;
00096             }
00097             else
00098             {
00099                 alpha = M_PI * 3.0 / 4.0;
00100             }
00101         }
00102         else
00103         {
00104             if (Y - X < 0)
00105             {
00106                 alpha = - atan(2.0 * (double)Z / (double)(Y - X)) * 0.5;
00107                 if (alpha < 0.0)
00108                 {
00109                     alpha += M_PI;
00110                 }
00111             }
00112             else
00113             {
00114                 alpha = M_PI / 2.0 - atan(2.0 * (double)Z / (double)(Y - X)) * 0.5;
00115             }
00116         }
00117 
00118         variance = 0;
00119         double maxLength = 0;
00120         for (std::vector<Eigen::Vector2d>::iterator it = functionPoints->begin(); it < functionPoints->end(); it++)
00121         {
00122             variance += pow(sin(alpha) * it[0] + cos(alpha) * it[1], 2.0);
00123             maxLength = max(maxLength, cos(alpha) * it[0] - sin(alpha) * it[1]);
00124         }
00125         approximatedVector = new Eigen::Vector2d(cos(alpha) * maxLength, sin(alpha) * maxLength);
00126         variance /= (double)functionPoints->size();
00127 
00128         angle = alpha;
00129         return variance;
00130     }*/


asr_mild_calibration_tool
Author(s): Aumann Florian, Heller Florian, Meißner Pascal
autogenerated on Thu Jun 6 2019 21:22:44