]> Creatis software - creaMaracasVisu.git/blob - bbtk/src/bbcreaMaracasVisuManualPaint_Model.cxx
Clean Code
[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         if (bbGetInputActive()==true)
150         {
151                 if (bbGetInputImage()!=NULL)
152                 {  
153                         manualpaintmodel->SetActive( bbGetInputActive() );
154                         manualpaintmodel->SetTool( bbGetInputTool() );
155                         manualpaintmodel->Set2D3D( bbGetInput2D3D() );
156                         manualpaintmodel->SetImages( bbGetInputImage(),bbGetInputImage2() );
157                         manualpaintmodel->SetGrayLevel( bbGetInputGrayLevel() );                                        
158                         manualpaintmodel->SetDirection( bbGetInputDirection() );
159                         manualpaintmodel->SetBrushSize( bbGetInputBrushSize() );
160                         manualpaintmodel->SetBrushForm( bbGetInputBrushForm() );
161                         manualpaintmodel->SetToleranceFill( bbGetInputToleranceFill() );
162                         manualpaintmodel->SetDistanceFill( bbGetInputDistanceFill() );                          
163                         manualpaintmodel->SetRangeMin( bbGetInputRange()[0] );
164                         manualpaintmodel->SetRangeMax( bbGetInputRange()[1] );
165                         if (bbGetInputPoint().size()==3)
166                         {
167                                 manualpaintmodel->PaintImage( bbGetInputPoint()[0] , bbGetInputPoint()[1] , bbGetInputPoint()[2] );
168                                 manualpaintmodel->SetUndoImage();
169                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
170                         } // if Points
171                         if ( bbGetInputByLstPointsX().size()!=0 )
172                         {
173                                 //-- Alternativa con openmp --
174 //                              LstPointOMPAnalice();
175
176 // /* -- Original ByLstPoints sin openmp --
177                                 int i,size = bbGetInputByLstPointsX().size();
178 long int k1omp=(double)(size-1)*0.0;
179 long int k2omp=(double)(size-1)*0.2;
180 long int k3omp=(double)(size-1)*0.4;
181 long int k4omp=(double)(size-1)*0.6;
182 long int k5omp=(double)(size-1)*0.8;
183 long int k6omp=(double)(size-1)*1.0;
184 printf("EED ManualPaint_Model::Process  list of points (with openmp)  size=%d\n",size);
185 #pragma omp parallel for 
186                                 for (i=0;i<size;i++)
187                                 {
188                                         if ( (k1omp==i) || (k2omp==i) || (k3omp==i) || 
189                                                  (k4omp==i) || (k5omp==i) || (k6omp==i) ) 
190                                         { 
191                                                 if (size==1) 
192                                                 { 
193                                                         printf("  100% (just one element)\n"); 
194                                                 } else {
195                                                         printf("  %d% \n", (int)(((double)i/(double)(size-1))*100 )); 
196                                                 } 
197                                         }// if K#omp
198                                         manualpaintmodel->PaintImage(   bbGetInputByLstPointsX()[i] , 
199                                                                                                         bbGetInputByLstPointsY()[i] , 
200                                                                                                         bbGetInputByLstPointsZ()[i] );
201                                         //_manualPaintModel->SetUndoImage();     // Probably needed.   Here is ok.
202                                 } // for
203 //  -- Original ByLstPoints sin openmp --*/
204
205                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
206
207                         }// if ByLstPointsX
208                         if ((bbGetInputByImagePoints()!=NULL) && (bbGetInputImage2()!=NULL) )
209                         {
210                                 // -- Alternativa con openmp --
211                                 // Calcular numero de cuadrantes
212                                 // Recorrer el primer cuadrante
213                                 //   Sacar la lista de todos los cuadrantes con el iElement Listo para analizar
214                                 // Recorrer la lista con openmp y ejecutar PaintImage
215                         
216 ///*  -------------------- Original ByImagePoints sin openmp----------------------------
217         //                      int ia,ja,ka;
218         //                      int ii,jj,kk;
219                                 int ext[6];
220         //EED 2017-01-01 Migration VTK7
221         #if VTK_MAJOR_VERSION <= 5
222                                 bbGetInputByImagePoints()->GetWholeExtent(ext);
223         #else
224                                 bbGetInputByImagePoints()->GetExtent(ext);
225         #endif
226                                 int dimX=ext[1]-ext[0]+1;
227                                 int dimY=ext[3]-ext[2]+1;
228                                 int dimZ=ext[5]-ext[4]+1;
229                                 DEF_POINTER_IMAGE_VTK_CREA(vBIP,sSBIP,pBIP,sTBIP,bbGetInputByImagePoints());
230                                 DEF_POINTER_IMAGE_VTK_CREA(vI2,sSI2,pI2,sTI2,bbGetInputImage2());
231
232                                 long int index, size=dimX*dimY*dimZ;
233
234 std::vector<int> lstX;
235 std::vector<int> lstY;
236 std::vector<int> lstZ;
237                                 int i,j,k;
238                                 double value1;
239                                 double value2;
240                                 for (index=0; index<size; index++)
241                                 {
242                                         GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
243                                         if (value1>0)
244                                         {
245                                                 GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
246                                                 if (value2==0)
247                                                 {
248                                                         i = index % dimX;
249                                                         j = (index % (dimX*dimY)) / dimX;
250                                                         k = index / (dimX*dimY);
251                                                         lstX.push_back(i);
252                                                         lstY.push_back(j);
253                                                         lstZ.push_back(k);
254                                                 } // if value2
255                                         } // if value1
256                                 } // for 
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                         { printf("  %d%\n", (int)(((double)index/(double)(size-1))*100 )); }
273                     
274 //                                      double value1;
275 //                                      GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
276 //                                      if (value1>0)
277 //                                      {
278 //                                              double value2;
279 //                                              GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
280 //                                              if (value2==0)
281 //                                              {
282 //                                                      int i = index % dimX;
283 //                                                      int j = (index % (dimX*dimY)) / dimX;
284 //                                                      int k = index / (dimX*dimY);
285                                                         manualpaintmodel->PaintImage(lstX[index],lstY[index],lstZ[index]);
286 // //                                                           manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
287 //                                              } // vI2                
288 //                                      } // vBIP 
289                                 } // for index    END openmp
290                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
291
292
293
294
295
296
297
298 /* openmp
299 long int k1omp=(double)(size-1)*0.0;
300 long int k2omp=(double)(size-1)*0.2;
301 long int k3omp=(double)(size-1)*0.4;
302 long int k4omp=(double)(size-1)*0.6;
303 long int k5omp=(double)(size-1)*0.8;
304 long int k6omp=(double)(size-1)*1.0;
305 printf("EED ManualPaint_Model::Process  ByImagePoints (with openmp)  size=%d    GrayLevel=%f this=%p\n",size,(double)bbGetInputGrayLevel(),this);
306 omp_set_num_threads( omp_get_max_threads()-1 );
307 #pragma omp parallel for 
308                                 for (index=0; index<size; index++)
309                                 {
310                                         if ( (k1omp==index) || (k2omp==index) || (k3omp==index) || 
311                                                  (k4omp==index) || (k5omp==index) || (k6omp==index) ) 
312                                         { 
313                                                 if (size==1) 
314                                                 { 
315                                                         printf("  100% (just one element)\n"); 
316                                                 } else {
317                                                         printf("  %d%%  omp_get_num_threads=%d\n", (int)(((double)index/(double)(size-1))*100 ),  omp_get_num_threads() ); 
318                                                 } 
319                                         }// if K#omp
320                                         double value1;
321                                         GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
322                                         if (value1>0)
323                                         {
324                                                 double value2;
325                                                 GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
326                                                 if (value2==0)
327                                                 {
328                                                         int i = index % dimX;
329                                                         int j = (index % (dimX*dimY)) / dimX;
330                                                         int k = index / (dimX*dimY);
331                                                         manualpaintmodel->PaintImage(i,j,k);
332 //                                                              manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
333                                                 } // vI2                
334                                         } // vBIP 
335                                 } // for index    END openmp
336                                 manualpaintmodel->Copy_GeneralAuxFill_to_Results();
337 */
338
339 /*
340                                 int i,j,k;
341         //#pragma omp parallel for
342                                 for (k=0;k<dimZ;k++) 
343                                 {
344                                         for (j=0;j<dimY;j++) 
345                                         {
346                                                 for (i=0;i<dimX;i++) 
347                                                 {
348                                                         GETVALUE_VTK_CREA(vBIP,pBIP,sTBIP)
349                                                         if (vBIP>0)
350                                                         {
351                                                                 GETVALUE_VTK_CREA(vI2,pI2,sTI2)
352                                                                 if (vI2==0)
353                                                                 {
354                                                                         manualpaintmodel->PaintImage(i,j,k);
355         //                                                              manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
356                                                                 } // vI2                
357                                                         } // vBIP 
358                                                         pBIP = pBIP + sSBIP;                            
359                                                         pI2  = pI2  + sSI2;                             
360                                                 }// for i
361                                         }// for j
362                                 }// for k
363 //-------  Original ByImagePoints sin openmp--------  */
364
365
366                         } // if ByImagePoints
367
368                  } else {
369                         printf("EED Warning!!  creaMaracasVisu::ManualPaint_Model (BBTK)   bbInputImage not set.  \n");
370                 } // if (bbGetInputImage()!=NULL)
371         } // if active
372
373         if (bbGetInputImage2()!=NULL)
374         { 
375                 bbSetOutputOut( bbGetInputImage2() );
376         } else if (bbGetInputImage()!=NULL) { 
377                 bbSetOutputOut( bbGetInputImage() );
378         } else { 
379                 bbSetOutputOut( NULL );
380         } 
381         bbSetOutputManualpaintmodel( manualpaintmodel );    
382 }
383 //===== 
384 // 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)
385 //===== 
386 void ManualPaint_Model::bbUserSetDefaultValues()
387 {
388
389 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
390 //    Here we initialize the input 'In' to 0
391
392         bbSetInputActive(true);
393         bbSetInputTool(1);              // Paint
394         bbSetInput2D3D(1);              // 3D
395         bbSetInputImage(NULL);
396         bbSetInputImage2(NULL);
397         bbSetInputGrayLevel(100);
398         bbSetInputDirection(2);         // XY
399         bbSetInputBrushSize(3);
400         bbSetInputBrushForm(1);         // sphere
401         bbSetInputToleranceFill(50);
402         bbSetInputDistanceFill(25);
403         std::vector<int> point;
404 //      point.push_back(0);
405 //      point.push_back(0);
406 //      point.push_back(0);
407 //      bbSetInputPoint(point);
408         std::vector<double> range;
409         range.push_back(0);
410         range.push_back(200);
411         bbSetInputRange(range);
412         bbSetInputByImagePoints(NULL);
413 }
414 //===== 
415 // 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)
416 //===== 
417 void ManualPaint_Model::bbUserInitializeProcessing()
418 {
419
420 //  THE INITIALIZATION METHOD BODY :
421 //    Here does nothing 
422 //    but this is where you should allocate the internal/output pointers 
423 //    if any 
424         manualpaintmodel=new ManualPaintModel();  
425 }
426 //===== 
427 // 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)
428 //===== 
429 void ManualPaint_Model::bbUserFinalizeProcessing()
430 {
431
432 //  THE FINALIZATION METHOD BODY :
433 //    Here does nothing 
434 //    but this is where you should desallocate the internal/output pointers 
435 //    if any
436         delete manualpaintmodel;
437   
438 }
439
440 }
441 // EO namespace bbcreaMaracasVisu
442
443