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;
27 // init vtk image result : valuesImage maskImage
28 if (this->okImagesResults==true){ 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]);
93 //------------------------------------------------------------------------
94 int ContourExtractData::AnalisisContourInsideV2(int x, int y, int iContour )
100 int nps=_lstlstlstVecX1[iContour][y].size();
103 double borderX, borderY;
104 double xx1, yy1,xx2, yy2;
108 for (i=0; i<nps; i++)
110 x1=_lstlstlstVecX1[iContour][y][i];
111 y1=_lstlstlstVecY1[iContour][y][i];
112 x2=_lstlstlstVecX2[iContour][y][i];
113 y2=_lstlstlstVecY2[iContour][y][i];
120 xx1=x1; yy1=y1; xx2=x2; yy2=y2;
122 xx1=x2; yy1=y2; xx2=x1; yy2=y1;
125 double difxx2xx1=fabs(xx2-xx1);
126 if (difxx2xx1==0) difxx2xx1=0.0000000001;
128 // Finding border looking in vertical direction AND verifing if pixel is at right of the line
129 if ( (yy>=yy1)&&(yy<=yy2) )
131 //by triangle similarity
132 d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
133 if ( (xx1<=xx2)&&(x<(xx1+d)) ) { result++; }
134 if ( (xx1>xx2)&&(x<(xx1-d)) ) { result++; }
136 if ( (yy2-yy1)/difxx2xx1 >= 1.0)
147 } // if point inside y
150 // Finding border looking in vertical direction
151 if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
153 if ( (yy2-yy1)/difxx2xx1 <= 1.0)
155 //by triangle similarity
156 d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
162 } // if point inside x
165 //Border verification
166 if ( (x==(int)borderX) && (y==(int)borderY) ) { inBorder=true; }// if point in border
168 // Verification : border in horizontal line
169 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2)) { inBorder=true; }
170 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1)) { inBorder=true; }
172 if (inBorder==true){ i=nps; }
175 if (inBorder==true) { result=1; }
182 //------------------------------------------------------------------------
183 bool ContourExtractData::isInside(int x, int y, int typeOperation)
187 int i,size = this->lstManConMod.size();
188 int numberInside = 0;
191 imagedata->GetExtent(ext);
193 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
196 if (typeOperation==0) // AND Intersection
200 numberLeft = AnalisisContourInsideV2(x,y, i );
201 if ( (numberLeft % 2) ==1){ numberInside++; }
203 if ( numberInside == (size) ){ result=true; }
204 } // AND Intersection
207 if (typeOperation==1) // OR All
211 numberLeft = AnalisisContourInsideV2(x,y, i );
212 if ( (numberLeft % 2) ==1){ result=true; }
216 if (typeOperation==2) // XOR crown
220 numberLeft = numberLeft + AnalisisContourInsideV2(x,y, i );
222 if ( numberLeft % 2 ==1){ result = true; }
232 //------------------------------------------------------------------------
234 double ContourExtractData::GetDataValue(int x, int y, int z)
236 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
237 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
239 //EED OJO avec JS _zz = z;
243 p = imagedata->GetScalarPointer(x,y,z);
245 if (imagedata->GetScalarType()==VTK_CHAR)
248 result = (double)(*pp);
250 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
252 signed char *pp = (signed char*)p;
253 result = (double)(*pp);
255 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
257 unsigned char *pp = (unsigned char*)p;
258 result = (double)(*pp);
260 else if (imagedata->GetScalarType()==VTK_SHORT)
262 short *pp = (short*)p;
263 result = (double)(*pp);
265 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
267 unsigned short *pp = (unsigned short*)p;
268 result = (double)(*pp);
270 else if (imagedata->GetScalarType()==VTK_INT)
273 result = (double)(*pp);
275 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
277 unsigned int *pp = (unsigned int*)p;
278 result = (double)(*pp);
280 else if (imagedata->GetScalarType()==VTK_LONG)
283 result = (double)(*pp);
285 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
287 unsigned long *pp = (unsigned long*)p;
288 result = (double)(*pp);
290 else if (imagedata->GetScalarType()==VTK_FLOAT)
292 float *pp = (float*)p;
293 result = (double)(*pp);
295 else if (imagedata->GetScalarType()==VTK_DOUBLE)
297 double *pp = (double*)p;
298 result = (double)(*pp);
304 //------------------------------------------------------------------------
306 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
308 unsigned short *pValue;
309 unsigned short *pMask;
310 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
311 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
313 *pValue = (unsigned short)value;
316 //------------------------------------------------------------------------
317 void ContourExtractData::ResetImageResult(int z)
319 if (okImagesResults==true)
321 unsigned short *pValue;
322 unsigned short *pMask;
323 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
324 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
327 imagedataValueResult->GetExtent(ext);
329 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
330 memset(pValue,0,size*2);
331 memset(pMask,0,size*2);
336 //------------------------------------------------------------------------
337 void ContourExtractData::CalculateImageResult()
339 if (okImagesResults==true)
341 ResetImageResult(zImage);
348 minPoint[0] = 999999;
349 minPoint[1] = 999999;
350 maxPoint[0] = -999999;
351 maxPoint[1] = -999999;
353 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
354 InitLstContoursLinesYPoints();
356 for (j=minPoint[1]; j<=maxPoint[1]; j++)
358 for (i=minPoint[0]; i<=maxPoint[0]; i++)
360 if (isInside(i,j,_typeOperation)==true)
362 value = GetDataValue(i,j,zImage);
363 PutVtkImageDataResultValue(i,j,zImage, value );
369 imagedataValueResult->Modified();
370 imagedataMaskResult->Modified();
371 imagedataValueResult->Update();
372 imagedataMaskResult->Update();
377 //------------------------------------------------------------------------
378 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
379 std::vector<double> *pLstValuePosX,
380 std::vector<double> *pLstValuePosY,
381 std::vector<double> *pLstValuePosZ)
384 pLstValuePosX->clear();
385 pLstValuePosY->clear();
386 pLstValuePosZ->clear();
388 // if (okImagesResults==true)
390 // ResetImageResult(zImage);
399 minPoint[0] = 999999;
400 minPoint[1] = 999999;
401 maxPoint[0] = -999999;
402 maxPoint[1] = -999999;
404 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
405 InitLstContoursLinesYPoints();
407 for (j=minPoint[1]; j<=maxPoint[1]; j++)
409 for (i=minPoint[0]; i<=maxPoint[0]; i++)
411 if (isInside(i,j,_typeOperation)==true)
413 value = GetDataValue(i,j,zImage);
416 // if (okImagesResults==true){
417 // PutVtkImageDataResultValue(i,j,zImage, value );
420 pLstValue -> push_back( value );
421 pLstValuePosX -> push_back( i );
422 pLstValuePosY -> push_back( j );
423 pLstValuePosZ -> push_back( -1 );
430 // if (this->okImagesResults==true){
431 // imagedataValueResult->Modified();
432 // imagedataMaskResult->Modified();
438 //------------------------------------------------------------------------
440 vtkImageData *ContourExtractData::GetVtkImageValueResult()
442 return imagedataValueResult;
444 //------------------------------------------------------------------------
445 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
447 return imagedataMaskResult;
449 // ------------------------------------------------------------------------
450 void ContourExtractData::InitVtkImagesResult()
457 imagedata->GetSpacing(spc);
458 imagedata->GetExtent(ext);
459 newDim[0]=ext[1]-ext[0]+1;
460 newDim[1]=ext[3]-ext[2]+1;
461 newDim[2]=ext[5]-ext[4]+1;
462 scalartype = imagedata->GetScalarType();
464 if (imagedataValueResult!=NULL)
466 imagedataValueResult->Delete();
468 imagedataValueResult = vtkImageData::New();
469 // imagedataValueResult->SetScalarType(scalartype);
470 imagedataValueResult->SetScalarTypeToUnsignedShort();
471 imagedataValueResult->SetSpacing(spc);
472 imagedataValueResult->SetDimensions( newDim );
473 imagedataValueResult->AllocateScalars();
475 if (imagedataMaskResult!=NULL)
477 imagedataMaskResult->Delete();
479 imagedataMaskResult = vtkImageData::New();
480 // imagedataMaskResult->SetScalarType(scalartype);
481 imagedataMaskResult->SetScalarTypeToUnsignedShort();
482 imagedataMaskResult->SetSpacing(spc);
483 imagedataMaskResult->SetDimensions( newDim );
484 imagedataMaskResult->AllocateScalars();
488 //------------------------------------------------------------------------
489 void ContourExtractData::InitVolumeStatistics()
493 vol_minValue = 9999999;
494 vol_maxValue =-9999999;
495 vol_acum_average = 0;
496 vol_acum_standardeviation = 0;
499 //------------------------------------------------------------------------
500 void ContourExtractData::SetVolumeStatistics(int rCountRange,
505 double acum_standardeviation)
507 vol_rCountRange = vol_rCountRange + rCountRange;
508 vol_rsize = vol_rsize + rsize;
510 if (minValue<vol_minValue){ vol_minValue = minValue; }
511 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
513 vol_acum_average = vol_acum_average + acum_average;
514 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
517 //------------------------------------------------------------------------
518 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
520 double *vol_minValue,
521 double *vol_maxValue,
523 double *vol_standardeviation)
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);
534 //------------------------------------------------------------------------
535 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
543 double *rstandardeviation
549 double standardeviation = 0;
550 double acum_average = 0;
551 double acum_standardeviation = 0;
556 if (inputLstValue!=NULL)
558 size=inputLstValue->size();
560 max=(*inputLstValue)[0];
561 min=(*inputLstValue)[0];
562 // Average , countRange
564 for ( i=0; i<size; i++ )
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
572 average = acum_average / size;
575 acum_standardeviation=0;
577 for ( i=0; i<size; i++ )
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,
585 acum_average,acum_standardeviation);
591 *rCountRange = countRange;
595 *rstandardeviation = standardeviation;
598 //------------------------------------------------------------------------
599 void ContourExtractData::SetTypeOperation(int type)
604 //------------------------------------------------------------------------
605 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
608 double x1,y1,z1,x2,y2,z2;
609 manualContourModel *manualcontourmodel= lstManConMod[iContour];
610 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
611 manualcontourmodel->UpdateSpline();
612 //------------------------------------------------------------------------------------------------------
614 for (y=0;y<sizeY;y++)
616 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
617 x1=x1+0.5; y1=y1+0.5;
618 for (i=1; i<nps; i++)
620 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
621 x2=x2+0.5; y2=y2+0.5;
622 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
624 _lstlstlstVecX1[iContour][y].push_back(x1);
625 _lstlstlstVecY1[iContour][y].push_back(y1);
626 _lstlstlstVecX2[iContour][y].push_back(x2);
627 _lstlstlstVecY2[iContour][y].push_back(y2);
630 } // for i Points in spline
635 void ContourExtractData::InitLstContoursLinesYPoints()
637 // init InInside Optimisation
640 _lstlstlstVecX1.clear();
641 _lstlstlstVecY1.clear();
642 _lstlstlstVecX2.clear();
643 _lstlstlstVecY2.clear();
646 this->imagedata->GetWholeExtent(ext);
647 int sizeY = ext[3]-ext[2]+1;
648 std::vector<double> vecDouble;
649 std::vector< std::vector<double> > vecVecDouble;
650 for ( i=0 ; i<sizeY ; i++ )
652 vecVecDouble.push_back( vecDouble );
655 //Fill structure with points
656 int sizeContours = lstManConMod.size();
657 for( i=0 ; i<sizeContours ; i++ )
659 _lstlstlstVecX1.push_back( vecVecDouble );
660 _lstlstlstVecY1.push_back( vecVecDouble );
661 _lstlstlstVecX2.push_back( vecVecDouble );
662 _lstlstlstVecY2.push_back( vecVecDouble );
663 Fill_lstlstlstVecXY(i,sizeY);