]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualPaint/FillFilter.cpp
#3207
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / manualPaint / FillFilter.cpp
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26 #include "FillFilter.h"
27
28 FillFilter::FillFilter() 
29 {
30         _tolerancefill          = 50;
31         _distancefill           = 500;
32         _limitRecursionFill = 50000;
33         _auxImageFill           = NULL;
34 }
35
36 //---------------------------------------------------------------------------
37 FillFilter::~FillFilter() 
38 {
39         if (_auxImageFill != NULL)
40         {
41                 _auxImageFill->Delete();
42         }
43 }
44
45 //---------------------------------------------------------------------------
46 void FillFilter::SetImages(vtkImageData *image,vtkImageData *image2) // virtual
47 {
48         baseFilterManualPaint::SetImages(image,image2);
49         if (_auxImageFill != NULL)
50         {
51                 _auxImageFill->Delete();
52         }
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
58
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();
64 #else
65         _auxImageFill->AllocateScalars(VTK_UNSIGNED_CHAR,1);
66 #endif
67
68
69 }
70
71 //---------------------------------------------------------------------------
72 void FillFilter::Run() // virtual
73 {
74         if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
75                         && (_pz >= _minZ) && (_pz <= _maxZ)) {
76                 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,
77                                 0);
78                 _pxfill = _px;
79                 _pyfill = _py;
80                 _pzfill = _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);
89
90                 FillToolRecursive(_px, _py, _pz);
91 //              printf("--\n");
92
93                 int ii, jj, kk;
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++) {
100                                                 pImage =
101                                                                 (unsigned char *) _auxImageFill->GetScalarPointer(
102                                                                                 ii, jj, kk);
103                                                 if ((*pImage) == 1) {
104                                                         FillToolRecursive(ii, jj, kk);
105                                                 }
106                                         } // for kk
107                                 } // for jj
108                         } //for ii
109 //                      printf("-\n");
110                 } // while
111
112         } //if _minX _maxX _minY _maxY _minZ _maxZ
113 }
114
115 //---------------------------------------------------------------------------
116 void FillFilter::FillToolRecursive(int px, int py, int pz) 
117 {
118         _countRecursiveFill++;
119
120         _countProgressingFill++;
121         if (_countProgressingFill > 200000) 
122         {
123                 printf("R %ld \n", _countRecursiveFill);
124                 _countProgressingFill = 0;
125         }
126
127         if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
128                         && (pz >= _minZ) && (pz <= _maxZ)) 
129         {
130                 if (_usingAuxImageFill == true) 
131                 {
132                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
133                         _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
134                 }
135                 _tmpDistfill = (px - _pxfill) * (px - _pxfill)
136                                 + (py - _pyfill) * (py - _pyfill)
137                                 + (pz - _pzfill) * (pz - _pzfill);
138                 _tmpiglfill     = _image->GetScalarComponentAsDouble(px, py, pz, 0);
139
140                 if (_image2!=NULL)
141                 {
142                         _tmpiglfill2    =       _image2->GetScalarComponentAsDouble(px, py, pz, 0);
143                 } else {
144                         _tmpiglfill2    =       _tmpiglfill;
145                 }
146
147                 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
148                 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
149                 bool isInRange = false;
150                 //DFCH
151                 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
152                         isInRange = true;
153                 } //fi esle
154                 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
155                         grayLBFMTOL = _RangeMin;
156                         isInRange = true;
157                 } //fi esle
158                 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
159                         grayLBFPTOL = _RangeMax;
160                         isInRange = true;
161                 } //fi esle
162                 else if ((_RangeMin <= _graylevelbasefill)
163                                 && (_graylevelbasefill <= _RangeMax)) {
164                         grayLBFMTOL = _RangeMin;
165                         grayLBFPTOL = _RangeMax;
166                         isInRange = true;
167                 } //fi esle
168
169                 if (isInRange) 
170                 {
171                         _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
172                                         && (_tmpiglfill2 != _graylevel)
173                                         && (_tmpiglfill >= grayLBFMTOL)
174                                         && (_tmpiglfill <= grayLBFPTOL)
175                                         && (_tmpDistfill <= _distbasefill); //DFCH
176                 } else {
177                         _auxGrayLevelValidationFill = false;
178                 } // if isInRange
179
180                   //DFCH
181                 /*_auxGrayLevelValidationFill =   (_tmpiglfill!=_graylevel) &&
182                  (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
183                  (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
184                  (_tmpDistfill<=_distbasefill);*/ //DFCH
185
186                 if (_auxGrayLevelValidationFill == true) 
187                 {
188                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
189                         if (_image2!=NULL)
190                         {
191                                 _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
192                         } else {
193                                 _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
194                         }
195
196                         if (_countRecursiveFill < _limitRecursionFill) 
197                         {
198
199                                 if (_2D3D == 0) //2D
200                                 {
201                                         if (_direction == 0) // YZ
202                                         {
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);
209                                         }
210                                         if (_direction == 1) // XZ
211                                                         {
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);
218                                         }
219                                         if (_direction == 2) // XY
220                                                         {
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);
227                                         }
228                                 } else { // 3D
229
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);
236                                 } // 2D 3D
237
238                         } //_countRecursiveFill
239                 } // _graylevel
240
241                 if ((_auxGrayLevelValidationFill == true)
242                                 && (_countRecursiveFill >= _limitRecursionFill)) 
243                 {
244                         _countRecursiveFillProblem++;
245
246                         if (_2D3D == 0) //2D
247                                         {
248                                 if (_direction == 0) // YZ
249                                                 {
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);
256                                 }
257                                 if (_direction == 1) // XZ
258                                                 {
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);
265                                 }
266                                 if (_direction == 2) // XY
267                                                 {
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);
274                                 }
275                         } else { // 3D
276
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);
283                         } // 2D 3D
284
285                 } // _graylevel   //_limitRecursionFill
286
287         } //if _minX _maxX _minY _maxY _minZ _maxZ
288
289         _countRecursiveFill--;
290
291 }
292
293 //---------------------------------------------------------------------------
294 void FillFilter::SetAuxImageFill(int px, int py, int pz) 
295 {
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);
300         }
301 }
302
303 //---------------------------------------------------------------------------
304 void FillFilter::SetToleranceFill(double tolerancefill) 
305 {
306         _tolerancefill = tolerancefill;
307 }
308
309 //---------------------------------------------------------------------------
310 void FillFilter::SetDistanceFill(int distancefill) 
311 {
312         _distancefill = distancefill;
313 }
314
315