]> 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 i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1); 
319                 for(i=0; i<size; i++)
320                 {
321                         *pMask  = 0;
322                         *pValue = 0;
323                         pMask++;
324                         pValue++;
325                 }// for
326         } // if
327 }
328
329
330 //------------------------------------------------------------------------
331 void ContourExtractData::CalculateImageResult()
332 {
333         if (okImagesResults==true)
334         {
335                 ResetImageResult(zImage);
336
337                 int minPoint[2];
338                 int maxPoint[2];
339                 int i,j;
340                 double value;
341
342                 minPoint[0] = 999999;
343                 minPoint[1] = 999999;
344                 maxPoint[0] = -999999;
345                 maxPoint[1] = -999999;
346
347                 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
348                 for (j=minPoint[1]; j<=maxPoint[1]; j++)
349                 {
350                         for (i=minPoint[0]; i<=maxPoint[0]; i++)
351                         {
352                                 if (isInside(i,j,_typeOperation)==true)
353                                 {
354                                         value = GetDataValue(i,j,zImage);
355                                         PutVtkImageDataResultValue(i,j,zImage,  value );
356                                 } // if
357                         } // for i
358                 } // for j
359
360
361                 imagedataValueResult->Modified();
362                 imagedataMaskResult->Modified();
363         }
364
365 }
366
367 //------------------------------------------------------------------------
368 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
369                                                                                                 std::vector<double> *pLstValuePosX,
370                                                                                                 std::vector<double> *pLstValuePosY,
371                                                                                                 std::vector<double> *pLstValuePosZ)
372 {
373         pLstValue->clear();
374         pLstValuePosX->clear();
375         pLstValuePosY->clear();
376         pLstValuePosZ->clear();
377
378 //      if (okImagesResults==true)
379 //      {
380 //              ResetImageResult(zImage);
381 //      }
382
383         int minPoint[2];
384         int maxPoint[2];
385         int i,j;
386         double value;
387
388
389         minPoint[0] = 999999;
390         minPoint[1] = 999999;
391         maxPoint[0] = -999999;
392         maxPoint[1] = -999999;
393
394         GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
395
396
397         for (j=minPoint[1]; j<=maxPoint[1]; j++)
398         {
399                 for (i=minPoint[0]; i<=maxPoint[0]; i++)
400                 {
401                         if (isInside(i,j,_typeOperation)==true)
402                         {
403                                 value = GetDataValue(i,j,zImage);
404
405 // Borrame
406 //                              if (okImagesResults==true){
407 //                                      PutVtkImageDataResultValue(i,j,zImage,  value );
408 //                              }
409
410                                 pLstValue               -> push_back( value );
411                                 pLstValuePosX   -> push_back( i );
412                                 pLstValuePosY   -> push_back( j );
413                                 pLstValuePosZ   -> push_back( -1 );
414                         } // if
415                 } // for
416         } // for
417
418
419 // Borrame
420 //      if (this->okImagesResults==true){
421 //              imagedataValueResult->Modified();
422 //              imagedataMaskResult->Modified();
423 //      }
424
425
426 }
427
428 //------------------------------------------------------------------------
429
430 vtkImageData *ContourExtractData::GetVtkImageValueResult()
431 {
432         return imagedataValueResult;
433 }
434 //------------------------------------------------------------------------
435 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
436 {
437         return imagedataMaskResult;
438 }
439 // ------------------------------------------------------------------------
440 void ContourExtractData::InitVtkImagesResult()
441 {
442           int ext[6];
443           int newDim[3];
444           double spc[3];
445           int scalartype;
446
447           imagedata->GetSpacing(spc);
448           imagedata->GetExtent(ext);
449           newDim[0]=ext[1]-ext[0]+1;
450           newDim[1]=ext[3]-ext[2]+1;
451           newDim[2]=ext[5]-ext[4]+1;
452           scalartype = imagedata->GetScalarType();
453
454           if (imagedataValueResult!=NULL)
455           {
456                   imagedataValueResult->Delete();
457           }
458           imagedataValueResult = vtkImageData::New();
459 //        imagedataValueResult->SetScalarType(scalartype);
460           imagedataValueResult->SetScalarTypeToUnsignedShort();
461           imagedataValueResult->SetSpacing(spc);
462           imagedataValueResult->SetDimensions( newDim );
463           imagedataValueResult->AllocateScalars();
464
465           if (imagedataMaskResult!=NULL)
466           {
467                   imagedataMaskResult->Delete();
468           }
469           imagedataMaskResult  = vtkImageData::New();
470 //        imagedataMaskResult->SetScalarType(scalartype);
471           imagedataMaskResult->SetScalarTypeToUnsignedShort();
472           imagedataMaskResult->SetSpacing(spc);
473           imagedataMaskResult->SetDimensions( newDim );
474           imagedataMaskResult->AllocateScalars();
475 }
476
477
478 //------------------------------------------------------------------------
479 void ContourExtractData::InitVolumeStatistics()
480 {
481         vol_rCountRange                         = 0;
482         vol_rsize                                       = 0;
483         vol_minValue                            = 9999999;
484         vol_maxValue                            =-9999999;
485         vol_acum_average                        = 0;
486         vol_acum_standardeviation       = 0;
487 }
488
489 //------------------------------------------------------------------------
490 void ContourExtractData::SetVolumeStatistics(int rCountRange, 
491                                                                                         int rsize,
492                                                                                         double minValue,
493                                                                                         double maxValue,
494                                                                                         double acum_average,
495                                                                                         double acum_standardeviation)
496 {
497         vol_rCountRange                         = vol_rCountRange + rCountRange; 
498         vol_rsize                                       = vol_rsize + rsize; 
499         
500         if (minValue<vol_minValue){ vol_minValue = minValue;  }
501         if (maxValue>vol_maxValue){ vol_maxValue = maxValue;  }
502         
503         vol_acum_average                        = vol_acum_average + acum_average; 
504         vol_acum_standardeviation       = vol_acum_standardeviation + acum_standardeviation; 
505 }
506
507 //------------------------------------------------------------------------
508 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange, 
509                                                                                          int *vol_rsize,
510                                                                                          double *vol_minValue,
511                                                                                          double *vol_maxValue,
512                                                                                          double *vol_average,
513                                                                                          double *vol_standardeviation)
514 {
515         *vol_rCountRange                = this->vol_rCountRange; 
516         *vol_rsize                              = this->vol_rsize; 
517         *vol_minValue                   = this->vol_minValue; 
518         *vol_maxValue                   = this->vol_maxValue; 
519         *vol_average                    = this->vol_acum_average / this->vol_rsize;
520         *vol_standardeviation   = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
521 }
522
523
524 //------------------------------------------------------------------------
525 void ContourExtractData::Statistics( std::vector<double> *inputLstValue, 
526                                                                         int     grayRangeMin,
527                                                                         int     grayRangeMax,
528                                                                         int             *rCountRange, 
529                                                                         int             *rsize, 
530                                                                         double  *rmin, 
531                                                                         double  *rmax,
532                                                                         double  *raverage,
533                                                                         double  *rstandardeviation
534                                                                                 )
535 {
536           double min                                            = 0;
537           double max                                            = 0;
538           double average                                        = 0;
539           double standardeviation                       = 0;
540           double acum_average                           = 0;
541           double acum_standardeviation          = 0;
542           int    size                                           = 0;
543           int    countRange                                     = 0;
544           double ng;
545
546           if (inputLstValue!=NULL)
547           {
548                 size=inputLstValue->size();
549                 if (size>0){
550                max=(*inputLstValue)[0];
551                    min=(*inputLstValue)[0];
552      // Average , countRange
553                         int i;
554                         for ( i=0; i<size; i++ )
555                         {
556                                 ng=(*inputLstValue)[i];
557                                 acum_average = acum_average + ng;
558                                 if (max<ng) max=ng;             // Max
559                                 if (min>ng) min=ng;     // Min
560                                 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;  // countRange
561                         } // for average
562                         average = acum_average / size;
563
564           // Standar Deviation
565                         acum_standardeviation=0;
566                         double tmp;
567                         for ( i=0; i<size; i++ )
568                         {
569                 tmp = (*inputLstValue)[i] - average;
570                                 acum_standardeviation = acum_standardeviation + tmp*tmp;
571                         } // for standar deviation
572                         standardeviation = sqrt(acum_standardeviation/size);
573                         SetVolumeStatistics(countRange, size,
574                                                                 min,max,
575                                                             acum_average,acum_standardeviation);
576                 } // if size
577           } // if NULL
578
579           // OUTPUT
580                 *rsize                          = size; 
581                 *rCountRange            = countRange;
582                 *rmin                           = min; 
583                 *rmax                           = max;
584                 *raverage                       = average;
585                 *rstandardeviation      = standardeviation;
586 }
587
588 //------------------------------------------------------------------------
589 void ContourExtractData::SetTypeOperation(int type)
590 {
591         _typeOperation=type;
592 }