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