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