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"
28 FillFilter::FillFilter()
32 _limitRecursionFill = 50000;
39 //---------------------------------------------------------------------------
40 FillFilter::~FillFilter()
42 if (_auxImageFill != NULL)
44 _auxImageFill->Delete();
48 //---------------------------------------------------------------------------
49 void FillFilter::SetImages(vtkImageData *image,vtkImageData *image2) // virtual
51 baseFilterManualPaint::SetImages(image,image2);
53 if ((_maxX!=_maxXback) || (_maxY!=_maxYback) || (_maxZ!=_maxZback))
55 if (_auxImageFill != NULL)
57 _auxImageFill->Delete();
59 _auxImageFill = vtkImageData::New();
60 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
61 _auxImageFill->SetOrigin(0, 0, 0);
62 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
63 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
64 _auxImageFill->SetScalarTypeToUnsignedChar();
65 _auxImageFill->AllocateScalars();
66 _ptrAuxImageFill = (unsigned char *)_auxImageFill->GetScalarPointer();
67 memset(_ptrAuxImageFill, 0, (_maxX+1) * (_maxY+1) * (_maxZ+1) );
75 //---------------------------------------------------------------------------
76 void FillFilter::Run() // virtual
79 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
80 && (_pz >= _minZ) && (_pz <= _maxZ))
82 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,0);
86 _distbasefill = _distancefill * _distancefill;
87 _countRecursiveFill = 0;
88 _countRecursiveFillProblem = 0;
89 _countProgressingFill = 0;
90 _usingAuxImageFill = false;
91 ivi = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1); // index vector image
93 // FillToolRecursive(_px, _py, _pz);
94 FillToolRecursive(_px, _py, _pz, ivi);
96 unsigned char *pImage;
97 pImage = (unsigned char *) _auxImageFill->GetScalarPointer();
100 while (_countRecursiveFillProblem != 0)
102 _countRecursiveFillProblem = 0;
103 _usingAuxImageFill = true;
104 for (kk = 0; kk <= _maxZ; kk++)
106 for (jj = 0; jj <= _maxY; jj++)
108 for (ii = 0; ii <= _maxX; ii++)
110 if (pImage[ivi] == 1)
113 // FillToolRecursive(ii, jj, kk);
114 FillToolRecursive(ii, jj, kk, ivi);
122 } //if _minX _maxX _minY _maxY _minZ _maxZ
125 //---------------------------------------------------------------------------
126 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi)
128 DEFINEPOINTERIMAGE_MANUALPAINT(v_image,ss_image,p_image,st_image,_image)
129 DEFINEPOINTERIMAGE_MANUALPAINT(v_image2,ss_image2,p_image2,st_image2,_image2)
131 _countRecursiveFill++;
132 _countProgressingFill++;
133 if (_countProgressingFill > 200000)
135 printf("R %ld \n", _countRecursiveFill);
136 _countProgressingFill = 0;
139 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
140 && (pz >= _minZ) && (pz <= _maxZ))
142 if (_usingAuxImageFill == true)
144 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
146 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
147 _ptrAuxImageFill[ ivi ]=0;
149 _tmpDistfill = (px-_pxfill)*(px-_pxfill)
150 + (py-_pyfill)*(py-_pyfill)
151 + (pz-_pzfill)*(pz-_pzfill);
153 //if (_countRecursiveFill >1 )
155 // printf(" -> %d %d %d cr=%ld r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
160 // _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
161 GETVALUE2_MANUALPAINT(_tmpiglfill,p_image,st_image,ivi)
166 // _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
167 GETVALUE2_MANUALPAINT(_tmpiglfill2,p_image2,st_image2,ivi)
169 _tmpiglfill2 = _tmpiglfill;
172 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
173 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
174 bool isInRange = false;
176 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
179 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
180 grayLBFMTOL = _RangeMin;
183 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
184 grayLBFPTOL = _RangeMax;
187 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
188 grayLBFMTOL = _RangeMin;
189 grayLBFPTOL = _RangeMax;
195 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
196 && (_tmpiglfill2 != _graylevel)
197 && (_tmpiglfill >= grayLBFMTOL)
198 && (_tmpiglfill <= grayLBFPTOL)
199 && (_tmpDistfill <= _distbasefill); //DFCH
201 _auxGrayLevelValidationFill = false;
205 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
206 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
207 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
208 (_tmpDistfill<=_distbasefill);*/ //DFCH
210 if (_auxGrayLevelValidationFill == true)
212 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
216 // _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
217 SETVALUE2_MANUALPAINT(_graylevel,p_image2,st_image2,ivi)
220 // _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
221 SETVALUE2_MANUALPAINT(_graylevel,p_image,st_image,ivi)
224 if (_countRecursiveFill < _limitRecursionFill)
229 if (_direction == 0) // YZ
231 //FillToolRecursive(px+1,py,pz);
232 //FillToolRecursive(px-1,py,pz);
233 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
234 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
235 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
236 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
238 if (_direction == 1) // XZ
240 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
241 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
242 //FillToolRecursive(px,py+1,pz);
243 //FillToolRecursive(px,py-1,pz);
244 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
245 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
247 if (_direction == 2) // XY
249 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
250 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
251 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
252 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
253 //FillToolRecursive(px,py,pz-1);
254 //FillToolRecursive(px,py,pz+1);
257 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
258 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
259 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
260 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
261 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
262 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
265 } //_countRecursiveFill
268 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill))
270 _countRecursiveFillProblem++;
273 if (_direction == 0) // YZ
275 //SetAuxImageFill(px+1,py,pz);
276 //SetAuxImageFill(px-1,py,pz);
277 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
278 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
279 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
280 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
282 if (_direction == 1) // XZ
284 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
285 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
286 //SetAuxImageFill(px,py+1,pz);
287 //SetAuxImageFill(px,py-1,pz);
288 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
289 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
291 if (_direction == 2) // XY
293 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
294 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
295 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
296 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
297 //SetAuxImageFill(px,py,pz-1);
298 //SetAuxImageFill(px,py,pz+1);
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,ivi-_OnePlane);
306 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
309 } // _graylevel //_limitRecursionFill
311 } //if _minX _maxX _minY _maxY _minZ _maxZ
312 _countRecursiveFill--;
315 //---------------------------------------------------------------------------
316 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi)
318 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
319 && (pz >= _minZ) && (pz <= _maxZ))
321 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
323 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
324 _ptrAuxImageFill[ ivi ]=1;
328 //---------------------------------------------------------------------------
329 void FillFilter::SetToleranceFill(double tolerancefill)
331 _tolerancefill = tolerancefill;
334 //---------------------------------------------------------------------------
335 void FillFilter::SetDistanceFill(int distancefill)
337 _distancefill = distancefill;