Subversion Repositories public iLand

Rev

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

Rev 639 Rev 671
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
/********************************************************************************************
-
 
3
**    iLand - an individual based forest landscape and disturbance model
-
 
4
**    http://iland.boku.ac.at
-
 
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
-
 
6
**
-
 
7
**    This program is free software: you can redistribute it and/or modify
-
 
8
**    it under the terms of the GNU General Public License as published by
-
 
9
**    the Free Software Foundation, either version 3 of the License, or
-
 
10
**    (at your option) any later version.
-
 
11
**
-
 
12
**    This program is distributed in the hope that it will be useful,
-
 
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
-
 
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-
 
15
**    GNU General Public License for more details.
-
 
16
**
-
 
17
**    You should have received a copy of the GNU General Public License
-
 
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
 
19
********************************************************************************************/
-
 
20
2
#ifndef GLOBAL_H
21
#ifndef GLOBAL_H
3
#define GLOBAL_H
22
#define GLOBAL_H
4
23
5
#define MSGRETURN(x) { qDebug() << x; return; }
24
#define MSGRETURN(x) { qDebug() << x; return; }
6
#define WARNINGRETURN(x) { qWarning() << x; return; }
25
#define WARNINGRETURN(x) { qWarning() << x; return; }
7
#define ERRORRETURN(x) { qError() << x; return; }
26
#define ERRORRETURN(x) { qError() << x; return; }
8
// conversions rad/degree
27
// conversions rad/degree
9
#define RAD(x) (x*M_PI/180.)
28
#define RAD(x) (x*M_PI/180.)
10
#define GRAD(x) (x/M_PI*180.)
29
#define GRAD(x) (x/M_PI*180.)
11
#define PI2 2*M_PI
30
#define PI2 2*M_PI
12
31
13
32
14
#include "globalsettings.h"
33
#include "globalsettings.h"
15
#include "exception.h"
34
#include "exception.h"
16
// general datatypes
35
// general datatypes
17
//typedef int TreeSpecies;
36
//typedef int TreeSpecies;
18
37
19
// global debug helpers (used by macros!)
38
// global debug helpers (used by macros!)
20
void dbg_helper(const char *where, const char *what,const char* file,int line);
39
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);
40
void dbg_helper_ext(const char *where, const char *what,const char* file,int line, const QString &s);
22
41
23
// change to enabled detailed debug messages.
42
// change to enabled detailed debug messages.
24
// if NO_DEBUG_MSGS is defined, NO debug outputs are generated.
43
// if NO_DEBUG_MSGS is defined, NO debug outputs are generated.
25
#if defined(QT_NO_DEBUG)
44
#if defined(QT_NO_DEBUG)
26
#define NO_DEBUG_MSGS
45
#define NO_DEBUG_MSGS
27
#endif
46
#endif
28
47
29
#if !defined(DBG_IF)
48
#if !defined(DBG_IF)
30
#  ifndef NO_DEBUG_MSGS
49
#  ifndef NO_DEBUG_MSGS
31
#    define DBG_IF(cond, where, what) ((cond) ? dbg_helper(where, what, __FILE__, __LINE__) : qt_noop())
50
#    define DBG_IF(cond, where, what) ((cond) ? dbg_helper(where, what, __FILE__, __LINE__) : qt_noop())
32
#  else
51
#  else
33
#    define DBG_IF(cond, where, what) qt_noop()
52
#    define DBG_IF(cond, where, what) qt_noop()
34
#  endif
53
#  endif
35
#endif
54
#endif
36
55
37
#if !defined(DBG_IF_X)
56
#if !defined(DBG_IF_X)
38
#  ifndef NO_DEBUG_MSGS
57
#  ifndef NO_DEBUG_MSGS
39
#    define DBG_IF_X(cond, where, what,more) ((cond) ? dbg_helper_ext(where, what, __FILE__, __LINE__,more) : qt_noop())
58
#    define DBG_IF_X(cond, where, what,more) ((cond) ? dbg_helper_ext(where, what, __FILE__, __LINE__,more) : qt_noop())
40
#  else
59
#  else
41
#    define DBG_IF_X(cond, where, what,more) qt_noop()
60
#    define DBG_IF_X(cond, where, what,more) qt_noop()
42
#  endif
61
#  endif
43
#endif
62
#endif
44
63
45
#if !defined(DBGMODE)
64
#if !defined(DBGMODE)
46
#  ifndef NO_DEBUG_MSGS
65
#  ifndef NO_DEBUG_MSGS
47
#    define DBGMODE(stmts) { stmts }
66
#    define DBGMODE(stmts) { stmts }
48
#  else
67
#  else
49
#    define DBGMODE(stmts) qt_noop()
68
#    define DBGMODE(stmts) qt_noop()
50
#  endif
69
#  endif
51
#endif
70
#endif
52
71
53
// log level functions
72
// log level functions
54
bool logLevelDebug(); // true, if detailed debug information is logged
73
bool logLevelDebug(); // true, if detailed debug information is logged
55
bool logLevelInfo(); // true, if only important aggreate info is logged
74
bool logLevelInfo(); // true, if only important aggreate info is logged
56
bool logLevelWarning(); // true if only severe warnings/errors are logged.
75
bool logLevelWarning(); // true if only severe warnings/errors are logged.
57
void setLogLevel(int loglevel); // setter function
76
void setLogLevel(int loglevel); // setter function
58
// cool random number generator (using the mersenne-twister) by http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
77
// cool random number generator (using the mersenne-twister) by http://www-personal.umich.edu/~wagnerr/MersenneTwister.html
59
#include "../3rdparty/MersenneTwister.h"
78
#include "../3rdparty/MersenneTwister.h"
60
// access the Mersenne-Twister-Random-Numbers
79
// access the Mersenne-Twister-Random-Numbers
61
MTRand &mtRand(); // static object lives in globalsettings
80
MTRand &mtRand(); // static object lives in globalsettings
62
/// nrandom returns a random number from [p1, p2] -> p2 is a possible result!
81
/// nrandom returns a random number from [p1, p2] -> p2 is a possible result!
63
inline double nrandom(const double& p1, const double& p2)
82
inline double nrandom(const double& p1, const double& p2)
64
{
83
{
65
    return p1 + mtRand().rand(p2-p1);
84
    return p1 + mtRand().rand(p2-p1);
66
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
85
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
67
}
86
}
68
/// 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!)
69
inline double drandom()
88
inline double drandom()
70
{
89
{
71
    return mtRand().rand();
90
    return mtRand().rand();
72
    //return rand()/double(RAND_MAX);
91
    //return rand()/double(RAND_MAX);
73
}
92
}
74
/// 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.
75
inline int irandom(int from, int to)
94
inline int irandom(int from, int to)
76
{
95
{
77
    return from + mtRand().randInt(to-from);
96
    return from + mtRand().randInt(to-from);
78
    //return from +  rand()%(to-from);
97
    //return from +  rand()%(to-from);
79
}
98
}
80
99
81
// random number function with additional ptr to random function
100
// random number function with additional ptr to random function
82
inline double nrandom(MTRand &random, const double& p1, const double& p2)
101
inline double nrandom(MTRand &random, const double& p1, const double& p2)
83
{
102
{
84
    return p1 + random.rand(p2-p1);
103
    return p1 + random.rand(p2-p1);
85
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
104
    //return p1 + (p2-p1)*(rand()/double(RAND_MAX));
86
}
105
}
87
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
106
/// returns a random number in [0,1] (i.e.="1" is a possible result!)
88
inline double drandom(MTRand &random)
107
inline double drandom(MTRand &random)
89
{
108
{
90
    return random.rand();
109
    return random.rand();
91
    //return rand()/double(RAND_MAX);
110
    //return rand()/double(RAND_MAX);
92
}
111
}
93
/// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5.
112
/// 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)
113
inline int irandom(MTRand &random, int from, int to)
95
{
114
{
96
    return from + random.randInt(to-from);
115
    return from + random.randInt(to-from);
97
    //return from +  rand()%(to-from);
116
    //return from +  rand()%(to-from);
98
}
117
}
99
118
100
119
101
120
102
inline double limit(const double value, const double lower, const double upper)
121
inline double limit(const double value, const double lower, const double upper)
103
{
122
{
104
    return qMax(qMin(value, upper), lower);
123
    return qMax(qMin(value, upper), lower);
105
}
124
}
106
inline int limit(const int value, const int lower, const int upper)
125
inline int limit(const int value, const int lower, const int upper)
107
{
126
{
108
    return qMax(qMin(value, upper), lower);
127
    return qMax(qMin(value, upper), lower);
109
}
128
}
110
inline void setBit(int &rTarget, const int bit, const bool value)
129
inline void setBit(int &rTarget, const int bit, const bool value)
111
{
130
{
112
    if (value)
131
    if (value)
113
        rTarget |= (1 << bit);  // set bit
132
        rTarget |= (1 << bit);  // set bit
114
    else
133
    else
115
        rTarget &= ( (1 << bit) ^ 0xffffff ); // clear bit
134
        rTarget &= ( (1 << bit) ^ 0xffffff ); // clear bit
116
}
135
}
117
inline bool isBitSet(const int value, const int bit)
136
inline bool isBitSet(const int value, const int bit)
118
{
137
{
119
    return value & (1 << bit);
138
    return value & (1 << bit);
120
}
139
}
121
140
122
// define a global isnan() function
141
// define a global isnan() function
123
#define isnan(x) ((x) != (x))
142
#define isnan(x) ((x) != (x))
124
143
125
#endif // GLOBAL_H
144
#endif // GLOBAL_H
126
 
145