genericWindow.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef _genericwindow_H
  19. #define _genericwindow_H
  20. #include <stdio.h>
  21. #include <grass/iostream/ami.h>
  22. #include "types.h"
  23. /* ************************************************************* *
  24. * class 'genericWindow' implements a 3x3 window in a grid;
  25. * ************************************************************* */
  26. template<class T>
  27. class genericWindow {
  28. protected:
  29. T data[9];
  30. public:
  31. /***************************************************************/
  32. /* initialize a window to 0 */
  33. genericWindow() {
  34. for (int i=0; i<9;i++) {
  35. data[i] = T();
  36. }
  37. }
  38. /***************************************************************/
  39. /* initialize a window from an array of 9 values */
  40. genericWindow(T* a) {
  41. assert(a);
  42. for (int i=0; i<9;i++) {
  43. data[i] = a[i];
  44. }
  45. }
  46. /***************************************************************/
  47. /* initialize a window from 3 arrays of 3 elements each */
  48. genericWindow(T *a, T *b, T*c) {
  49. int i;
  50. assert(a); assert(b); assert(c);
  51. for (i=0;i<3;i++) {
  52. data[i] = a[i];
  53. data[i+3] = b[i];
  54. data[i+6] = c[i];
  55. }
  56. }
  57. /***************************************************************/
  58. /* initialize a window from 3 arrays of 3 elements each */
  59. template<class C>
  60. genericWindow(C *a, C *b, C*c) {
  61. int i;
  62. assert(a); assert(b); assert(c);
  63. for (i=0;i<3;i++) {
  64. data[i] = a[i];
  65. data[i+3] = b[i];
  66. data[i+6] = c[i];
  67. }
  68. }
  69. /***************************************************************/
  70. genericWindow(const genericWindow<T> &win) {
  71. for (int i=0;i<9;i++) {
  72. data[i] = win.data[i];
  73. }
  74. }
  75. /***************************************************************/
  76. /* get specified neighbour di,dj in {-1,0,1} */
  77. T get(short di, short dj) const {
  78. assert (di>=-1 && di<=1);
  79. assert(dj>=-1 && dj<=1);
  80. return data[4+dj+di*3];
  81. }
  82. /***************************************************************/
  83. /* get specified neighbour i in 0..8 */
  84. T get(unsigned short i=4) const {
  85. assert(i <= 8);
  86. return data[i];
  87. }
  88. /***************************************************************/
  89. /* set specified neighbour i in 0..8 */
  90. void set(unsigned short i, T val) {
  91. assert(i <= 8);
  92. data[i] = val;
  93. }
  94. /***************************************************************/
  95. /* set specified neighbour di,dj in {-1,0,1} */
  96. void set(int di, int dj, T val) {
  97. assert (di>=-1 && di<=1);
  98. assert(dj>=-1 && dj<=1);
  99. data[4+dj+di*3] = val;
  100. }
  101. /***************************************************************/
  102. /* multiply all elements by a scalar */
  103. void scalarMultiply(T mult) {
  104. for(int i=0; i<9; i++) {
  105. data[i] *= mult;
  106. }
  107. }
  108. /***************************************************************/
  109. inline friend ostream& operator<<(ostream& s, const genericWindow<T> &x) {
  110. s << "[" << x.data[0] << "," << x.data[1] << "," << x.data[2] << "]\n";
  111. s << "[" << x.data[3] << "," << x.data[4] << "," << x.data[5] << "]\n";
  112. s << "[" << x.data[6] << "," << x.data[7] << "," << x.data[8] << "]\n";
  113. return s;
  114. }
  115. };
  116. typedef genericWindow<elevation_type> ElevationWindow;
  117. void fillPit(ElevationWindow& win);
  118. #endif