]> Creatis software - creaMaracasVisu.git/blobdiff - bbtk/src/bbcreaMaracasVisuManualPaint_Model.cxx
Bug Modified pipeline
[creaMaracasVisu.git] / bbtk / src / bbcreaMaracasVisuManualPaint_Model.cxx
index e36413448e29d70fe33e61093d2f50958584bbcd..aba4fc8bf54805e111a86d1bf00efdab3750eba9 100644 (file)
@@ -1,8 +1,14 @@
 //===== 
 // 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)
 //===== 
+
+#include "creaVtk_MACROS.h"
 #include "bbcreaMaracasVisuManualPaint_Model.h"
 #include "bbcreaMaracasVisuPackage.h"
+
+#include <omp.h>
+
+
 namespace bbcreaMaracasVisu
 {
 
@@ -11,86 +17,364 @@ BBTK_BLACK_BOX_IMPLEMENTATION(ManualPaint_Model,bbtk::AtomicBlackBox);
 //===== 
 // 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)
 //===== 
-void ManualPaint_Model::Process()
+
+//-- Alternativa con openmp --
+void ManualPaint_Model::LstPointOMPAnalice()
 {
 
-// THE MAIN PROCESSING METHOD BODY
-//   Here we simply set the input 'In' value to the output 'Out'
-//   And print out the output value
-// INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
-//    void bbSet{Input|Output}NAME(const TYPE&)
-//    const TYPE& bbGet{Input|Output}NAME() const 
-//    Where :
-//    * NAME is the name of the input/output
-//      (the one provided in the attribute 'name' of the tag 'input')
-//    * TYPE is the C++ type of the input/output
-//      (the one provided in the attribute 'type' of the tag 'input')
-//    bbSetOutputOut( bbGetInputIn() );
-//    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
-
-       if (bbGetInputImage()!=NULL)
-       {  
-               manualpaintmodel->SetActive( bbGetInputActive() );
-               manualpaintmodel->SetTool( bbGetInputTool() );
-               manualpaintmodel->Set2D3D( bbGetInput2D3D() );
-               manualpaintmodel->SetImages( bbGetInputImage(),bbGetInputImage2() );
-               manualpaintmodel->SetGrayLevel( bbGetInputGrayLevel() );
-               manualpaintmodel->SetDirection( bbGetInputDirection() );
-               manualpaintmodel->SetBrushSize( bbGetInputBrushSize() );
-               manualpaintmodel->SetBrushForm( bbGetInputBrushForm() );
-               manualpaintmodel->SetToleranceFill( bbGetInputToleranceFill() );
-               manualpaintmodel->SetDistanceFill( bbGetInputDistanceFill() );          
-               manualpaintmodel->SetRangeMin( bbGetInputRange()[0] );
-               manualpaintmodel->SetRangeMax( bbGetInputRange()[1] );
-               if (bbGetInputPoint().size()==3)
-               {
-                       manualpaintmodel->PaintImage( bbGetInputPoint()[0] , bbGetInputPoint()[1] , bbGetInputPoint()[2] );
-               } // if Points
+printf("EED ManualPaint_Model::LstPointOMPAnalice\n" );
+printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
+printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
+printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
+printf("EED FillFilter::Run ojo toca limpiar bien el codigo  _px  px  ...... \n"); 
+
 
-               if ( bbGetInputByLstPointsX().size()!=0 )
+printf("EED ManualPaint_Model::LstPointOMPAnalice Start \n");
+//-- Alternativa con openmp --
+// A. Create copie of index original list => lst1
+// B. if list1 es not empty do..
+// B.1. Create copy of lst1 to lst2
+// B.2. For each element of lst2 (incrimental) check the distance of the rest of the list (decremental) and take it out
+// B.3. openmp sobre la La lst2 que queda (PaintImage)
+// B.4. Quitar los elementos de lst2 en lst1 y recomenzar desde el punto B;
+       
+       double distMin,dist2Min;
+       if (bbGetInputTool()==0) 
+       { 
+               distMin=bbGetInputBrushSize(); 
+       } else {
+               distMin=bbGetInputDistanceFill();
+       }
+       dist2Min=distMin*distMin*1.2;
+
+// A. Create copie of index original list => lst1
+       std::vector<int> lst1;
+       int i1,size1 = bbGetInputByLstPointsX().size();
+       for (i1=0;i1<size1;i1++)
+       {
+               lst1.push_back(i1);
+       } // for i1
+
+// B. if list1 es not empty do..
+       int i2,i3,size2;
+       double dist2,dx,dy,dz;
+       while (lst1.size()!=0)
+       {
+// B.1. Create copy of lst1 to lst2
+               size1=lst1.size();
+               std::vector<int> lst2;
+               for (i1=0;i1<size1;i1++)
                {
-                       int i,size = bbGetInputByLstPointsX().size();
-                       for (i=0;i<size;i++)
-                       {
-                               manualpaintmodel->PaintImage( bbGetInputByLstPointsX()[i] , bbGetInputByLstPointsY()[i] , bbGetInputByLstPointsZ()[i] );
-                       } // for
-               }// if ByLstPointsX
+                       lst2.push_back( lst1[i1] );
+               } // for i1
                
-               if (bbGetInputByImagePoints()!=NULL)
+// B.2. For each element of lst2 (incrimental) check the distance of the rest of the list (decremental) and take it out
+               for (i2=0;i2<lst2.size();i2++)
+               {
+                       size2=lst2.size();
+                       for ( i3=size2-1 ; i3>i2 ; i3-- )
+                       {
+                               dx              = bbGetInputByLstPointsX()[ lst2[i2] ]-bbGetInputByLstPointsX()[ lst2[i3] ];
+                               dy              = bbGetInputByLstPointsY()[ lst2[i2] ]-bbGetInputByLstPointsY()[ lst2[i3] ];
+                               dz              = bbGetInputByLstPointsZ()[ lst2[i2] ]-bbGetInputByLstPointsZ()[ lst2[i3] ];
+                               dist2   = dx*dx +dy*dy + dz*dz ;
+                               if (dist2<dist2Min)
+                               {
+                                       lst2.erase( lst2.begin()+i3 );
+                               }                       } // for i3
+               } // for i2
+
+// B.3. openmp sobre la La lst2 que queda (PaintImage)
+               size2=lst2.size();
+long int k1omp=(double)(size2-1)*0.0;
+long int k2omp=(double)(size2-1)*0.2;
+long int k3omp=(double)(size2-1)*0.4;
+long int k4omp=(double)(size2-1)*0.6;
+long int k5omp=(double)(size2-1)*0.8;
+long int k6omp=(double)(size2-1)*1.0;
+printf("EED ManualPaint_Model::LstPointOMPAnalice (with openmp)  size=%d\n",size2);
+#pragma omp parallel for 
+               for (i2=0;i2<size2;i2++)
+               {
+                       if ( (k1omp==i2) || (k2omp==i2) || (k3omp==i2) || 
+                                (k4omp==i2) || (k5omp==i2) || (k6omp==i2) ) 
+                       { 
+                               if (size2==1) 
+                               { 
+                                       printf("  100% (just one element)\n"); 
+                               } else {
+                                       printf("  %d% \n", (int)(((double)i2/(double)(size2-1))*100 )); 
+                               } 
+                       }// if K#omp
+                       manualpaintmodel->PaintImage(   bbGetInputByLstPointsX()[ lst2[i2] ] , 
+                                                                                       bbGetInputByLstPointsY()[ lst2[i2] ] , 
+                                                                                       bbGetInputByLstPointsZ()[ lst2[i2] ] );
+               } // for i2
+
+// B.4. Quitar los elementos de lst2 en lst1 y recomenzar desde el punto B;
+               for (i2=0;i2<size2;i2++)
                {
-                       int i,j,k;
-//                     int ia,ja,ka;
-//                     int ii,jj,kk;
-                       int ext[6];
-                       bbGetInputByImagePoints()->GetWholeExtent(ext);
-                       int dimX=ext[1]-ext[0]+1;
-                       int dimY=ext[3]-ext[2]+1;
-                       int dimZ=ext[5]-ext[4]+1;
-                       for (i=0;i<dimX;i++) 
+                       size1=lst1.size();
+                       for (i1=0;i1<size1;i1++)
+                       {
+                               if (lst2[i2]==lst1[i1])
+                               {
+                                       lst1.erase( lst1.begin()+i1 );
+                                       i1=size1;       
+                               } // if
+                       } // for i1
+               } // for i2
+
+       } // while
+printf("EED ManualPaint_Model::LstPointOMPAnalice End\n");
+
+}
+
+
+void ManualPaint_Model::Process()
+{
+/* Notas
+0. Quitar comentarion ByLstPoints openmp
+1. Falta openmp ByImagePoints
+2. Desligar cada PaintImage de la mascara
+    -pequeno bloque de memoria para cada PaintImage
+       -el resultado volverlo a colocar en la image2 original
+       -Si no existe image2 original y hay que colocarlo en la imagen de origien 
+        hay que crear una imagen2 temporal y luego bajar la imagen 
+3. Verificar el mecanismo de undo con ByLstPoints
+4. Verificar el mecanismo de undo con ByImagePoints
+*/ 
+       if (bbGetInputActive()==true)
+       {
+               if (bbGetInputImage()!=NULL)
+               {  
+                       manualpaintmodel->SetActive( bbGetInputActive() );
+                       manualpaintmodel->SetTool( bbGetInputTool() );
+                       manualpaintmodel->Set2D3D( bbGetInput2D3D() );
+                       manualpaintmodel->SetImages( bbGetInputImage(),bbGetInputImage2() );
+                       manualpaintmodel->SetGrayLevel( bbGetInputGrayLevel() );                                        
+                       manualpaintmodel->SetDirection( bbGetInputDirection() );
+                       manualpaintmodel->SetBrushSize( bbGetInputBrushSize() );
+                       manualpaintmodel->SetBrushForm( bbGetInputBrushForm() );
+                       manualpaintmodel->SetToleranceFill( bbGetInputToleranceFill() );
+                       manualpaintmodel->SetDistanceFill( bbGetInputDistanceFill() );                          
+                       manualpaintmodel->SetRangeMin( bbGetInputRange()[0] );
+                       manualpaintmodel->SetRangeMax( bbGetInputRange()[1] );
+                       if (bbGetInputPoint().size()==3)
+                       {
+                               manualpaintmodel->PaintImage( bbGetInputPoint()[0] , bbGetInputPoint()[1] , bbGetInputPoint()[2] );
+                               manualpaintmodel->SetUndoImage();
+                               manualpaintmodel->Copy_GeneralAuxFill_to_Results();
+                       } // if Points
+                       if ( bbGetInputByLstPointsX().size()!=0 )
+                       {
+                               //-- Alternativa con openmp --
+//                             LstPointOMPAnalice();
+
+// /* -- Original ByLstPoints sin openmp --
+                               int i,size = bbGetInputByLstPointsX().size();
+long int k1omp=(double)(size-1)*0.0;
+long int k2omp=(double)(size-1)*0.2;
+long int k3omp=(double)(size-1)*0.4;
+long int k4omp=(double)(size-1)*0.6;
+long int k5omp=(double)(size-1)*0.8;
+long int k6omp=(double)(size-1)*1.0;
+printf("EED ManualPaint_Model::Process  list of points (with openmp)  size=%d\n",size);
+#pragma omp parallel for 
+                               for (i=0;i<size;i++)
+                               {
+                                       if ( (k1omp==i) || (k2omp==i) || (k3omp==i) || 
+                                                (k4omp==i) || (k5omp==i) || (k6omp==i) ) 
+                                       { 
+                                               if (size==1) 
+                                               { 
+                                                       printf("  100% (just one element)\n"); 
+                                               } else {
+                                                       printf("  %d% \n", (int)(((double)i/(double)(size-1))*100 )); 
+                                               } 
+                                       }// if K#omp
+                                       manualpaintmodel->PaintImage(   bbGetInputByLstPointsX()[i] , 
+                                                                                                       bbGetInputByLstPointsY()[i] , 
+                                                                                                       bbGetInputByLstPointsZ()[i] );
+                                       //_manualPaintModel->SetUndoImage();     // Probably needed.   Here is ok.
+                               } // for
+//  -- Original ByLstPoints sin openmp --*/
+
+                               manualpaintmodel->Copy_GeneralAuxFill_to_Results();
+
+                       }// if ByLstPointsX
+                       if ((bbGetInputByImagePoints()!=NULL) && (bbGetInputImage2()!=NULL) )
                        {
-                               if (i%20==0)
+                               // -- Alternativa con openmp --
+                               // Calcular numero de cuadrantes
+                               // Recorrer el primer cuadrante
+                               //   Sacar la lista de todos los cuadrantes con el iElement Listo para analizar
+                               // Recorrer la lista con openmp y ejecutar PaintImage
+                       
+///*  -------------------- Original ByImagePoints sin openmp----------------------------
+       //                      int ia,ja,ka;
+       //                      int ii,jj,kk;
+                               int ext[6];
+       //EED 2017-01-01 Migration VTK7
+       #if VTK_MAJOR_VERSION <= 5
+                               bbGetInputByImagePoints()->GetWholeExtent(ext);
+       #else
+                               bbGetInputByImagePoints()->GetExtent(ext);
+       #endif
+                               int dimX=ext[1]-ext[0]+1;
+                               int dimY=ext[3]-ext[2]+1;
+                               int dimZ=ext[5]-ext[4]+1;
+                               DEF_POINTER_IMAGE_VTK_CREA(vBIP,sSBIP,pBIP,sTBIP,bbGetInputByImagePoints());
+                               DEF_POINTER_IMAGE_VTK_CREA(vI2,sSI2,pI2,sTI2,bbGetInputImage2());
+
+                               long int index, size=dimX*dimY*dimZ;
+
+printf("EED ManualPaint_Model::Process A.start\n");
+std::vector<int> lstX;
+std::vector<int> lstY;
+std::vector<int> lstZ;
+                               int i,j,k;
+                               double value1;
+                               double value2;
+                               for (index=0; index<size; index++)
+                               {
+                                       GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
+                                       if (value1>0)
+                                       {
+                                               GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
+                                               if (value2==0)
+                                               {
+                                                       i = index % dimX;
+                                                       j = (index % (dimX*dimY)) / dimX;
+                                                       k = index / (dimX*dimY);
+                                                       lstX.push_back(i);
+                                                       lstY.push_back(j);
+                                                       lstZ.push_back(k);
+                                               } // if value2
+                                       } // if value1
+                               } // for 
+printf("EED ManualPaint_Model::Process B.end\n");
+
+size=lstX.size();
+long int k1omp=(double)(size-1)*0.0;
+long int k2omp=(double)(size-1)*0.2;
+long int k3omp=(double)(size-1)*0.4;
+long int k4omp=(double)(size-1)*0.6;
+long int k5omp=(double)(size-1)*0.8;
+long int k6omp=(double)(size-1)*1.0;
+printf("EED ManualPaint_Model::Process  ByImagePoints (with openmp)  size=%d    GrayLevel=%f this=%p\n",size,(double)bbGetInputGrayLevel(),this);
+//omp_set_num_threads( omp_get_max_threads()-1 );
+#pragma omp parallel for 
+                               for (index=0; index<size; index++)
+                               {
+                                       if ( (k1omp==index) || (k2omp==index) || (k3omp==index) || 
+                                                (k4omp==index) || (k5omp==index) || (k6omp==index) ) 
+                                       { 
+                                               if (size==1) 
+                                               { 
+                                                       printf("  100% (just one element)\n"); 
+                                               } else {
+                                                       printf("  %d%%  omp_get_num_threads=%d\n", (int)(((double)index/(double)(size-1))*100 ),  omp_get_num_threads() ); 
+                                               } 
+                                       }// if K#omp
+//                                     double value1;
+//                                     GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
+//                                     if (value1>0)
+//                                     {
+//                                             double value2;
+//                                             GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
+//                                             if (value2==0)
+//                                             {
+//                                                     int i = index % dimX;
+//                                                     int j = (index % (dimX*dimY)) / dimX;
+//                                                     int k = index / (dimX*dimY);
+                                                       manualpaintmodel->PaintImage(lstX[index],lstY[index],lstZ[index]);
+// //                                                          manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
+//                                             } // vI2                
+//                                     } // vBIP 
+                               } // for index    END openmp
+                               manualpaintmodel->Copy_GeneralAuxFill_to_Results();
+
+
+
+
+
+
+
+/* openmp
+long int k1omp=(double)(size-1)*0.0;
+long int k2omp=(double)(size-1)*0.2;
+long int k3omp=(double)(size-1)*0.4;
+long int k4omp=(double)(size-1)*0.6;
+long int k5omp=(double)(size-1)*0.8;
+long int k6omp=(double)(size-1)*1.0;
+printf("EED ManualPaint_Model::Process  ByImagePoints (with openmp)  size=%d    GrayLevel=%f this=%p\n",size,(double)bbGetInputGrayLevel(),this);
+omp_set_num_threads( omp_get_max_threads()-1 );
+#pragma omp parallel for 
+                               for (index=0; index<size; index++)
                                {
-                                       printf("ManualPaint_Model %d%\n", (int)(i*100.0/dimX) );
-                               }
-                               for (j=0;j<dimY;j++) 
+                                       if ( (k1omp==index) || (k2omp==index) || (k3omp==index) || 
+                                                (k4omp==index) || (k5omp==index) || (k6omp==index) ) 
+                                       { 
+                                               if (size==1) 
+                                               { 
+                                                       printf("  100% (just one element)\n"); 
+                                               } else {
+                                                       printf("  %d%%  omp_get_num_threads=%d\n", (int)(((double)index/(double)(size-1))*100 ),  omp_get_num_threads() ); 
+                                               } 
+                                       }// if K#omp
+                                       double value1;
+                                       GETVALUE2_VTK_CREA(value1,pBIP,sTBIP,index)
+                                       if (value1>0)
+                                       {
+                                               double value2;
+                                               GETVALUE2_VTK_CREA(value2,pI2,sTI2,index)
+                                               if (value2==0)
+                                               {
+                                                       int i = index % dimX;
+                                                       int j = (index % (dimX*dimY)) / dimX;
+                                                       int k = index / (dimX*dimY);
+                                                       manualpaintmodel->PaintImage(i,j,k);
+//                                                             manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
+                                               } // vI2                
+                                       } // vBIP 
+                               } // for index    END openmp
+                               manualpaintmodel->Copy_GeneralAuxFill_to_Results();
+*/
+
+/*
+                               int i,j,k;
+       //#pragma omp parallel for
+                               for (k=0;k<dimZ;k++) 
                                {
-                                       for (k=0;k<dimZ;k++) 
+                                       for (j=0;j<dimY;j++) 
                                        {
-                                               if ( bbGetInputByImagePoints()->GetScalarComponentAsDouble(i,j,k, 0)>0)
+                                               for (i=0;i<dimX;i++) 
                                                {
-                                                       if (bbGetInputImage2()->GetScalarComponentAsDouble(i,j,k, 0)==0)
+                                                       GETVALUE_VTK_CREA(vBIP,pBIP,sTBIP)
+                                                       if (vBIP>0)
                                                        {
-                                                               manualpaintmodel->PaintImage(i,j,k);
-                                                       } // bbGetInputImage2           
-                                               } // GetScalarComponentAsDouble                                         
-                                       }// for k
-                               }// for j
-                       }// for i
-                       printf("ManualPaint_Model %d%\n", 100 );
-               } // if ByImagePoints
-        } else {
-               printf("EED Warning: Image not set. Box creaMaracasVisu::ManualPaint_Model (BBTK) \n");
-       } // if 
+                                                               GETVALUE_VTK_CREA(vI2,pI2,sTI2)
+                                                               if (vI2==0)
+                                                               {
+                                                                       manualpaintmodel->PaintImage(i,j,k);
+       //                                                              manualpaintmodel->SetUndoImage();     // Probably needed.   Here is ok.
+                                                               } // vI2                
+                                                       } // vBIP 
+                                                       pBIP = pBIP + sSBIP;                            
+                                                       pI2  = pI2  + sSI2;                             
+                                               }// for i
+                                       }// for j
+                               }// for k
+//-------  Original ByImagePoints sin openmp--------  */
+
+
+                       } // if ByImagePoints
+
+                } else {
+                       printf("EED Warning!!  creaMaracasVisu::ManualPaint_Model (BBTK)   bbInputImage not set.  \n");
+               } // if (bbGetInputImage()!=NULL)
+       } // if active
 
        if (bbGetInputImage2()!=NULL)
        { 
@@ -100,6 +384,7 @@ void ManualPaint_Model::Process()
        } else { 
                bbSetOutputOut( NULL );
        } 
+       bbSetOutputManualpaintmodel( manualpaintmodel );
 }
 //===== 
 // 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)
@@ -111,30 +396,26 @@ void ManualPaint_Model::bbUserSetDefaultValues()
 //    Here we initialize the input 'In' to 0
 
        bbSetInputActive(true);
-       bbSetInputTool(1);  // Paint
-       bbSetInput2D3D(1);  // 3D
+       bbSetInputTool(1);              // Paint
+       bbSetInput2D3D(1);              // 3D
        bbSetInputImage(NULL);
        bbSetInputImage2(NULL);
        bbSetInputGrayLevel(100);
-       bbSetInputDirection(2); // XY
+       bbSetInputDirection(2);         // XY
        bbSetInputBrushSize(3);
-       bbSetInputBrushForm(1); // sphere
+       bbSetInputBrushForm(1);         // sphere
        bbSetInputToleranceFill(50);
        bbSetInputDistanceFill(25);
-
        std::vector<int> point;
-       point.push_back(0);
-       point.push_back(0);
-       point.push_back(0);
-       bbSetInputPoint(point);
-
+//     point.push_back(0);
+//     point.push_back(0);
+//     point.push_back(0);
+//     bbSetInputPoint(point);
        std::vector<double> range;
        range.push_back(0);
        range.push_back(200);
        bbSetInputRange(range);
-
        bbSetInputByImagePoints(NULL);
-  
 }
 //===== 
 // 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)
@@ -146,10 +427,7 @@ void ManualPaint_Model::bbUserInitializeProcessing()
 //    Here does nothing 
 //    but this is where you should allocate the internal/output pointers 
 //    if any 
-
-       manualpaintmodel=new ManualPaintModel();
-
-  
+       manualpaintmodel=new ManualPaintModel();  
 }
 //===== 
 // 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)