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;
36 //---------------------------------------------------------------------------
37 FillFilter::~FillFilter()
39 if (_auxImageFill != NULL)
41 _auxImageFill->Delete();
45 //---------------------------------------------------------------------------
46 void FillFilter::SetImages(vtkImageData *image,vtkImageData *image2) // virtual
48 baseFilterManualPaint::SetImages(image,image2);
49 if (_auxImageFill != NULL)
51 _auxImageFill->Delete();
53 _auxImageFill = vtkImageData::New();
54 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
55 _auxImageFill->SetOrigin(0, 0, 0);
56 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
59 //EED 2017-01-01 Migration VTK7
60 #if VTK_MAJOR_VERSION <= 5
61 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
62 _auxImageFill->SetScalarTypeToUnsignedChar();
63 _auxImageFill->AllocateScalars();
65 _auxImageFill->AllocateScalars(VTK_UNSIGNED_CHAR,1);
71 //---------------------------------------------------------------------------
72 void FillFilter::Run() // virtual
74 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
75 && (_pz >= _minZ) && (_pz <= _maxZ)) {
76 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,
81 _distbasefill = _distancefill * _distancefill;
82 _countRecursiveFill = 0;
83 _countRecursiveFillProblem = 0;
84 _countProgressingFill = 0;
85 unsigned char *pImage =
86 (unsigned char *) _auxImageFill->GetScalarPointer();
87 _usingAuxImageFill = false;
88 memset(pImage, 0, _maxX * _maxY * _maxZ);
90 FillToolRecursive(_px, _py, _pz);
94 while (_countRecursiveFillProblem != 0) {
95 _countRecursiveFillProblem = 0;
96 _usingAuxImageFill = true;
97 for (ii = 0; ii <= _maxX; ii++) {
98 for (jj = 0; jj <= _maxY; jj++) {
99 for (kk = 0; kk <= _maxZ; kk++) {
101 (unsigned char *) _auxImageFill->GetScalarPointer(
103 if ((*pImage) == 1) {
104 FillToolRecursive(ii, jj, kk);
112 } //if _minX _maxX _minY _maxY _minZ _maxZ
115 //---------------------------------------------------------------------------
116 void FillFilter::FillToolRecursive(int px, int py, int pz)
118 _countRecursiveFill++;
120 _countProgressingFill++;
121 if (_countProgressingFill > 200000)
123 printf("R %ld \n", _countRecursiveFill);
124 _countProgressingFill = 0;
127 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
128 && (pz >= _minZ) && (pz <= _maxZ))
130 if (_usingAuxImageFill == true)
132 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
133 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
135 _tmpDistfill = (px - _pxfill) * (px - _pxfill)
136 + (py - _pyfill) * (py - _pyfill)
137 + (pz - _pzfill) * (pz - _pzfill);
138 _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
142 _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
144 _tmpiglfill2 = _tmpiglfill;
147 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
148 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
149 bool isInRange = false;
151 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
154 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
155 grayLBFMTOL = _RangeMin;
158 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
159 grayLBFPTOL = _RangeMax;
162 else if ((_RangeMin <= _graylevelbasefill)
163 && (_graylevelbasefill <= _RangeMax)) {
164 grayLBFMTOL = _RangeMin;
165 grayLBFPTOL = _RangeMax;
171 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
172 && (_tmpiglfill2 != _graylevel)
173 && (_tmpiglfill >= grayLBFMTOL)
174 && (_tmpiglfill <= grayLBFPTOL)
175 && (_tmpDistfill <= _distbasefill); //DFCH
177 _auxGrayLevelValidationFill = false;
181 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
182 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
183 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
184 (_tmpDistfill<=_distbasefill);*/ //DFCH
186 if (_auxGrayLevelValidationFill == true)
188 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
191 _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
193 _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
196 if (_countRecursiveFill < _limitRecursionFill)
201 if (_direction == 0) // YZ
203 //FillToolRecursive(px+1,py,pz);
204 //FillToolRecursive(px-1,py,pz);
205 FillToolRecursive(px, py + 1, pz);
206 FillToolRecursive(px, py - 1, pz);
207 FillToolRecursive(px, py, pz - 1);
208 FillToolRecursive(px, py, pz + 1);
210 if (_direction == 1) // XZ
212 FillToolRecursive(px + 1, py, pz);
213 FillToolRecursive(px - 1, py, pz);
214 //FillToolRecursive(px,py+1,pz);
215 //FillToolRecursive(px,py-1,pz);
216 FillToolRecursive(px, py, pz - 1);
217 FillToolRecursive(px, py, pz + 1);
219 if (_direction == 2) // XY
221 FillToolRecursive(px + 1, py, pz);
222 FillToolRecursive(px, py + 1, pz);
223 FillToolRecursive(px - 1, py, pz);
224 FillToolRecursive(px, py - 1, pz);
225 //FillToolRecursive(px,py,pz-1);
226 //FillToolRecursive(px,py,pz+1);
230 FillToolRecursive(px + 1, py, pz);
231 FillToolRecursive(px - 1, py, pz);
232 FillToolRecursive(px, py + 1, pz);
233 FillToolRecursive(px, py - 1, pz);
234 FillToolRecursive(px, py, pz - 1);
235 FillToolRecursive(px, py, pz + 1);
238 } //_countRecursiveFill
241 if ((_auxGrayLevelValidationFill == true)
242 && (_countRecursiveFill >= _limitRecursionFill))
244 _countRecursiveFillProblem++;
248 if (_direction == 0) // YZ
250 //SetAuxImageFill(px+1,py,pz);
251 //SetAuxImageFill(px-1,py,pz);
252 SetAuxImageFill(px, py + 1, pz);
253 SetAuxImageFill(px, py - 1, pz);
254 SetAuxImageFill(px, py, pz - 1);
255 SetAuxImageFill(px, py, pz + 1);
257 if (_direction == 1) // XZ
259 SetAuxImageFill(px + 1, py, pz);
260 SetAuxImageFill(px - 1, py, pz);
261 //SetAuxImageFill(px,py+1,pz);
262 //SetAuxImageFill(px,py-1,pz);
263 SetAuxImageFill(px, py, pz - 1);
264 SetAuxImageFill(px, py, pz + 1);
266 if (_direction == 2) // XY
268 SetAuxImageFill(px + 1, py, pz);
269 SetAuxImageFill(px - 1, py, pz);
270 SetAuxImageFill(px, py + 1, pz);
271 SetAuxImageFill(px, py - 1, pz);
272 //SetAuxImageFill(px,py,pz-1);
273 //SetAuxImageFill(px,py,pz+1);
277 SetAuxImageFill(px + 1, py, pz);
278 SetAuxImageFill(px - 1, py, pz);
279 SetAuxImageFill(px, py + 1, pz);
280 SetAuxImageFill(px, py - 1, pz);
281 SetAuxImageFill(px, py, pz - 1);
282 SetAuxImageFill(px, py, pz + 1);
285 } // _graylevel //_limitRecursionFill
287 } //if _minX _maxX _minY _maxY _minZ _maxZ
289 _countRecursiveFill--;
293 //---------------------------------------------------------------------------
294 void FillFilter::SetAuxImageFill(int px, int py, int pz)
296 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
297 && (pz >= _minZ) && (pz <= _maxZ)) {
298 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
299 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
303 //---------------------------------------------------------------------------
304 void FillFilter::SetToleranceFill(double tolerancefill)
306 _tolerancefill = tolerancefill;
309 //---------------------------------------------------------------------------
310 void FillFilter::SetDistanceFill(int distancefill)
312 _distancefill = distancefill;