2 #include "ContourExtractData.h"
5 //----------------------------------------------------------------------
6 ContourExtractData::ContourExtractData( bool okImagesResults)
8 this->imagedata = NULL;
9 imagedataValueResult = NULL;
10 imagedataMaskResult = NULL;
11 this->okImagesResults = okImagesResults;
15 // ------------------------------------------------------------------------
17 ContourExtractData::~ContourExtractData()
22 //----------------------------------------------------------------------
23 void ContourExtractData::SetImage( vtkImageData* imagedata)
25 this->imagedata = imagedata;
26 if (this->okImagesResults==true){
27 InitVtkImagesResult();
30 //----------------------------------------------------------------------
31 void ContourExtractData::SetZtoBeAnalys( int z )
36 //------------------------------------------------------------------------
37 void ContourExtractData::SetLstManualContourModel( std::vector<manualContourModel*> lstManConMod)
39 this->lstManConMod = lstManConMod;
43 //------------------------------------------------------------------------
44 void ContourExtractData::GetMinMaxPoint(int *minPoint,
46 manualContourModel *manualcontourmodel
50 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
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 //------------------------------------------------------------------------------------------------------
57 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
60 //double delta=( double ) ( np ) / ( double ) ( nps );
61 manualcontourmodel->UpdateSpline();
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; }
76 //------------------------------------------------------------------------------------------------------
79 //------------------------------------------------------------------------
80 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
85 int i,size = lstManConMod.size();
86 for(i=0 ; i<size ; i++)
88 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
91 //------------------------------------------------------------------------
93 int ContourExtractData::AnalisisContourInside(int x,
95 manualContourModel *manualcontourmodel
101 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
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 //------------------------------------------------------------------------------------------------------
116 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
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++)
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 //------------------------------------------------------------------------------------------------------
133 //by triangle similarity
134 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
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)) )
144 if ( (xx1>xx2)&&(x<(xx1-d)) ) {
149 } // if point inside contour
153 if ( (x==(int)borderX) && (y==(int)borderY) )
157 }// if point in border
164 if (inBorder==true) { result=1; }
169 //------------------------------------------------------------------------
172 bool ContourExtractData::isInside(int x, int y, int typeOperation)
176 int i,size = this->lstManConMod.size();
177 int numberInside = 0;
180 imagedata->GetExtent(ext);
182 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
185 if (typeOperation==0) // AND Intersection
189 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
190 if ( (numberLeft % 2) ==1){ numberInside++; }
192 if ( numberInside == (size) ){ result=true; }
193 } // AND Intersection
196 if (typeOperation==1) // OR All
200 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
201 if ( (numberLeft % 2) ==1){ result=true; }
205 if (typeOperation==2) // XOR crown
209 numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
211 if ( numberLeft % 2 ==1){ result = true; }
221 //------------------------------------------------------------------------
223 double ContourExtractData::GetDataValue(int x, int y, int z)
225 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
226 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
228 //EED OJO avec JS _zz = z;
232 p = imagedata->GetScalarPointer(x,y,z);
234 if (imagedata->GetScalarType()==VTK_CHAR)
237 result = (double)(*pp);
239 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
241 signed char *pp = (signed char*)p;
242 result = (double)(*pp);
244 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
246 unsigned char *pp = (unsigned char*)p;
247 result = (double)(*pp);
249 else if (imagedata->GetScalarType()==VTK_SHORT)
251 short *pp = (short*)p;
252 result = (double)(*pp);
254 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
256 unsigned short *pp = (unsigned short*)p;
257 result = (double)(*pp);
259 else if (imagedata->GetScalarType()==VTK_INT)
262 result = (double)(*pp);
264 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
266 unsigned int *pp = (unsigned int*)p;
267 result = (double)(*pp);
269 else if (imagedata->GetScalarType()==VTK_LONG)
272 result = (double)(*pp);
274 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
276 unsigned long *pp = (unsigned long*)p;
277 result = (double)(*pp);
279 else if (imagedata->GetScalarType()==VTK_FLOAT)
281 float *pp = (float*)p;
282 result = (double)(*pp);
284 else if (imagedata->GetScalarType()==VTK_DOUBLE)
286 double *pp = (double*)p;
287 result = (double)(*pp);
293 //------------------------------------------------------------------------
295 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
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);
302 *pValue = (unsigned short)value;
305 //------------------------------------------------------------------------
306 void ContourExtractData::ResetImageResult(int z)
308 if (okImagesResults==true)
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);
316 imagedataValueResult->GetExtent(ext);
318 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
319 for(i=0; i<size; i++)
330 //------------------------------------------------------------------------
331 void ContourExtractData::CalculateImageResult()
333 if (okImagesResults==true)
335 ResetImageResult(zImage);
342 minPoint[0] = 999999;
343 minPoint[1] = 999999;
344 maxPoint[0] = -999999;
345 maxPoint[1] = -999999;
347 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
348 for (j=minPoint[1]; j<=maxPoint[1]; j++)
350 for (i=minPoint[0]; i<=maxPoint[0]; i++)
352 if (isInside(i,j,_typeOperation)==true)
354 value = GetDataValue(i,j,zImage);
355 PutVtkImageDataResultValue(i,j,zImage, value );
361 imagedataValueResult->Modified();
362 imagedataMaskResult->Modified();
367 //------------------------------------------------------------------------
368 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
369 std::vector<double> *pLstValuePosX,
370 std::vector<double> *pLstValuePosY,
371 std::vector<double> *pLstValuePosZ)
374 pLstValuePosX->clear();
375 pLstValuePosY->clear();
376 pLstValuePosZ->clear();
378 // if (okImagesResults==true)
380 // ResetImageResult(zImage);
389 minPoint[0] = 999999;
390 minPoint[1] = 999999;
391 maxPoint[0] = -999999;
392 maxPoint[1] = -999999;
394 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
397 for (j=minPoint[1]; j<=maxPoint[1]; j++)
399 for (i=minPoint[0]; i<=maxPoint[0]; i++)
401 if (isInside(i,j,_typeOperation)==true)
403 value = GetDataValue(i,j,zImage);
406 // if (okImagesResults==true){
407 // PutVtkImageDataResultValue(i,j,zImage, value );
410 pLstValue -> push_back( value );
411 pLstValuePosX -> push_back( i );
412 pLstValuePosY -> push_back( j );
413 pLstValuePosZ -> push_back( -1 );
420 // if (this->okImagesResults==true){
421 // imagedataValueResult->Modified();
422 // imagedataMaskResult->Modified();
428 //------------------------------------------------------------------------
430 vtkImageData *ContourExtractData::GetVtkImageValueResult()
432 return imagedataValueResult;
434 //------------------------------------------------------------------------
435 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
437 return imagedataMaskResult;
439 // ------------------------------------------------------------------------
440 void ContourExtractData::InitVtkImagesResult()
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();
454 if (imagedataValueResult!=NULL)
456 imagedataValueResult->Delete();
458 imagedataValueResult = vtkImageData::New();
459 // imagedataValueResult->SetScalarType(scalartype);
460 imagedataValueResult->SetScalarTypeToUnsignedShort();
461 imagedataValueResult->SetSpacing(spc);
462 imagedataValueResult->SetDimensions( newDim );
463 imagedataValueResult->AllocateScalars();
465 if (imagedataMaskResult!=NULL)
467 imagedataMaskResult->Delete();
469 imagedataMaskResult = vtkImageData::New();
470 // imagedataMaskResult->SetScalarType(scalartype);
471 imagedataMaskResult->SetScalarTypeToUnsignedShort();
472 imagedataMaskResult->SetSpacing(spc);
473 imagedataMaskResult->SetDimensions( newDim );
474 imagedataMaskResult->AllocateScalars();
478 //------------------------------------------------------------------------
479 void ContourExtractData::InitVolumeStatistics()
483 vol_minValue = 9999999;
484 vol_maxValue =-9999999;
485 vol_acum_average = 0;
486 vol_acum_standardeviation = 0;
489 //------------------------------------------------------------------------
490 void ContourExtractData::SetVolumeStatistics(int rCountRange,
495 double acum_standardeviation)
497 vol_rCountRange = vol_rCountRange + rCountRange;
498 vol_rsize = vol_rsize + rsize;
500 if (minValue<vol_minValue){ vol_minValue = minValue; }
501 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
503 vol_acum_average = vol_acum_average + acum_average;
504 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
507 //------------------------------------------------------------------------
508 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
510 double *vol_minValue,
511 double *vol_maxValue,
513 double *vol_standardeviation)
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);
524 //------------------------------------------------------------------------
525 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
533 double *rstandardeviation
539 double standardeviation = 0;
540 double acum_average = 0;
541 double acum_standardeviation = 0;
546 if (inputLstValue!=NULL)
548 size=inputLstValue->size();
550 max=(*inputLstValue)[0];
551 min=(*inputLstValue)[0];
552 // Average , countRange
554 for ( i=0; i<size; i++ )
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
562 average = acum_average / size;
565 acum_standardeviation=0;
567 for ( i=0; i<size; i++ )
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,
575 acum_average,acum_standardeviation);
581 *rCountRange = countRange;
585 *rstandardeviation = standardeviation;
588 //------------------------------------------------------------------------
589 void ContourExtractData::SetTypeOperation(int type)