33 #ifndef __H__UG_field_util_impl
34 #define __H__UG_field_util_impl
46 for(
size_t mainIter = 0; mainIter < numIterations; ++mainIter){
47 for(
int iy = 0; iy < (int)field.
height(); ++iy){
48 for(
int ix = 0; ix < (int)field.
width(); ++ix){
49 if(field.
at(ix, iy) != noDataValue){
52 for(
int iny = max<int>(iy - 1, 0); iny < min<int>(iy + 2, (
int)field.
height()); ++iny){
53 for(
int inx = max<int>(ix - 1, 0); inx < min<int>(ix + 2, (
int)field.
width()); ++inx){
54 if(!(inx == 0 && iny == 0) && (field.
at(inx, iny) != noDataValue)){
55 val += field.
at(inx, iny);
64 field.
at(ix, iy) += val;
89 number inProgressValue = -noDataValue;
91 const int numNbrs = 8;
92 const int xadd[numNbrs] = {-1, 0, 1, -1, 1, -1, 0, 1};
93 const int yadd[numNbrs] = {-1, -1, -1, 0, 0, 1, 1, 1};
95 const int maxNumSteps = 4;
96 const int minNumValidNbrsInStep[maxNumSteps] = {4, 3, 2, 1};
99 size_t numInvalidCells = 0;
100 for(
int iy = 0; iy < (int)field.
height(); ++iy){
101 for(
int ix = 0; ix < (int)field.
width(); ++ix){
102 if(field.
at(ix, iy) == noDataValue)
111 for(
int istep = 0; (istep < maxNumSteps) && (numInvalidCells > 0); ++istep){
112 const int minNumValidNbrs = minNumValidNbrsInStep[istep];
114 for(
int iy = 0; iy < (int)field.
height(); ++iy){
115 for(
int ix = 0; ix < (int)field.
width(); ++ix){
116 if(field.
at(ix, iy) != noDataValue)
119 int numValidNbrs = 0;
120 for(
int i = 0; i < numNbrs; ++i){
121 const int inx = ix + xadd[i];
122 const int iny = iy + yadd[i];
124 if((inx >= 0) && (inx < (int)field.
width())
125 && (iny >= 0) && (iny < (int)field.
height())
126 && (field.
at(inx, iny) != noDataValue)
127 && (field.
at(inx, iny) != inProgressValue))
133 if(numValidNbrs >= minNumValidNbrs){
134 cells.push_back(Cell(ix, iy, inProgressValue));
135 field.
at(ix, iy) = inProgressValue;
141 while(!cells.empty()){
143 for(
typename deque<Cell>::iterator cellIter = cells.begin(); cellIter != cells.end(); ++cellIter)
145 Cell& cell = *cellIter;
146 const int ix = cell.x;
147 const int iy = cell.y;
152 for(
int i = 0; i < numNbrs; ++i){
153 const int inx = ix + xadd[i];
154 const int iny = iy + yadd[i];
156 if((inx >= 0) && (inx < (int)field.
width())
157 && (iny >= 0) && (iny < (int)field.
height()))
159 if((field.
at(inx, iny) != noDataValue)
160 && (field.
at(inx, iny) != inProgressValue))
162 avVal += field.
at(inx, iny);
169 avVal *= 1. / numValidNbrs;
175 while(!(cells.empty() || (cells.front().value == inProgressValue)))
177 const int ix = cells.front().x;
178 const int iy = cells.front().y;
179 field.
at(ix, iy) = cells.front().value;
182 for(
int i = 0; i < numNbrs; ++i){
183 const int inx = ix + xadd[i];
184 const int iny = iy + yadd[i];
186 if((inx >= 0) && (inx < (
int)field.
width())
187 && (iny >= 0) && (iny < (
int)field.
height()))
189 if(field.
at(inx, iny) == noDataValue){
193 int numValidNbrsOfNbr = 0;
194 for(
int j = 0; j < numNbrs; ++j){
195 const int inxNbr = inx + xadd[j];
196 const int inyNbr = iny + yadd[j];
198 if((inxNbr >= 0) && (inxNbr < (
int)field.
width())
199 && (inyNbr >= 0) && (inyNbr < (
int)field.
height())
200 && (field.
at(inxNbr, inyNbr) != noDataValue)
201 && (field.
at(inxNbr, inyNbr) != inProgressValue))
206 if(numValidNbrsOfNbr >= minNumValidNbrs){
207 cells.push_back(Cell(inx, iny, inProgressValue));
208 field.
at(inx, iny) = inProgressValue;
217 return numInvalidCells == 0;
223 const T& noDataValue)
230 const size_t numNbrs = 4;
231 const int xadd[numNbrs] = {0, -1, 1, 0};
232 const int yadd[numNbrs] = {-1, 0, 0, 1};
236 vector<pair<int, int> > cells;
238 const int fwidth = (int)field.
width();
239 const int fheight = (int)field.
height();
241 for(
int outerIy = 0; outerIy < fheight; ++outerIy){
242 for(
int outerIx = 0; outerIx < fwidth; ++outerIx){
243 if(visited.
at(outerIx, outerIy)
244 || (field.
at(outerIx, outerIy) == noDataValue))
250 cells.push_back(make_pair(outerIx, outerIy));
252 while(curCell < cells.size()){
253 int ix = cells[curCell].first;
254 int iy = cells[curCell].second;
256 for(
size_t inbr = 0; inbr < numNbrs; ++inbr){
257 int nx = ix + xadd[inbr];
258 int ny = iy + yadd[inbr];
259 if((nx >= 0 && nx < fwidth && ny >= 0 && ny < fheight)
260 && !visited.
at(nx, ny))
262 visited.
at(nx, ny) =
true;
263 if(field.
at(nx, ny) != noDataValue){
264 cells.push_back(make_pair(nx, ny));
271 if(cells.size() < thresholdCellCount){
272 for(
size_t i = 0; i < cells.size(); ++i){
273 int ix = cells[i].first;
274 int iy = cells[i].second;
275 field.
at(ix, iy) = noDataValue;
size_t width() const
Definition: field.h:57
T & at(size_t x, size_t y)
Definition: field_impl.hpp:113
size_t height() const
Definition: field.h:58
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
double number
Definition: types.h:124
Definition: smart_pointer.h:814
void BlurField(Field< T > &field, number alpha, size_t numIterations, const T &noDataValue)
Smoothens the field by adjusting the value of each pixel towards the average of its neighbours.
Definition: field_util_impl.h:43
bool EliminateInvalidCells(Field< T > &field, const T &noDataValue)
eliminates invalid cells by repeatedly filling those cells with averages of neighboring cells
Definition: field_util_impl.h:83
void InvalidateSmallLenses(Field< T > &field, size_t thresholdCellCount, const T &noDataValue)
invalidates cells that belong to a small lense
Definition: field_util_impl.h:222
Definition: field_util_impl.h:74
int x
Definition: field_util_impl.h:76
int y
Definition: field_util_impl.h:77
T value
Definition: field_util_impl.h:78
Cell(int _x, int _y, T _val)
Definition: field_util_impl.h:75