]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/manualPaint/FillFilter.cpp
#2864 creaMaracasVisu Feature New Normal - Manual Paint , modify external image
[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         _auxImageFill->SetWholeExtent(0, _maxX, 0, _maxY, 0, _maxZ);
58         _auxImageFill->SetScalarTypeToUnsignedChar();
59         _auxImageFill->AllocateScalars();
60 }
61
62 //---------------------------------------------------------------------------
63 void FillFilter::Run() // virtual
64 {
65         if ((_px >= _minX) && (_px <= _maxX) && (_py >= _minY) && (_py <= _maxY)
66                         && (_pz >= _minZ) && (_pz <= _maxZ)) {
67                 _graylevelbasefill = _image->GetScalarComponentAsDouble(_px, _py, _pz,
68                                 0);
69                 _pxfill = _px;
70                 _pyfill = _py;
71                 _pzfill = _pz;
72                 _distbasefill = _distancefill * _distancefill;
73                 _countRecursiveFill = 0;
74                 _countRecursiveFillProblem = 0;
75                 _countProgressingFill = 0;
76                 unsigned char *pImage =
77                                 (unsigned char *) _auxImageFill->GetScalarPointer();
78                 _usingAuxImageFill = false;
79                 memset(pImage, 0, _maxX * _maxY * _maxZ);
80
81                 FillToolRecursive(_px, _py, _pz);
82 //              printf("--\n");
83
84                 int ii, jj, kk;
85                 while (_countRecursiveFillProblem != 0) {
86                         _countRecursiveFillProblem = 0;
87                         _usingAuxImageFill = true;
88                         for (ii = 0; ii <= _maxX; ii++) {
89                                 for (jj = 0; jj <= _maxY; jj++) {
90                                         for (kk = 0; kk <= _maxZ; kk++) {
91                                                 pImage =
92                                                                 (unsigned char *) _auxImageFill->GetScalarPointer(
93                                                                                 ii, jj, kk);
94                                                 if ((*pImage) == 1) {
95                                                         FillToolRecursive(ii, jj, kk);
96                                                 }
97                                         } // for kk
98                                 } // for jj
99                         } //for ii
100 //                      printf("-\n");
101                 } // while
102
103         } //if _minX _maxX _minY _maxY _minZ _maxZ
104 }
105
106 //---------------------------------------------------------------------------
107 void FillFilter::FillToolRecursive(int px, int py, int pz) 
108 {
109         _countRecursiveFill++;
110
111         _countProgressingFill++;
112         if (_countProgressingFill > 200000) 
113         {
114                 printf("R %ld \n", _countRecursiveFill);
115                 _countProgressingFill = 0;
116         }
117
118         if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
119                         && (pz >= _minZ) && (pz <= _maxZ)) 
120         {
121                 if (_usingAuxImageFill == true) 
122                 {
123                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
124                         _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 0);
125                 }
126                 _tmpDistfill = (px - _pxfill) * (px - _pxfill)
127                                 + (py - _pyfill) * (py - _pyfill)
128                                 + (pz - _pzfill) * (pz - _pzfill);
129                 _tmpiglfill     = _image->GetScalarComponentAsDouble(px, py, pz, 0);
130
131                 if (_image2!=NULL)
132                 {
133                         _tmpiglfill2    =       _image2->GetScalarComponentAsDouble(px, py, pz, 0);
134                 } else {
135                         _tmpiglfill2    =       _tmpiglfill;
136                 }
137
138                 float grayLBFMTOL = _graylevelbasefill - _tolerancefill;
139                 float grayLBFPTOL = _graylevelbasefill + _tolerancefill;
140                 bool isInRange = false;
141                 //DFCH
142                 if (_RangeMin <= grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
143                         isInRange = true;
144                 } //fi esle
145                 else if (_RangeMin > grayLBFMTOL && _RangeMax >= grayLBFPTOL) {
146                         grayLBFMTOL = _RangeMin;
147                         isInRange = true;
148                 } //fi esle
149                 else if (_RangeMin <= grayLBFMTOL && _RangeMax < grayLBFPTOL) {
150                         grayLBFPTOL = _RangeMax;
151                         isInRange = true;
152                 } //fi esle
153                 else if ((_RangeMin <= _graylevelbasefill)
154                                 && (_graylevelbasefill <= _RangeMax)) {
155                         grayLBFMTOL = _RangeMin;
156                         grayLBFPTOL = _RangeMax;
157                         isInRange = true;
158                 } //fi esle
159
160                 if (isInRange) 
161                 {
162                         _auxGrayLevelValidationFill = (_tmpiglfill != _graylevel)
163                                         && (_tmpiglfill2 != _graylevel)
164                                         && (_tmpiglfill >= grayLBFMTOL)
165                                         && (_tmpiglfill <= grayLBFPTOL)
166                                         && (_tmpDistfill <= _distbasefill); //DFCH
167                 } else {
168                         _auxGrayLevelValidationFill = false;
169                 } // if isInRange
170
171                   //DFCH
172                 /*_auxGrayLevelValidationFill =   (_tmpiglfill!=_graylevel) &&
173                  (_tmpiglfill>=_graylevelbasefill-_tolerancefill) &&
174                  (_tmpiglfill<=_graylevelbasefill+_tolerancefill) &&
175                  (_tmpDistfill<=_distbasefill);*/ //DFCH
176
177                 if (_auxGrayLevelValidationFill == true) 
178                 {
179                         this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
180                         if (_image2!=NULL)
181                         {
182                                 _image2->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
183                         } else {
184                                 _image->SetScalarComponentFromFloat(px, py, pz, 0,(float) _graylevel);
185                         }
186
187                         if (_countRecursiveFill < _limitRecursionFill) 
188                         {
189
190                                 if (_2D3D == 0) //2D
191                                 {
192                                         if (_direction == 0) // YZ
193                                         {
194                                                 //FillToolRecursive(px+1,py,pz);
195                                                 //FillToolRecursive(px-1,py,pz);
196                                                 FillToolRecursive(px, py + 1, pz);
197                                                 FillToolRecursive(px, py - 1, pz);
198                                                 FillToolRecursive(px, py, pz - 1);
199                                                 FillToolRecursive(px, py, pz + 1);
200                                         }
201                                         if (_direction == 1) // XZ
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 == 2) // XY
211                                                         {
212                                                 FillToolRecursive(px + 1, py, pz);
213                                                 FillToolRecursive(px, py + 1, pz);
214                                                 FillToolRecursive(px - 1, py, pz);
215                                                 FillToolRecursive(px, py - 1, pz);
216                                                 //FillToolRecursive(px,py,pz-1);
217                                                 //FillToolRecursive(px,py,pz+1);
218                                         }
219                                 } else { // 3D
220
221                                         FillToolRecursive(px + 1, py, pz);
222                                         FillToolRecursive(px - 1, py, pz);
223                                         FillToolRecursive(px, py + 1, pz);
224                                         FillToolRecursive(px, py - 1, pz);
225                                         FillToolRecursive(px, py, pz - 1);
226                                         FillToolRecursive(px, py, pz + 1);
227                                 } // 2D 3D
228
229                         } //_countRecursiveFill
230                 } // _graylevel
231
232                 if ((_auxGrayLevelValidationFill == true)
233                                 && (_countRecursiveFill >= _limitRecursionFill)) 
234                 {
235                         _countRecursiveFillProblem++;
236
237                         if (_2D3D == 0) //2D
238                                         {
239                                 if (_direction == 0) // YZ
240                                                 {
241                                         //SetAuxImageFill(px+1,py,pz);
242                                         //SetAuxImageFill(px-1,py,pz);
243                                         SetAuxImageFill(px, py + 1, pz);
244                                         SetAuxImageFill(px, py - 1, pz);
245                                         SetAuxImageFill(px, py, pz - 1);
246                                         SetAuxImageFill(px, py, pz + 1);
247                                 }
248                                 if (_direction == 1) // XZ
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 == 2) // XY
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                         } else { // 3D
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                         } // 2D 3D
275
276                 } // _graylevel   //_limitRecursionFill
277
278         } //if _minX _maxX _minY _maxY _minZ _maxZ
279
280         _countRecursiveFill--;
281
282 }
283
284 //---------------------------------------------------------------------------
285 void FillFilter::SetAuxImageFill(int px, int py, int pz) 
286 {
287         if ((px >= _minX) && (px <= _maxX) && (py >= _minY) && (py <= _maxY)
288                         && (pz >= _minZ) && (pz <= _maxZ)) {
289                 this->_IMManager->AddModifiedPixel(px, py, pz); //DFCH
290                 _auxImageFill->SetScalarComponentFromFloat(px, py, pz, 0, 1);
291         }
292 }
293
294 //---------------------------------------------------------------------------
295 void FillFilter::SetToleranceFill(double tolerancefill) 
296 {
297         _tolerancefill = tolerancefill;
298 }
299
300 //---------------------------------------------------------------------------
301 void FillFilter::SetDistanceFill(int distancefill) 
302 {
303         _distancefill = distancefill;
304 }
305
306