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;
100 ivi = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1); // index vector image
103 DEF_POINTER_IMAGE_VTK_CREA_set(v_image,ss_image,p_image,st_image,_image)
104 DEF_POINTER_IMAGE_VTK_CREA_set(v_image2,ss_image2,p_image2,st_image2,_image2)
109 // FillToolRecursive(_px, _py, _pz);
110 FillToolRecursive(_px, _py, _pz, ivi);
112 unsigned char *pImage;
113 pImage = (unsigned char *) _auxImageFill->GetScalarPointer();
116 while (_countRecursiveFillProblem != 0)
118 _countRecursiveFillProblem = 0;
119 _usingAuxImageFill = true;
120 for (kk = 0; kk <= _maxZ; kk++)
122 for (jj = 0; jj <= _maxY; jj++)
124 for (ii = 0; ii <= _maxX; ii++)
126 if (pImage[ivi] == 1)
129 // FillToolRecursive(ii, jj, kk);
130 FillToolRecursive(ii, jj, kk, ivi);
138 } //if _minX _maxX _minY _maxY _minZ _maxZ
141 //---------------------------------------------------------------------------
142 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi)
144 //ups ??EED DEF_POINTER_IMAGE_VTK_CREA(v_image,ss_image,p_image,st_image,_image)
145 //ups ??EED DEF_POINTER_IMAGE_VTK_CREA(v_image2,ss_image2,p_image2,st_image2,_image2)
147 _countRecursiveFill++;
148 _countProgressingFill++;
149 if (_countProgressingFill > 200000)
151 printf("R %ld \n", _countRecursiveFill);
152 _countProgressingFill = 0;
155 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
156 && (pz >= _minZ) && (pz <= _maxZ))
158 if (_usingAuxImageFill == true)
160 //UPS ??EED this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
162 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
163 _ptrAuxImageFill[ ivi ]=0;
168 _tmpDistfill = difX*difX + difY*difY + difZ*difZ;
169 // _tmpDistfill = (px-_pxfill)*(px-_pxfill)
170 // + (py-_pyfill)*(py-_pyfill)
171 // + (pz-_pzfill)*(pz-_pzfill);
173 //if (_countRecursiveFill >1 )
175 // printf(" -> %d %d %d cr=%ld r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
180 // _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
181 GETVALUE2_VTK_CREA(_tmpiglfill,p_image,st_image,ivi)
186 // _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
187 GETVALUE2_VTK_CREA(_tmpiglfill2,p_image2,st_image2,ivi)
189 _tmpiglfill2 = _tmpiglfill;
192 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
193 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
194 bool isInRange = false;
196 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
199 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
200 grayLBFMTOL = _RangeMin;
203 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
204 grayLBFPTOL = _RangeMax;
207 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
208 grayLBFMTOL = _RangeMin;
209 grayLBFPTOL = _RangeMax;
215 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
216 && (_tmpiglfill2 != _graylevel)
217 && (_tmpiglfill >= grayLBFMTOL)
218 && (_tmpiglfill <= grayLBFPTOL)
219 && (_tmpDistfill <= _distbasefill); //DFCH
221 _auxGrayLevelValidationFill = false;
225 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
226 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
227 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
228 (_tmpDistfill<=_distbasefill);*/ //DFCH
230 if (_auxGrayLevelValidationFill == true)
232 //UPS ??EED this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
236 // _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
237 SETVALUE2_VTK_CREA(_graylevel,p_image2,st_image2,ivi)
240 // _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
241 SETVALUE2_VTK_CREA(_graylevel,p_image,st_image,ivi)
244 if (_countRecursiveFill < _limitRecursionFill)
249 if (_direction == 0) // YZ
251 //FillToolRecursive(px+1,py,pz);
252 //FillToolRecursive(px-1,py,pz);
253 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
254 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
255 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
256 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
258 if (_direction == 1) // XZ
260 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
261 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
262 //FillToolRecursive(px,py+1,pz);
263 //FillToolRecursive(px,py-1,pz);
264 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
265 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
267 if (_direction == 2) // XY
269 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
270 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
271 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
272 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
273 //FillToolRecursive(px,py,pz-1);
274 //FillToolRecursive(px,py,pz+1);
277 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
278 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
279 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
280 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
281 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
282 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
285 } //_countRecursiveFill
288 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill))
290 _countRecursiveFillProblem++;
293 if (_direction == 0) // YZ
295 //SetAuxImageFill(px+1,py,pz);
296 //SetAuxImageFill(px-1,py,pz);
297 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
298 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
299 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
300 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
302 if (_direction == 1) // XZ
304 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
305 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
306 //SetAuxImageFill(px,py+1,pz);
307 //SetAuxImageFill(px,py-1,pz);
308 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
309 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
311 if (_direction == 2) // XY
313 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
314 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
315 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
316 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
317 //SetAuxImageFill(px,py,pz-1);
318 //SetAuxImageFill(px,py,pz+1);
321 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
322 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
323 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
324 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
325 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
326 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
329 } // _graylevel //_limitRecursionFill
331 } //if _minX _maxX _minY _maxY _minZ _maxZ
332 _countRecursiveFill--;
335 //---------------------------------------------------------------------------
336 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi)
338 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
339 && (pz >= _minZ) && (pz <= _maxZ))
341 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
343 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
344 _ptrAuxImageFill[ ivi ]=1;
348 //---------------------------------------------------------------------------
349 void FillFilter::SetToleranceFill(double tolerancefill)
351 _tolerancefill = tolerancefill;
354 //---------------------------------------------------------------------------
355 void FillFilter::SetDistanceFill(int distancefill)
357 _distancefill = distancefill;