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);
57 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
58 _auxImageFill->SetScalarTypeToUnsignedChar();
59 _auxImageFill->AllocateScalars();
62 //---------------------------------------------------------------------------
63 void FillFilter::Run() // virtual
65 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
66 && (_pz >= _minZ) && (_pz <= _maxZ)) {
67 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,
72 _distbasefill = _distancefill * _distancefill;
73 _countRecursiveFill = 0;
74 _countRecursiveFillProblem = 0;
75 _countProgressingFill = 0;
76 unsigned char *pImage =
77 (unsigned char *) _auxImageFill->GetScalarPointer();
78 _usingAuxImageFill = false;
79 memset(pImage, 0, _maxX * _maxY * _maxZ);
81 FillToolRecursive(_px, _py, _pz);
85 while (_countRecursiveFillProblem != 0) {
86 _countRecursiveFillProblem = 0;
87 _usingAuxImageFill = true;
88 for (ii = 0; ii <= _maxX; ii++) {
89 for (jj = 0; jj <= _maxY; jj++) {
90 for (kk = 0; kk <= _maxZ; kk++) {
92 (unsigned char *) _auxImageFill->GetScalarPointer(
95 FillToolRecursive(ii, jj, kk);
103 } //if _minX _maxX _minY _maxY _minZ _maxZ
106 //---------------------------------------------------------------------------
107 void FillFilter::FillToolRecursive(int px, int py, int pz)
109 _countRecursiveFill++;
111 _countProgressingFill++;
112 if (_countProgressingFill > 200000)
114 printf("R %ld \n", _countRecursiveFill);
115 _countProgressingFill = 0;
118 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
119 && (pz >= _minZ) && (pz <= _maxZ))
121 if (_usingAuxImageFill == true)
123 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
124 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
126 _tmpDistfill = (px - _pxfill) * (px - _pxfill)
127 + (py - _pyfill) * (py - _pyfill)
128 + (pz - _pzfill) * (pz - _pzfill);
129 _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
133 _tmpiglfill2 = _image2->GetScalarComponentAsDouble(px, py, pz, 0);
135 _tmpiglfill2 = _tmpiglfill;
138 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
139 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
140 bool isInRange = false;
142 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
145 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
146 grayLBFMTOL = _RangeMin;
149 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
150 grayLBFPTOL = _RangeMax;
153 else if ((_RangeMin <= _graylevelbasefill)
154 && (_graylevelbasefill <= _RangeMax)) {
155 grayLBFMTOL = _RangeMin;
156 grayLBFPTOL = _RangeMax;
162 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
163 && (_tmpiglfill2 != _graylevel)
164 && (_tmpiglfill >= grayLBFMTOL)
165 && (_tmpiglfill <= grayLBFPTOL)
166 && (_tmpDistfill <= _distbasefill); //DFCH
168 _auxGrayLevelValidationFill = false;
172 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
173 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
174 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
175 (_tmpDistfill<=_distbasefill);*/ //DFCH
177 if (_auxGrayLevelValidationFill == true)
179 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
182 _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
184 _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
187 if (_countRecursiveFill < _limitRecursionFill)
192 if (_direction == 0) // YZ
194 //FillToolRecursive(px+1,py,pz);
195 //FillToolRecursive(px-1,py,pz);
196 FillToolRecursive(px, py + 1, pz);
197 FillToolRecursive(px, py - 1, pz);
198 FillToolRecursive(px, py, pz - 1);
199 FillToolRecursive(px, py, pz + 1);
201 if (_direction == 1) // XZ
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 == 2) // XY
212 FillToolRecursive(px + 1, py, pz);
213 FillToolRecursive(px, py + 1, pz);
214 FillToolRecursive(px - 1, py, pz);
215 FillToolRecursive(px, py - 1, pz);
216 //FillToolRecursive(px,py,pz-1);
217 //FillToolRecursive(px,py,pz+1);
221 FillToolRecursive(px + 1, py, pz);
222 FillToolRecursive(px - 1, py, pz);
223 FillToolRecursive(px, py + 1, pz);
224 FillToolRecursive(px, py - 1, pz);
225 FillToolRecursive(px, py, pz - 1);
226 FillToolRecursive(px, py, pz + 1);
229 } //_countRecursiveFill
232 if ((_auxGrayLevelValidationFill == true)
233 && (_countRecursiveFill >= _limitRecursionFill))
235 _countRecursiveFillProblem++;
239 if (_direction == 0) // YZ
241 //SetAuxImageFill(px+1,py,pz);
242 //SetAuxImageFill(px-1,py,pz);
243 SetAuxImageFill(px, py + 1, pz);
244 SetAuxImageFill(px, py - 1, pz);
245 SetAuxImageFill(px, py, pz - 1);
246 SetAuxImageFill(px, py, pz + 1);
248 if (_direction == 1) // XZ
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 == 2) // XY
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);
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);
276 } // _graylevel //_limitRecursionFill
278 } //if _minX _maxX _minY _maxY _minZ _maxZ
280 _countRecursiveFill--;
284 //---------------------------------------------------------------------------
285 void FillFilter::SetAuxImageFill(int px, int py, int pz)
287 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
288 && (pz >= _minZ) && (pz <= _maxZ)) {
289 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
290 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
294 //---------------------------------------------------------------------------
295 void FillFilter::SetToleranceFill(double tolerancefill)
297 _tolerancefill = tolerancefill;
300 //---------------------------------------------------------------------------
301 void FillFilter::SetDistanceFill(int distancefill)
303 _distancefill = distancefill;