MathFunctions.cpp
Go to the documentation of this file.
00001 #include <convenience_math_functions/MathFunctions.h>
00002 #include <algorithm>
00003 #include <eigen_conversions/eigen_msg.h>
00004 
00005 using convenience_math_functions::MathFunctions;
00006 
00007 double MathFunctions::capToPI(const double value)
00008 {
00009     static const double pi_2 = 2.0 * M_PI;
00010     double v = value;
00011     if (v <= -M_PI || v > M_PI)
00012     {
00013         v = fmod(v, pi_2);
00014         if (v <= -M_PI)
00015             v += pi_2;
00016         else if (v > M_PI)
00017             v -= pi_2;
00018     }
00019     return v;
00020 }
00021 
00022 double MathFunctions::limitsToTwoPI(const double value, const double lowLimit, const double highLimit)
00023 {
00024     double ret = value;
00025     if (value > highLimit) ret = value - 2 * M_PI;
00026     if (value < lowLimit) ret = value + 2 * M_PI;
00027     return ret;
00028 }
00029 
00030 
00031 Eigen::Quaterniond MathFunctions::getRotationFromTo(const Eigen::Quaterniond& q1, const Eigen::Quaterniond& q2)
00032 {
00033     Eigen::Quaterniond ret = q2 * q1.inverse();
00034     ret.normalize();
00035     return ret;
00036 }
00037 
00038 
00039 double MathFunctions::angleDistance(const double _f1, const double _f2)
00040 {
00041     double f1 = capToPI(_f1);
00042     double f2 = capToPI(_f2);
00043     double diff = f2 - f1;
00044     diff = capToPI(diff);
00045     /*ROS_INFO("Cap1 %f to %f",_f1,f1);
00046     ROS_INFO("Cap2 %f to %f",_f2,f2);
00047     ROS_INFO("DIFF %f",diff);*/
00048     /*      if (diff > M_PI) diff=M_PI-diff;
00049         else if (diff < -M_PI) diff=-M_PI+diff;
00050         ROS_INFO("DIFF2 %f",diff);*/
00051     return diff;
00052 }
00053 
00054 
00055 double MathFunctions::quatAngularDistance(const geometry_msgs::Quaternion& _q1, const geometry_msgs::Quaternion& _q2)
00056 {
00057     Eigen::Quaterniond q1_in, q2_in;
00058     tf::quaternionMsgToEigen(_q1, q1_in);
00059     tf::quaternionMsgToEigen(_q2, q2_in);
00060     return quatAngularDistance(q1_in, q2_in);
00061 }
00062 
00063 
00064 double MathFunctions::quatAngularDistance(const Eigen::Quaterniond& _q1, const Eigen::Quaterniond& _q2)
00065 {
00066     if (_q1.isApprox(_q2, 0.001)) return 0;
00067     Eigen::Quaterniond q1 = _q1;
00068     Eigen::Quaterniond q2 = _q2;
00069     q1.normalize();
00070     q2.normalize();
00071     return q1.angularDistance(q2);
00072 }
00073 
00074 double MathFunctions::vecAngularDistance(const Eigen::Vector3d& _v1, const Eigen::Vector3d& _v2)
00075 {
00076     Eigen::Vector3d v1 = _v1;
00077     Eigen::Vector3d v2 = _v2;
00078     v1.normalize();
00079     v2.normalize();
00080     return acos(v1.dot(v2));
00081 }
00082 
00083 
00084 bool MathFunctions::equalFlt(float first, float second, float tolerance)
00085 {
00086     return ((first <= second + tolerance) && (first >= second - tolerance));
00087 }
00088 
00089 bool MathFunctions::equalFloats(const std::vector<float>& first, const std::vector<float>& second, float tolerance)
00090 {
00091     if (first.size() != second.size()) return false;
00092     for (int i = 0; i < first.size(); ++i)
00093         if (!MathFunctions::equalFlt(first[i], second[i], tolerance)) return false;
00094     return true;
00095 }


convenience_math_functions
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:39