Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Noise.h

Go to the documentation of this file.
00001 /* SimData: Data Infrastructure for Simulations
00002  * Copyright (C) 2002 Mark Rose <tm2@stm.lbl.gov>
00003  * 
00004  * This file is part of SimData.
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 
00029 #ifndef __SIMDATA_NOISE_H__
00030 #define __SIMDATA_NOISE_H__
00031 
00032 #include <cmath>
00033 #include <vector>
00034 
00035 #include <SimData/Export.h>
00036 #include <SimData/Namespace.h>
00037 
00038 
00039 NAMESPACE_SIMDATA
00040 
00041 
00052 class SIMDATA_EXPORT Perlin1D {
00053         
00054 public:
00055 
00056         typedef enum { LINEAR, COSINE, CUBIC } Interpolation;
00057 
00065         Perlin1D(double persistence=0.5, int octaves=1, Interpolation interpolation=LINEAR);
00066 
00073         void setParameters(double persistence, int octaves);
00074 
00080         void setInterpolation(Interpolation interpolation);
00081 
00089         void setOffset(int idx);
00090         
00098         void randomize();
00099 
00106         double _simpleRandom(int x) const {
00107                 x = (x << 13) ^ x;
00108                 return (1.0 -
00109                         ((x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) /
00110                         1073741824.0);
00111         } 
00112 
00119         double _getSmoothedNoise(int x) {
00120                 return 0.5 * (_simpleRandom(x) + 0.5 * (_simpleRandom(x - 1) + _simpleRandom(x + 1)));
00121         } 
00122 
00131         double _linearInterpolate(double a, double b, double x) {
00132                 return  a*(1.0-x) + b*x;
00133         }
00134         
00143         double _cosineInterpolate(double a, double b, double x) {
00144                 x = (1.0 - cos(x * 3.14159265)) * 0.5;
00145                 return  a*(1.0-x) + b*x;
00146         }
00147         
00158         double _cubicInterpolate(double v0, double v1, double v2, double v3, double x) {
00159                 double P = (v3 - v2) - (v0 - v1);
00160                 double Q = (v0 - v1) - P;
00161                 double R = v2 - v0;
00162                 return  (((P*x + Q) * x) + R) * x + v1;
00163         }
00164 
00172         double _getInterpolatedNoise(double x);
00173 
00180         double getValue(double x);
00181 
00197         std::vector<float> generate(int n, bool periodic, double scale=1.0, double amplitude=1.0, double offset=0.0);
00198 
00199 private:
00200         double m_persistence;
00201         int m_octaves;
00202         int m_offset;
00203         int m_interpolation;
00204         
00205 };
00206 
00207 
00208 NAMESPACE_SIMDATA_END
00209 
00210 
00211 #endif //__SIMDATA_NOISE_H__
00212 

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:39 2003, using Doxygen 1.2.18.

[SF.net]