Moka kernel
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
math-extension.hh
Go to the documentation of this file.
1 /*
2  * lib-gmapkernel : Un noyau de 3-G-cartes et des opérations.
3  * Copyright (C) 2004, Moka Team, Université de Poitiers, Laboratoire SIC
4  * http://www.sic.sp2mi.univ-poitiers.fr/
5  * Copyright (C) 2009, Guillaume Damiand, CNRS, LIRIS,
6  * guillaume.damiand@liris.cnrs.fr, http://liris.cnrs.fr/
7  *
8  * This file is part of lib-gmapkernel
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 //******************************************************************************
25 #ifndef MATH_EXTENSION_HH
26 #define MATH_EXTENSION_HH
27 //******************************************************************************
28 #include <cassert>
29 #include <cmath> // Pour la macro M_PI et fabs
30 //******************************************************************************
31 class CVertex;
32 //******************************************************************************
45 //******************************************************************************
46 // @name Macros
47 // @{
48 
49 #ifndef MIN
50 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
51 #endif //MIN
52 
53 #ifndef MAX
54 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
55 #endif //MAX
56 
57 #ifndef M_PI
58 #define M_PI (3.141592653589793238462643383279)
59 #endif // M_PI
60 
61 // @}
62 //******************************************************************************
63 // @name Types de base
64 // @{
65 
71 typedef double TCoordinate;
72 
76 extern const TCoordinate EPSILON;
77 
86 typedef enum
87 {
88  FUNCTION_LIN , //@< x -> x
89  FUNCTION_QUAD , //@< x -> x^2
90  FUNCTION_EXP , //@< x -> e^x
91  FUNCTION_LOG , //@< x -> ln(x) si x>0, x -> 0
92  FUNCTION_SIN , //@< x -> sin(x)
93  FUNCTION_COS //@< x -> cos(x)
94 }
96 
100 static const int NB_FUNCTION_TYPES = 6;
101 
102 // @}
103 //******************************************************************************
104 // @name Fonctions mathématiques de base
105 // @{
106 
113 inline int sqr(int AValue)
114 {
115  return AValue*AValue;
116 }
117 
118 inline float sqr(float AValue)
119 {
120  return AValue*AValue;
121 }
122 
123 inline double sqr(double AValue)
124 {
125  return AValue*AValue;
126 }
127 
134 inline bool isPositive(const TCoordinate& AValue)
135 {
136  // return AValue > 0 || areEqual(fabs(AValue), 0.0);
137  return AValue > -EPSILON;
138 }
139 
146 inline bool isNegative(const TCoordinate& AValue)
147 {
148  // return AValue < 0 || areEqual(fabs(AValue), 0.0);
149  return AValue < +EPSILON;
150 }
151 
158 inline bool isZero(const TCoordinate& AValue)
159 {
160  // return areEqual(fabs(AValue), 0.0);
161  return fabs(AValue) < EPSILON;
162 }
163 
170 inline int sign(const TCoordinate& AValue)
171 {
172  if (isZero(AValue))
173  return 0;
174 
175  return AValue<0 ? -1 : +1;
176 }
177 
186 inline int combineSigns(int ASign1, int ASign2)
187 {
188  assert(-1 <= ASign1 && ASign1 <= +1);
189  assert(-1 <= ASign2 && ASign2 <= +1);
190 
191  if (ASign1==0 || ASign2==0)
192  return 0;
193 
194  return ASign1==ASign2 ? +1 : -1;
195 }
196 
205 inline bool isLessThan(const TCoordinate& AValue1, const TCoordinate& AValue2)
206 {
207  return AValue1 + EPSILON < AValue2;
208 }
209 
217 inline bool areEqual(const TCoordinate& AValue1, const TCoordinate& AValue2)
218 {
219  return fabs(AValue1 - AValue2) < EPSILON;
220 }
221 
228 inline TCoordinate deg(const TCoordinate& AAngle)
229 {
230  return AAngle/M_PI*180.0;
231 }
232 
239 inline TCoordinate rad(const TCoordinate& AAngle)
240 {
241  return AAngle*M_PI/180.0;
242 }
243 
250 inline TCoordinate dSin(const TCoordinate& AAngle)
251 {
252  return sin(rad(AAngle));
253 }
254 
261 inline TCoordinate dCos(const TCoordinate& AAngle)
262 {
263  return cos(rad(AAngle));
264 }
265 
272 inline TCoordinate dTan(const TCoordinate& AAngle)
273 {
274  return tan(rad(AAngle));
275 }
276 
283 inline TCoordinate dAsin(const TCoordinate& AValue)
284 {
285  return deg(asin(AValue));
286 }
287 
294 inline TCoordinate dAcos(const TCoordinate& AValue)
295 {
296  return deg(acos(AValue));
297 }
298 
305 inline TCoordinate dAtan(const TCoordinate& AValue)
306 {
307  return deg(atan(AValue));
308 }
309 
316 inline TCoordinate limit(const TCoordinate& AAngle)
317 {
318  TCoordinate angle = AAngle;
319 
320  if (angle<0)
321  while (angle<=-180)
322  angle += 360;
323  else
324  while (angle> +180)
325  angle -= 360;
326 
327  return angle;
328 }
329 
336 inline TCoordinate positifAngle(const TCoordinate& AAngle)
337 {
338  TCoordinate angle = AAngle;
339 
340  if ( angle<0 )
341  {
342  do { angle += 360; } while (angle<0);
343  }
344  else
345  while (angle>=360 )
346  angle -= 360;
347 
348  return angle;
349 }
350 
358 inline TCoordinate angle(const TCoordinate& Ax, const TCoordinate& Ay)
359 {
360  if (isZero(Ax))
361  {
362  if (isZero(Ay))
363  return 0;
364 
365  if (Ay<0)
366  return -90;
367  else
368  return +90;
369  }
370 
371  if (Ax>0)
372  return dAtan(Ay/Ax);
373  else
374  return dAtan(Ay/Ax)+180.0;
375 }
376 
384 TCoordinate det(const CVertex& ALine1, const CVertex& ALine2,
385  const CVertex& ALine3);
386 
392 inline int pgcd(int Ax, int Ay)
393 {
394  assert(0 <= Ax);
395  assert(0 <= Ay);
396 
397  if (Ax < Ay)
398  {
399  int temp = Ax;
400  Ax = Ay;
401  Ay = temp;
402  }
403 
404  while (Ay != 0)
405  {
406  int temp = Ax % Ay;
407  Ax = Ay;
408  Ay = temp;
409  }
410 
411  return Ax;
412 }
413 
414 // @}
415 //******************************************************************************
416 #endif // MATH_EXTENSION_HH
417 //******************************************************************************