💿🐜 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.

90 lines
2.6 KiB

  1. /*
  2. * Copyright (C) 2020 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. #include "pheromone-matrix.hpp"
  20. #include "math/math.hpp"
  21. void convolve(pheromone_matrix* matrix, const float* kernel, int kernel_size)
  22. {
  23. float* front = matrix->buffers[matrix->current];
  24. float* back = matrix->buffers[(matrix->current + 1) % 2];
  25. const int kernel_radius = kernel_size << 1;
  26. // For each pheromone matrix row
  27. for (int i = 0; i < matrix->rows; ++i)
  28. {
  29. // For each pheromone in the row
  30. for (int j = 0; j < matrix->columns; ++j)
  31. {
  32. // Reset accumulator
  33. float accumulator = 0.0f;
  34. // For each kernel row
  35. for (int k = -kernel_radius; k <= kernel_radius; ++k)
  36. {
  37. // For each kernel element
  38. for (int l = -kernel_radius; l <= kernel_radius; ++l)
  39. {
  40. // Determine row and column of pheromone corresponding to the kernel element.
  41. int m = i + k;
  42. int n = j + l;
  43. // If the row and column indices are within the matrix
  44. if (m >= 0 && m < matrix->rows && n >= 0 && n < matrix->columns)
  45. {
  46. // Multiply pheromone strength in the front buffer by the kernel value and add it to the accumulator.
  47. accumulator += front[m * matrix->columns + n] * kernel[(k + kernel_radius) * kernel_size + l + kernel_radius];
  48. }
  49. }
  50. }
  51. // Set pheromone strength in the back buffer equal to the accumulator value
  52. back[i * matrix->columns + j] = accumulator;
  53. }
  54. }
  55. // Swap buffers
  56. matrix->current = (matrix->current + 1) % 2;
  57. }
  58. void evaporate(pheromone_matrix* matrix, float factor)
  59. {
  60. const int size = matrix->columns * matrix->rows;
  61. for (int i = 0; i < size; ++i)
  62. {
  63. matrix->buffers[matrix->current][i] *= factor;
  64. }
  65. }
  66. void diffuse(pheromone_matrix* matrix)
  67. {
  68. const math::matrix<float, 3, 3> diffusion_kernel =
  69. math::mul(
  70. math::matrix<float, 3, 3>
  71. {{
  72. {1, 2, 1},
  73. {2, 4, 2},
  74. {1, 2, 1}
  75. }},
  76. 1.0f / 16.0f);
  77. convolve(matrix, &diffusion_kernel[0][0], 3);
  78. }