00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef VERVE_GLOBALS_H
00025 #define VERVE_GLOBALS_H
00026
00027 #include "Defines.h"
00028
00029 class TiXmlNode;
00030
00031 namespace verve
00032 {
00034 namespace globals
00035 {
00037 inline real abs(real value)
00038 {
00039 #ifdef VERVE_USE_DOUBLE
00040 return ::fabs(value);
00041 #else
00042 return ::fabsf(value);
00043 #endif
00044 }
00045
00047 inline real sqrt(real value)
00048 {
00049 #ifdef VERVE_USE_DOUBLE
00050 return ::sqrt(value);
00051 #else
00052 return ::sqrtf(value);
00053 #endif
00054 }
00055
00057 inline real exp(real exponent)
00058 {
00059 #ifdef VERVE_USE_DOUBLE
00060 return ::exp(exponent);
00061 #else
00062 return ::expf(exponent);
00063 #endif
00064 }
00065
00067 inline real pow(real base, real exponent)
00068 {
00069 #ifdef VERVE_USE_DOUBLE
00070 return ::pow(base, exponent);
00071 #else
00072 return ::powf(base, exponent);
00073 #endif
00074 }
00075
00077 inline int roundToInt(real value)
00078 {
00079 int nearestInt = (int)value;
00080 real fraction = value - (real)nearestInt;
00081 if (value < 0)
00082 {
00083 if (fraction <= (real)-0.5)
00084 {
00085 --nearestInt;
00086 }
00087 }
00088 else
00089 {
00090 if (fraction >= (real)0.5)
00091 {
00092 ++nearestInt;
00093 }
00094 }
00095
00096 return nearestInt;
00097 }
00098
00102 inline real checkParamValid(real value, real rangeMin,
00103 real rangeMax, const std::string& callingFunction)
00104 {
00105 const real initialValue = value;
00106 bool invalid = false;
00107
00108 if (value < rangeMin)
00109 {
00110 value = rangeMin;
00111 invalid = true;
00112 }
00113 else if (value > rangeMax)
00114 {
00115 value = rangeMax;
00116 invalid = true;
00117 }
00118
00119 if (invalid)
00120 {
00121 VERVE_LOGGER("warning") << callingFunction << ": "
00122 << "Actual value " << initialValue << " should be "
00123 << "within [" << rangeMin << ", " << rangeMax << "]. "
00124 << "The value will be clamped." << std::endl;
00125 }
00126
00127 return value;
00128 }
00129
00146 inline real calcDecayConstant(real decayPercent, real decayTime,
00147 real dt)
00148 {
00149
00150
00151 assert(decayPercent <= 1);
00152
00153
00154
00155
00156
00157
00158 if (1 == decayPercent || 0 == decayTime)
00159 {
00160 return 0;
00161 }
00162
00163 return globals::pow(1 - decayPercent, dt / decayTime);
00164 }
00165
00168 inline real calcDecayConstant(real timeConstant, real dt)
00169 {
00170 return calcDecayConstant(1 - VERVE_E, -timeConstant, dt);
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00182 inline bool randomBool()
00183 {
00184 if (rand() % 2 == 0)
00185 {
00186 return true;
00187 }
00188 else
00189 {
00190 return false;
00191 }
00192 }
00193
00196 inline int randomIntUniform(int min, int max)
00197 {
00198 return int((max - min + 1) * rand() / (RAND_MAX + 1.0)) + min;
00199 }
00200
00203 inline real randomRealUniform(real min, real max)
00204 {
00205 return (real)rand() / RAND_MAX * (max - min) + min;
00206 }
00207
00212 inline real randomRealGauss(real variance)
00213 {
00214 static int iset = 0;
00215 static real gset;
00216 real fac, rsq, v1, v2;
00217
00218 if (0 == iset)
00219 {
00220 do
00221 {
00222 static const real invDenominator =
00223 1 / (RAND_MAX + (real)1.0);
00224 v1 = (real)2.0 * (rand() * invDenominator) - (real)1.0;
00225 v2 = (real)2.0 * (rand() * invDenominator) - (real)1.0;
00226 rsq = v1 * v1 + v2 * v2;
00227 }
00228 while (rsq >= 1.0 || rsq == 0.0);
00229
00230 fac = globals::sqrt((real)-2.0 * log(rsq) / rsq);
00231 gset = v1 * fac;
00232 iset = 1;
00233 return v2 * fac * variance;
00234 }
00235 else
00236 {
00237 iset = 0;
00238 return gset * variance;
00239 }
00240 }
00241
00245 inline real scaleZeroToOne(real value, real rangeMin,
00246 real rangeMax)
00247 {
00248 assert(rangeMax >= rangeMin);
00249
00250 if (rangeMax == rangeMin)
00251 {
00252 return 0;
00253 }
00254
00255 if (value >= rangeMax)
00256 {
00257 return 1;
00258 }
00259
00260 if (value <= rangeMin)
00261 {
00262 return 0;
00263 }
00264
00265
00266 real range = rangeMax - rangeMin;
00267
00268
00269
00270 value -= rangeMin;
00271
00272
00273 value = value / range;
00274
00275 return value;
00276 }
00277
00281 inline real scaleNegOneToOne(real value, real rangeMin,
00282 real rangeMax)
00283 {
00284 if (rangeMax == rangeMin)
00285 {
00286 return 0;
00287 }
00288
00289 value = scaleZeroToOne(value, rangeMin, rangeMax);
00290
00291
00292 value = (2 * value) - 1;
00293
00294 return value;
00295 }
00296
00300 real VERVE_CALL getAttributeReal(const TiXmlNode* nodePtr,
00301 const std::string& name);
00302
00306 int VERVE_CALL getAttributeInt(const TiXmlNode* nodePtr,
00307 const std::string& name);
00308
00313 bool VERVE_CALL getAttributeBool(const TiXmlNode* nodePtr,
00314 const std::string& name);
00315
00319 std::string VERVE_CALL getAttributeString(const TiXmlNode* nodePtr,
00320 const std::string& name);
00321 }
00322 }
00323
00324 #endif