Rev 671 | Rev 706 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | |||
671 | werner | 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 | |||
24 | Werner | 21 | #ifndef GLOBAL_H |
22 | #define GLOBAL_H |
||
23 | |||
24 | #define MSGRETURN(x) { qDebug() << x; return; } |
||
87 | Werner | 25 | #define WARNINGRETURN(x) { qWarning() << x; return; } |
91 | Werner | 26 | #define ERRORRETURN(x) { qError() << x; return; } |
24 | Werner | 27 | // conversions rad/degree |
28 | #define RAD(x) (x*M_PI/180.) |
||
29 | #define GRAD(x) (x/M_PI*180.) |
||
32 | Werner | 30 | #define PI2 2*M_PI |
33 | Werner | 31 | |
32 | |||
102 | Werner | 33 | #include "exception.h" |
33 | Werner | 34 | // general datatypes |
39 | Werner | 35 | //typedef int TreeSpecies; |
117 | Werner | 36 | |
37 | // global debug helpers (used by macros!) |
||
38 | void dbg_helper(const char *where, const char *what,const char* file,int line); |
||
39 | void dbg_helper_ext(const char *where, const char *what,const char* file,int line, const QString &s); |
||
40 | |||
216 | werner | 41 | // change to enabled detailed debug messages. |
42 | // if NO_DEBUG_MSGS is defined, NO debug outputs are generated. |
||
43 | #if defined(QT_NO_DEBUG) |
||
44 | #define NO_DEBUG_MSGS |
||
45 | #endif |
||
46 | |||
117 | Werner | 47 | #if !defined(DBG_IF) |
216 | werner | 48 | # ifndef NO_DEBUG_MSGS |
117 | Werner | 49 | # define DBG_IF(cond, where, what) ((cond) ? dbg_helper(where, what, __FILE__, __LINE__) : qt_noop()) |
50 | # else |
||
51 | # define DBG_IF(cond, where, what) qt_noop() |
||
52 | # endif |
||
53 | #endif |
||
54 | |||
55 | #if !defined(DBG_IF_X) |
||
216 | werner | 56 | # ifndef NO_DEBUG_MSGS |
117 | Werner | 57 | # define DBG_IF_X(cond, where, what,more) ((cond) ? dbg_helper_ext(where, what, __FILE__, __LINE__,more) : qt_noop()) |
58 | # else |
||
59 | # define DBG_IF_X(cond, where, what,more) qt_noop() |
||
60 | # endif |
||
61 | #endif |
||
62 | |||
130 | Werner | 63 | #if !defined(DBGMODE) |
216 | werner | 64 | # ifndef NO_DEBUG_MSGS |
130 | Werner | 65 | # define DBGMODE(stmts) { stmts } |
66 | # else |
||
67 | # define DBGMODE(stmts) qt_noop() |
||
68 | # endif |
||
69 | #endif |
||
70 | |||
431 | werner | 71 | // log level functions |
72 | bool logLevelDebug(); // true, if detailed debug information is logged |
||
73 | bool logLevelInfo(); // true, if only important aggreate info is logged |
||
74 | bool logLevelWarning(); // true if only severe warnings/errors are logged. |
||
75 | void setLogLevel(int loglevel); // setter function |
||
705 | werner | 76 | // choose the random number generator: |
77 | // either: |
||
289 | werner | 78 | // cool random number generator (using the mersenne-twister) by http://www-personal.umich.edu/~wagnerr/MersenneTwister.html |
705 | werner | 79 | // #include "../3rdparty/MersenneTwister.h" |
80 | // or |
||
81 | #include "randomwell.h" |
||
82 | |||
83 | MTRand *randomGenerator(); // static object lives in globalsettings |
||
349 | werner | 84 | /// nrandom returns a random number from [p1, p2] -> p2 is a possible result! |
157 | werner | 85 | inline double nrandom(const double& p1, const double& p2) |
86 | { |
||
705 | werner | 87 | return p1 + randomGenerator()->rand(p2-p1); |
289 | werner | 88 | //return p1 + (p2-p1)*(rand()/double(RAND_MAX)); |
157 | werner | 89 | } |
349 | werner | 90 | /// returns a random number in [0,1] (i.e.="1" is a possible result!) |
187 | iland | 91 | inline double drandom() |
157 | werner | 92 | { |
705 | werner | 93 | return randomGenerator()->rand(); |
289 | werner | 94 | //return rand()/double(RAND_MAX); |
157 | werner | 95 | } |
349 | werner | 96 | /// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5. |
187 | iland | 97 | inline int irandom(int from, int to) |
186 | werner | 98 | { |
705 | werner | 99 | return from + randomGenerator()->randInt(to-from); |
289 | werner | 100 | //return from + rand()%(to-from); |
186 | werner | 101 | } |
210 | werner | 102 | |
443 | werner | 103 | // random number function with additional ptr to random function |
705 | werner | 104 | inline double nrandom(MTRand *random, const double& p1, const double& p2) |
443 | werner | 105 | { |
705 | werner | 106 | return p1 + random->rand(p2-p1); |
443 | werner | 107 | //return p1 + (p2-p1)*(rand()/double(RAND_MAX)); |
108 | } |
||
109 | /// returns a random number in [0,1] (i.e.="1" is a possible result!) |
||
705 | werner | 110 | inline double drandom(MTRand *random) |
443 | werner | 111 | { |
705 | werner | 112 | return random->rand(); |
443 | werner | 113 | //return rand()/double(RAND_MAX); |
114 | } |
||
115 | /// return a random number from "from" to "to" (incl.), i.e. irandom(3,5) results in 3, 4 or 5. |
||
705 | werner | 116 | inline int irandom(MTRand *random, int from, int to) |
443 | werner | 117 | { |
705 | werner | 118 | return from + random->randInt(to-from); |
443 | werner | 119 | //return from + rand()%(to-from); |
120 | } |
||
121 | |||
122 | |||
123 | |||
210 | werner | 124 | inline double limit(const double value, const double lower, const double upper) |
125 | { |
||
126 | return qMax(qMin(value, upper), lower); |
||
127 | } |
||
216 | werner | 128 | inline int limit(const int value, const int lower, const int upper) |
129 | { |
||
130 | return qMax(qMin(value, upper), lower); |
||
131 | } |
||
285 | werner | 132 | inline void setBit(int &rTarget, const int bit, const bool value) |
133 | { |
||
134 | if (value) |
||
135 | rTarget |= (1 << bit); // set bit |
||
136 | else |
||
137 | rTarget &= ( (1 << bit) ^ 0xffffff ); // clear bit |
||
138 | } |
||
287 | werner | 139 | inline bool isBitSet(const int value, const int bit) |
140 | { |
||
141 | return value & (1 << bit); |
||
142 | } |
||
615 | werner | 143 | |
144 | // define a global isnan() function |
||
145 | #define isnan(x) ((x) != (x)) |
||
146 | |||
705 | werner | 147 | #include "globalsettings.h" |
148 | |||
24 | Werner | 149 | #endif // GLOBAL_H |