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
52 baseFilterManualPaint::SetImages(image,image2);
54 if ((_maxX!=_maxXback) || (_maxY!=_maxYback) || (_maxZ!=_maxZback))
56 if (_auxImageFill != NULL)
58 _auxImageFill->Delete();
60 _auxImageFill = vtkImageData::New();
61 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
62 _auxImageFill->SetOrigin(0, 0, 0);
63 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
64 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
65 _auxImageFill->SetScalarTypeToUnsignedChar();
66 _auxImageFill->AllocateScalars();
67 _ptrAuxImageFill = (unsigned char *)_auxImageFill->GetScalarPointer();
68 memset(_ptrAuxImageFill, 0, (_maxX+1) * (_maxY+1) * (_maxZ+1) );
71 _auxImageFill = vtkImageData::New();
72 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
73 _auxImageFill->SetOrigin(0, 0, 0);
74 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
77 //EED 2017-01-01 Migration VTK7
78 #if VTK_MAJOR_VERSION <= 5
79 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
80 _auxImageFill->SetScalarTypeToUnsignedChar();
81 _auxImageFill->AllocateScalars();
83 _auxImageFill->AllocateScalars(VTK_UNSIGNED_CHAR,1);
92 //---------------------------------------------------------------------------
93 void FillFilter::Run() // virtual
96 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
97 && (_pz >= _minZ) && (_pz <= _maxZ))
99 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,0);
103 _distbasefill = _distancefill * _distancefill;
104 _countRecursiveFill = 0;
105 _countRecursiveFillProblem = 0;
106 _countProgressingFill = 0;
107 _usingAuxImageFill = false;
108 ivi = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1); // index vector image
110 // FillToolRecursive(_px, _py, _pz);
111 FillToolRecursive(_px, _py, _pz, ivi);
113 unsigned char *pImage;
114 pImage = (unsigned char *) _auxImageFill->GetScalarPointer();
117 while (_countRecursiveFillProblem != 0)
119 _countRecursiveFillProblem = 0;
120 _usingAuxImageFill = true;
121 for (kk = 0; kk <= _maxZ; kk++)
123 for (jj = 0; jj <= _maxY; jj++)
125 for (ii = 0; ii <= _maxX; ii++)
127 if (pImage[ivi] == 1)
130 // FillToolRecursive(ii, jj, kk);
131 FillToolRecursive(ii, jj, kk, ivi);
139 } //if _minX _maxX _minY _maxY _minZ _maxZ
142 //---------------------------------------------------------------------------
143 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi)
145 DEF_POINTER_IMAGE_VTK_CREA(v_image,ss_image,p_image,st_image,_image)
146 DEF_POINTER_IMAGE_VTK_CREA(v_image2,ss_image2,p_image2,st_image2,_image2)
148 _countRecursiveFill++;
149 _countProgressingFill++;
150 if (_countProgressingFill > 200000)
152 printf("R %ld \n", _countRecursiveFill);
153 _countProgressingFill = 0;
156 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
157 && (pz >= _minZ) && (pz <= _maxZ))
159 if (_usingAuxImageFill == true)
161 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
163 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
164 _ptrAuxImageFill[ ivi ]=0;
166 _tmpDistfill = (px-_pxfill)*(px-_pxfill)
167 + (py-_pyfill)*(py-_pyfill)
168 + (pz-_pzfill)*(pz-_pzfill);
170 //if (_countRecursiveFill >1 )
172 // printf(" -> %d %d %d cr=%ld r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
177 // _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
178 GETVALUE2_VTK_CREA(_tmpiglfill,p_image,st_image,ivi)
183 // _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
184 GETVALUE2_VTK_CREA(_tmpiglfill2,p_image2,st_image2,ivi)
186 _tmpiglfill2 = _tmpiglfill;
189 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
190 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
191 bool isInRange = false;
193 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
196 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
197 grayLBFMTOL = _RangeMin;
200 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
201 grayLBFPTOL = _RangeMax;
204 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
205 grayLBFMTOL = _RangeMin;
206 grayLBFPTOL = _RangeMax;
212 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
213 && (_tmpiglfill2 != _graylevel)
214 && (_tmpiglfill >= grayLBFMTOL)
215 && (_tmpiglfill <= grayLBFPTOL)
216 && (_tmpDistfill <= _distbasefill); //DFCH
218 _auxGrayLevelValidationFill = false;
222 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
223 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
224 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
225 (_tmpDistfill<=_distbasefill);*/ //DFCH
227 if (_auxGrayLevelValidationFill == true)
229 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
233 // _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
234 SETVALUE2_VTK_CREA(_graylevel,p_image2,st_image2,ivi)
237 // _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
238 SETVALUE2_VTK_CREA(_graylevel,p_image,st_image,ivi)
241 if (_countRecursiveFill < _limitRecursionFill)
246 if (_direction == 0) // YZ
248 //FillToolRecursive(px+1,py,pz);
249 //FillToolRecursive(px-1,py,pz);
250 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
251 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
252 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
253 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
255 if (_direction == 1) // XZ
257 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
258 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
259 //FillToolRecursive(px,py+1,pz);
260 //FillToolRecursive(px,py-1,pz);
261 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
262 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
264 if (_direction == 2) // XY
266 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
267 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
268 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
269 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
270 //FillToolRecursive(px,py,pz-1);
271 //FillToolRecursive(px,py,pz+1);
274 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
275 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
276 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
277 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
278 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
279 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
282 } //_countRecursiveFill
285 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill))
287 _countRecursiveFillProblem++;
290 if (_direction == 0) // YZ
292 //SetAuxImageFill(px+1,py,pz);
293 //SetAuxImageFill(px-1,py,pz);
294 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
295 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
296 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
297 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
299 if (_direction == 1) // XZ
301 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
302 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
303 //SetAuxImageFill(px,py+1,pz);
304 //SetAuxImageFill(px,py-1,pz);
305 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
306 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
308 if (_direction == 2) // XY
310 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
311 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
312 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
313 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
314 //SetAuxImageFill(px,py,pz-1);
315 //SetAuxImageFill(px,py,pz+1);
318 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
319 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
320 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
321 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
322 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
323 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
326 } // _graylevel //_limitRecursionFill
328 } //if _minX _maxX _minY _maxY _minZ _maxZ
329 _countRecursiveFill--;
332 //---------------------------------------------------------------------------
333 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi)
335 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
336 && (pz >= _minZ) && (pz <= _maxZ))
338 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
340 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
341 _ptrAuxImageFill[ ivi ]=1;
345 //---------------------------------------------------------------------------
346 void FillFilter::SetToleranceFill(double tolerancefill)
348 _tolerancefill = tolerancefill;
351 //---------------------------------------------------------------------------
352 void FillFilter::SetDistanceFill(int distancefill)
354 _distancefill = distancefill;