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) );
76 //---------------------------------------------------------------------------
77 void FillFilter::Run() // virtual
80 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
81 && (_pz >= _minZ) && (_pz <= _maxZ))
83 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,0);
87 _distbasefill = _distancefill * _distancefill;
88 _countRecursiveFill = 0;
89 _countRecursiveFillProblem = 0;
90 _countProgressingFill = 0;
91 _usingAuxImageFill = false;
92 ivi = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1); // index vector image
94 // FillToolRecursive(_px, _py, _pz);
95 FillToolRecursive(_px, _py, _pz, ivi);
97 unsigned char *pImage;
98 pImage = (unsigned char *) _auxImageFill->GetScalarPointer();
101 while (_countRecursiveFillProblem != 0)
103 _countRecursiveFillProblem = 0;
104 _usingAuxImageFill = true;
105 for (kk = 0; kk <= _maxZ; kk++)
107 for (jj = 0; jj <= _maxY; jj++)
109 for (ii = 0; ii <= _maxX; ii++)
111 if (pImage[ivi] == 1)
114 // FillToolRecursive(ii, jj, kk);
115 FillToolRecursive(ii, jj, kk, ivi);
123 } //if _minX _maxX _minY _maxY _minZ _maxZ
126 //---------------------------------------------------------------------------
127 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi)
129 DEF_POINTER_IMAGE_VTK_CREA(v_image,ss_image,p_image,st_image,_image)
130 DEF_POINTER_IMAGE_VTK_CREA(v_image2,ss_image2,p_image2,st_image2,_image2)
132 _countRecursiveFill++;
133 _countProgressingFill++;
134 if (_countProgressingFill > 200000)
136 printf("R %ld \n", _countRecursiveFill);
137 _countProgressingFill = 0;
140 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
141 && (pz >= _minZ) && (pz <= _maxZ))
143 if (_usingAuxImageFill == true)
145 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
147 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
148 _ptrAuxImageFill[ ivi ]=0;
150 _tmpDistfill = (px-_pxfill)*(px-_pxfill)
151 + (py-_pyfill)*(py-_pyfill)
152 + (pz-_pzfill)*(pz-_pzfill);
154 //if (_countRecursiveFill >1 )
156 // printf(" -> %d %d %d cr=%ld r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
161 // _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
162 GETVALUE2_VTK_CREA(_tmpiglfill,p_image,st_image,ivi)
167 // _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
168 GETVALUE2_VTK_CREA(_tmpiglfill2,p_image2,st_image2,ivi)
170 _tmpiglfill2 = _tmpiglfill;
173 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
174 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
175 bool isInRange = false;
177 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
180 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
181 grayLBFMTOL = _RangeMin;
184 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
185 grayLBFPTOL = _RangeMax;
188 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
189 grayLBFMTOL = _RangeMin;
190 grayLBFPTOL = _RangeMax;
196 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
197 && (_tmpiglfill2 != _graylevel)
198 && (_tmpiglfill >= grayLBFMTOL)
199 && (_tmpiglfill <= grayLBFPTOL)
200 && (_tmpDistfill <= _distbasefill); //DFCH
202 _auxGrayLevelValidationFill = false;
206 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
207 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
208 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
209 (_tmpDistfill<=_distbasefill);*/ //DFCH
211 if (_auxGrayLevelValidationFill == true)
213 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
217 // _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
218 SETVALUE2_VTK_CREA(_graylevel,p_image2,st_image2,ivi)
221 // _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
222 SETVALUE2_VTK_CREA(_graylevel,p_image,st_image,ivi)
225 if (_countRecursiveFill < _limitRecursionFill)
230 if (_direction == 0) // YZ
232 //FillToolRecursive(px+1,py,pz);
233 //FillToolRecursive(px-1,py,pz);
234 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
235 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
236 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
237 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
239 if (_direction == 1) // XZ
241 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
242 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
243 //FillToolRecursive(px,py+1,pz);
244 //FillToolRecursive(px,py-1,pz);
245 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
246 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
248 if (_direction == 2) // XY
250 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
251 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
252 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
253 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
254 //FillToolRecursive(px,py,pz-1);
255 //FillToolRecursive(px,py,pz+1);
258 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
259 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
260 FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
261 FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
262 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
263 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
266 } //_countRecursiveFill
269 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill))
271 _countRecursiveFillProblem++;
274 if (_direction == 0) // YZ
276 //SetAuxImageFill(px+1,py,pz);
277 //SetAuxImageFill(px-1,py,pz);
278 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
279 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
280 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
281 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
283 if (_direction == 1) // XZ
285 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
286 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
287 //SetAuxImageFill(px,py+1,pz);
288 //SetAuxImageFill(px,py-1,pz);
289 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
290 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
292 if (_direction == 2) // XY
294 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
295 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
296 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
297 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
298 //SetAuxImageFill(px,py,pz-1);
299 //SetAuxImageFill(px,py,pz+1);
302 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
303 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
304 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
305 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
306 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
307 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
310 } // _graylevel //_limitRecursionFill
312 } //if _minX _maxX _minY _maxY _minZ _maxZ
313 _countRecursiveFill--;
316 //---------------------------------------------------------------------------
317 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi)
319 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
320 && (pz >= _minZ) && (pz <= _maxZ))
322 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
324 // _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
325 _ptrAuxImageFill[ ivi ]=1;
329 //---------------------------------------------------------------------------
330 void FillFilter::SetToleranceFill(double tolerancefill)
332 _tolerancefill = tolerancefill;
335 //---------------------------------------------------------------------------
336 void FillFilter::SetDistanceFill(int distancefill)
338 _distancefill = distancefill;