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