src/GenUtils.cpp

Go to the documentation of this file.
00001 #include "GenUtils.h"
00002 
00003 /*******************************************************************************************/
00004 /*NamedObject                                                                              */
00005 /*                                                                                         */
00006 /*******************************************************************************************/
00007 
00008 NamedObject::NamedObject(const char*  argName)
00009 {
00010   if(argName)
00011     name =  argName;
00012 }
00013 
00014 NamedObject::NamedObject(const std::string &argName)
00015 {
00016   name = argName;
00017 }
00018 
00019 NamedObject::NamedObject(const NamedObject &copy)
00020 {
00021   if(this != &copy)
00022     name = copy.name;
00023 }
00024 
00025 NamedObject &NamedObject::operator=(const NamedObject &copy)
00026 {
00027   if(this != &copy)
00028     name = copy.name;
00029   return *this;
00030 }
00031 
00032 void NamedObject::setName(const char   *nameArg)
00033 {
00034   if(nameArg)
00035     name = nameArg;
00036   else
00037     name.clear();
00038 }
00039 
00040 void NamedObject::setName(const std::string &nameArg)
00041 {
00042   name = nameArg;
00043 }
00044 
00045 const std::string &NamedObject::getName() const
00046 {
00047   return name;
00048 }
00049 
00050 const char *NamedObject::getCharName() const
00051 {
00052   return  name.c_str();
00053 }
00054 
00055 NamedObject::~NamedObject()
00056 {
00057   name.clear();
00058 }
00059 
00060 /*******************************************************************************************/
00061 /*Logger                                                                                   */
00062 /*                                                                                         */
00063 /*******************************************************************************************/
00064 
00065 std::vector<std::string> Logger::logStrings;
00066 std::string              Logger::logPath;
00067 
00068 void Logger::initialize(const char* logfilename)
00069 {
00070   logPath     = !logfilename ? "Log.txt" : logfilename;
00071   std::ofstream logFile(logPath.c_str());
00072   logFile.close();
00073 }
00074 
00075 void Logger::flush()
00076 {
00077   if(!logPath.size() || !logStrings.size())
00078     return;
00079 
00080   std::ofstream logFile(logPath.c_str(), std::ios::app);
00081 
00082   for(size_t t = 0; t < logStrings.size(); t++)
00083     logFile << logStrings[t];
00084 
00085   logStrings.clear();
00086   logFile.close();
00087 }
00088 
00089 void Logger::writeImmidiateInfoLog(const std::string &info)
00090 {
00091   if(info.size())
00092   {
00093     logStrings.push_back(std::string("<+>") + info + "\n");
00094     flush();
00095   }
00096 }
00097 
00098 void Logger::writeInfoLog(const std::string &info)
00099 {
00100   logStrings.push_back(std::string("<+>") + info + "\n");
00101   if(logStrings.size() >= 10)
00102     flush();
00103 }
00104 
00105 bool Logger::writeErrorLog(const std::string &info)
00106 {
00107   if(info.size())
00108   {
00109     logStrings.push_back(std::string("<!>") + info + "\n");
00110     flush();
00111   }
00112   return false;
00113 }
00114 
00115 void Logger::writeFatalErrorLog(const std::string &info)
00116 {
00117   if(info.size())
00118   {
00119    logStrings.push_back(std::string("<X>") + info + "\n");
00120 
00121     flush();
00122   }
00123   exit(1);
00124 }
00125 
00126 /*******************************************************************************************/
00127 /*MediaPathManager                                                                         */
00128 /*                                                                                         */
00129 /*******************************************************************************************/
00130 
00131 std::vector<std::string> MediaPathManager::dataPaths;
00132 
00133 const std::string MediaPathManager::lookUpMediaPath(const std::string  &path)
00134 {
00135   std::ifstream test;
00136   std::string   pathBuffer = path;
00137   size_t        count      = dataPaths.size();
00138 
00139   test.open(path.c_str());
00140 
00141   if(test.is_open())
00142   {
00143     test.close();
00144     return pathBuffer;
00145   }
00146 
00147   for(size_t i = 0; i < count; i++)
00148   {
00149     pathBuffer  = dataPaths[i];
00150     pathBuffer += path;
00151 
00152     test.open(pathBuffer.c_str());
00153     if(test.is_open())
00154     {
00155       test.close();
00156           return pathBuffer;
00157       break;
00158     }
00159   }
00160 
00161   return "";
00162 }
00163 
00164 bool MediaPathManager::registerPath(const TiXmlElement *mediaPathNode)
00165 
00166 {
00167   if(mediaPathNode)
00168    return  registerPath(mediaPathNode->Attribute("path"));
00169 
00170   return false;
00171 }
00172 
00173 bool MediaPathManager::registerPath(const std::string  &path)
00174 {
00175   if(!path.size())
00176     return Logger::writeErrorLog("Failed to register data path -> NULL");
00177 
00178   for(size_t i = 0; i < dataPaths.size(); i++)
00179     if(dataPaths[i] == path)
00180       return true;
00181 
00182   std::string stringBuffer = path;
00183 
00184   Logger::writeInfoLog(std::string("Registering data path -> ") + path);
00185   dataPaths.push_back(stringBuffer);
00186   return true;
00187 }
00188 
00189 int MediaPathManager::getPathCount()
00190 {
00191   return int(dataPaths.size());
00192 }
00193 
00194 const std::string MediaPathManager::getPathAt(int index)
00195 {
00196   if(!dataPaths.size() || index >= int(dataPaths.size()) || index < 0)
00197     return NULL;
00198   return dataPaths[size_t(index)];
00199 }
00200 
00201 void  MediaPathManager::printAllPaths()
00202 {
00203   std::cout << "List of registred Media Paths: \n";
00204 
00205   for(size_t i = 0; i < dataPaths.size(); i++)
00206     std::cout << int(i) << "-" << dataPaths[i].c_str() << std::endl;
00207 
00208   std::cout << std::endl;
00209 }
00210 
00211 /*******************************************************************************************/
00212 /*Perlin                                                                                   */
00213 /*                                                                                         */
00214 /*******************************************************************************************/
00215 
00216 #include <sstream>
00217 
00218 #define MAXB 0x100
00219 #define N 0x1000
00220 #define NP 12   // 2^N
00221 #define NM 0xfff
00222 
00223 #define s_curve(t) ( t * t * (3. - 2. * t) )
00224 #define lerp(t, a, b) ( a + t * (b - a) )
00225 #define setup(i, b0, b1, r0, r1)\
00226         t = vec[i] + N;\
00227         b0 = ((int)t) & BM;\
00228         b1 = (b0+1) & BM;\
00229         r0 = t - (int)t;\
00230         r1 = r0 - 1.;
00231 #define at2(rx, ry) ( rx * q[0] + ry * q[1] )
00232 #define at3(rx, ry, rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
00233 
00234 static int p[MAXB + MAXB + 2];
00235 static double g3[MAXB + MAXB + 2][3];
00236 static double g2[MAXB + MAXB + 2][2];
00237 static double g1[MAXB + MAXB + 2];
00238 
00239 int start = 1;
00240 int B     = 4;
00241 int BM    = 3;
00242 
00243 
00244 int  Perlin::getNoiseFrequency()
00245 {
00246   return B;
00247 }
00248 
00249 void Perlin::setNoiseFrequency(int frequency)
00250 {
00251   start = 1;
00252   B     = frequency;
00253   BM    = B-1;
00254 }
00255 
00256 double Perlin::noise1(double arg)
00257 {
00258         int bx0, bx1;
00259         double rx0, rx1, sx, t, u, v, vec[1];
00260 
00261         vec[0] = arg;
00262         if (start)
00263         {
00264                 start = 0;
00265                 initialize();
00266         }
00267 
00268         setup(0, bx0, bx1, rx0, rx1);
00269 
00270         sx = s_curve(rx0);
00271         u = rx0 * g1[p[bx0]];
00272         v = rx1 * g1[p[bx1]];
00273 
00274         return(lerp(sx, u, v));
00275 }
00276 
00277 double Perlin::noise2(double vec[2])
00278 {
00279         int bx0, bx1, by0, by1, b00, b10, b01, b11;
00280         double rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
00281         int i, j;
00282 
00283         if (start)
00284         {
00285                 start = 0;
00286                 initialize();
00287         }
00288 
00289         setup(0, bx0, bx1, rx0, rx1);
00290         setup(1, by0, by1, ry0, ry1);
00291 
00292         i = p[bx0];
00293         j = p[bx1];
00294 
00295         b00 = p[i + by0];
00296         b10 = p[j + by0];
00297         b01 = p[i + by1];
00298         b11 = p[j + by1];
00299 
00300         sx = s_curve(rx0);
00301         sy = s_curve(ry0);
00302 
00303         q = g2[b00]; u = at2(rx0, ry0);
00304         q = g2[b10]; v = at2(rx1, ry0);
00305         a = lerp(sx, u, v);
00306 
00307         q = g2[b01]; u = at2(rx0, ry1);
00308         q = g2[b11]; v = at2(rx1, ry1);
00309         b = lerp(sx, u, v);
00310 
00311         return lerp(sy, a, b);
00312 }
00313 
00314 double Perlin::noise3(double vec[3])
00315 {
00316         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
00317         double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
00318         int i, j;
00319 
00320         if (start)
00321         {
00322                 start = 0;
00323                 initialize();
00324         }
00325 
00326         setup(0, bx0, bx1, rx0, rx1);
00327         setup(1, by0, by1, ry0, ry1);
00328         setup(2, bz0, bz1, rz0, rz1);
00329 
00330         i = p[bx0];
00331         j = p[bx1];
00332 
00333         b00 = p[i + by0];
00334         b10 = p[j + by0];
00335         b01 = p[i + by1];
00336         b11 = p[j + by1];
00337 
00338         t  = s_curve(rx0);
00339         sy = s_curve(ry0);
00340         sz = s_curve(rz0);
00341 
00342         q = g3[b00 + bz0]; u = at3(rx0, ry0, rz0);
00343         q = g3[b10 + bz0]; v = at3(rx1, ry0, rz0);
00344         a = lerp(t, u, v);
00345 
00346         q = g3[b01 + bz0]; u = at3(rx0, ry1, rz0);
00347         q = g3[b11 + bz0]; v = at3(rx1, ry1, rz0);
00348         b = lerp(t, u, v);
00349 
00350         c = lerp(sy, a, b);
00351 
00352         q = g3[b00 + bz1]; u = at3(rx0, ry0, rz1);
00353         q = g3[b10 + bz1]; v = at3(rx1, ry0, rz1);
00354         a = lerp(t, u, v);
00355 
00356         q = g3[b01 + bz1]; u = at3(rx0, ry1, rz1);
00357         q = g3[b11 + bz1]; v = at3(rx1, ry1, rz1);
00358         b = lerp(t, u, v);
00359 
00360         d = lerp(sy, a, b);
00361 
00362         return lerp(sz, c, d);
00363 }
00364 
00365 void Perlin::normalize2(double v[2])
00366 {
00367         double s;
00368 
00369         s = sqrt(v[0] * v[0] + v[1] * v[1]);
00370         v[0] = v[0] / s;
00371         v[1] = v[1] / s;
00372 }
00373 
00374 void Perlin::normalize3(double v[3])
00375 {
00376    double s;
00377 
00378   s    = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
00379   v[0] = v[0] / s;
00380   v[1] = v[1] / s;
00381   v[2] = v[2] / s;
00382 }
00383 
00384 void Perlin::initialize()
00385 {
00386         int i, j, k;
00387 
00388         srand(30757);
00389         for (i = 0; i < B; i++)
00390         {
00391                 p[i] = i;
00392                 g1[i] = (double)((rand() % (B + B)) - B) / B;
00393 
00394                 for (j = 0; j < 2; j++)
00395                         g2[i][j] = (double)((rand() % (B + B)) - B) / B;
00396                 normalize2(g2[i]);
00397 
00398                 for (j = 0; j < 3; j++)
00399                         g3[i][j] = (double)((rand() % (B + B)) - B) / B;
00400                 normalize3(g3[i]);
00401         }
00402 
00403         while (--i)
00404         {
00405                 k = p[i];
00406                 p[i] = p[j = rand() % B];
00407                 p[j] = k;
00408         }
00409 
00410         for (i = 0; i < B + 2; i++)
00411         {
00412                 p[B + i] = p[i];
00413                 g1[B + i] = g1[i];
00414                 for (j = 0; j < 2; j++)
00415                         g2[B + i][j] = g2[i][j];
00416                 for (j = 0; j < 3; j++)
00417                         g3[B + i][j] = g3[i][j];
00418         }
00419 }
00420 
00421 // My harmonic summing functions - PDB
00422 
00423 //
00424 // In what follows "alpha" is the weight when the sum is formed.
00425 // Typically it is 2, As this approaches 1 the function is noisier.
00426 // "beta" is the harmonic scaling/spacing, typically 2.
00427 //
00428 
00429 double Perlin::noise1D(double x,double alpha,double beta,int n)
00430 {
00431         int i;
00432         double val,sum = 0;
00433         double p,scale = 1;
00434 
00435         p = x;
00436         for (i = 0; i < n; i++)
00437         {
00438                 val = noise1(p);
00439                 sum += val / scale;
00440                 scale *= alpha;
00441                 p *= beta;
00442         }
00443         return(sum);
00444 }
00445 
00446 double Perlin::noise2D(double x, double y, double alpha, double beta, int n)
00447 {
00448         int i;
00449         double val, sum = 0;
00450         double p[2], scale = 1;
00451 
00452         p[0] = x;
00453         p[1] = y;
00454         for (i = 0; i < n; i++)
00455         {
00456                 val = noise2(p);
00457                 sum += val / scale;
00458                 scale *= alpha;
00459                 p[0] *= beta;
00460                 p[1] *= beta;
00461         }
00462         return(sum);
00463         }
00464 
00465 double Perlin::noise3D(double x, double y, double z, double alpha, double beta, int n)
00466 {
00467   int i;
00468   double val,sum = 0;
00469   double p[3],scale = 1;
00470 
00471   p[0] = x;
00472   p[1] = y;
00473   p[2] = z;
00474 
00475   for (i = 0; i < n; i++)
00476   {
00477     val    = noise3(p);
00478     sum   += val / scale;
00479     scale *= alpha;
00480     p[0]  *= beta;
00481     p[1]  *= beta;
00482     p[2]  *= beta;
00483   }
00484   return(sum);
00485 }
00486 
00487 

Generated on Wed Dec 5 20:32:03 2007 for GLWX by  doxygen 1.5.3