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