NoiseInterpolator.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "NoiseInterpolator.h"
  2. #include "Text.h"
  3. using namespace Framework;
  4. NoiseInterpolator::NoiseInterpolator(int seed, std::function<Noise* (int)> noiseSupplier, int width, int height)
  5. : Noise(),
  6. noiseSize(width, height),
  7. cache(10, [](int k) { return k; }, CacheCleanupStrategy::LONGEST_NOT_USED),
  8. noiseSupplier(noiseSupplier),
  9. seed(seed)
  10. {}
  11. double NoiseInterpolator::getRealNoise(double x, double y, double z)
  12. {
  13. int ix = (int)ceil(x), iy = (int)ceil(y);
  14. int xOff = ix % noiseSize.x, yOff = iy % noiseSize.y;
  15. int xI = ix / noiseSize.x, yI = iy / noiseSize.y;
  16. if (ix < 0)
  17. xI--;
  18. if (iy < 0)
  19. yI--;
  20. int key = ((xI & 0xFFFF) << 16) | (yI & 0xFFFF);
  21. if (!cache.has(key))
  22. cache.put(key, RCPointer<Noise>::of(noiseSupplier(key + seed)));
  23. return cache.get(key)->getNoise((xOff + 1 - (ix - x)), (yOff + 1 - (iy - y)), z);
  24. }
  25. double NoiseInterpolator::getNoise(double x, double y, double z)
  26. {
  27. const double weights[6] = { 0.32, 0.17, 0.09, 0.05, 0.03, 0.02 };
  28. double sum = 0;
  29. for (int xi = -5; xi <= 5; xi++)
  30. {
  31. for (int yi = -5; yi <= 5; yi++)
  32. sum += getRealNoise(x + xi, y + yi, z) * weights[abs(xi)] * weights[abs(yi)];
  33. }
  34. return sum;
  35. }
  36. int NoiseInterpolator::getSeed() const
  37. {
  38. return seed;
  39. }