]> Creatis software - creaMaracasVisu.git/blobdiff - lib/maracasVisuLib/src/interface/wxWindows/Contour/ContourExtractData.cxx
no newline at end of file
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / Contour / ContourExtractData.cxx
index 4d7c69ab42994432e4888a4dc47ad4ec64a1ee42..10e04156dc251ce1914f30ecb440173da28a4e72 100644 (file)
@@ -22,10 +22,10 @@ ContourExtractData::ContourExtractData( bool okImagesResults)
 //----------------------------------------------------------------------
 void ContourExtractData::SetImage( vtkImageData* imagedata)
   {
-    this->imagedata                    = imagedata;
-       if (this->okImagesResults==true){
-               InitVtkImagesResult();
-       }
+       this->imagedata                 = imagedata;
+               
+       // init vtk image result : valuesImage maskImage  
+       if (this->okImagesResults==true){  InitVtkImagesResult(); }
   }
 //----------------------------------------------------------------------
 void ContourExtractData::SetZtoBeAnalys( int z )
@@ -34,7 +34,7 @@ void ContourExtractData::SetZtoBeAnalys( int z )
   }
 
 //------------------------------------------------------------------------
-  void ContourExtractData::SetLstManualContourModel( std::vector<manualContourModel*> lstManConMod)
+  void ContourExtractData::SetLstManualContourModel( std::vector<manualBaseModel*> lstManConMod)
   {
          this->lstManConMod = lstManConMod;
   }
@@ -43,12 +43,12 @@ void ContourExtractData::SetZtoBeAnalys( int z )
 //------------------------------------------------------------------------
 void ContourExtractData::GetMinMaxPoint(int *minPoint, 
                                                                                  int *maxPoint, 
-                                                                                 manualContourModel *manualcontourmodel
+                                                                                 manualBaseModel *manualcontourmodel
                                                                                  )
 {
        int i;
        //int   np              = manualcontourmodel->GetSizeLstPoints( );  // number of control points // JPRx
-
+               
 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
        //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
        int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
@@ -83,92 +83,104 @@ void ContourExtractData::GetMinMaxPoint_Of_LstManConMod(   int *minPoint,
 
 {
        int i,size = lstManConMod.size();
+
        for(i=0 ; i<size ; i++)
        {
                GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
        }
 }
-//------------------------------------------------------------------------
 
-int ContourExtractData::AnalisisContourInside(int x, 
-                                           int y, 
-                                           manualContourModel *manualcontourmodel
-                                           )
+
+//------------------------------------------------------------------------
+int ContourExtractData::AnalisisContourInsideV2(int x, int y, int iContour )
 {
        bool inBorder=false;
        int result      = 0;
        int i;
-       //int   np              = manualcontourmodel->GetSizeLstPoints( );  // number of control points // JPRx
 
-// JSTG 26-02-08 ---------------------------------------------------------------------------------------
-       //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
-       int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
-       double x1,y1,z1,x2,y2,z2;
+       int nps=_lstlstlstVecX1[iContour][y].size();
+       
+       double x1,y1,x2,y2;
        double borderX, borderY;
        double xx1, yy1,xx2, yy2;
-       //double delta=( double ) ( np  ) / ( double ) ( nps  );
-       manualcontourmodel->UpdateSpline();
-//------------------------------------------------------------------------------------------------------
+       double xx=x, yy=y;
        double d;
-       bool ok;
-//     if (np>=2)
-//     {
-// JSTG 26-02-08 ---------------------------------------------------------------------------------------
-//             nps--;
-               //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
-               manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
-           x1=x1+0.5; y1=y1+0.5;
-               for (i=1; i<=nps; i++)
+
+       for (i=0; i<nps; i++)
+       {
+               x1=_lstlstlstVecX1[iContour][y][i];
+               y1=_lstlstlstVecY1[iContour][y][i];
+               x2=_lstlstlstVecX2[iContour][y][i];
+               y2=_lstlstlstVecY2[iContour][y][i];
+
+               borderX=x1;
+               borderY=y1;
+               
+               if (y1<y2) 
+               { 
+                       xx1=x1; yy1=y1; xx2=x2; yy2=y2;
+               } else {
+                       xx1=x2; yy1=y2; xx2=x1; yy2=y1; 
+               } 
+               
+               double difxx2xx1=fabs(xx2-xx1);
+               if (difxx2xx1==0) difxx2xx1=0.0000000001;
+               
+               // Finding border looking in vertical direction  AND  verifing if pixel is at right of the line 
+               if  ( (yy>=yy1)&&(yy<=yy2) ) 
                {
-                       borderX=x1;
-                       borderY=y1;
-                       
-                       ok=false;
-                       //t= delta * (double)(i%nps);
-                       //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
-                       manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
-                       x2=x2+0.5; y2=y2+0.5;
-//------------------------------------------------------------------------------------------------------
-                                                                       
                        //by triangle similarity
-                       if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
+                       d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
+                       if ( (xx1<=xx2)&&(x<(xx1+d)) )  {       result++;       }
+                       if ( (xx1>xx2)&&(x<(xx1-d)) )   {       result++;       } 
+                       
+                       if ( (yy2-yy1)/difxx2xx1 >= 1.0)
                        {
-                               if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; } 
-                               d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
-                               if ( (xx1<xx2)&&(x<(xx1+d)) )   
+                               if (xx1<=xx2)   
                                { 
-                                       result++; 
-                                       borderX=xx1+d;
-                                       borderY=y;
+                                       borderX = xx1+d;
+                                       borderY = y;
+                               } else { 
+                                       borderX = xx1-d;
+                                       borderY = y;
                                } 
-                               if ( (xx1>xx2)&&(x<(xx1-d)) )  { 
-                                       result++; 
-                                       borderX=xx1-d;
-                                       borderY=y;
-                               } 
-                       } // if point inside contour 
-                       
-                       
-                       //Border verication
-                       if (   (x==(int)borderX) && (y==(int)borderY)   )  
-                       { 
-                               inBorder=true;
-                               i=nps;
-                       }// if point in border
-                       
-                       x1=x2; y1=y2; z1=z2;
-                       
-               } // for i
-//     } //if
+                       } 
+               } // if point inside y 
+               
+               
+               // Finding border looking in vertical direction
+               if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
+               {
+                       if ( (yy2-yy1)/difxx2xx1 <= 1.0)
+                       {
+                               //by triangle similarity
+                               d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
+                               if (yy1+d<=yy2) {
+                                       borderX=x;
+                                       borderY=yy1+d;
+                               }
+                       }                       
+               } // if point inside x                  
+               
+               
+               //Border verification
+               if (   (x==(int)borderX) && (y==(int)borderY)   )  { inBorder=true; }// if point in border
+               
+               // Verification : border in horizontal line 
+               if ( ((int)y1==(int)y2)  &&  ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2))             { inBorder=true;        }
+               if ( ((int)y1==(int)y2)  &&  ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1))             { inBorder=true;        }
+               
+               if (inBorder==true){ i=nps; }           
+       } // for i
        
        if (inBorder==true)     { result=1;     }
        
        return result;
 }
 
-//------------------------------------------------------------------------
 
 
+//------------------------------------------------------------------------
 bool ContourExtractData::isInside(int x, int y, int typeOperation)
 {
        bool result                             = false;
@@ -186,27 +198,71 @@ bool ContourExtractData::isInside(int x, int y, int typeOperation)
                {
                        for (i=0;i<size;i++)
                        {
-                               numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
+                               // To process the statistics of the Points contour the procedure is different
+                               // RaC 19-09-09
+                               manualBaseModel *mbm = lstManConMod[i];
+                               if(mbm->GetTypeModel()==7)
+                               {
+                                       if(mbm->IsPoint(x,y)==true)
+                                       {
+                                               numberLeft=1;
+                                       }
+                               }//if
+                               else
+                               {
+                                       numberLeft =  AnalisisContourInsideV2(x,y, i );
+                               }//else
+
                                if ( (numberLeft % 2) ==1){         numberInside++;  }
                        }
                        if ( numberInside == (size) ){ result=true; }
                } // AND  Intersection
 
+               numberLeft=0;
 
                if (typeOperation==1)  // OR  All
                {
                        for (i=0;i<size;i++)
                        {
-                               numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
+                               // To process the statistics of the Points contour the procedure is different
+                               // RaC 19-09-09
+                               manualBaseModel *mbm = lstManConMod[i];
+                               if(mbm->GetTypeModel()==7)
+                               {
+                                       if(mbm->IsPoint(x,y)==true)
+                                       {
+                                               numberLeft=1;
+                                       }
+                               }//if
+                               else
+                               {
+                                       numberLeft =  AnalisisContourInsideV2(x,y, i );
+                               }//else
                                if ( (numberLeft % 2) ==1){ result=true;  }
                        }
                } // OR  All
 
+               numberLeft=0;
+
                if (typeOperation==2)  // XOR  crown
                {
                        for (i=0;i<size;i++)
                        {
-                               numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
+                               // To process the statistics of the Points contour the procedure is different
+                               // RaC 19-09-09
+                               manualBaseModel *mbm = lstManConMod[i];
+                               if(mbm->GetTypeModel()==7)
+                               {
+                                       if(mbm->IsPoint(x,y)==true)
+                                       {
+                                               numberLeft=1;
+                                       }
+                               }//if
+                               else
+                               {
+                                       numberLeft =  numberLeft + AnalisisContourInsideV2(x,y, i );
+                               }//else
+                               
                        }
                        if ( numberLeft % 2 ==1){       result = true;  } 
                }// XOR  crown
@@ -315,14 +371,9 @@ void ContourExtractData::ResetImageResult(int z)
                int ext[6];
                imagedataValueResult->GetExtent(ext);
 
-               int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1); 
-               for(i=0; i<size; i++)
-               {
-                       *pMask  = 0;
-                       *pValue = 0;
-                       pMask++;
-                       pValue++;
-               }// for
+               int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1); 
+               memset(pValue,0,size*2);
+               memset(pMask,0,size*2);
        } // if
 }
 
@@ -345,6 +396,8 @@ void ContourExtractData::CalculateImageResult()
                maxPoint[1] = -999999;
 
                GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
+               InitLstContoursLinesYPoints();
+               
                for (j=minPoint[1]; j<=maxPoint[1]; j++)
                {
                        for (i=minPoint[0]; i<=maxPoint[0]; i++)
@@ -360,7 +413,9 @@ void ContourExtractData::CalculateImageResult()
 
                imagedataValueResult->Modified();
                imagedataMaskResult->Modified();
-       }
+               imagedataValueResult->Update();
+               imagedataMaskResult->Update();
+       } // if
 
 }
 
@@ -392,7 +447,7 @@ void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
        maxPoint[1] = -999999;
 
        GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
-
+    InitLstContoursLinesYPoints();
 
        for (j=minPoint[1]; j<=maxPoint[1]; j++)
        {
@@ -415,7 +470,6 @@ void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
                } // for
        } // for
 
-
 // Borrame
 //     if (this->okImagesResults==true){
 //             imagedataValueResult->Modified();
@@ -475,6 +529,51 @@ void ContourExtractData::InitVtkImagesResult()
 }
 
 
+//------------------------------------------------------------------------
+void ContourExtractData::InitVolumeStatistics()
+{
+       vol_rCountRange                         = 0;
+       vol_rsize                                       = 0;
+       vol_minValue                            = 9999999;
+       vol_maxValue                            =-9999999;
+       vol_acum_average                        = 0;
+       vol_acum_standardeviation       = 0;
+}
+
+//------------------------------------------------------------------------
+void ContourExtractData::SetVolumeStatistics(int rCountRange, 
+                                                                                       int rsize,
+                                                                                       double minValue,
+                                                                                       double maxValue,
+                                                                                       double acum_average,
+                                                                                       double acum_standardeviation)
+{
+       vol_rCountRange                         = vol_rCountRange + rCountRange; 
+       vol_rsize                                       = vol_rsize + rsize; 
+       
+       if (minValue<vol_minValue){ vol_minValue = minValue;  }
+       if (maxValue>vol_maxValue){ vol_maxValue = maxValue;  }
+       
+       vol_acum_average                        = vol_acum_average + acum_average; 
+       vol_acum_standardeviation       = vol_acum_standardeviation + acum_standardeviation; 
+}
+
+//------------------------------------------------------------------------
+void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange, 
+                                                                                        int *vol_rsize,
+                                                                                        double *vol_minValue,
+                                                                                        double *vol_maxValue,
+                                                                                        double *vol_average,
+                                                                                        double *vol_standardeviation)
+{
+       *vol_rCountRange                = this->vol_rCountRange; 
+       *vol_rsize                              = this->vol_rsize; 
+       *vol_minValue                   = this->vol_minValue; 
+       *vol_maxValue                   = this->vol_maxValue; 
+       *vol_average                    = this->vol_acum_average / this->vol_rsize;
+       *vol_standardeviation   = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
+}
+
 
 //------------------------------------------------------------------------
 void ContourExtractData::Statistics( std::vector<double> *inputLstValue, 
@@ -485,15 +584,17 @@ void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
                                                                        double  *rmin, 
                                                                        double  *rmax,
                                                                        double  *raverage,
-                                                                       double  *rstandardeviation)
+                                                                       double  *rstandardeviation
+                                                                               )
 {
-         double min                            = 0;
-         double max                            = 0;
-         double average                        = 0;
-         double standardeviation       = 0;
-         double acum                           = 0;
-         int    size                           = 0;
-         int    countRange                     = 0;
+         double min                                            = 0;
+         double max                                            = 0;
+         double average                                        = 0;
+         double standardeviation                       = 0;
+         double acum_average                           = 0;
+         double acum_standardeviation          = 0;
+         int    size                                           = 0;
+         int    countRange                                     = 0;
          double ng;
 
          if (inputLstValue!=NULL)
@@ -507,25 +608,27 @@ void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
                        for ( i=0; i<size; i++ )
                        {
                                ng=(*inputLstValue)[i];
-                               acum = acum + ng;
+                               acum_average = acum_average + ng;
                                if (max<ng) max=ng;             // Max
                                if (min>ng) min=ng;     // Min
                                if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;  // countRange
-                       }
-                       average = acum / size;
+                       } // for average
+                       average = acum_average / size;
 
          // Standar Deviation
-                       acum=0;
+                       acum_standardeviation=0;
                        double tmp;
                        for ( i=0; i<size; i++ )
                        {
                 tmp = (*inputLstValue)[i] - average;
-                               acum = acum + tmp*tmp;
-                       }
-                       standardeviation = sqrt(acum/size);
-
-               }
-         }
+                               acum_standardeviation = acum_standardeviation + tmp*tmp;
+                       } // for standar deviation
+                       standardeviation = sqrt(acum_standardeviation/size);
+                       SetVolumeStatistics(countRange, size,
+                                                               min,max,
+                                                           acum_average,acum_standardeviation);
+               } // if size
+         } // if NULL
 
          // OUTPUT
                *rsize                          = size; 
@@ -541,3 +644,69 @@ void ContourExtractData::SetTypeOperation(int type)
 {
        _typeOperation=type;
 }
+
+//------------------------------------------------------------------------
+void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
+{
+       int i,y;
+       double x1,y1,z1,x2,y2,z2;
+       manualBaseModel *manualcontourmodel= lstManConMod[iContour];    
+       int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
+       manualcontourmodel->UpdateSpline();
+       //------------------------------------------------------------------------------------------------------
+       
+       for (y=0;y<sizeY;y++)
+       {
+               manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
+               x1=x1+0.5; y1=y1+0.5;
+               for (i=1; i<nps; i++)
+               {
+                       manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
+                       x2=x2+0.5; y2=y2+0.5;
+                       if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y)  )
+                       {
+                               _lstlstlstVecX1[iContour][y].push_back(x1);
+                               _lstlstlstVecY1[iContour][y].push_back(y1);
+                               _lstlstlstVecX2[iContour][y].push_back(x2);
+                               _lstlstlstVecY2[iContour][y].push_back(y2);
+                       } 
+                       x1=x2; y1=y2; z1=z2;
+               } // for i  Points in spline
+       } // y
+
+}
+
+void ContourExtractData::InitLstContoursLinesYPoints()
+{
+       // init InInside Optimisation  
+       int i;
+       
+       _lstlstlstVecX1.clear();
+       _lstlstlstVecY1.clear();
+       _lstlstlstVecX2.clear();
+       _lstlstlstVecY2.clear();
+       
+       int ext[6];
+       this->imagedata->GetWholeExtent(ext);
+       int sizeY = ext[3]-ext[2]+1;
+       std::vector<double> vecDouble;
+       std::vector< std::vector<double> > vecVecDouble;
+       for ( i=0 ; i<sizeY ; i++ )
+       {
+               vecVecDouble.push_back( vecDouble );
+       }
+       
+       //Fill structure with points
+       int sizeContours = lstManConMod.size();
+       for( i=0 ; i<sizeContours ; i++ )
+       {
+               _lstlstlstVecX1.push_back( vecVecDouble );
+               _lstlstlstVecY1.push_back( vecVecDouble );
+               _lstlstlstVecX2.push_back( vecVecDouble );
+               _lstlstlstVecY2.push_back( vecVecDouble );
+               Fill_lstlstlstVecXY(i,sizeY);
+       }
+       
+}
+
+