1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #include "NoiseInterpolator.h"
- #include "Text.h"
- using namespace Framework;
- NoiseInterpolator::NoiseInterpolator( int seed, std::function<Noise* (int)> noiseSupplier, int width, int height )
- : Noise(),
- noiseSize( width, height ),
- cache( 10, []( int k ) {return k; }, CacheCleanupStrategy::LONGEST_NOT_USED ),
- noiseSupplier( noiseSupplier ),
- seed( seed )
- {}
- double NoiseInterpolator::getRealNoise( double x, double y, double z )
- {
- int ix = (int)ceil( x ), iy = (int)ceil( y );
- int xOff = ix % noiseSize.x, yOff = iy % noiseSize.y;
- int xI = ix / noiseSize.x, yI = iy / noiseSize.y;
- if( ix < 0 )
- xI--;
- if( iy < 0 )
- yI--;
- int key = ((xI & 0xFFFF) << 16) | (yI & 0xFFFF);
- if( !cache.has( key ) )
- cache.put( key, RCPointer<Noise>::of( noiseSupplier( key + seed ) ) );
- return cache.get( key )->getNoise( (xOff + 1 - (ix - x)), (yOff + 1 - (iy - y)), z );
- }
- double NoiseInterpolator::getNoise( double x, double y, double z )
- {
- const double weights[ 6 ] = { 0.32, 0.17, 0.09, 0.05, 0.03, 0.02 };
- double sum = 0;
- for( int xi = -5; xi <= 5; xi++ )
- {
- for( int yi = -5; yi <= 5; yi++ )
- sum += getRealNoise( x + xi, y + yi, z ) * weights[ abs( xi ) ] * weights[ abs( yi ) ];
- }
- return sum;
- }
- int NoiseInterpolator::getSeed() const
- {
- return seed;
- }
|