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