MathSup.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #ifndef MATHSUP_INCLUDEDEFX_H
00019 #define MATHSUP_INCLUDEDEFX_H
00020 
00021 #include <math.h>
00022 
00023 
00024 //-----------------------------------------------
00029 class MathSup
00030 {
00031 public:
00032         // -------- Constants
00034         static const double PI;
00035 
00037         static const double TWO_PI;
00038 
00040         static const double HALF_PI;
00041 
00042         // -------- Angle conversion
00046         static double convRadToDeg(const double& dAngRad)
00047         {
00048                 return (dAngRad * 180.0 / PI);
00049         }
00050 
00054         static double convDegToRad(const double& dAngDeg)
00055         {
00056                 return ( dAngDeg * PI / 180.0 );
00057         }
00058 
00062         static void normalize2Pi(double& angle)
00063         {
00064                 angle-=floor(angle/(2 * PI))* 2 * PI;
00065         }
00066 
00070         static void normalizePi(double& angle)
00071         {
00072                 normalize2Pi(angle);
00073                 if ( angle> PI ) angle-=2 * PI;
00074         }
00075 
00079         static void normalizePiHalf(double& angle)
00080         {
00081                 normalize2Pi(angle);
00082                 if (angle > PI) angle-=2*PI;
00083                 if (angle > PI/2.0) angle -= PI;
00084                 if (angle <= -PI/2.0) angle += PI;
00085         }
00086 
00090         static double sign(const double& x)
00091         {
00092                 if (x<0)
00093                         return -1.0;
00094                 else
00095                         return 1.0;
00096         }
00097 
00101         static double getMin(const double& a, const double& b)
00102         {
00103                 if (a < b)
00104                         return a;
00105                 else
00106                         return b;
00107         }
00108 
00112         static double getMax(const double& a, const double& b)
00113         {
00114                 if (a > b)
00115                         return a;
00116                 else
00117                         return b;
00118         }
00119 
00124         static double calcDeltaAng(const double& a, const double& b)
00125         {
00126                 double c = a-b;
00127                 normalizePi(c);
00128                 return c;
00129         }
00130 
00131 
00135         static double atan4quad(double y, double x)
00136         {
00137                 double result;
00138 
00139                 if((x==0.0) && (y==0.0))
00140                         result = 0.0;
00141                 else if((x==0.0) && (y > 0.0))
00142                         result = HALF_PI;
00143                 else if((x==0.0) && (y < 0.0))
00144                         result = -HALF_PI;
00145                 else if((y==0.0) && (x > 0.0))
00146                         result = 0.0;
00147                 else if((y==0.0) && (x < 0.0))
00148                         result = PI;
00149                 else
00150                 {
00151                         result = atan(y/x);
00152                         if(x<0.0)
00153                                 {
00154                                         if(y>0.0)               // Quadrant 2 -> correct
00155                                                 result += PI;
00156                                         else                    // Quadrant 3 -> correct
00157                                                 result -= PI;
00158                                 }
00159                 }
00160                 normalizePi(result);
00161                 return result;
00162         }
00163 
00164 
00168         static double distance(double x1, double y1, double x2, double y2)
00169         {
00170                 return sqrt( distanceSq( x1, y1, x2, y2 ) );
00171         }
00172 
00176         static double distanceSq(double x1, double y1, double x2, double y2)
00177         {
00178                 double dx = x2 - x1;
00179                 double dy = y2 - y1;
00180 
00181                 return dx*dx + dy*dy;
00182         }
00183 
00187         static bool isBitSet(int iVal, int iNrBit)
00188         {
00189                 if( (iVal & (1 << iNrBit)) == 0)
00190                         return false;
00191                 else
00192                         return true;
00193         }
00194 
00199         static double convFloatToInt4Byte(double dVal)
00200         {
00201                 //Todo
00202                 return 1.0;
00203         }
00204 
00209         static double convInt4ByteToFloat(int iVal)
00210         {
00211                 unsigned char c[4];
00212                 double dSign;
00213                 double dFraction;
00214                 double dExp;
00215                 double dVal;
00216 
00217                 c[0] = iVal >> 24;
00218                 c[1] = iVal >> 16;
00219                 c[2] = iVal >> 8;
00220                 c[3] = iVal;
00221 
00222                 if( (c[0] & 0x80) == 0)
00223                         dSign = 1;
00224                 else
00225                         dSign = -1;
00226 
00227                 dFraction = (iVal & 0xFFFFFF) | 0x800000;
00228                 dFraction = dFraction * pow(2., -23);
00229 
00230                 dExp = ((c[0] << 1) | (c[1] >> 7)) - 127;
00231 
00232                 dVal = dSign * dFraction * pow(10., dExp);
00233 
00234                 return dVal;
00235         }
00236 
00244         static int limit(double* pdToLimit, double dLimit)
00245         {
00246                 int iRet = 0;
00247 
00248                 if(*pdToLimit < -dLimit)
00249                 {
00250                         *pdToLimit = -dLimit;
00251                         iRet = 1;
00252                 }
00253                 if(*pdToLimit > dLimit)
00254                 {
00255                         *pdToLimit = dLimit;
00256                         iRet = 2;
00257                 }
00258 
00259                 return iRet;
00260         }
00261 
00269         static int limit(int* piToLimit, int iLimit)
00270         {
00271                 int iRet = 0;
00272 
00273                 if(*piToLimit < -iLimit)
00274                 {
00275                         *piToLimit = -iLimit;
00276                         iRet = 1;
00277                 }
00278                 if(*piToLimit > iLimit)
00279                 {
00280                         *piToLimit = iLimit;
00281                         iRet = 2;
00282                 }
00283 
00284                 return iRet;
00285         }
00286 
00294         static bool isInInterval(double dLow, double dHigh, double dVal)
00295         {
00296                 if( (dVal >= dLow) && (dVal <= dHigh) )
00297                         return true;
00298 
00299                 else
00300                         return false;
00301         }
00302 
00303 };
00304 
00305 
00306 //-----------------------------------------------
00307 #endif


cob_utilities
Author(s): Christian Connette
autogenerated on Sat Jun 8 2019 21:02:25