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 this->imagedata->GetScalarRange(scalarRange);
28 // RaC 20-11-09 Changes in InitLstContoursLinesYPoints
30 this->imagedata->GetWholeExtent(ext);
31 _sizeImageY = ext[3]-ext[2]+1;
33 // init vtk image result : valuesImage maskImage
34 if (this->okImagesResults==true){ InitVtkImagesResult(); }
36 //----------------------------------------------------------------------
37 void ContourExtractData::SetZtoBeAnalys( int z )
42 //------------------------------------------------------------------------
43 void ContourExtractData::SetLstManualContourModel( std::vector<manualBaseModel*> lstManConMod)
45 this->lstManConMod = lstManConMod;
49 //------------------------------------------------------------------------
50 void ContourExtractData::GetMinMaxPoint(int *minPoint,
52 manualBaseModel *manualcontourmodel
56 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
58 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
59 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
60 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
61 //------------------------------------------------------------------------------------------------------
63 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
66 //double delta=( double ) ( np ) / ( double ) ( nps );
67 manualcontourmodel->UpdateSpline();
70 //t= delta * (double)i;
71 //manualcontourmodel->GetSplinePoint(t,x,y,z);
72 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
73 if (x<minPoint[0]){ minPoint[0]=(int)x; }
74 if (y<minPoint[1]){ minPoint[1]=(int)y; }
75 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
76 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
82 //------------------------------------------------------------------------------------------------------
85 //------------------------------------------------------------------------
86 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
91 int i,size = lstManConMod.size();
93 for(i=0 ; i<size ; i++)
95 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
100 //------------------------------------------------------------------------
101 int ContourExtractData::AnalisisContourInsideV2(int x, int y, int iContour )
107 int nps=_lstlstlstVecX1[iContour][y].size();
110 double borderX, borderY;
111 double xx1, yy1,xx2, yy2;
115 for (i=0; i<nps; i++)
117 x1=_lstlstlstVecX1[iContour][y][i];
118 y1=_lstlstlstVecY1[iContour][y][i];
119 x2=_lstlstlstVecX2[iContour][y][i];
120 y2=_lstlstlstVecY2[iContour][y][i];
127 xx1=x1; yy1=y1; xx2=x2; yy2=y2;
129 xx1=x2; yy1=y2; xx2=x1; yy2=y1;
132 double difxx2xx1=fabs(xx2-xx1);
133 if (difxx2xx1==0) difxx2xx1=0.0000000001;
135 // Finding border looking in vertical direction AND verifing if pixel is at right of the line
136 if ( (yy>=yy1)&&(yy<=yy2) )
138 //by triangle similarity
139 d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
140 if ( (xx1<=xx2)&&(x<(xx1+d)) ) { result++; }
141 if ( (xx1>xx2)&&(x<(xx1-d)) ) { result++; }
143 if ( (yy2-yy1)/difxx2xx1 >= 1.0)
154 } // if point inside y
157 // Finding border looking in vertical direction
158 if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
160 if ( (yy2-yy1)/difxx2xx1 <= 1.0)
162 //by triangle similarity
163 d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
169 } // if point inside x
172 //Border verification
173 if ( (x==(int)borderX) && (y==(int)borderY) ) { inBorder=true; }// if point in border
175 // Verification : border in horizontal line
176 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2)) { inBorder=true; }
177 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1)) { inBorder=true; }
179 if (inBorder==true){ i=nps; }
182 if (inBorder==true) { result=1; }
189 //------------------------------------------------------------------------
190 // typeOperation=0 AND
191 // typeOperation=1 OR
192 // typeOperation=2 XOR
193 bool ContourExtractData::isInside(int x, int y, int typeOperation)
197 int i,size = this->lstManConMod.size();
198 int numberInside = 0;
200 /* RaC 20-11-09 (C1) Changes to use the method without the image.
202 imagedata->GetExtent(ext);
204 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
208 if (typeOperation==0) // AND Intersection
212 // To process the statistics of the Points contour the procedure is different
214 manualBaseModel *mbm = lstManConMod[i];
215 if(mbm->GetTypeModel()==7)
217 if(mbm->IsPoint(x,y)==true)
224 numberLeft = AnalisisContourInsideV2(x,y, i );
227 if ( (numberLeft % 2) ==1){ numberInside++; }
229 if ( numberInside == (size) ){ result=true; }
230 } // AND Intersection
234 if (typeOperation==1) // OR All
238 // To process the statistics of the Points contour the procedure is different
240 manualBaseModel *mbm = lstManConMod[i];
241 if(mbm->GetTypeModel()==7)
243 if(mbm->IsPoint(x,y)==true)
250 numberLeft = AnalisisContourInsideV2(x,y, i );
252 if ( (numberLeft % 2) ==1){ result=true; }
258 if (typeOperation==2) // XOR crown
262 // To process the statistics of the Points contour the procedure is different
264 manualBaseModel *mbm = lstManConMod[i];
265 if(mbm->GetTypeModel()==7)
267 if(mbm->IsPoint(x,y)==true)
274 numberLeft = numberLeft + AnalisisContourInsideV2(x,y, i );
278 if ( numberLeft % 2 ==1){ result = true; }
288 //------------------------------------------------------------------------
290 double ContourExtractData::GetDataValue(int x, int y, int z)
292 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
293 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
295 //EED OJO avec JS _zz = z;
299 p = imagedata->GetScalarPointer(x,y,z);
301 if (imagedata->GetScalarType()==VTK_CHAR)
304 result = (double)(*pp);
306 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
308 signed char *pp = (signed char*)p;
309 result = (double)(*pp);
311 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
313 unsigned char *pp = (unsigned char*)p;
314 result = (double)(*pp);
316 else if (imagedata->GetScalarType()==VTK_SHORT)
318 short *pp = (short*)p;
319 result = (double)(*pp);
321 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
323 unsigned short *pp = (unsigned short*)p;
324 result = (double)(*pp);
326 else if (imagedata->GetScalarType()==VTK_INT)
329 result = (double)(*pp);
331 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
333 unsigned int *pp = (unsigned int*)p;
334 result = (double)(*pp);
336 else if (imagedata->GetScalarType()==VTK_LONG)
339 result = (double)(*pp);
341 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
343 unsigned long *pp = (unsigned long*)p;
344 result = (double)(*pp);
346 else if (imagedata->GetScalarType()==VTK_FLOAT)
348 float *pp = (float*)p;
349 result = (double)(*pp);
351 else if (imagedata->GetScalarType()==VTK_DOUBLE)
353 double *pp = (double*)p;
354 result = (double)(*pp);
360 //------------------------------------------------------------------------
362 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
364 unsigned short *pValue;
365 unsigned short *pMask;
366 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
367 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
369 *pValue = (unsigned short)value;
372 //------------------------------------------------------------------------
373 void ContourExtractData::ResetImageResult(int z)
375 if (okImagesResults==true)
377 unsigned short *pValue;
378 unsigned short *pMask;
379 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
380 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
383 imagedataValueResult->GetExtent(ext);
385 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
386 memset(pValue,0,size*2);
387 memset(pMask,0,size*2);
392 //------------------------------------------------------------------------
393 void ContourExtractData::CalculateImageResult()
395 if (okImagesResults==true)
397 ResetImageResult(zImage);
404 minPoint[0] = 999999;
405 minPoint[1] = 999999;
406 maxPoint[0] = -999999;
407 maxPoint[1] = -999999;
409 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
410 InitLstContoursLinesYPoints();
412 for (j=minPoint[1]; j<=maxPoint[1]; j++)
414 for (i=minPoint[0]; i<=maxPoint[0]; i++)
417 //RaC 20-11-09 Changes specified in isInside (C1)
419 imagedata->GetExtent(ext);
421 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
423 if (isInside(i,j,_typeOperation)==true)
425 value = GetDataValue(i,j,zImage);
426 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
428 PutVtkImageDataResultValue(i,j,zImage, value );
437 imagedataValueResult->Modified();
438 imagedataMaskResult->Modified();
439 imagedataValueResult->Update();
440 imagedataMaskResult->Update();
445 //------------------------------------------------------------------------
446 void ContourExtractData::GetValuesInsideCrown( int *numberOfPixels,
447 std::vector<double> *pLstValue,
448 std::vector<double> *pLstValuePosX,
449 std::vector<double> *pLstValuePosY,
450 std::vector<double> *pLstValuePosZ)
453 pLstValuePosX->clear();
454 pLstValuePosY->clear();
455 pLstValuePosZ->clear();
457 // if (okImagesResults==true)
459 // ResetImageResult(zImage);
468 minPoint[0] = 999999;
469 minPoint[1] = 999999;
470 maxPoint[0] = -999999;
471 maxPoint[1] = -999999;
473 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
474 InitLstContoursLinesYPoints();
476 for (j=minPoint[1]; j<=maxPoint[1]; j++)
478 for (i=minPoint[0]; i<=maxPoint[0]; i++)
480 //RaC 20-11-09 Changes specified in isInside (C1)
482 imagedata->GetExtent(ext);
484 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
486 if (isInside(i,j,_typeOperation)==true)
490 value = GetDataValue(i,j,zImage);
491 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
493 pLstValue -> push_back( value );
494 pLstValuePosX -> push_back( i );
495 pLstValuePosY -> push_back( j );
496 pLstValuePosZ -> push_back( -1 );
503 *numberOfPixels = acum;
506 //------------------------------------------------------------------------
508 vtkImageData *ContourExtractData::GetVtkImageValueResult()
510 return imagedataValueResult;
512 //------------------------------------------------------------------------
513 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
515 return imagedataMaskResult;
517 // ------------------------------------------------------------------------
518 void ContourExtractData::InitVtkImagesResult()
525 imagedata->GetSpacing(spc);
526 imagedata->GetExtent(ext);
527 newDim[0]=ext[1]-ext[0]+1;
528 newDim[1]=ext[3]-ext[2]+1;
529 newDim[2]=ext[5]-ext[4]+1;
530 scalartype = imagedata->GetScalarType();
532 if (imagedataValueResult!=NULL)
534 imagedataValueResult->Delete();
536 imagedataValueResult = vtkImageData::New();
537 // imagedataValueResult->SetScalarType(scalartype);
538 imagedataValueResult->SetScalarTypeToUnsignedShort();
539 imagedataValueResult->SetSpacing(spc);
540 imagedataValueResult->SetDimensions( newDim );
541 imagedataValueResult->AllocateScalars();
543 if (imagedataMaskResult!=NULL)
545 imagedataMaskResult->Delete();
547 imagedataMaskResult = vtkImageData::New();
548 // imagedataMaskResult->SetScalarType(scalartype);
549 imagedataMaskResult->SetScalarTypeToUnsignedShort();
550 imagedataMaskResult->SetSpacing(spc);
551 imagedataMaskResult->SetDimensions( newDim );
552 imagedataMaskResult->AllocateScalars();
556 //------------------------------------------------------------------------
557 void ContourExtractData::InitVolumeStatistics()
561 vol_minValue = 9999999;
562 vol_maxValue =-9999999;
563 vol_acum_average = 0;
564 vol_acum_standardeviation = 0;
567 //------------------------------------------------------------------------
568 void ContourExtractData::SetVolumeStatistics(int rCountRange,
573 double acum_standardeviation)
575 vol_rCountRange = vol_rCountRange + rCountRange;
576 vol_rsize = vol_rsize + rsize;
578 if (minValue<vol_minValue){ vol_minValue = minValue; }
579 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
581 vol_acum_average = vol_acum_average + acum_average;
582 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
585 //------------------------------------------------------------------------
586 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
588 double *vol_minValue,
589 double *vol_maxValue,
591 double *vol_standardeviation)
593 *vol_rCountRange = this->vol_rCountRange;
594 *vol_rsize = this->vol_rsize;
595 *vol_minValue = this->vol_minValue;
596 *vol_maxValue = this->vol_maxValue;
597 *vol_average = this->vol_acum_average / this->vol_rsize;
598 *vol_standardeviation = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
602 //------------------------------------------------------------------------
603 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
611 double *rstandardeviation
617 double standardeviation = 0;
618 double acum_average = 0;
619 double acum_standardeviation = 0;
624 if (inputLstValue!=NULL)
626 size=inputLstValue->size();
628 max=(*inputLstValue)[0];
629 min=(*inputLstValue)[0];
630 // Average , countRange
632 for ( i=0; i<size; i++ )
634 ng=(*inputLstValue)[i];
635 acum_average = acum_average + ng;
636 if (max<ng) max=ng; // Max
637 if (min>ng) min=ng; // Min
638 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
640 average = acum_average / size;
643 acum_standardeviation=0;
645 for ( i=0; i<size; i++ )
647 tmp = (*inputLstValue)[i] - average;
648 acum_standardeviation = acum_standardeviation + tmp*tmp;
649 } // for standar deviation
650 standardeviation = sqrt(acum_standardeviation/size);
651 SetVolumeStatistics(countRange, (*rsize),
653 acum_average,acum_standardeviation);
659 *rCountRange = countRange;
663 *rstandardeviation = standardeviation;
666 //------------------------------------------------------------------------
667 void ContourExtractData::SetTypeOperation(int type)
672 //------------------------------------------------------------------------
673 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
676 double x1,y1,z1,x2,y2,z2;
677 manualBaseModel *manualcontourmodel= lstManConMod[iContour];
678 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
679 manualcontourmodel->UpdateSpline();
680 //------------------------------------------------------------------------------------------------------
682 for (y=0;y<sizeY;y++)
684 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
685 x1=x1+0.5; y1=y1+0.5;
686 for (i=1; i<nps; i++)
688 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
689 x2=x2+0.5; y2=y2+0.5;
690 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
692 _lstlstlstVecX1[iContour][y].push_back(x1);
693 _lstlstlstVecY1[iContour][y].push_back(y1);
694 _lstlstlstVecX2[iContour][y].push_back(x2);
695 _lstlstlstVecY2[iContour][y].push_back(y2);
698 } // for i Points in spline
703 void ContourExtractData::InitLstContoursLinesYPoints()
705 // init InInside Optimisation
708 _lstlstlstVecX1.clear();
709 _lstlstlstVecY1.clear();
710 _lstlstlstVecX2.clear();
711 _lstlstlstVecY2.clear();
715 this->imagedata->GetWholeExtent(ext);
716 int sizeY = ext[3]-ext[2]+1;
718 std::vector<double> vecDouble;
719 std::vector< std::vector<double> > vecVecDouble;
720 for ( i=0 ; i<_sizeImageY ; i++ )
722 vecVecDouble.push_back( vecDouble );
725 //Fill structure with points
726 int sizeContours = lstManConMod.size();
727 for( i=0 ; i<sizeContours ; i++ )
729 _lstlstlstVecX1.push_back( vecVecDouble );
730 _lstlstlstVecY1.push_back( vecVecDouble );
731 _lstlstlstVecX2.push_back( vecVecDouble );
732 _lstlstlstVecY2.push_back( vecVecDouble );
733 Fill_lstlstlstVecXY(i,_sizeImageY);
738 void ContourExtractData::SetScalarRange(double min, double max)
744 void ContourExtractData::SetSizeImageY(int pSizeImageY)
746 _sizeImageY=pSizeImageY;