qwt_math.h 3.29 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
2
3
4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6
7
8
9
10
11
12
13
14
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_MATH_H
#define QWT_MATH_H

#include "qwt_global.h"

Bryant's avatar
Bryant committed
15
16
17
#if defined(_MSC_VER)
/*
  Microsoft says:
pixhawk's avatar
pixhawk committed
18

Bryant's avatar
Bryant committed
19
20
21
22
23
  Define _USE_MATH_DEFINES before including math.h to expose these macro
  definitions for common math constants.  These are placed under an #ifdef
  since these commonly-defined names are not part of the C/C++ standards.
*/
#define _USE_MATH_DEFINES 1
pixhawk's avatar
pixhawk committed
24
25
#endif

Bryant's avatar
Bryant committed
26
27
#include <qmath.h>
#include "qwt_global.h"
pixhawk's avatar
pixhawk committed
28

Bryant's avatar
Bryant committed
29
30
31
32
#ifndef M_PI_2
// For Qt <= 4.8.4 M_PI_2 is not known by MinGW-w64 
// when compiling with -std=c++11
#define M_PI_2 (1.57079632679489661923)
pixhawk's avatar
pixhawk committed
33
34
35
#endif

#ifndef LOG_MIN
Bryant's avatar
Bryant committed
36
//! Minimum value for logarithmic scales
pixhawk's avatar
pixhawk committed
37
38
39
40
41
42
43
44
#define LOG_MIN 1.0e-100
#endif

#ifndef LOG_MAX
//! Maximum value for logarithmic scales
#define LOG_MAX 1.0e100
#endif

Bryant's avatar
Bryant committed
45
46
QWT_EXPORT double qwtGetMin( const double *array, int size );
QWT_EXPORT double qwtGetMax( const double *array, int size );
pixhawk's avatar
pixhawk committed
47

Bryant's avatar
Bryant committed
48
49
QWT_EXPORT double qwtNormalizeRadians( double radians );
QWT_EXPORT double qwtNormalizeDegrees( double degrees );
pixhawk's avatar
pixhawk committed
50

Bryant's avatar
Bryant committed
51
52
/*!
  \brief Compare 2 values, relative to an interval
pixhawk's avatar
pixhawk committed
53

Bryant's avatar
Bryant committed
54
55
  Values are "equal", when :
  \f$\cdot value2 - value1 <= abs(intervalSize * 10e^{-6})\f$
pixhawk's avatar
pixhawk committed
56

Bryant's avatar
Bryant committed
57
58
59
  \param value1 First value to compare
  \param value2 Second value to compare
  \param intervalSize interval size
pixhawk's avatar
pixhawk committed
60

Bryant's avatar
Bryant committed
61
62
63
64
65
  \return 0: if equal, -1: if value2 > value1, 1: if value1 > value2
*/
inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
{
    const double eps = qAbs( 1.0e-6 * intervalSize );
pixhawk's avatar
pixhawk committed
66

Bryant's avatar
Bryant committed
67
68
    if ( value2 - value1 > eps )
        return -1;
pixhawk's avatar
pixhawk committed
69

Bryant's avatar
Bryant committed
70
71
    if ( value1 - value2 > eps )
        return 1;
pixhawk's avatar
pixhawk committed
72

Bryant's avatar
Bryant committed
73
74
    return 0;
}
pixhawk's avatar
pixhawk committed
75
76


Bryant's avatar
Bryant committed
77
78
79
80
inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
{
    return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
}
pixhawk's avatar
pixhawk committed
81

Bryant's avatar
Bryant committed
82
83
84
85
inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
{
    return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
}
pixhawk's avatar
pixhawk committed
86

87
//! Return the sign
Bryant's avatar
Bryant committed
88
inline int qwtSign( double x )
pixhawk's avatar
pixhawk committed
89
{
Bryant's avatar
Bryant committed
90
    if ( x > 0.0 )
91
        return 1;
Bryant's avatar
Bryant committed
92
93
    else if ( x < 0.0 )
        return ( -1 );
pixhawk's avatar
pixhawk committed
94
    else
95
96
        return 0;
}
pixhawk's avatar
pixhawk committed
97
98

//! Return the square of a number
Bryant's avatar
Bryant committed
99
inline double qwtSqr( double x )
pixhawk's avatar
pixhawk committed
100
{
Bryant's avatar
Bryant committed
101
    return x * x;
pixhawk's avatar
pixhawk committed
102
103
}

Bryant's avatar
Bryant committed
104
105
//! Approximation of arc tangent ( error below 0,005 radians )
inline double qwtFastAtan( double x )
pixhawk's avatar
pixhawk committed
106
{
Bryant's avatar
Bryant committed
107
108
    if ( x < -1.0 )
        return -M_PI_2 - x / ( x * x + 0.28 );
109

Bryant's avatar
Bryant committed
110
111
    if ( x > 1.0 )
        return M_PI_2 - x / ( x * x + 0.28 );
pixhawk's avatar
pixhawk committed
112

Bryant's avatar
Bryant committed
113
    return x / ( 1.0 + x * x * 0.28 );
pixhawk's avatar
pixhawk committed
114
115
}

Bryant's avatar
Bryant committed
116
117
//! Approximation of arc tangent ( error below 0,005 radians )
inline double qwtFastAtan2( double y, double x )
pixhawk's avatar
pixhawk committed
118
{
Bryant's avatar
Bryant committed
119
120
    if ( x > 0 )
        return qwtFastAtan( y / x );
pixhawk's avatar
pixhawk committed
121

Bryant's avatar
Bryant committed
122
123
124
125
126
    if ( x < 0 )
    {
        const double d = qwtFastAtan( y / x );
        return ( y >= 0 ) ? d + M_PI : d - M_PI;
    }
pixhawk's avatar
pixhawk committed
127

Bryant's avatar
Bryant committed
128
129
    if ( y < 0.0 )
        return -M_PI_2;
pixhawk's avatar
pixhawk committed
130

Bryant's avatar
Bryant committed
131
132
    if ( y > 0.0 )
        return M_PI_2;
pixhawk's avatar
pixhawk committed
133

Bryant's avatar
Bryant committed
134
    return 0.0;
pixhawk's avatar
pixhawk committed
135
136
}

Bryant's avatar
Bryant committed
137
138
// Translate degrees into radians
inline double qwtRadians( double degrees )
pixhawk's avatar
pixhawk committed
139
{
Bryant's avatar
Bryant committed
140
    return degrees * M_PI / 180.0;
pixhawk's avatar
pixhawk committed
141
142
}

Bryant's avatar
Bryant committed
143
144
// Translate radians into degrees
inline double qwtDegrees( double degrees )
pixhawk's avatar
pixhawk committed
145
{
Bryant's avatar
Bryant committed
146
    return degrees * 180.0 / M_PI;
pixhawk's avatar
pixhawk committed
147
148
149
}

#endif