]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/Contour/ContourExtractData.cxx
*** empty log 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 //------------------------------------------------------------------------------------------------------
73 }
74
75 //------------------------------------------------------------------------
76 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod(        int *minPoint, 
77                                                                                         int *maxPoint
78                                                                                 )
79
80 {
81         int i,size = lstManConMod.size();
82         for(i=0 ; i<size ; i++)
83         {
84                 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
85         }
86 }
87 //------------------------------------------------------------------------
88
89 int ContourExtractData::AnalisisContourInside(int x, 
90                                             int y, 
91                                             manualContourModel *manualcontourmodel
92                                             )
93 {
94         int result      = 0;
95         int i;
96         //int   np              = manualcontourmodel->GetSizeLstPoints( );  // number of control points // JPRx
97
98 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
99         //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
100         int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
101         //double x1,y1,z1,x2,y2,z2,t;
102         double x1,y1,z1,x2,y2,z2;
103         double xx1, yy1,xx2, yy2;
104         //double delta=( double ) ( np  ) / ( double ) ( nps  );
105         manualcontourmodel->UpdateSpline();
106 //------------------------------------------------------------------------------------------------------
107         double d;
108         bool ok;
109 //      if (np>=2)
110 //      {
111 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
112                 nps--;
113                 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
114                 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
115                 for (i=1; i<=nps; i++)
116                 {
117                         ok=false;
118                         //t= delta * (double)(i%nps);
119                         //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
120                         manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
121 //------------------------------------------------------------------------------------------------------
122                         //by triangle similarity
123                         if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
124                         {
125                                 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; } 
126                                 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
127                                 if (  ((xx1<xx2)&&(x<(xx1+d)))  ||  ((xx1>xx2)&&(x<(xx1-d)))  ) { result++; }
128                         } // if
129                         x1=x2; y1=y2; z1=z2;
130                 } // for i
131 //      } //if
132         return result;
133 }
134
135 //------------------------------------------------------------------------
136
137
138 bool ContourExtractData::isInside(int x, int y, int typeOperation)
139 {
140         bool result                             = false;
141         int numberLeft                  = 0;
142         int i,size                              = this->lstManConMod.size();
143         int numberInside        = 0;
144
145         int ext[6];
146         imagedata->GetExtent(ext);
147
148         if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
149         {
150
151                 if (typeOperation==0)  // AND  Intersection
152                 {
153                         for (i=0;i<size;i++)
154                         {
155                                 numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
156                                 if ( (numberLeft % 2) ==1){         numberInside++;  }
157                         }
158                         if ( numberInside == (size) ){ result=true; }
159                 } // AND  Intersection
160
161
162                 if (typeOperation==1)  // OR  All
163                 {
164                         for (i=0;i<size;i++)
165                         {
166                                 numberLeft =  AnalisisContourInside(x,y, lstManConMod[i] );
167                                 if ( (numberLeft % 2) ==1){ result=true;  }
168                         }
169                 } // OR  All
170
171                 if (typeOperation==2)  // XOR  crown
172                 {
173                         for (i=0;i<size;i++)
174                         {
175                                 numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
176                         }
177                         if ( numberLeft % 2 ==1){       result = true;  } 
178                 }// XOR  crown
179
180
181
182         }
183
184         return result;
185 }
186
187 //------------------------------------------------------------------------
188
189 double ContourExtractData::GetDataValue(int x, int y, int z)
190 {
191 //      wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
192 //      int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
193         //JSTG 13-03-08-----
194 //EED OJO avec JS       _zz = z;
195         //------------------
196         double result;
197         void *p;
198         p = imagedata->GetScalarPointer(x,y,z);
199
200         if (imagedata->GetScalarType()==VTK_CHAR)
201         {
202                 char *pp = (char*)p;
203                 result = (double)(*pp);
204         }
205         else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
206         {
207                 signed char *pp = (signed char*)p;
208                 result = (double)(*pp);
209         }
210         else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
211         {
212                 unsigned char *pp = (unsigned char*)p;
213                 result = (double)(*pp);
214         }
215         else if (imagedata->GetScalarType()==VTK_SHORT)
216         {
217                 short *pp = (short*)p;
218                 result = (double)(*pp);
219         }
220         else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
221         {
222                 unsigned short *pp = (unsigned short*)p;
223                 result = (double)(*pp);
224         }
225         else if (imagedata->GetScalarType()==VTK_INT)
226         {
227                 int *pp = (int*)p;
228                 result = (double)(*pp);
229         }
230         else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
231         {
232                 unsigned int *pp = (unsigned int*)p;
233                 result = (double)(*pp);
234         }
235         else if (imagedata->GetScalarType()==VTK_LONG)
236         {
237                 long *pp = (long*)p;
238                 result = (double)(*pp);
239         }
240         else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
241         {
242                 unsigned long *pp = (unsigned long*)p;
243                 result = (double)(*pp);
244         }
245         else if (imagedata->GetScalarType()==VTK_FLOAT)
246         {       
247                 float *pp = (float*)p;
248                 result = (double)(*pp);
249         }
250         else if (imagedata->GetScalarType()==VTK_DOUBLE)
251         {
252                 double *pp = (double*)p;
253                 result = (double)(*pp);
254         }
255
256         return result;
257 }
258
259 //------------------------------------------------------------------------
260
261 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
262 {
263         unsigned short *pValue;
264         unsigned short *pMask;
265         pValue  = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
266         pMask   = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
267         *pMask  = 255;
268         *pValue = (unsigned short)value;
269 }
270
271 //------------------------------------------------------------------------
272 void ContourExtractData::ResetImageResult(int z)
273 {
274         if (okImagesResults==true)
275         {
276                 unsigned short *pValue;
277                 unsigned short *pMask;
278                 pValue  = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
279                 pMask   = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
280                 
281                 int ext[6];
282                 imagedataValueResult->GetExtent(ext);
283
284                 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1); 
285                 for(i=0; i<size; i++)
286                 {
287                         *pMask  = 0;
288                         *pValue = 0;
289                         pMask++;
290                         pValue++;
291                 }// for
292         } // if
293 }
294
295
296 //------------------------------------------------------------------------
297 void ContourExtractData::CalculateImageResult()
298 {
299         if (okImagesResults==true)
300         {
301                 ResetImageResult(zImage);
302
303                 int minPoint[2];
304                 int maxPoint[2];
305                 int i,j;
306                 double value;
307
308                 minPoint[0] = 999999;
309                 minPoint[1] = 999999;
310                 maxPoint[0] = -999999;
311                 maxPoint[1] = -999999;
312
313                 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
314                 for (j=minPoint[1]; j<maxPoint[1]; j++)
315                 {
316                         for (i=minPoint[0]; i<maxPoint[0]; i++)
317                         {
318                                 if (isInside(i,j,_typeOperation)==true)
319                                 {
320                                         value = GetDataValue(i,j,zImage);
321                                         PutVtkImageDataResultValue(i,j,zImage,  value );
322                                 } // if
323                         } // for i
324                 } // for j
325
326
327                 imagedataValueResult->Modified();
328                 imagedataMaskResult->Modified();
329         }
330
331 }
332
333 //------------------------------------------------------------------------
334 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
335                                                                                                 std::vector<double> *pLstValuePosX,
336                                                                                                 std::vector<double> *pLstValuePosY,
337                                                                                                 std::vector<double> *pLstValuePosZ)
338 {
339         pLstValue->clear();
340         pLstValuePosX->clear();
341         pLstValuePosY->clear();
342         pLstValuePosZ->clear();
343
344 //      if (okImagesResults==true)
345 //      {
346 //              ResetImageResult(zImage);
347 //      }
348
349         int minPoint[2];
350         int maxPoint[2];
351         int i,j;
352         double value;
353
354
355         minPoint[0] = 999999;
356         minPoint[1] = 999999;
357         maxPoint[0] = -999999;
358         maxPoint[1] = -999999;
359
360         GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
361
362
363         for (j=minPoint[1]; j<maxPoint[1]; j++)
364         {
365                 for (i=minPoint[0]; i<maxPoint[0]; i++)
366                 {
367                         if (isInside(i,j,_typeOperation)==true)
368                         {
369                                 value = GetDataValue(i,j,zImage);
370
371 // Borrame
372 //                              if (okImagesResults==true){
373 //                                      PutVtkImageDataResultValue(i,j,zImage,  value );
374 //                              }
375
376                                 pLstValue               -> push_back( value );
377                                 pLstValuePosX   -> push_back( i );
378                                 pLstValuePosY   -> push_back( j );
379                                 pLstValuePosZ   -> push_back( -1 );
380                         } // if
381                 } // for
382         } // for
383
384
385 // Borrame
386 //      if (this->okImagesResults==true){
387 //              imagedataValueResult->Modified();
388 //              imagedataMaskResult->Modified();
389 //      }
390
391
392 }
393
394 //------------------------------------------------------------------------
395
396 vtkImageData *ContourExtractData::GetVtkImageValueResult()
397 {
398         return imagedataValueResult;
399 }
400 //------------------------------------------------------------------------
401 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
402 {
403         return imagedataMaskResult;
404 }
405 // ------------------------------------------------------------------------
406 void ContourExtractData::InitVtkImagesResult()
407 {
408           int ext[6];
409           int newDim[3];
410           double spc[3];
411           int scalartype;
412
413           imagedata->GetSpacing(spc);
414           imagedata->GetExtent(ext);
415           newDim[0]=ext[1]-ext[0]+1;
416           newDim[1]=ext[3]-ext[2]+1;
417           newDim[2]=ext[5]-ext[4]+1;
418           scalartype = imagedata->GetScalarType();
419
420           if (imagedataValueResult!=NULL)
421           {
422                   imagedataValueResult->Delete();
423           }
424           imagedataValueResult = vtkImageData::New();
425 //        imagedataValueResult->SetScalarType(scalartype);
426           imagedataValueResult->SetScalarTypeToUnsignedShort();
427           imagedataValueResult->SetSpacing(spc);
428           imagedataValueResult->SetDimensions( newDim );
429           imagedataValueResult->AllocateScalars();
430
431           if (imagedataMaskResult!=NULL)
432           {
433                   imagedataMaskResult->Delete();
434           }
435           imagedataMaskResult  = vtkImageData::New();
436 //        imagedataMaskResult->SetScalarType(scalartype);
437           imagedataMaskResult->SetScalarTypeToUnsignedShort();
438           imagedataMaskResult->SetSpacing(spc);
439           imagedataMaskResult->SetDimensions( newDim );
440           imagedataMaskResult->AllocateScalars();
441 }
442
443
444
445 //------------------------------------------------------------------------
446 void ContourExtractData::Statistics( std::vector<double> *inputLstValue, 
447                                                                         int     grayRangeMin,
448                                                                         int     grayRangeMax,
449                                                                         int             *rCountRange, 
450                                                                         int             *rsize, 
451                                                                         double  *rmin, 
452                                                                         double  *rmax,
453                                                                         double  *raverage,
454                                                                         double  *rstandardeviation)
455 {
456           double min                            = 0;
457           double max                            = 0;
458           double average                        = 0;
459           double standardeviation       = 0;
460           double acum                           = 0;
461           int    size                           = 0;
462           int    countRange                     = 0;
463           double ng;
464
465           if (inputLstValue!=NULL)
466           {
467                 size=inputLstValue->size();
468                 if (size>0){
469                max=(*inputLstValue)[0];
470                    min=(*inputLstValue)[0];
471      // Average , countRange
472                         int i;
473                         for ( i=0; i<size; i++ )
474                         {
475                                 ng=(*inputLstValue)[i];
476                                 acum = acum + ng;
477                                 if (max<ng) max=ng;             // Max
478                                 if (min>ng) min=ng;     // Min
479                                 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;  // countRange
480                         }
481                         average = acum / size;
482
483           // Standar Deviation
484                         acum=0;
485                         double tmp;
486                         for ( i=0; i<size; i++ )
487                         {
488                 tmp = (*inputLstValue)[i] - average;
489                                 acum = acum + tmp*tmp;
490                         }
491                         standardeviation = sqrt(acum/size);
492
493                 }
494           }
495
496           // OUTPUT
497                 *rsize                          = size; 
498                 *rCountRange            = countRange;
499                 *rmin                           = min; 
500                 *rmax                           = max;
501                 *raverage                       = average;
502                 *rstandardeviation      = standardeviation;
503 }
504
505 //------------------------------------------------------------------------
506 void ContourExtractData::SetTypeOperation(int type)
507 {
508         _typeOperation=type;
509 }