MathSup.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef MATHSUP_INCLUDEDEFX_H
19 #define MATHSUP_INCLUDEDEFX_H
20 
21 #include <math.h>
22 
23 
24 //-----------------------------------------------
29 class MathSup
30 {
31 public:
32  // -------- Constants
34  static const double PI;
35 
37  static const double TWO_PI;
38 
40  static const double HALF_PI;
41 
42  // -------- Angle conversion
46  static double convRadToDeg(const double& dAngRad)
47  {
48  return (dAngRad * 180.0 / PI);
49  }
50 
54  static double convDegToRad(const double& dAngDeg)
55  {
56  return ( dAngDeg * PI / 180.0 );
57  }
58 
62  static void normalize2Pi(double& angle)
63  {
64  angle-=floor(angle/(2 * PI))* 2 * PI;
65  }
66 
70  static void normalizePi(double& angle)
71  {
72  normalize2Pi(angle);
73  if ( angle> PI ) angle-=2 * PI;
74  }
75 
79  static void normalizePiHalf(double& angle)
80  {
81  normalize2Pi(angle);
82  if (angle > PI) angle-=2*PI;
83  if (angle > PI/2.0) angle -= PI;
84  if (angle <= -PI/2.0) angle += PI;
85  }
86 
90  static double sign(const double& x)
91  {
92  if (x<0)
93  return -1.0;
94  else
95  return 1.0;
96  }
97 
101  static double getMin(const double& a, const double& b)
102  {
103  if (a < b)
104  return a;
105  else
106  return b;
107  }
108 
112  static double getMax(const double& a, const double& b)
113  {
114  if (a > b)
115  return a;
116  else
117  return b;
118  }
119 
124  static double calcDeltaAng(const double& a, const double& b)
125  {
126  double c = a-b;
127  normalizePi(c);
128  return c;
129  }
130 
131 
135  static double atan4quad(double y, double x)
136  {
137  double result;
138 
139  if((x==0.0) && (y==0.0))
140  result = 0.0;
141  else if((x==0.0) && (y > 0.0))
142  result = HALF_PI;
143  else if((x==0.0) && (y < 0.0))
144  result = -HALF_PI;
145  else if((y==0.0) && (x > 0.0))
146  result = 0.0;
147  else if((y==0.0) && (x < 0.0))
148  result = PI;
149  else
150  {
151  result = atan(y/x);
152  if(x<0.0)
153  {
154  if(y>0.0) // Quadrant 2 -> correct
155  result += PI;
156  else // Quadrant 3 -> correct
157  result -= PI;
158  }
159  }
160  normalizePi(result);
161  return result;
162  }
163 
164 
168  static double distance(double x1, double y1, double x2, double y2)
169  {
170  return sqrt( distanceSq( x1, y1, x2, y2 ) );
171  }
172 
176  static double distanceSq(double x1, double y1, double x2, double y2)
177  {
178  double dx = x2 - x1;
179  double dy = y2 - y1;
180 
181  return dx*dx + dy*dy;
182  }
183 
187  static bool isBitSet(int iVal, int iNrBit)
188  {
189  if( (iVal & (1 << iNrBit)) == 0)
190  return false;
191  else
192  return true;
193  }
194 
199  static double convFloatToInt4Byte(double dVal)
200  {
201  //Todo
202  return 1.0;
203  }
204 
209  static double convInt4ByteToFloat(int iVal)
210  {
211  unsigned char c[4];
212  double dSign;
213  double dFraction;
214  double dExp;
215  double dVal;
216 
217  c[0] = iVal >> 24;
218  c[1] = iVal >> 16;
219  c[2] = iVal >> 8;
220  c[3] = iVal;
221 
222  if( (c[0] & 0x80) == 0)
223  dSign = 1;
224  else
225  dSign = -1;
226 
227  dFraction = (iVal & 0xFFFFFF) | 0x800000;
228  dFraction = dFraction * pow(2., -23);
229 
230  dExp = ((c[0] << 1) | (c[1] >> 7)) - 127;
231 
232  dVal = dSign * dFraction * pow(10., dExp);
233 
234  return dVal;
235  }
236 
244  static int limit(double* pdToLimit, double dLimit)
245  {
246  int iRet = 0;
247 
248  if(*pdToLimit < -dLimit)
249  {
250  *pdToLimit = -dLimit;
251  iRet = 1;
252  }
253  if(*pdToLimit > dLimit)
254  {
255  *pdToLimit = dLimit;
256  iRet = 2;
257  }
258 
259  return iRet;
260  }
261 
269  static int limit(int* piToLimit, int iLimit)
270  {
271  int iRet = 0;
272 
273  if(*piToLimit < -iLimit)
274  {
275  *piToLimit = -iLimit;
276  iRet = 1;
277  }
278  if(*piToLimit > iLimit)
279  {
280  *piToLimit = iLimit;
281  iRet = 2;
282  }
283 
284  return iRet;
285  }
286 
294  static bool isInInterval(double dLow, double dHigh, double dVal)
295  {
296  if( (dVal >= dLow) && (dVal <= dHigh) )
297  return true;
298 
299  else
300  return false;
301  }
302 
303 };
304 
305 
306 //-----------------------------------------------
307 #endif
static int limit(double *pdToLimit, double dLimit)
Definition: MathSup.h:244
static const double HALF_PI
Constant for PI/2.
Definition: MathSup.h:40
static double convRadToDeg(const double &dAngRad)
Definition: MathSup.h:46
static double atan4quad(double y, double x)
Definition: MathSup.h:135
static double sign(const double &x)
Definition: MathSup.h:90
static void normalize2Pi(double &angle)
Definition: MathSup.h:62
static const double PI
Constant for PI.
Definition: MathSup.h:34
static double calcDeltaAng(const double &a, const double &b)
Definition: MathSup.h:124
static int limit(int *piToLimit, int iLimit)
Definition: MathSup.h:269
static double getMin(const double &a, const double &b)
Definition: MathSup.h:101
static void normalizePi(double &angle)
Definition: MathSup.h:70
static double distanceSq(double x1, double y1, double x2, double y2)
Definition: MathSup.h:176
static double distance(double x1, double y1, double x2, double y2)
Definition: MathSup.h:168
static bool isInInterval(double dLow, double dHigh, double dVal)
Definition: MathSup.h:294
static bool isBitSet(int iVal, int iNrBit)
Definition: MathSup.h:187
static double convDegToRad(const double &dAngDeg)
Definition: MathSup.h:54
static double getMax(const double &a, const double &b)
Definition: MathSup.h:112
static double convInt4ByteToFloat(int iVal)
Definition: MathSup.h:209
static double convFloatToInt4Byte(double dVal)
Definition: MathSup.h:199
static void normalizePiHalf(double &angle)
Definition: MathSup.h:79
static const double TWO_PI
Constant for 2*PI.
Definition: MathSup.h:37


cob_utilities
Author(s): Christian Connette
autogenerated on Wed Apr 7 2021 02:11:52