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