Blame view

kernels/plugins/grid_search2.cl 2.61 KB
89f70c1ec   glaville   import current mc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #ifndef WIDTH
  #error "WIDTH is not defined"
  #endif
  
  #ifndef TYPE
  #error "TYPE is not defined"
  #endif
  
  // Assume WIDTH contains the width of the grid
  #define offset(x, y) (y * WIDTH  + x)
  
  #define grid_get(grid, x, y) (grid[offset(x, y)])
  #define grid_getp(grid, x, y) (grid + offset(x, y))
  
  #define grid_xy(grid, x, y) (grid_get(grid, x, y))
  
  #define grid_set(grid, x, y, v) (grid + offset(x, y) = v)
  #define grid_setp(grid, x, y, v) (grid + offset(x, y) = v)
  
  void search_max(
      global TYPE grid[],
      int radius,
      int x, int y,
      global int *rx, global int *ry
  );
  
  kernel void grid_search_max(
      global TYPE grid[],
      int radius,
      global int xPos[],
      global int yPos[],
      global int newXPos[],
      global int newYPos[]
  ){
      const int i = get_global_id(0);
      search_max(grid, radius, xPos[i], yPos[i], newXPos + i, newYPos + i);
  }
  
  void search_max(
      global TYPE grid[],
      int radius,
      int x, int y,
      global int *rx, global int *ry
  ){
      int i, j, r;
      int tmp_x = x, tmp_y = y;
      TYPE memo = grid_xy(grid, x, y);
      
      for (r = 1; r <= radius; r++) {
          
          j = y - r;
          for (i = x - r; i < x + r; i++) {
              if (i < 0 || i >= WIDTH || j < 0 || j >= HEIGHT) {
                  continue;
              }
              
              TYPE value = grid_xy(grid, i, j);
              
              if (value > memo) {
                  tmp_x = i;
                  tmp_y = j;
                  memo = value;
              }
          }
  
          i = x + r;
          for (j = y - r; j < y + r; j++) {
              if (i < 0 || i >= WIDTH || j < 0 || j >= HEIGHT) {
                  continue;
              }
              
              TYPE value = grid_xy(grid, i, j);
               
              if (value > memo) {
                  tmp_x = i;
                  tmp_y = j;
                  memo = value;
              }
          }
  
          j = y + r;
          for (i = x + r; i > x - r; i--) {
              if (i < 0 || i >= WIDTH || j < 0 || j >= HEIGHT) {
                  continue;
              }
              
              TYPE value = grid_xy(grid, i, j);
               
              if (value > memo) {
                  tmp_x = i;
                  tmp_y = j;
                  memo = value;
              }
          }
  
          i = x - r;
          for (j = y + r; j > y - r; j--) {
              if (i < 0 || i >= WIDTH || j < 0 || j >= HEIGHT) {
                  continue;
              }
              
              TYPE value = grid_xy(grid, i, j);
               
              if (value > memo) {
                  tmp_x = i;
                  tmp_y = j;
                  memo = value;
              }
          }
      }
      
      *rx = tmp_x;
      *ry = tmp_y;
      return;
  }