KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blob_local_optimize.h
Go to the documentation of this file.
1 
5 /*
6  * $Id$
7  */
8 
9 #ifndef LOCAL_OPTIMIZE_H_INCLUDED_
10 #define LOCAL_OPTIMIZE_H_INCLUDED_
11 
18 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
19 OutputIterator local_argoptima_1D(
20  const DiscreteFunction& f,
21  double thresh,
22  OutputIterator result,
23  ConvertIndex convert_index
24 );
25 
32 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
33 OutputIterator local_argoptima_2D(
34  const DiscreteFunction& f,
35  double thresh,
36  OutputIterator result,
37  ConvertIndex convert_index
38 );
39 
46 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
47 OutputIterator local_argoptima_3D(
48  const DiscreteFunction& f,
49  double thresh,
50  OutputIterator result,
51  ConvertIndex convert_index
52 );
53 
54 
55 //helper functions
56 
57 template<class DiscreteFunction>
58 bool less_than_neighbors_2D(int i, int j, const DiscreteFunction& f);
59 
60 template<class DiscreteFunction>
61 bool greater_than_neighbors_2D(int i, int j, const DiscreteFunction& f);
62 
63 template<class DiscreteFunction>
64 bool less_than_neighbors_3D(int i, int j, int k, const DiscreteFunction& f);
65 
66 template<class DiscreteFunction>
67 bool greater_than_neighbors_3D(int i, int j, int k, const DiscreteFunction& f);
68 
69 
70 //============================================================================
71 // Definition of template and inline functions
72 //============================================================================
73 
74 /************************************************
75  * ONE DIMENSION *
76  ************************************************/
77 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
78 OutputIterator local_argoptima_1D(
79  const DiscreteFunction& f,
80  double thresh,
81  OutputIterator result,
82  ConvertIndex convert_index
83 )
84 {
85  const int FSZ = f.size() - 1;
86  for (int i = 1; i < FSZ; i++)
87  {
88  if (f[i] < f[i - 1] && f[i] < f[i + 1] && f[i] <= -thresh)
89  {
90  *result++ = convert_index(i);
91  }
92 
93  if (f[i] > f[i - 1] && f[i] > f[i + 1] && f[i] >= thresh)
94  {
95  *result++ = convert_index(i);
96  }
97  }
98 
99  return result;
100 }
101 
102 
103 /************************************************
104  * TWO DIMENSIONS *
105  ************************************************/
106 
107 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
108 OutputIterator local_argoptima_2D(
109  const DiscreteFunction& f,
110  double thresh,
111  OutputIterator result,
112  ConvertIndex convert_index
113 )
114 {
115  const int FSZ = f.size() - 1;
116  for (int i = 1; i < FSZ; ++i)
117  {
118  const int FI_SZ = f[i].size() - 1;
119  for (int j = 1; j < FI_SZ; ++j)
120  {
121  if (less_than_neighbors_2D(i, j, f) && f[i][j] <= -thresh)
122  {
123  *result++ = convert_index(i, j);
124  }
125 
126  if (greater_than_neighbors_2D(i, j, f) && f[i][j] >= thresh)
127  {
128  *result++ = convert_index(i, j);
129  }
130  }
131  }
132 
133  return result;
134 }
135 
136 template<class DiscreteFunction>
137 bool less_than_neighbors_2D(int i, int j, const DiscreteFunction& f)
138 {
139  for (int l_i = i - 1; l_i <= i + 1; ++l_i)
140  {
141  for (int l_j = j - 1; l_j <= j + 1; ++l_j)
142  {
143  if (f[l_i][l_j] <= f[i][j])
144  {
145  if (i != l_i || j != l_j)
146  {
147  return false;
148  }
149  }
150  }
151  }
152 
153  return true;
154 }
155 
156 template<class DiscreteFunction>
157 bool greater_than_neighbors_2D(int i, int j, const DiscreteFunction& f)
158 {
159  for (int l_i = i - 1; l_i <= i + 1; ++l_i)
160  {
161  for (int l_j = j - 1; l_j <= j + 1; ++l_j)
162  {
163  if (f[l_i][l_j] >= f[i][j])
164  {
165  if (i != l_i || j != l_j)
166  {
167  return false;
168  }
169  }
170  }
171  }
172 
173  return true;
174 }
175 
176 
177 /************************************************
178  * THREE DIMENSIONS *
179  ************************************************/
180 
181 template<class DiscreteFunction, class OutputIterator, class ConvertIndex>
182 OutputIterator local_argoptima_3D(
183  const DiscreteFunction& f,
184  double thresh,
185  OutputIterator result,
186  ConvertIndex convert_index
187 )
188 {
189  const int FSZ = f.size() - 1;
190  for (int i = 1; i < FSZ; ++i)
191  {
192  const int FI_SZ = f[i].size() - 1;
193  for (int j = 1; j < FI_SZ; ++j)
194  {
195  const int FIJ_SZ = f[i][j].size() - 1;
196  for (int k = 1; k < FIJ_SZ; ++k)
197  {
198  if (less_than_neighbors_3D(i,j,k,f) && f[i][j][k] <= -thresh)
199  {
200  *result++ = convert_index(i, j, k);
201  }
202 
203  if (greater_than_neighbors_3D(i,j,k,f) && f[i][j][k] >= thresh)
204  {
205  *result++ = convert_index(i, j, k);
206  }
207  }
208  }
209  }
210 
211  return result;
212 }
213 
214 template<class DiscreteFunction>
215 bool less_than_neighbors_3D(int i, int j, int k, const DiscreteFunction& f)
216 {
217  for (int l_i = i - 1; l_i <= i + 1; ++l_i)
218  {
219  for (int l_j = j - 1; l_j <= j + 1; ++l_j)
220  {
221  for (int l_k = k - 1; l_k <= k + 1; ++l_k)
222  {
223  if (f[l_i][l_j][l_k] <= f[i][j][k])
224  {
225  if (i != l_i || j != l_j || k != l_k)
226  {
227  return false;
228  }
229  }
230  }
231  }
232  }
233 
234  return true;
235 }
236 
237 template<class DiscreteFunction>
238 bool greater_than_neighbors_3D(int i, int j, int k, const DiscreteFunction& f)
239 {
240  for (int l_i = i - 1; l_i <= i + 1; ++l_i)
241  {
242  for (int l_j = j - 1; l_j <= j + 1; ++l_j)
243  {
244  for (int l_k = k - 1; l_k <= k + 1; ++l_k)
245  {
246  if (f[l_i][l_j][l_k] >= f[i][j][k])
247  {
248  if (i != l_i || j != l_j || k != l_k)
249  {
250  return false;
251  }
252  }
253  }
254  }
255  }
256 
257  return true;
258 }
259 
260 #endif /*LOCAL_OPTIMIZE_H_INCLUDED_ */
261 
for k
Definition: APPgetLargeConnectedEdges.m:61
OutputIterator local_argoptima_1D(const DiscreteFunction &f, double thresh, OutputIterator result, ConvertIndex convert_index)
Find the local optima of a 1-dimensional discrete function.
Definition: blob_local_optimize.h:78
bool less_than_neighbors_3D(int i, int j, int k, const DiscreteFunction &f)
Definition: blob_local_optimize.h:215
bool less_than_neighbors_2D(int i, int j, const DiscreteFunction &f)
Definition: blob_local_optimize.h:137
bool greater_than_neighbors_3D(int i, int j, int k, const DiscreteFunction &f)
Definition: blob_local_optimize.h:238
bool greater_than_neighbors_2D(int i, int j, const DiscreteFunction &f)
Definition: blob_local_optimize.h:157
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
OutputIterator local_argoptima_2D(const DiscreteFunction &f, double thresh, OutputIterator result, ConvertIndex convert_index)
Find the local optima of a 2-dimensional discrete function.
Definition: blob_local_optimize.h:108
OutputIterator local_argoptima_3D(const DiscreteFunction &f, double thresh, OutputIterator result, ConvertIndex convert_index)
Find the local optima of a 3-dimensional discrete function.
Definition: blob_local_optimize.h:182