💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_NOISE_FBM_HPP
  20. #define ANTKEEPER_MATH_NOISE_FBM_HPP
  21. #include "math/hash/make-uint.hpp"
  22. #include "math/hash/pcg.hpp"
  23. #include "math/noise/simplex.hpp"
  24. #include "math/vector.hpp"
  25. #include <cstdint>
  26. namespace math {
  27. namespace noise {
  28. /**
  29. * Fractional Brownian motion (fBm).
  30. *
  31. * @tparam T Real type.
  32. * @tparam N Number of dimensions.
  33. *
  34. * @param position Input position.
  35. * @param octaves Number of octaves.
  36. * @param lacunarity Frequency multiplier.
  37. * @param gain Amplitude multiplier.
  38. * @param noise Noise function.
  39. * @param hash Hash function.
  40. *
  41. */
  42. template <class T, std::size_t N>
  43. [[nodiscard]] T fbm
  44. (
  45. vector<T, N> position,
  46. std::size_t octaves,
  47. T lacunarity,
  48. T gain,
  49. T (*noise)(const vector<T, N>&, vector<hash::make_uint_t<T>, N> (*)(const vector<T, N>&)) = &simplex<T, N>,
  50. vector<hash::make_uint_t<T>, N> (*hash)(const vector<T, N>&) = &hash::pcg<T, N>
  51. )
  52. {
  53. T amplitude{1};
  54. T value{0};
  55. while (octaves--)
  56. {
  57. value += noise(position, hash) * amplitude;
  58. position *= lacunarity;
  59. amplitude *= gain;
  60. }
  61. return value;
  62. }
  63. } // namespace noise
  64. } // namespace math
  65. #endif // ANTKEEPER_MATH_NOISE_FBM_HPP