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