]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/Contour/ContourExtractData.cxx
no message
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / Contour / ContourExtractData.cxx
1
2 #include "ContourExtractData.h"
3
4
5 //----------------------------------------------------------------------
6 ContourExtractData::ContourExtractData( bool okImagesResults)
7   {
8     this->imagedata                     = NULL;
9         imagedataValueResult    = NULL;
10         imagedataMaskResult             = NULL;
11         this->okImagesResults   = okImagesResults;
12         _typeOperation                  = 0;
13   }
14
15   // ------------------------------------------------------------------------
16
17   ContourExtractData::~ContourExtractData()
18   {
19   }
20
21
22 //----------------------------------------------------------------------
23 void ContourExtractData::SetImage( vtkImageData* imagedata)
24   {
25     this->imagedata                     = imagedata;
26         if (this->okImagesResults==true){
27                 InitVtkImagesResult();
28         }
29   }
30 //----------------------------------------------------------------------
31 void ContourExtractData::SetZtoBeAnalys( int z )
32   {
33         this->zImage                    = z;
34   }
35
36 //------------------------------------------------------------------------
37   void ContourExtractData::SetLstManualContourModel( std::vector<manualContourModel*> lstManConMod)
38   {
39           this->lstManConMod = lstManConMod;
40   }
41
42
43 //------------------------------------------------------------------------
44 void ContourExtractData::GetMinMaxPoint(int *minPoint, 
45                                                                                   int *maxPoint, 
46                                                                                   manualContourModel *manualcontourmodel
47                                                                                   )
48 {
49         int i;
50         //int   np              = manualcontourmodel->GetSizeLstPoints( );  // number of control points // JPRx
51
52 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
53         //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
54         int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
55 //------------------------------------------------------------------------------------------------------
56         
57 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
58         //double x,y,z,t;
59         double x,y,z;
60         //double delta=( double ) ( np  ) / ( double ) ( nps  );
61         manualcontourmodel->UpdateSpline();
62         for (i=0; i<nps; i++)
63         {
64                 //t= delta * (double)i;
65                 //manualcontourmodel->GetSplinePoint(t,x,y,z);
66                 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
67                 if (x<minPoint[0]){ minPoint[0]=(int)x; }
68                 if (y<minPoint[1]){ minPoint[1]=(int)y; }
69                 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
70                 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
71         }
72         minPoint[0]--;
73         minPoint[1]--;
74         maxPoint[0]++;
75         maxPoint[1]++;
76 //------------------------------------------------------------------------------------------------------
77 }
78
79 //------------------------------------------------------------------------
80 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod(        int *minPoint, 
81                                                                                         int *maxPoint
82                                                                                 )
83
84 {
85         int i,size = lstManConMod.size();
86         for(i=0 ; i<size ; i++)
87         {
88                 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
89         }
90 }
91 //------------------------------------------------------------------------
92
93 int ContourExtractData::AnalisisContourInside(int x, 
94                                             int y, 
95                                             manualContourModel *manualcontourmodel
96                                             )
97 {
98         bool inBorder=false;
99         int result      = 0;
100         int i;
101         //int   np              = manualcontourmodel->GetSizeLstPoints( );  // number of control points // JPRx
102
103 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
104         //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
105         int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
106         double x1,y1,z1,x2,y2,z2;
107         double borderX, borderY;
108         double xx1, yy1,xx2, yy2;
109         //double delta=( double ) ( np  ) / ( double ) ( nps  );
110         manualcontourmodel->UpdateSpline();
111 //------------------------------------------------------------------------------------------------------
112         double d;
113         bool ok;
114 //      if (np>=2)
115 //      {
116 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
117 //              nps--;
118                 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
119                 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
120             x1=x1+0.5; y1=y1+0.5;
121                 for (i=1; i<=nps; i++)
122                 {
123                         borderX=x1;
124                         borderY=y1;
125                         
126                         ok=false;
127                         //t= delta * (double)(i%nps);
128                         //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
129                         manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
130                         x2=x2+0.5; y2=y2+0.5;
131 //------------------------------------------------------------------------------------------------------
132                                                                         
133                         //by triangle similarity
134                         if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
135                         {
136                                 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; } 
137                                 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
138                                 if ( (xx1<xx2)&&(x<(xx1+d)) )   
139                                 { 
140                                         result++; 
141                                         borderX=xx1+d;
142                                         borderY=y;
143                                 } 
144                                 if ( (xx1>xx2)&&(x<(xx1-d)) )  { 
145                                         result++; 
146                                         borderX=xx1-d;
147                                         borderY=y;
148                                 } 
149                         } // if point inside contour 
150                         
151                         
152                         //Border verication
153                         if (   (x==(int)borderX) && (y==(int)borderY)   )  
154                         { 
155                                 inBorder=true;
156                                 i=nps;
157                         }// if point in border
158                         
159                         x1=x2; y1=y2; z1=z2;
160                         
161                 } // for i
162 //      } //if
163         
164         if (inBorder==true)     { result=1;     }
165         
166         return result;
167 }
168
169 //------------------------------------------------------------------------
170
171
172 bool ContourExtractData::isInside(int x, int y, int typeOperation)
173 {
174         bool result                             = false;
175         int numberLeft                  = 0;
176         int i,size                              = this->lstManConMod.size();
177         int numberInside        = 0;
178
179         int ext[6];
180         imagedata->GetExtent(ext);
181
182         if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
183         {
184
185                 if (typeOperation==0)  // AND  Intersection
186                 {
187                         for (i=0;i<size;i++)
188                         {
189                                 numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
190                                 if ( (numberLeft % 2) ==1){         numberInside++;  }
191                         }
192                         if ( numberInside == (size) ){ result=true; }
193                 } // AND  Intersection
194
195
196                 if (typeOperation==1)  // OR  All
197                 {
198                         for (i=0;i<size;i++)
199                         {
200                                 numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
201                                 if ( (numberLeft % 2) ==1){ result=true;  }
202                         }
203                 } // OR  All
204
205                 if (typeOperation==2)  // XOR  crown
206                 {
207                         for (i=0;i<size;i++)
208                         {
209                                 numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
210                         }
211                         if ( numberLeft % 2 ==1){       result = true;  } 
212                 }// XOR  crown
213
214
215
216         }
217
218         return result;
219 }
220
221 //------------------------------------------------------------------------
222
223 double ContourExtractData::GetDataValue(int x, int y, int z)
224 {
225 //      wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
226 //      int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
227         //JSTG 13-03-08-----
228 //EED OJO avec JS       _zz = z;
229         //------------------
230         double result;
231         void *p;
232         p = imagedata->GetScalarPointer(x,y,z);
233
234         if (imagedata->GetScalarType()==VTK_CHAR)
235         {
236                 char *pp = (char*)p;
237                 result = (double)(*pp);
238         }
239         else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
240         {
241                 signed char *pp = (signed char*)p;
242                 result = (double)(*pp);
243         }
244         else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
245         {
246                 unsigned char *pp = (unsigned char*)p;
247                 result = (double)(*pp);
248         }
249         else if (imagedata->GetScalarType()==VTK_SHORT)
250         {
251                 short *pp = (short*)p;
252                 result = (double)(*pp);
253         }
254         else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
255         {
256                 unsigned short *pp = (unsigned short*)p;
257                 result = (double)(*pp);
258         }
259         else if (imagedata->GetScalarType()==VTK_INT)
260         {
261                 int *pp = (int*)p;
262                 result = (double)(*pp);
263         }
264         else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
265         {
266                 unsigned int *pp = (unsigned int*)p;
267                 result = (double)(*pp);
268         }
269         else if (imagedata->GetScalarType()==VTK_LONG)
270         {
271                 long *pp = (long*)p;
272                 result = (double)(*pp);
273         }
274         else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
275         {
276                 unsigned long *pp = (unsigned long*)p;
277                 result = (double)(*pp);
278         }
279         else if (imagedata->GetScalarType()==VTK_FLOAT)
280         {       
281                 float *pp = (float*)p;
282                 result = (double)(*pp);
283         }
284         else if (imagedata->GetScalarType()==VTK_DOUBLE)
285         {
286                 double *pp = (double*)p;
287                 result = (double)(*pp);
288         }
289
290         return result;
291 }
292
293 //------------------------------------------------------------------------
294
295 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
296 {
297         unsigned short *pValue;
298         unsigned short *pMask;
299         pValue  = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
300         pMask   = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
301         *pMask  = 255;
302         *pValue = (unsigned short)value;
303 }
304
305 //------------------------------------------------------------------------
306 void ContourExtractData::ResetImageResult(int z)
307 {
308         if (okImagesResults==true)
309         {
310                 unsigned short *pValue;
311                 unsigned short *pMask;
312                 pValue  = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
313                 pMask   = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
314                 
315                 int ext[6];
316                 imagedataValueResult->GetExtent(ext);
317
318                 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1); 
319                 memset(pValue,0,size*2);
320                 memset(pMask,0,size*2);
321         /*      
322                 int i; 
323                 for(i=0; i<size; i++)
324                 {
325                         *pMask  = 0;
326                         *pValue = 0;
327                         pMask++;
328                         pValue++;
329                 }// for
330         */ 
331                 
332         } // if
333 }
334
335
336 //------------------------------------------------------------------------
337 void ContourExtractData::CalculateImageResult()
338 {
339         if (okImagesResults==true)
340         {
341                 ResetImageResult(zImage);
342
343                 int minPoint[2];
344                 int maxPoint[2];
345                 int i,j;
346                 double value;
347
348                 minPoint[0] = 999999;
349                 minPoint[1] = 999999;
350                 maxPoint[0] = -999999;
351                 maxPoint[1] = -999999;
352
353                 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
354                 for (j=minPoint[1]; j<=maxPoint[1]; j++)
355                 {
356                         for (i=minPoint[0]; i<=maxPoint[0]; i++)
357                         {
358                                 if (isInside(i,j,_typeOperation)==true)
359                                 {
360                                         value = GetDataValue(i,j,zImage);
361                                         PutVtkImageDataResultValue(i,j,zImage,  value );
362                                 } // if
363                         } // for i
364                 } // for j
365
366
367                 imagedataValueResult->Modified();
368                 imagedataMaskResult->Modified();
369                 imagedataValueResult->Update();
370                 imagedataMaskResult->Update();
371         } // if
372
373 }
374
375 //------------------------------------------------------------------------
376 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
377                                                                                                 std::vector<double> *pLstValuePosX,
378                                                                                                 std::vector<double> *pLstValuePosY,
379                                                                                                 std::vector<double> *pLstValuePosZ)
380 {
381         pLstValue->clear();
382         pLstValuePosX->clear();
383         pLstValuePosY->clear();
384         pLstValuePosZ->clear();
385
386 //      if (okImagesResults==true)
387 //      {
388 //              ResetImageResult(zImage);
389 //      }
390
391         int minPoint[2];
392         int maxPoint[2];
393         int i,j;
394         double value;
395
396
397         minPoint[0] = 999999;
398         minPoint[1] = 999999;
399         maxPoint[0] = -999999;
400         maxPoint[1] = -999999;
401
402         GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
403
404
405         for (j=minPoint[1]; j<=maxPoint[1]; j++)
406         {
407                 for (i=minPoint[0]; i<=maxPoint[0]; i++)
408                 {
409                         if (isInside(i,j,_typeOperation)==true)
410                         {
411                                 value = GetDataValue(i,j,zImage);
412
413 // Borrame
414 //                              if (okImagesResults==true){
415 //                                      PutVtkImageDataResultValue(i,j,zImage,  value );
416 //                              }
417
418                                 pLstValue               -> push_back( value );
419                                 pLstValuePosX   -> push_back( i );
420                                 pLstValuePosY   -> push_back( j );
421                                 pLstValuePosZ   -> push_back( -1 );
422                         } // if
423                 } // for
424         } // for
425
426
427 // Borrame
428 //      if (this->okImagesResults==true){
429 //              imagedataValueResult->Modified();
430 //              imagedataMaskResult->Modified();
431 //      }
432
433
434 }
435
436 //------------------------------------------------------------------------
437
438 vtkImageData *ContourExtractData::GetVtkImageValueResult()
439 {
440         return imagedataValueResult;
441 }
442 //------------------------------------------------------------------------
443 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
444 {
445         return imagedataMaskResult;
446 }
447 // ------------------------------------------------------------------------
448 void ContourExtractData::InitVtkImagesResult()
449 {
450         printf("EED ContourExtractData::InitVtkImagesResult\n");
451           int ext[6];
452           int newDim[3];
453           double spc[3];
454           int scalartype;
455
456           imagedata->GetSpacing(spc);
457           imagedata->GetExtent(ext);
458           newDim[0]=ext[1]-ext[0]+1;
459           newDim[1]=ext[3]-ext[2]+1;
460           newDim[2]=ext[5]-ext[4]+1;
461           scalartype = imagedata->GetScalarType();
462
463           if (imagedataValueResult!=NULL)
464           {
465                   imagedataValueResult->Delete();
466           }
467           imagedataValueResult = vtkImageData::New();
468 //        imagedataValueResult->SetScalarType(scalartype);
469           imagedataValueResult->SetScalarTypeToUnsignedShort();
470           imagedataValueResult->SetSpacing(spc);
471           imagedataValueResult->SetDimensions( newDim );
472           imagedataValueResult->AllocateScalars();
473
474           if (imagedataMaskResult!=NULL)
475           {
476                   imagedataMaskResult->Delete();
477           }
478           imagedataMaskResult  = vtkImageData::New();
479 //        imagedataMaskResult->SetScalarType(scalartype);
480           imagedataMaskResult->SetScalarTypeToUnsignedShort();
481           imagedataMaskResult->SetSpacing(spc);
482           imagedataMaskResult->SetDimensions( newDim );
483           imagedataMaskResult->AllocateScalars();
484         printf("EED ContourExtractData::InitVtkImagesResult %p %p\n", imagedataValueResult, imagedataMaskResult);
485 }
486
487
488 //------------------------------------------------------------------------
489 void ContourExtractData::InitVolumeStatistics()
490 {
491         vol_rCountRange                         = 0;
492         vol_rsize                                       = 0;
493         vol_minValue                            = 9999999;
494         vol_maxValue                            =-9999999;
495         vol_acum_average                        = 0;
496         vol_acum_standardeviation       = 0;
497 }
498
499 //------------------------------------------------------------------------
500 void ContourExtractData::SetVolumeStatistics(int rCountRange, 
501                                                                                         int rsize,
502                                                                                         double minValue,
503                                                                                         double maxValue,
504                                                                                         double acum_average,
505                                                                                         double acum_standardeviation)
506 {
507         vol_rCountRange                         = vol_rCountRange + rCountRange; 
508         vol_rsize                                       = vol_rsize + rsize; 
509         
510         if (minValue<vol_minValue){ vol_minValue = minValue;  }
511         if (maxValue>vol_maxValue){ vol_maxValue = maxValue;  }
512         
513         vol_acum_average                        = vol_acum_average + acum_average; 
514         vol_acum_standardeviation       = vol_acum_standardeviation + acum_standardeviation; 
515 }
516
517 //------------------------------------------------------------------------
518 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange, 
519                                                                                          int *vol_rsize,
520                                                                                          double *vol_minValue,
521                                                                                          double *vol_maxValue,
522                                                                                          double *vol_average,
523                                                                                          double *vol_standardeviation)
524 {
525         *vol_rCountRange                = this->vol_rCountRange; 
526         *vol_rsize                              = this->vol_rsize; 
527         *vol_minValue                   = this->vol_minValue; 
528         *vol_maxValue                   = this->vol_maxValue; 
529         *vol_average                    = this->vol_acum_average / this->vol_rsize;
530         *vol_standardeviation   = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
531 }
532
533
534 //------------------------------------------------------------------------
535 void ContourExtractData::Statistics( std::vector<double> *inputLstValue, 
536                                                                         int     grayRangeMin,
537                                                                         int     grayRangeMax,
538                                                                         int             *rCountRange, 
539                                                                         int             *rsize, 
540                                                                         double  *rmin, 
541                                                                         double  *rmax,
542                                                                         double  *raverage,
543                                                                         double  *rstandardeviation
544                                                                                 )
545 {
546           double min                                            = 0;
547           double max                                            = 0;
548           double average                                        = 0;
549           double standardeviation                       = 0;
550           double acum_average                           = 0;
551           double acum_standardeviation          = 0;
552           int    size                                           = 0;
553           int    countRange                                     = 0;
554           double ng;
555
556           if (inputLstValue!=NULL)
557           {
558                 size=inputLstValue->size();
559                 if (size>0){
560                max=(*inputLstValue)[0];
561                    min=(*inputLstValue)[0];
562      // Average , countRange
563                         int i;
564                         for ( i=0; i<size; i++ )
565                         {
566                                 ng=(*inputLstValue)[i];
567                                 acum_average = acum_average + ng;
568                                 if (max<ng) max=ng;             // Max
569                                 if (min>ng) min=ng;     // Min
570                                 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;  // countRange
571                         } // for average
572                         average = acum_average / size;
573
574           // Standar Deviation
575                         acum_standardeviation=0;
576                         double tmp;
577                         for ( i=0; i<size; i++ )
578                         {
579                 tmp = (*inputLstValue)[i] - average;
580                                 acum_standardeviation = acum_standardeviation + tmp*tmp;
581                         } // for standar deviation
582                         standardeviation = sqrt(acum_standardeviation/size);
583                         SetVolumeStatistics(countRange, size,
584                                                                 min,max,
585                                                             acum_average,acum_standardeviation);
586                 } // if size
587           } // if NULL
588
589           // OUTPUT
590                 *rsize                          = size; 
591                 *rCountRange            = countRange;
592                 *rmin                           = min; 
593                 *rmax                           = max;
594                 *raverage                       = average;
595                 *rstandardeviation      = standardeviation;
596 }
597
598 //------------------------------------------------------------------------
599 void ContourExtractData::SetTypeOperation(int type)
600 {
601         _typeOperation=type;
602 }