gather.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include "ncb.h"
  4. /*
  5. given the starting col of the neighborhood,
  6. copy the cell values from the bufs into the array of values
  7. and return the number of values copied.
  8. */
  9. #define sqr(x) ((x) * (x))
  10. void circle_mask(void)
  11. {
  12. int i, j;
  13. if (ncb.mask)
  14. return;
  15. ncb.mask = G_malloc(ncb.nsize * sizeof(char *));
  16. for (i = 0; i < ncb.nsize; i++)
  17. ncb.mask[i] = G_malloc(ncb.nsize);
  18. for (i = 0; i < ncb.nsize; i++)
  19. for (j = 0; j < ncb.nsize; j++)
  20. ncb.mask[i][j] =
  21. sqr(i - ncb.dist) + sqr(j - ncb.dist) <= sqr(ncb.dist);
  22. }
  23. void weights_mask(void)
  24. {
  25. int i, j;
  26. if (ncb.mask)
  27. return;
  28. ncb.mask = G_malloc(ncb.nsize * sizeof(char *));
  29. for (i = 0; i < ncb.nsize; i++)
  30. ncb.mask[i] = G_malloc(ncb.nsize);
  31. for (i = 0; i < ncb.nsize; i++)
  32. for (j = 0; j < ncb.nsize; j++)
  33. ncb.mask[i][j] = ncb.weights[i][j] != 0;
  34. }
  35. int gather(DCELL *values, int offset)
  36. {
  37. int row, col;
  38. int n = 0;
  39. *values = 0;
  40. for (row = 0; row < ncb.nsize; row++) {
  41. for (col = 0; col < ncb.nsize; col++) {
  42. if (ncb.mask && !ncb.mask[row][col])
  43. continue;
  44. values[n] = ncb.buf[row][offset + col];
  45. n++;
  46. }
  47. }
  48. return n;
  49. }
  50. int gather_w(DCELL *values, DCELL (*values_w)[2], int offset)
  51. {
  52. int row, col;
  53. int n = 0;
  54. values_w[0][0] = 0;
  55. values_w[0][1] = 1;
  56. for (row = 0; row < ncb.nsize; row++) {
  57. for (col = 0; col < ncb.nsize; col++) {
  58. values[n] = values_w[n][0] = ncb.buf[row][offset + col];
  59. values_w[n][1] = ncb.weights[row][col];
  60. n++;
  61. }
  62. }
  63. return n;
  64. }