Subversion Repositories public iLand

Rev

Rev 615 | Rev 671 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 615 Rev 639
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/global.h':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/global.h':
2
#ifndef GLOBAL_H
2
#ifndef GLOBAL_H
3
#define GLOBAL_H
3
#define GLOBAL_H
4
4
5
#define MSGRETURN(x) { qDebug() << x; return; }
5
#define MSGRETURN(x) { qDebug() << x; return; }
6
#define WARNINGRETURN(x) { qWarning() << x; return; }
6
#define WARNINGRETURN(x) { qWarning() << x; return; }
7
#define ERRORRETURN(x) { qError() << x; return; }
7
#define ERRORRETURN(x) { qError() << x; return; }
8
// conversions rad/degree
8
// conversions rad/degree
9
#define RAD(x) (x*M_PI/180.)
9
#define RAD(x) (x*M_PI/180.)
10
#define GRAD(x) (x/M_PI*180.)
10
#define GRAD(x) (x/M_PI*180.)
11
#define PI2 2*M_PI
11
#define PI2 2*M_PI
12
12
13
13
14
#include "globalsettings.h"
14
#include "globalsettings.h"
15
#include "exception.h"
15
#include "exception.h"
16
// general datatypes
16
// general datatypes
17
//typedef int TreeSpecies;
17
//typedef int TreeSpecies;
18
18
19
// global debug helpers (used by macros!)
19
// global debug helpers (used by macros!)
20
void dbg_helper(const char *where, const char *what,const char* file,int line);
20
void dbg_helper(const char *where, const char *what,const char* file,int line);
21
void dbg_helper_ext(const char *where, const char *what,const char* file,int line, const QString &s);
21
void dbg_helper_ext(const char *where, const char *what,const char* file,int line, const QString &s);
22
22
23
// change to enabled detailed debug messages.
23
// change to enabled detailed debug messages.
24
// if NO_DEBUG_MSGS is defined, NO debug outputs are generated.
24
// if NO_DEBUG_MSGS is defined, NO debug outputs are generated.
25
#if defined(QT_NO_DEBUG)
25
#if defined(QT_NO_DEBUG)
26
#define NO_DEBUG_MSGS
26
#define NO_DEBUG_MSGS
27
#endif
27
#endif
28
28
29
#if !defined(DBG_IF)
29
#if !defined(DBG_IF)
30
#  ifndef NO_DEBUG_MSGS
30
#  ifndef NO_DEBUG_MSGS
31
#    define DBG_IF(cond, where, what) ((cond) ? dbg_helper(where, what, __FILE__, __LINE__) : qt_noop())
31
#    define DBG_IF(cond, where, what) ((cond) ? dbg_helper(where, what, __FILE__, __LINE__) : qt_noop())
32
#  else
32
#  else
33
#    define DBG_IF(cond, where, what) qt_noop()
33
#    define DBG_IF(cond, where, what) qt_noop()
34
#  endif
34
#  endif
35
#endif
35
#endif
36
36
37
#if !defined(DBG_IF_X)
37
#if !defined(DBG_IF_X)
38
#  ifndef NO_DEBUG_MSGS
38
#  ifndef NO_DEBUG_MSGS
39
#    define DBG_IF_X(cond, where, what,more) ((cond) ? dbg_helper_ext(where, what, __FILE__, __LINE__,more) : qt_noop())
39
#    define DBG_IF_X(cond, where, what,more) ((cond) ? dbg_helper_ext(where, what, __FILE__, __LINE__,more) : qt_noop())
40
#  else
40
#  else
41
#    define DBG_IF_X(cond, where, what,more) qt_noop()
41
#    define DBG_IF_X(cond, where, what,more) qt_noop()
42
#  endif
42
#  endif
43
#endif
43
#endif
44
44
45
#if !defined(DBGMODE)
45
#if !defined(DBGMODE)
46
#  ifndef NO_DEBUG_MSGS
46
#  ifndef NO_DEBUG_MSGS
47
#    define DBGMODE(stmts) { stmts }
47
#    define DBGMODE(stmts) { stmts }
48
#  else
48
#  else
49
#    define DBGMODE(stmts) qt_noop()
49
#    define DBGMODE(stmts) qt_noop()
50
#  endif
50
#  endif
51
#endif
51
#endif
52
52
53
// log level functions
53
// log level functions
54
bool logLevelDebug(); // true, if detailed debug information is logged
54
bool logLevelDebug(); // true, if detailed debug information is logged
55
bool logLevelInfo(); // true, if only important aggreate info is logged
55
bool logLevelInfo(); // true, if only important aggreate info is logged
56
bool logLevelWarning(); // true if only severe warnings/errors are logged.
56
bool logLevelWarning(); // true if only severe warnings/errors are logged.
57
void setLogLevel(int loglevel); // setter function
57
void setLogLevel(int loglevel); // setter function
58
// cool random number generator (using the mersenne-twister) by http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
58
// cool random number generator (using the mersenne-twister) by http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
59
#include "../3rdparty/MersenneTwister.h"
59
#include "../3rdparty/MersenneTwister.h"
60
// access the Mersenne-Twister-Random-Numbers
60
// access the Mersenne-Twister-Random-Numbers
61
MTRand &mtRand(); // static object lives in globalsettings
61
MTRand &mtRand(); // static object lives in globalsettings
62
/// nrandom returns a random number from [p1, p2] -> p2 is a possible result!
62
/// nrandom returns a random number from [p1, p2] -> p2 is a possible result!
63
inline double nrandom(const double& p1, const double& p2)
63
inline double nrandom(const double& p1, const double& p2)
64
{
64
{
65
    return p1 + mtRand().rand(p2-p1);
65
    return p1 + mtRand().rand(p2-p1);
66
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
66
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
67
}
67
}
68
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
68
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
69
inline double drandom()
69
inline double drandom()
70
{
70
{
71
    return mtRand().rand();
71
    return mtRand().rand();
72
    //return rand()/double(RAND_MAX);
72
    //return rand()/double(RAND_MAX);
73
}
73
}
74
/// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5.
74
/// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5.
75
inline int irandom(int from, int to)
75
inline int irandom(int from, int to)
76
{
76
{
77
    return from + mtRand().randInt(to-from);
77
    return from + mtRand().randInt(to-from);
78
    //return from +  rand()%(to-from);
78
    //return from +  rand()%(to-from);
79
}
79
}
80
80
81
// random number function with additional ptr to random function
81
// random number function with additional ptr to random function
82
inline double nrandom(MTRand &random, const double& p1, const double& p2)
82
inline double nrandom(MTRand &random, const double& p1, const double& p2)
83
{
83
{
84
    return p1 + random.rand(p2-p1);
84
    return p1 + random.rand(p2-p1);
85
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
85
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
86
}
86
}
87
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
87
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
88
inline double drandom(MTRand &random)
88
inline double drandom(MTRand &random)
89
{
89
{
90
    return random.rand();
90
    return random.rand();
91
    //return rand()/double(RAND_MAX);
91
    //return rand()/double(RAND_MAX);
92
}
92
}
93
/// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5.
93
/// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5.
94
inline int irandom(MTRand &random, int from, int to)
94
inline int irandom(MTRand &random, int from, int to)
95
{
95
{
96
    return from + random.randInt(to-from);
96
    return from + random.randInt(to-from);
97
    //return from +  rand()%(to-from);
97
    //return from +  rand()%(to-from);
98
}
98
}
99
99
100
100
101
101
102
inline double limit(const double value, const double lower, const double upper)
102
inline double limit(const double value, const double lower, const double upper)
103
{
103
{
104
    return qMax(qMin(value, upper), lower);
104
    return qMax(qMin(value, upper), lower);
105
}
105
}
106
inline int limit(const int value, const int lower, const int upper)
106
inline int limit(const int value, const int lower, const int upper)
107
{
107
{
108
    return qMax(qMin(value, upper), lower);
108
    return qMax(qMin(value, upper), lower);
109
}
109
}
110
inline void setBit(int &rTarget, const int bit, const bool value)
110
inline void setBit(int &rTarget, const int bit, const bool value)
111
{
111
{
112
    if (value)
112
    if (value)
113
        rTarget |= (1 << bit);  // set bit
113
        rTarget |= (1 << bit);  // set bit
114
    else
114
    else
115
        rTarget &= ( (1 << bit) ^ 0xffffff ); // clear bit
115
        rTarget &= ( (1 << bit) ^ 0xffffff ); // clear bit
116
}
116
}
117
inline bool isBitSet(const int value, const int bit)
117
inline bool isBitSet(const int value, const int bit)
118
{
118
{
119
    return value & (1 << bit);
119
    return value & (1 << bit);
120
}
120
}
121
121
122
// define a global isnan() function
122
// define a global isnan() function
123
#define isnan(x) ((x) != (x))
123
#define isnan(x) ((x) != (x))
124
124
125
#endif // GLOBAL_H
125
#endif // GLOBAL_H
126
 
126