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() {
31 _limitRecursionFill = 50000;
35 //---------------------------------------------------------------------------
36 FillFilter::~FillFilter() {
37 if (_auxImageFill != NULL)
39 _auxImageFill->Delete();
43 //---------------------------------------------------------------------------
44 void FillFilter::SetImage(vtkImageData *image) // virtual
46 baseFilterManualPaint::SetImage(image);
47 if (_auxImageFill != NULL)
49 _auxImageFill->Delete();
51 _auxImageFill = vtkImageData::New();
52 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
53 _auxImageFill->SetOrigin(0, 0, 0);
54 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
55 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
56 _auxImageFill->SetScalarTypeToUnsignedChar();
57 _auxImageFill->AllocateScalars();
60 //---------------------------------------------------------------------------
61 void FillFilter::Run() // virtual
63 if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
64 && (_pz >= _minZ) && (_pz <= _maxZ)) {
65 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,
70 _distbasefill = _distancefill * _distancefill;
71 _countRecursiveFill = 0;
72 _countRecursiveFillProblem = 0;
73 _countProgressingFill = 0;
74 unsigned char *pImage =
75 (unsigned char *) _auxImageFill->GetScalarPointer();
76 _usingAuxImageFill = false;
77 memset(pImage, 0, _maxX * _maxY * _maxZ);
79 FillToolRecursive(_px, _py, _pz);
83 while (_countRecursiveFillProblem != 0) {
84 _countRecursiveFillProblem = 0;
85 _usingAuxImageFill = true;
86 for (ii = 0; ii <= _maxX; ii++) {
87 for (jj = 0; jj <= _maxY; jj++) {
88 for (kk = 0; kk <= _maxZ; kk++) {
90 (unsigned char *) _auxImageFill->GetScalarPointer(
93 FillToolRecursive(ii, jj, kk);
101 } //if _minX _maxX _minY _maxY _minZ _maxZ
104 //---------------------------------------------------------------------------
105 void FillFilter::FillToolRecursive(int px, int py, int pz) {
106 _countRecursiveFill++;
108 _countProgressingFill++;
109 if (_countProgressingFill > 200000) {
110 printf("R %ld \n", _countRecursiveFill);
111 _countProgressingFill = 0;
114 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
115 && (pz >= _minZ) && (pz <= _maxZ)) {
116 if (_usingAuxImageFill == true) {
117 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
118 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
120 _tmpDistfill = (px - _pxfill) * (px - _pxfill)
121 + (py - _pyfill) * (py - _pyfill)
122 + (pz - _pzfill) * (pz - _pzfill);
123 _tmpiglfill = _image->GetScalarComponentAsDouble(px, py, pz, 0);
125 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
126 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
127 bool isInRange = false;
129 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
132 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
133 grayLBFMTOL = _RangeMin;
136 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
137 grayLBFPTOL = _RangeMax;
140 else if ((_RangeMin <= _graylevelbasefill)
141 && (_graylevelbasefill <= _RangeMax)) {
142 grayLBFMTOL = _RangeMin;
143 grayLBFPTOL = _RangeMax;
148 _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
149 && (_tmpiglfill >= grayLBFMTOL)
150 && (_tmpiglfill <= grayLBFPTOL)
151 && (_tmpDistfill <= _distbasefill); //DFCH
154 _auxGrayLevelValidationFill = false;
157 /*_auxGrayLevelValidationFill = (_tmpiglfill!=_graylevel) &&
158 (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
159 (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
160 (_tmpDistfill<=_distbasefill);*/ //DFCH
161 if (_auxGrayLevelValidationFill == true) {
162 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
163 _image->SetScalarComponentFromFloat(px, py, pz, 0,
166 if (_countRecursiveFill < _limitRecursionFill) {
170 if (_direction == 0) // YZ
172 //FillToolRecursive(px+1,py,pz);
173 //FillToolRecursive(px-1,py,pz);
174 FillToolRecursive(px, py + 1, pz);
175 FillToolRecursive(px, py - 1, pz);
176 FillToolRecursive(px, py, pz - 1);
177 FillToolRecursive(px, py, pz + 1);
179 if (_direction == 1) // XZ
181 FillToolRecursive(px + 1, py, pz);
182 FillToolRecursive(px - 1, py, pz);
183 //FillToolRecursive(px,py+1,pz);
184 //FillToolRecursive(px,py-1,pz);
185 FillToolRecursive(px, py, pz - 1);
186 FillToolRecursive(px, py, pz + 1);
188 if (_direction == 2) // XY
190 FillToolRecursive(px + 1, py, pz);
191 FillToolRecursive(px, py + 1, pz);
192 FillToolRecursive(px - 1, py, pz);
193 FillToolRecursive(px, py - 1, pz);
194 //FillToolRecursive(px,py,pz-1);
195 //FillToolRecursive(px,py,pz+1);
199 FillToolRecursive(px + 1, py, pz);
200 FillToolRecursive(px - 1, py, pz);
201 FillToolRecursive(px, py + 1, pz);
202 FillToolRecursive(px, py - 1, pz);
203 FillToolRecursive(px, py, pz - 1);
204 FillToolRecursive(px, py, pz + 1);
207 } //_countRecursiveFill
210 if ((_auxGrayLevelValidationFill == true)
211 && (_countRecursiveFill >= _limitRecursionFill)) {
212 _countRecursiveFillProblem++;
216 if (_direction == 0) // YZ
218 //SetAuxImageFill(px+1,py,pz);
219 //SetAuxImageFill(px-1,py,pz);
220 SetAuxImageFill(px, py + 1, pz);
221 SetAuxImageFill(px, py - 1, pz);
222 SetAuxImageFill(px, py, pz - 1);
223 SetAuxImageFill(px, py, pz + 1);
225 if (_direction == 1) // XZ
227 SetAuxImageFill(px + 1, py, pz);
228 SetAuxImageFill(px - 1, py, pz);
229 //SetAuxImageFill(px,py+1,pz);
230 //SetAuxImageFill(px,py-1,pz);
231 SetAuxImageFill(px, py, pz - 1);
232 SetAuxImageFill(px, py, pz + 1);
234 if (_direction == 2) // XY
236 SetAuxImageFill(px + 1, py, pz);
237 SetAuxImageFill(px - 1, py, pz);
238 SetAuxImageFill(px, py + 1, pz);
239 SetAuxImageFill(px, py - 1, pz);
240 //SetAuxImageFill(px,py,pz-1);
241 //SetAuxImageFill(px,py,pz+1);
245 SetAuxImageFill(px + 1, py, pz);
246 SetAuxImageFill(px - 1, py, pz);
247 SetAuxImageFill(px, py + 1, pz);
248 SetAuxImageFill(px, py - 1, pz);
249 SetAuxImageFill(px, py, pz - 1);
250 SetAuxImageFill(px, py, pz + 1);
253 } // _graylevel //_limitRecursionFill
255 } //if _minX _maxX _minY _maxY _minZ _maxZ
257 _countRecursiveFill--;
261 //---------------------------------------------------------------------------
262 void FillFilter::SetAuxImageFill(int px, int py, int pz) {
263 if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
264 && (pz >= _minZ) && (pz <= _maxZ)) {
265 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
266 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
270 //---------------------------------------------------------------------------
271 void FillFilter::SetToleranceFill(double tolerancefill) {
272 _tolerancefill = tolerancefill;
275 //---------------------------------------------------------------------------
276 void FillFilter::SetDistanceFill(int distancefill) {
277 _distancefill = distancefill;
280 //---------------------------------------------------------------------------
281 void FillFilter::SetRangeMin(int min) {
285 //---------------------------------------------------------------------------
286 void FillFilter::SetRangeMax(int max) {
290 //---------------------------------------------------------------------------
291 int FillFilter::GetRangeMin() {
295 //---------------------------------------------------------------------------
296 int FillFilter::GetRangeMax() {