00001 #ifndef OPENTISSUE_CORE_MATH_NOISE_NOISE_IMPROVED_PERLIN_H
00002 #define OPENTISSUE_CORE_MATH_NOISE_NOISE_IMPROVED_PERLIN_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012
00013 namespace OpenTissue
00014 {
00015
00016 namespace noise
00017 {
00018
00022 template<typename real_type_>
00023 class ImprovedPerlinNoise
00024 {
00025 public:
00026
00027 typedef real_type_ real_type;
00028
00029 protected:
00030
00031 int m_p[512];
00032
00033 protected:
00034
00035 real_type fade(real_type const & t) const
00036 {
00037 return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
00038 }
00039
00040 real_type lerp(real_type const & t, real_type const & a, real_type const & b)const
00041 {
00042 return a + t * (b - a);
00043 }
00044
00045 real_type grad(int hash, real_type const & x, real_type const & y, real_type const & z) const
00046 {
00047 int h = hash & 15;
00048 real_type u = h<8 ? x : y;
00049 real_type v = h<4 ? y : h==12||h==14 ? x : z;
00050 return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
00051 }
00052
00053 public:
00054
00055 ImprovedPerlinNoise()
00056 {
00057 static int m_permutation[256] = {
00058 151,160,137,91,90,15,
00059 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
00060 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
00061 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
00062 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
00063 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
00064 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
00065 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
00066 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
00067 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
00068 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
00069 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
00070 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
00071 };
00072 for (int i=0; i < 256 ; i++)
00073 m_p[256+i] = m_p[i] = m_permutation[i];
00074 }
00075
00076 real_type operator()(real_type const &x, real_type const & y, real_type const & z) const
00077 {
00078 int X = static_cast<int>( std::floor(x) ) & 0xFF;
00079 int Y = static_cast<int>( std::floor(y) ) & 0xFF;
00080 int Z = static_cast<int>( std::floor(z) ) & 0xFF;
00081 real_type rx = x - static_cast<real_type>( std::floor(x) );
00082 real_type ry = y - static_cast<real_type>( std::floor(y) );
00083 real_type rz = z - static_cast<real_type>( std::floor(z) );
00084 real_type u = fade(rx);
00085 real_type v = fade(ry);
00086 real_type w = fade(rz);
00087
00088 int A = m_p[X ] + Y;
00089 int AA = m_p[A ] + Z;
00090 int AB = m_p[A+1] + Z;
00091 int B = m_p[X+1] + Y;
00092 int BA = m_p[B ] + Z;
00093 int BB = m_p[B+1] + Z;
00094
00095 return lerp(w, lerp(v, lerp(u, grad(m_p[AA ], rx , ry , rz ),
00096 grad(m_p[BA ], rx-1.0, ry , rz )),
00097 lerp(u, grad(m_p[AB ], rx , ry-1.0, rz ),
00098 grad(m_p[BB ], rx-1.0, ry-1.0, rz ))),
00099 lerp(v, lerp(u, grad(m_p[AA+1], rx , ry , rz-1.0 ),
00100 grad(m_p[BA+1], rx-1.0, ry , rz-1.0 )),
00101 lerp(u, grad(m_p[AB+1], rx , ry-1.0, rz-1.0 ),
00102 grad(m_p[BB+1], rx-1.0, ry-1.0, rz-1.0 ))));
00103 }
00104
00105
00106 real_type operator()(real_type const &x, real_type const & y) const
00107 {
00108 return (*this)(x,y,0.);
00109 }
00110
00111 real_type operator()(real_type const &x ) const
00112 {
00113 return (*this)(x,0.,0.);
00114 }
00115
00116 };
00117
00118 }
00119
00120 }
00121
00122
00123 #endif