Globals.h

Go to the documentation of this file.
00001 /************************************************************************
00002 * Verve                                                                 *
00003 * Copyright (C) 2004-2006                                               *
00004 * Tyler Streeter  tylerstreeter@gmail.com                               *
00005 * All rights reserved.                                                  *
00006 * Web: http://verve-agents.sourceforge.net                              *
00007 *                                                                       *
00008 * This library is free software; you can redistribute it and/or         *
00009 * modify it under the terms of EITHER:                                  *
00010 *   (1) The GNU Lesser General Public License as published by the Free  *
00011 *       Software Foundation; either version 2.1 of the License, or (at  *
00012 *       your option) any later version. The text of the GNU Lesser      *
00013 *       General Public License is included with this library in the     *
00014 *       file license-LGPL.txt.                                          *
00015 *   (2) The BSD-style license that is included with this library in     *
00016 *       the file license-BSD.txt.                                       *
00017 *                                                                       *
00018 * This library is distributed in the hope that it will be useful,       *
00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00021 * license-LGPL.txt and license-BSD.txt for more details.                *
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                         // 'decayPercent' values greater than 1 are bad because they 
00150                         // would present the pow function with a negative base.
00151                         assert(decayPercent <= 1);
00152 
00153                         // A 'decayPercent' value of 1 means that we are decaying the 
00154                         // value to 0 in one step, so we just return a constant of 0.  
00155                         // A 'decayTime' value of 0 gives us a divide by zero error 
00156                         // later; since a 'decayTime' of 0 actually means we are 
00157                         // decaying the value instantly, we just return a constant of 0.
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                 // Note: this decay method avoids using the pow function, but it 
00174                 // is too unstable when decayRate is 0 or >= 1
00175                 //inline real calcDecayConstant2(real decayRate, real dt)
00176                 //{
00177                 //      return (1 - dt/(1-decayRate));
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                         // Calculate the range, which will always be positive.
00266                         real range = rangeMax - rangeMin;
00267 
00268                         // Shift the value to be correctly positioned within the new 
00269                         // range.
00270                         value -= rangeMin;
00271 
00272                         // Normalize the value to be between 0 and 1.
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                         // Map the normalized value to [-1, 1].
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

Generated on Tue Jan 24 21:46:37 2006 for Verve by  doxygen 1.4.6-NO