]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualPaint_Model.cxx
#3482 Bug Manual Paint - MacOs and UChar images
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuManualPaint_Model.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4
5 #include "creaVtk_MACROS.h"
6 #include "bbcreaMaracasVisuManualPaint_Model.h"
7 #include "bbcreaMaracasVisuPackage.h"
8
9 /* EED 20210-09-13  Not compiling in MacOs
10  #include <omp.h>
11 */
12
13 namespace bbcreaMaracasVisu
14 {
15
16 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaMaracasVisu,ManualPaint_Model)
17 BBTK_BLACK_BOX_IMPLEMENTATION(ManualPaint_Model,bbtk::AtomicBlackBox);
18 //===== 
19 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
20 //===== 
21
22 //-- Alternativa con openmp --
23 void ManualPaint_Model::LstPointOMPAnalice()
24 {
25
26 printf("EED ManualPaint_Model::LstPointOMPAnalice\n" );
27 printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
28 printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
29 printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
30 printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
31
32
33 printf("EED ManualPaint_Model::LstPointOMPAnalice Start \n");
34 //-- Alternativa con openmp --
35 // A. Create copie of index original list => lst1
36 // B. if list1 es not empty do..
37 // B.1. Create copy of lst1 to lst2
38 // B.2. For each element of lst2 (incrimental) check the distance of the rest of the list (decremental) and take it out
39 // B.3. openmp sobre la La lst2 que queda (PaintImage)
40 // B.4. Quitar los elementos de lst2 en lst1 y recomenzar desde el punto B;
41         
42         double distMin,dist2Min;
43         if (bbGetInputTool()==0) 
44         { 
45                 distMin=bbGetInputBrushSize(); 
46         } else {
47                 distMin=bbGetInputDistanceFill();
48         }
49         dist2Min=distMin*distMin*1.2;
50
51 // A. Create copie of index original list => lst1
52         std::vector<int> lst1;
53         int i1,size1 = bbGetInputByLstPointsX().size();
54         for (i1=0;i1<size1;i1++)
55         {
56                 lst1.push_back(i1);
57         } // for i1
58
59 // B. if list1 es not empty do..
60         int i2,i3,size2;
61         double dist2,dx,dy,dz;
62         while (lst1.size()!=0)
63         {
64 // B.1. Create copy of lst1 to lst2
65                 size1=lst1.size();
66                 std::vector<int> lst2;
67                 for (i1=0;i1<size1;i1++)
68                 {
69                         lst2.push_back( lst1[i1] );
70                 } // for i1
71                 
72 // B.2. For each element of lst2 (incrimental) check the distance of the rest of the list (decremental) and take it out
73                 for (i2=0;i2<lst2.size();i2++)
74                 {
75                         size2=lst2.size();
76                         for ( i3=size2-1 ; i3>i2 ; i3-- )
77                         {
78                                 dx              = bbGetInputByLstPointsX()[ lst2[i2] ]-bbGetInputByLstPointsX()[ lst2[i3] ];
79                                 dy              = bbGetInputByLstPointsY()[ lst2[i2] ]-bbGetInputByLstPointsY()[ lst2[i3] ];
80                                 dz              = bbGetInputByLstPointsZ()[ lst2[i2] ]-bbGetInputByLstPointsZ()[ lst2[i3] ];
81                                 dist2   = dx*dx +dy*dy + dz*dz ;
82                                 if (dist2<dist2Min)
83                                 {
84                                         lst2.erase( lst2.begin()+i3 );
85                                 }                       } // for i3
86                 } // for i2
87
88 // B.3. openmp sobre la La lst2 que queda (PaintImage)
89                 size2=lst2.size();
90 long int k1omp=(double)(size2-1)*0.0;
91 long int k2omp=(double)(size2-1)*0.2;
92 long int k3omp=(double)(size2-1)*0.4;
93 long int k4omp=(double)(size2-1)*0.6;
94 long int k5omp=(double)(size2-1)*0.8;
95 long int k6omp=(double)(size2-1)*1.0;
96 printf("EED ManualPaint_Model::LstPointOMPAnalice (with openmp)  size=%d\n",size2);
97 #pragma omp parallel for 
98                 for (i2=0;i2<size2;i2++)
99                 {
100                         if ( (k1omp==i2) || (k2omp==i2) || (k3omp==i2) || 
101                                  (k4omp==i2) || (k5omp==i2) || (k6omp==i2) ) 
102                         { 
103                                 if (size2==1) 
104                                 { 
105                                         printf("  100% (just one element)\n"); 
106                                 } else {
107                                         printf("  %d% \n", (int)(((double)i2/(double)(size2-1))*100 )); 
108                                 } 
109                         }// if K#omp
110                         manualpaintmodel->PaintImage(   bbGetInputByLstPointsX()[ lst2[i2] ] , 
111                                                                                         bbGetInputByLstPointsY()[ lst2[i2] ] , 
112                                                                                         bbGetInputByLstPointsZ()[ lst2[i2] ] );
113                 } // for i2
114
115 // B.4. Quitar los elementos de lst2 en lst1 y recomenzar desde el punto B;
116                 for (i2=0;i2<size2;i2++)
117                 {
118                         size1=lst1.size();
119                         for (i1=0;i1<size1;i1++)
120                         {
121                                 if (lst2[i2]==lst1[i1])
122                                 {
123                                         lst1.erase( lst1.begin()+i1 );
124                                         i1=size1;       
125                                 } // if
126                         } // for i1
127                 } // for i2
128
129         } // while
130 printf("EED ManualPaint_Model::LstPointOMPAnalice End\n");
131
132 }
133
134
135 void ManualPaint_Model::Process()
136 {
137 /* Notas
138 0. Quitar comentarion ByLstPoints openmp
139 1. Falta openmp ByImagePoints
140 2. Desligar cada PaintImage de la mascara
141     -pequeno bloque de memoria para cada PaintImage
142         -el resultado volverlo a colocar en la image2 original
143         -Si no existe image2 original y hay que colocarlo en la imagen de origien 
144          hay que crear una imagen2 temporal y luego bajar la imagen 
145 3. Verificar el mecanismo de undo con ByLstPoints
146 4. Verificar el mecanismo de undo con ByImagePoints
147 */
148     
149     printf("EED ManualPaint_Model::Process Start \n");
150         if (bbGetInputActive()==true)
151         {
152                 if (bbGetInputImage()!=NULL)
153                 {  
154                         manualpaintmodel->SetActive( bbGetInputActive() );
155                         manualpaintmodel->SetTool( bbGetInputTool() );
156                         manualpaintmodel->Set2D3D( bbGetInput2D3D() );
157                         manualpaintmodel->SetImages( bbGetInputImage(),bbGetInputImage2() );
158                         manualpaintmodel->SetGrayLevel( bbGetInputGrayLevel() );                                        
159                         manualpaintmodel->SetDirection( bbGetInputDirection() );
160                         manualpaintmodel->SetBrushSize( bbGetInputBrushSize() );
161                         manualpaintmodel->SetBrushForm( bbGetInputBrushForm() );
162                         manualpaintmodel->SetToleranceFill( bbGetInputToleranceFill() );
163                         manualpaintmodel->SetDistanceFill( bbGetInputDistanceFill() );                          
164                         manualpaintmodel->SetRangeMin( bbGetInputRange()[0] );
165                         manualpaintmodel->SetRangeMax( bbGetInputRange()[1] );
166                         if (bbGetInputPoint().size()==3)
167                         {
168                                 manualpaintmodel->PaintImage( bbGetInputPoint()[0] , bbGetInputPoint()[1] , bbGetInputPoint()[2] );
169                                 manualpaintmodel->SetUndoImage();
170                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
171                         } // if Points
172                         if ( bbGetInputByLstPointsX().size()!=0 )
173                         {
174                                 //-- Alternativa con openmp --
175 //                              LstPointOMPAnalice();
176
177 // /* -- Original ByLstPoints sin openmp --
178                                 int i,size = bbGetInputByLstPointsX().size();
179 long int k1omp=(double)(size-1)*0.0;
180 long int k2omp=(double)(size-1)*0.2;
181 long int k3omp=(double)(size-1)*0.4;
182 long int k4omp=(double)(size-1)*0.6;
183 long int k5omp=(double)(size-1)*0.8;
184 long int k6omp=(double)(size-1)*1.0;
185 printf("EED ManualPaint_Model::Process  list of points (with openmp)  size=%d\n",size);
186 #pragma omp parallel for 
187                                 for (i=0;i<size;i++)
188                                 {
189                                         if ( (k1omp==i) || (k2omp==i) || (k3omp==i) || 
190                                                  (k4omp==i) || (k5omp==i) || (k6omp==i) ) 
191                                         { 
192                                                 if (size==1) 
193                                                 { 
194                                                         printf("  100% (just one element)\n"); 
195                                                 } else {
196                                                         printf("  %d% \n", (int)(((double)i/(double)(size-1))*100 )); 
197                                                 } 
198                                         }// if K#omp
199                                         manualpaintmodel->PaintImage(   bbGetInputByLstPointsX()[i] , 
200                                                                                                         bbGetInputByLstPointsY()[i] , 
201                                                                                                         bbGetInputByLstPointsZ()[i] );
202                                         //_manualPaintModel->SetUndoImage();     // Probably needed.   Here is ok.
203                                 } // for
204 //  -- Original ByLstPoints sin openmp --*/
205
206                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
207
208                         }// if ByLstPointsX
209                         if ((bbGetInputByImagePoints()!=NULL) && (bbGetInputImage2()!=NULL) )
210                         {
211                                 // -- Alternativa con openmp --
212                                 // Calcular numero de cuadrantes
213                                 // Recorrer el primer cuadrante
214                                 //   Sacar la lista de todos los cuadrantes con el iElement Listo para analizar
215                                 // Recorrer la lista con openmp y ejecutar PaintImage
216                         
217 ///*  -------------------- Original ByImagePoints sin openmp----------------------------
218         //                      int ia,ja,ka;
219         //                      int ii,jj,kk;
220                                 int ext[6];
221         //EED 2017-01-01 Migration VTK7
222         #if VTK_MAJOR_VERSION <= 5
223                                 bbGetInputByImagePoints()->GetWholeExtent(ext);
224         #else
225                                 bbGetInputByImagePoints()->GetExtent(ext);
226         #endif
227                                 int dimX=ext[1]-ext[0]+1;
228                                 int dimY=ext[3]-ext[2]+1;
229                                 int dimZ=ext[5]-ext[4]+1;
230                                 DEF_POINTER_IMAGE_VTK_CREA(vBIP,sSBIP,pBIP,sTBIP,bbGetInputByImagePoints());
231                                 DEF_POINTER_IMAGE_VTK_CREA(vI2,sSI2,pI2,sTI2,bbGetInputImage2());
232
233                                 long int index, size=dimX*dimY*dimZ;
234
235 printf("EED ManualPaint_Model::Process A.start\n");
236 std::vector<int> lstX;
237 std::vector<int> lstY;
238 std::vector<int> lstZ;
239                                 int i,j,k;
240                                 double value1;
241                                 double value2;
242                                 for (index=0; index<size; index++)
243                                 {
244                                         GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
245                                         if (value1>0)
246                                         {
247                                                 GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
248                                                 if (value2==0)
249                                                 {
250                                                         i = index % dimX;
251                                                         j = (index % (dimX*dimY)) / dimX;
252                                                         k = index / (dimX*dimY);
253                                                         lstX.push_back(i);
254                                                         lstY.push_back(j);
255                                                         lstZ.push_back(k);
256                                                 } // if value2
257                                         } // if value1
258                                 } // for 
259 printf("EED ManualPaint_Model::Process B.end\n");
260
261 size=lstX.size();
262 long int k1omp=(double)(size-1)*0.0;
263 long int k2omp=(double)(size-1)*0.2;
264 long int k3omp=(double)(size-1)*0.4;
265 long int k4omp=(double)(size-1)*0.6;
266 long int k5omp=(double)(size-1)*0.8;
267 long int k6omp=(double)(size-1)*1.0;
268 printf("EED ManualPaint_Model::Process  ByImagePoints (with openmp)  size=%d    GrayLevel=%f this=%p\n",size,(double)bbGetInputGrayLevel(),this);
269 //omp_set_num_threads( omp_get_max_threads()-1 );
270 #pragma omp parallel for 
271                                 for (index=0; index<size; index++)
272                                 {
273                                         if ( (k1omp==index) || (k2omp==index) || (k3omp==index) || 
274                                                  (k4omp==index) || (k5omp==index) || (k6omp==index) )
275                         { printf("  %d%\n", (int)(((double)index/(double)(size-1))*100 )); }
276                     
277 //                                      double value1;
278 //                                      GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
279 //                                      if (value1>0)
280 //                                      {
281 //                                              double value2;
282 //                                              GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
283 //                                              if (value2==0)
284 //                                              {
285 //                                                      int i = index % dimX;
286 //                                                      int j = (index % (dimX*dimY)) / dimX;
287 //                                                      int k = index / (dimX*dimY);
288                                                         manualpaintmodel->PaintImage(lstX[index],lstY[index],lstZ[index]);
289 // //                                                           manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
290 //                                              } // vI2                
291 //                                      } // vBIP 
292                                 } // for index    END openmp
293                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
294
295
296
297
298
299
300
301 /* openmp
302 long int k1omp=(double)(size-1)*0.0;
303 long int k2omp=(double)(size-1)*0.2;
304 long int k3omp=(double)(size-1)*0.4;
305 long int k4omp=(double)(size-1)*0.6;
306 long int k5omp=(double)(size-1)*0.8;
307 long int k6omp=(double)(size-1)*1.0;
308 printf("EED ManualPaint_Model::Process  ByImagePoints (with openmp)  size=%d    GrayLevel=%f this=%p\n",size,(double)bbGetInputGrayLevel(),this);
309 omp_set_num_threads( omp_get_max_threads()-1 );
310 #pragma omp parallel for 
311                                 for (index=0; index<size; index++)
312                                 {
313                                         if ( (k1omp==index) || (k2omp==index) || (k3omp==index) || 
314                                                  (k4omp==index) || (k5omp==index) || (k6omp==index) ) 
315                                         { 
316                                                 if (size==1) 
317                                                 { 
318                                                         printf("  100% (just one element)\n"); 
319                                                 } else {
320                                                         printf("  %d%%  omp_get_num_threads=%d\n", (int)(((double)index/(double)(size-1))*100 ),  omp_get_num_threads() ); 
321                                                 } 
322                                         }// if K#omp
323                                         double value1;
324                                         GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
325                                         if (value1>0)
326                                         {
327                                                 double value2;
328                                                 GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
329                                                 if (value2==0)
330                                                 {
331                                                         int i = index % dimX;
332                                                         int j = (index % (dimX*dimY)) / dimX;
333                                                         int k = index / (dimX*dimY);
334                                                         manualpaintmodel->PaintImage(i,j,k);
335 //                                                              manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
336                                                 } // vI2                
337                                         } // vBIP 
338                                 } // for index    END openmp
339                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
340 */
341
342 /*
343                                 int i,j,k;
344         //#pragma omp parallel for
345                                 for (k=0;k<dimZ;k++) 
346                                 {
347                                         for (j=0;j<dimY;j++) 
348                                         {
349                                                 for (i=0;i<dimX;i++) 
350                                                 {
351                                                         GETVALUE_VTK_CREA(vBIP,pBIP,sTBIP)
352                                                         if (vBIP>0)
353                                                         {
354                                                                 GETVALUE_VTK_CREA(vI2,pI2,sTI2)
355                                                                 if (vI2==0)
356                                                                 {
357                                                                         manualpaintmodel->PaintImage(i,j,k);
358         //                                                              manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
359                                                                 } // vI2                
360                                                         } // vBIP 
361                                                         pBIP = pBIP + sSBIP;                            
362                                                         pI2  = pI2  + sSI2;                             
363                                                 }// for i
364                                         }// for j
365                                 }// for k
366 //-------  Original ByImagePoints sin openmp--------  */
367
368
369                         } // if ByImagePoints
370
371                  } else {
372                         printf("EED Warning!!  creaMaracasVisu::ManualPaint_Model (BBTK)   bbInputImage not set.  \n");
373                 } // if (bbGetInputImage()!=NULL)
374         } // if active
375
376         if (bbGetInputImage2()!=NULL)
377         { 
378                 bbSetOutputOut( bbGetInputImage2() );
379         } else if (bbGetInputImage()!=NULL) { 
380                 bbSetOutputOut( bbGetInputImage() );
381         } else { 
382                 bbSetOutputOut( NULL );
383         } 
384         bbSetOutputManualpaintmodel( manualpaintmodel );
385     
386     printf("EED ManualPaint_Model::Process End \n");
387
388 }
389 //===== 
390 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
391 //===== 
392 void ManualPaint_Model::bbUserSetDefaultValues()
393 {
394
395 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
396 //    Here we initialize the input 'In' to 0
397
398         bbSetInputActive(true);
399         bbSetInputTool(1);              // Paint
400         bbSetInput2D3D(1);              // 3D
401         bbSetInputImage(NULL);
402         bbSetInputImage2(NULL);
403         bbSetInputGrayLevel(100);
404         bbSetInputDirection(2);         // XY
405         bbSetInputBrushSize(3);
406         bbSetInputBrushForm(1);         // sphere
407         bbSetInputToleranceFill(50);
408         bbSetInputDistanceFill(25);
409         std::vector<int> point;
410 //      point.push_back(0);
411 //      point.push_back(0);
412 //      point.push_back(0);
413 //      bbSetInputPoint(point);
414         std::vector<double> range;
415         range.push_back(0);
416         range.push_back(200);
417         bbSetInputRange(range);
418         bbSetInputByImagePoints(NULL);
419 }
420 //===== 
421 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
422 //===== 
423 void ManualPaint_Model::bbUserInitializeProcessing()
424 {
425
426 //  THE INITIALIZATION METHOD BODY :
427 //    Here does nothing 
428 //    but this is where you should allocate the internal/output pointers 
429 //    if any 
430         manualpaintmodel=new ManualPaintModel();  
431 }
432 //===== 
433 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
434 //===== 
435 void ManualPaint_Model::bbUserFinalizeProcessing()
436 {
437
438 //  THE FINALIZATION METHOD BODY :
439 //    Here does nothing 
440 //    but this is where you should desallocate the internal/output pointers 
441 //    if any
442         delete manualpaintmodel;
443   
444 }
445
446 }
447 // EO namespace bbcreaMaracasVisu
448
449