]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualPaint/FillFilter.cpp
3f9c7ad0fb9ad34e7e8cc6537c75d52c2e529e73
[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         _maxXback                       = 0;
35         _maxYback                       = 0;
36         _maxZback                       = 0;
37 }
38
39 //---------------------------------------------------------------------------
40 FillFilter::~FillFilter() 
41 {
42         if (_auxImageFill != NULL)
43         {
44                 _auxImageFill->Delete();
45         }
46 }
47
48 //---------------------------------------------------------------------------
49 void FillFilter::SetImages(vtkImageData *image,vtkImageData *image2) // virtual
50 {
51         baseFilterManualPaint::SetImages(image,image2);
52         
53         if ((_maxX!=_maxXback) || (_maxY!=_maxYback) || (_maxZ!=_maxZback)) 
54         {
55                 if (_auxImageFill != NULL)
56                 {
57                         _auxImageFill->Delete();
58                 }
59                 _auxImageFill = vtkImageData::New();
60                 _auxImageFill->SetDimensions(_maxX + 1, _maxY + 1, _maxZ + 1);
61                 _auxImageFill->SetOrigin(0, 0, 0);
62                 _auxImageFill->SetExtent(0, _maxX, 0, _maxY, 0, _maxZ);
63                 _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
64                 _auxImageFill->SetScalarTypeToUnsignedChar();
65                 _auxImageFill->AllocateScalars();
66                 _ptrAuxImageFill = (unsigned char       *)_auxImageFill->GetScalarPointer();
67                 memset(_ptrAuxImageFill, 0, (_maxX+1) * (_maxY+1) * (_maxZ+1) );
68         }
69         _maxXback=_maxX;
70         _maxYback=_maxY;
71         _maxYback=_maxZ;
72 }
73
74
75 //---------------------------------------------------------------------------
76 void FillFilter::Run() // virtual
77 {
78         long int ivi;
79         if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
80                         && (_pz >= _minZ) && (_pz <= _maxZ)) 
81         {
82                 _graylevelbasefill                      = _image->GetScalarComponentAsDouble(_px, _py, _pz,0);
83                 _pxfill                                         = _px;
84                 _pyfill                                         = _py;
85                 _pzfill                                         = _pz;
86                 _distbasefill                           = _distancefill * _distancefill;
87                 _countRecursiveFill             = 0;
88                 _countRecursiveFillProblem      = 0;
89                 _countProgressingFill           = 0;
90                 _usingAuxImageFill                      = false;
91                 ivi                                             = _px + _py*(_maxX+1) + _pz*(_maxX+1)*(_maxY+1);  // index vector image
92 //EED01
93 //              FillToolRecursive(_px, _py, _pz);
94                 FillToolRecursive(_px, _py, _pz, ivi);
95
96                 unsigned char *pImage;
97                 pImage                                          = (unsigned char *) _auxImageFill->GetScalarPointer();
98                 int ii, jj, kk;
99                 ivi                                                     = 0;
100                 while (_countRecursiveFillProblem != 0) 
101                 {
102                         _countRecursiveFillProblem  = 0;
103                         _usingAuxImageFill                      = true;
104                         for (kk = 0; kk <= _maxZ; kk++) 
105                         {
106                                 for (jj = 0; jj <= _maxY; jj++) 
107                                 {
108                                         for (ii = 0; ii <= _maxX; ii++) 
109                                         {
110                                                 if (pImage[ivi] == 1) 
111                                                 {
112 //EED01
113 //                                                      FillToolRecursive(ii, jj, kk);
114                                                         FillToolRecursive(ii, jj, kk, ivi);
115                                                 }  // if
116                                                 ivi++;
117                                         } // for ii
118                                 } // for jj
119                         } //for kk
120 //                      printf("-\n");
121                 } // while
122         } //if _minX _maxX _minY _maxY _minZ _maxZ
123 }
124
125 //---------------------------------------------------------------------------
126 void FillFilter::FillToolRecursive(int px, int py, int pz,long int ivi) 
127 {
128 DEFINEPOINTERIMAGE_MANUALPAINT(v_image,ss_image,p_image,st_image,_image)                
129 DEFINEPOINTERIMAGE_MANUALPAINT(v_image2,ss_image2,p_image2,st_image2,_image2)           
130
131         _countRecursiveFill++;
132         _countProgressingFill++;
133         if (_countProgressingFill > 200000) 
134         {
135                 printf("R %ld \n", _countRecursiveFill);
136                 _countProgressingFill = 0;
137         }
138
139         if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
140                         && (pz >= _minZ) && (pz <= _maxZ)) 
141         {
142                 if (_usingAuxImageFill == true) 
143                 {
144                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
145 //EED01
146 //                      _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
147                         _ptrAuxImageFill[ ivi ]=0;
148                 }
149                 _tmpDistfill = (px-_pxfill)*(px-_pxfill)
150                                         +  (py-_pyfill)*(py-_pyfill)
151                                         +  (pz-_pzfill)*(pz-_pzfill);
152
153 //if (_countRecursiveFill >1 )
154 //{
155 //      printf("   -> %d %d %d   cr=%ld  r=%f\n", px,py,pz,_countRecursiveFill , _tmpDistfill);
156 //}
157  
158
159 //EED01 
160 //              _tmpiglfill     = _image->GetScalarComponentAsDouble(px, py, pz, 0);
161 GETVALUE2_MANUALPAINT(_tmpiglfill,p_image,st_image,ivi)         
162
163                 if (_image2!=NULL)
164                 {
165 //EED01
166 //                      _tmpiglfill2    =       _image2->GetScalarComponentAsDouble(px, py, pz, 0);
167 GETVALUE2_MANUALPAINT(_tmpiglfill2,p_image2,st_image2,ivi)      
168                 } else {
169                         _tmpiglfill2    =       _tmpiglfill;
170                 }
171
172                 float grayLBFMTOL       = _graylevelbasefill - _tolerancefill;
173                 float grayLBFPTOL       = _graylevelbasefill + _tolerancefill;
174                 bool isInRange          = false;
175                 //DFCH
176                 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
177                         isInRange               = true;
178                 } //fi esle
179                 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
180                         grayLBFMTOL     = _RangeMin;
181                         isInRange               = true;
182                 } //fi esle
183                 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
184                         grayLBFPTOL     = _RangeMax;
185                         isInRange               = true;
186                 } //fi esle
187                 else if ((_RangeMin <= _graylevelbasefill) && (_graylevelbasefill <= _RangeMax)) {
188                         grayLBFMTOL     = _RangeMin;
189                         grayLBFPTOL     = _RangeMax;
190                         isInRange               = true;
191                 } //fi Range
192
193                 if (isInRange) 
194                 {
195                         _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
196                                         && (_tmpiglfill2 != _graylevel)
197                                         && (_tmpiglfill >= grayLBFMTOL)
198                                         && (_tmpiglfill <= grayLBFPTOL)
199                                         && (_tmpDistfill <= _distbasefill); //DFCH
200                 } else {
201                         _auxGrayLevelValidationFill = false;
202                 } // if isInRange
203
204                   //DFCH
205                 /*_auxGrayLevelValidationFill =   (_tmpiglfill!=_graylevel) &&
206                  (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
207                  (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
208                  (_tmpDistfill<=_distbasefill);*/ //DFCH
209
210                 if (_auxGrayLevelValidationFill == true) 
211                 {
212                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
213                         if (_image2!=NULL)
214                         {
215 //EED01
216 //                              _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
217 SETVALUE2_MANUALPAINT(_graylevel,p_image2,st_image2,ivi)
218                         } else {
219 //EED01
220 //                              _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
221 SETVALUE2_MANUALPAINT(_graylevel,p_image,st_image,ivi)
222                         }
223
224                         if (_countRecursiveFill < _limitRecursionFill) 
225                         {
226
227                                 if (_2D3D == 0) //2D
228                                 {
229                                         if (_direction == 0) // YZ
230                                         {
231                                                 //FillToolRecursive(px+1,py,pz);
232                                                 //FillToolRecursive(px-1,py,pz);
233                                                 FillToolRecursive(px, py + 1, pz, ivi+_OneLine  );
234                                                 FillToolRecursive(px, py - 1, pz, ivi-_OneLine  );
235                                                 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
236                                                 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
237                                         }
238                                         if (_direction == 1) // XZ
239                                                         {
240                                                 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
241                                                 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
242                                                 //FillToolRecursive(px,py+1,pz);
243                                                 //FillToolRecursive(px,py-1,pz);
244                                                 FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
245                                                 FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
246                                         }
247                                         if (_direction == 2) // XY
248                                                         {
249                                                 FillToolRecursive(px + 1, py, pz, ivi+_OneColumn);
250                                                 FillToolRecursive(px, py + 1, pz, ivi+_OneLine);
251                                                 FillToolRecursive(px - 1, py, pz, ivi-_OneColumn);
252                                                 FillToolRecursive(px, py - 1, pz, ivi-_OneLine);
253                                                 //FillToolRecursive(px,py,pz-1);
254                                                 //FillToolRecursive(px,py,pz+1);
255                                         }
256                                 } else { // 3D
257                                         FillToolRecursive(px + 1, py, pz, ivi+_OneColumn );
258                                         FillToolRecursive(px - 1, py, pz, ivi-_OneColumn );
259                                         FillToolRecursive(px, py + 1, pz, ivi+_OneLine );
260                                         FillToolRecursive(px, py - 1, pz, ivi-_OneLine );
261                                         FillToolRecursive(px, py, pz - 1, ivi-_OnePlane );
262                                         FillToolRecursive(px, py, pz + 1, ivi+_OnePlane );
263                                 } // 2D 3D
264
265                         } //_countRecursiveFill
266                 } // _graylevel
267
268                 if ((_auxGrayLevelValidationFill == true) && (_countRecursiveFill >= _limitRecursionFill)) 
269                 {
270                         _countRecursiveFillProblem++;
271                         if (_2D3D == 0) //2D
272                         {
273                                 if (_direction == 0) // YZ
274                                 {
275                                         //SetAuxImageFill(px+1,py,pz);
276                                         //SetAuxImageFill(px-1,py,pz);
277                                         SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
278                                         SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
279                                         SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
280                                         SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
281                                 }
282                                 if (_direction == 1) // XZ
283                                 {
284                                         SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
285                                         SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
286                                         //SetAuxImageFill(px,py+1,pz);
287                                         //SetAuxImageFill(px,py-1,pz);
288                                         SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
289                                         SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
290                                 }
291                                 if (_direction == 2) // XY
292                                 {
293                                         SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
294                                         SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
295                                         SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
296                                         SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
297                                         //SetAuxImageFill(px,py,pz-1);
298                                         //SetAuxImageFill(px,py,pz+1);
299                                 }
300                         } else { // 3D
301                                 SetAuxImageFill(px + 1, py, pz,ivi+_OneColumn);
302                                 SetAuxImageFill(px - 1, py, pz,ivi-_OneColumn);
303                                 SetAuxImageFill(px, py + 1, pz,ivi+_OneLine);
304                                 SetAuxImageFill(px, py - 1, pz,ivi-_OneLine);
305                                 SetAuxImageFill(px, py, pz - 1,ivi-_OnePlane);
306                                 SetAuxImageFill(px, py, pz + 1,ivi+_OnePlane);
307                         } // 2D 3D
308
309                 } // _graylevel   //_limitRecursionFill
310
311         } //if _minX _maxX _minY _maxY _minZ _maxZ
312         _countRecursiveFill--;
313 }
314
315 //---------------------------------------------------------------------------
316 void FillFilter::SetAuxImageFill(int px, int py, int pz, long int ivi) 
317 {
318         if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
319                         && (pz >= _minZ) && (pz <= _maxZ)) 
320         {
321                 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
322 //EED01
323 //              _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
324                         _ptrAuxImageFill[ ivi ]=1;
325         } // if px py pz
326 }
327
328 //---------------------------------------------------------------------------
329 void FillFilter::SetToleranceFill(double tolerancefill) 
330 {
331         _tolerancefill = tolerancefill;
332 }
333
334 //---------------------------------------------------------------------------
335 void FillFilter::SetDistanceFill(int distancefill) 
336 {
337         _distancefill = distancefill;
338 }
339
340