NoiseInterpolator.cpp 1.3 KB

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