]> 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 //------------------------------------------------------------------------
480 void ContourExtractData::Statistics( std::vector<double> *inputLstValue, 
481                                                                         int     grayRangeMin,
482                                                                         int     grayRangeMax,
483                                                                         int             *rCountRange, 
484                                                                         int             *rsize, 
485                                                                         double  *rmin, 
486                                                                         double  *rmax,
487                                                                         double  *raverage,
488                                                                         double  *rstandardeviation)
489 {
490           double min                            = 0;
491           double max                            = 0;
492           double average                        = 0;
493           double standardeviation       = 0;
494           double acum                           = 0;
495           int    size                           = 0;
496           int    countRange                     = 0;
497           double ng;
498
499           if (inputLstValue!=NULL)
500           {
501                 size=inputLstValue->size();
502                 if (size>0){
503                max=(*inputLstValue)[0];
504                    min=(*inputLstValue)[0];
505      // Average , countRange
506                         int i;
507                         for ( i=0; i<size; i++ )
508                         {
509                                 ng=(*inputLstValue)[i];
510                                 acum = acum + ng;
511                                 if (max<ng) max=ng;             // Max
512                                 if (min>ng) min=ng;     // Min
513                                 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;  // countRange
514                         }
515                         average = acum / size;
516
517           // Standar Deviation
518                         acum=0;
519                         double tmp;
520                         for ( i=0; i<size; i++ )
521                         {
522                 tmp = (*inputLstValue)[i] - average;
523                                 acum = acum + tmp*tmp;
524                         }
525                         standardeviation = sqrt(acum/size);
526
527                 }
528           }
529
530           // OUTPUT
531                 *rsize                          = size; 
532                 *rCountRange            = countRange;
533                 *rmin                           = min; 
534                 *rmax                           = max;
535                 *raverage                       = average;
536                 *rstandardeviation      = standardeviation;
537 }
538
539 //------------------------------------------------------------------------
540 void ContourExtractData::SetTypeOperation(int type)
541 {
542         _typeOperation=type;
543 }