1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 #include "FillFilter.h"
27 #include "creaVtk_MACROS.h"
29 FillFilter::FillFilter()
33 _limitRecursionFill = 50000;
40 //---------------------------------------------------------------------------
41 FillFilter::~FillFilter()
43 if (_auxImageFill != NULL)
45 _auxImageFill->Delete();
49 //---------------------------------------------------------------------------
50 void FillFilter::SetImages(vtkImageData *image,vtkImageData *image2) // virtual
54 baseFilterManualPaint::SetImages(image,image2);
56 if ((_maxX!=_maxXback) || (_maxY!=_maxYback) || (_maxZ!=_maxZback))
58 if (_auxImageFill != NULL)
60 _auxImageFill->Delete();
62 _auxImageFill = vtkImageData::New();
63 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
64 _auxImageFill->SetOrigin(0, 0, 0);
65 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
66 //EED 2017-01-01 Migration VTK7
67 #if VTK_MAJOR_VERSION <= 5
68 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
69 _auxImageFill->SetScalarTypeToUnsignedChar();
70 _auxImageFill->AllocateScalars();
72 _auxImageFill->AllocateScalars(VTK_UNSIGNED_CHAR,1);
74 _ptrAuxImageFill = (unsigned char *)_auxImageFill->GetScalarPointer();
75 memset(_ptrAuxImageFill, 0, (_maxX+1) * (_maxY+1) * (_maxZ+1) );
83 //---------------------------------------------------------------------------
84 void FillFilter::Run() // virtual
87 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
88 && (_pz >= _minZ) && (_pz <= _maxZ))
90 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,0);
94 _distbasefill = _distancefill * _distancefill;
95 _countRecursiveFill = 0;
96 _countRecursiveFillProblem = 0;
97 _countProgressingFill = 0;
98 _usingAuxImageFill = false;
99 ivi = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1); // index vector image
101 // FillToolRecursive(_px, _py, _pz);
102 FillToolRecursive(_px, _py, _pz, ivi);
104 unsigned char *pImage;
105 pImage = (unsigned char *) _auxImageFill->GetScalarPointer();
108 while (_countRecursiveFillProblem != 0)
110 _countRecursiveFillProblem = 0;
111 _usingAuxImageFill = true;
112 for (kk = 0; kk <= _maxZ; kk++)
114 for (jj = 0; jj <= _maxY; jj++)
116 for (ii = 0; ii <= _maxX; ii++)
118 if (pImage[ivi] == 1)
121 // FillToolRecursive(ii, jj, kk);
122 FillToolRecursive(ii, jj, kk, ivi);
130 } //if _minX _maxX _minY _maxY _minZ _maxZ
133 //---------------------------------------------------------------------------
134 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi)
136 DEF_POINTER_IMAGE_VTK_CREA(v_image,ss_image,p_image,st_image,_image)
137 DEF_POINTER_IMAGE_VTK_CREA(v_image2,ss_image2,p_image2,st_image2,_image2)
139 _countRecursiveFill++;
140 _countProgressingFill++;
141 if (_countProgressingFill > 200000)
143 printf("R %ld \n", _countRecursiveFill);
144 _countProgressingFill = 0;
147 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
148 && (pz >= _minZ) && (pz <= _maxZ))
150 if (_usingAuxImageFill == true)
152 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
154 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
155 _ptrAuxImageFill[ ivi ]=0;
157 _tmpDistfill = (px-_pxfill)*(px-_pxfill)
158 + (py-_pyfill)*(py-_pyfill)
159 + (pz-_pzfill)*(pz-_pzfill);
161 //if (_countRecursiveFill >1 )
163 // printf(" -> %d %d %d cr=%ld r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
168 // _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
169 GETVALUE2_VTK_CREA(_tmpiglfill,p_image,st_image,ivi)
174 // _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
175 GETVALUE2_VTK_CREA(_tmpiglfill2,p_image2,st_image2,ivi)
177 _tmpiglfill2 = _tmpiglfill;
180 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
181 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
182 bool isInRange = false;
184 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
187 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
188 grayLBFMTOL = _RangeMin;
191 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
192 grayLBFPTOL = _RangeMax;
195 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
196 grayLBFMTOL = _RangeMin;
197 grayLBFPTOL = _RangeMax;
203 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
204 && (_tmpiglfill2 != _graylevel)
205 && (_tmpiglfill >= grayLBFMTOL)
206 && (_tmpiglfill <= grayLBFPTOL)
207 && (_tmpDistfill <= _distbasefill); //DFCH
209 _auxGrayLevelValidationFill = false;
213 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
214 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
215 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
216 (_tmpDistfill<=_distbasefill);*/ //DFCH
218 if (_auxGrayLevelValidationFill == true)
220 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
224 // _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
225 SETVALUE2_VTK_CREA(_graylevel,p_image2,st_image2,ivi)
228 // _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
229 SETVALUE2_VTK_CREA(_graylevel,p_image,st_image,ivi)
232 if (_countRecursiveFill < _limitRecursionFill)
237 if (_direction == 0) // YZ
239 //FillToolRecursive(px+1,py,pz);
240 //FillToolRecursive(px-1,py,pz);
241 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
242 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
243 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
244 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
246 if (_direction == 1) // XZ
248 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
249 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
250 //FillToolRecursive(px,py+1,pz);
251 //FillToolRecursive(px,py-1,pz);
252 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
253 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
255 if (_direction == 2) // XY
257 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
258 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
259 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
260 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
261 //FillToolRecursive(px,py,pz-1);
262 //FillToolRecursive(px,py,pz+1);
265 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
266 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
267 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
268 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
269 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
270 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
273 } //_countRecursiveFill
276 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill))
278 _countRecursiveFillProblem++;
281 if (_direction == 0) // YZ
283 //SetAuxImageFill(px+1,py,pz);
284 //SetAuxImageFill(px-1,py,pz);
285 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
286 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
287 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
288 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
290 if (_direction == 1) // XZ
292 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
293 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
294 //SetAuxImageFill(px,py+1,pz);
295 //SetAuxImageFill(px,py-1,pz);
296 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
297 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
299 if (_direction == 2) // XY
301 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
302 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
303 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
304 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
305 //SetAuxImageFill(px,py,pz-1);
306 //SetAuxImageFill(px,py,pz+1);
309 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
310 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
311 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
312 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
313 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
314 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
317 } // _graylevel //_limitRecursionFill
319 } //if _minX _maxX _minY _maxY _minZ _maxZ
320 _countRecursiveFill--;
323 //---------------------------------------------------------------------------
324 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi)
326 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
327 && (pz >= _minZ) && (pz <= _maxZ))
329 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
331 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
332 _ptrAuxImageFill[ ivi ]=1;
336 //---------------------------------------------------------------------------
337 void FillFilter::SetToleranceFill(double tolerancefill)
339 _tolerancefill = tolerancefill;
342 //---------------------------------------------------------------------------
343 void FillFilter::SetDistanceFill(int distancefill)
345 _distancefill = distancefill;