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