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 //cout<<"RaC ContourExtractData::AnalisisContourInsideV2-iContour:"<<iContour<<" y:"<<y<<endl;
109 int nps=_lstlstlstVecX1[iContour][y].size();
112 double borderX, borderY;
113 double xx1, yy1,xx2, yy2;
117 for (i=0; i<nps; i++)
119 x1=_lstlstlstVecX1[iContour][y][i];
120 y1=_lstlstlstVecY1[iContour][y][i];
121 x2=_lstlstlstVecX2[iContour][y][i];
122 y2=_lstlstlstVecY2[iContour][y][i];
129 xx1=x1; yy1=y1; xx2=x2; yy2=y2;
131 xx1=x2; yy1=y2; xx2=x1; yy2=y1;
134 double difxx2xx1=fabs(xx2-xx1);
135 if (difxx2xx1==0) difxx2xx1=0.0000000001;
137 // Finding border looking in vertical direction AND verifing if pixel is at right of the line
138 if ( (yy>=yy1)&&(yy<=yy2) )
140 //by triangle similarity
141 d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
142 if ( (xx1<=xx2)&&(x<(xx1+d)) ) { result++; }
143 if ( (xx1>xx2)&&(x<(xx1-d)) ) { result++; }
145 if ( (yy2-yy1)/difxx2xx1 >= 1.0)
156 } // if point inside y
159 // Finding border looking in vertical direction
160 if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
162 if ( (yy2-yy1)/difxx2xx1 <= 1.0)
164 //by triangle similarity
165 d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
171 } // if point inside x
174 //Border verification
175 if ( (x==(int)borderX) && (y==(int)borderY) ) { inBorder=true; }// if point in border
177 // Verification : border in horizontal line
178 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2)) { inBorder=true; }
179 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1)) { inBorder=true; }
181 if (inBorder==true){ i=nps; }
184 if (inBorder==true) { result=1; }
191 //------------------------------------------------------------------------
192 // typeOperation=0 AND
193 // typeOperation=1 OR
194 // typeOperation=2 XOR
195 bool ContourExtractData::isInside(int x, int y, int typeOperation)
199 int i,size = this->lstManConMod.size();
200 int numberInside = 0;
202 /* RaC 20-11-09 (C1) Changes to use the method without the image.
204 imagedata->GetExtent(ext);
206 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
210 if (typeOperation==0) // AND Intersection
214 // To process the statistics of the Points contour the procedure is different
216 manualBaseModel *mbm = lstManConMod[i];
217 if(mbm->GetTypeModel()==7)
219 if(mbm->IsPoint(x,y)==true)
226 numberLeft = AnalisisContourInsideV2(x,y, i );
229 if ( (numberLeft % 2) ==1){ numberInside++; }
231 if ( numberInside == (size) ){ result=true; }
232 } // AND Intersection
236 if (typeOperation==1) // OR All
240 // To process the statistics of the Points contour the procedure is different
242 manualBaseModel *mbm = lstManConMod[i];
243 if(mbm->GetTypeModel()==7)
245 if(mbm->IsPoint(x,y)==true)
252 numberLeft = AnalisisContourInsideV2(x,y, i );
254 if ( (numberLeft % 2) ==1){ result=true; }
260 if (typeOperation==2) // XOR crown
264 // To process the statistics of the Points contour the procedure is different
266 manualBaseModel *mbm = lstManConMod[i];
267 if(mbm->GetTypeModel()==7)
269 if(mbm->IsPoint(x,y)==true)
276 numberLeft = numberLeft + AnalisisContourInsideV2(x,y, i );
280 if ( numberLeft % 2 ==1){ result = true; }
290 //------------------------------------------------------------------------
292 double ContourExtractData::GetDataValue(int x, int y, int z)
294 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
295 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
297 //EED OJO avec JS _zz = z;
301 p = imagedata->GetScalarPointer(x,y,z);
303 if (imagedata->GetScalarType()==VTK_CHAR)
306 result = (double)(*pp);
308 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
310 signed char *pp = (signed char*)p;
311 result = (double)(*pp);
313 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
315 unsigned char *pp = (unsigned char*)p;
316 result = (double)(*pp);
318 else if (imagedata->GetScalarType()==VTK_SHORT)
320 short *pp = (short*)p;
321 result = (double)(*pp);
323 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
325 unsigned short *pp = (unsigned short*)p;
326 result = (double)(*pp);
328 else if (imagedata->GetScalarType()==VTK_INT)
331 result = (double)(*pp);
333 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
335 unsigned int *pp = (unsigned int*)p;
336 result = (double)(*pp);
338 else if (imagedata->GetScalarType()==VTK_LONG)
341 result = (double)(*pp);
343 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
345 unsigned long *pp = (unsigned long*)p;
346 result = (double)(*pp);
348 else if (imagedata->GetScalarType()==VTK_FLOAT)
350 float *pp = (float*)p;
351 result = (double)(*pp);
353 else if (imagedata->GetScalarType()==VTK_DOUBLE)
355 double *pp = (double*)p;
356 result = (double)(*pp);
362 //------------------------------------------------------------------------
364 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
366 unsigned short *pValue;
367 unsigned short *pMask;
368 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
369 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
371 *pValue = (unsigned short)value;
374 //------------------------------------------------------------------------
375 void ContourExtractData::ResetImageResult(int z)
377 if (okImagesResults==true)
379 unsigned short *pValue;
380 unsigned short *pMask;
381 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
382 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
385 imagedataValueResult->GetExtent(ext);
387 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
388 memset(pValue,0,size*2);
389 memset(pMask,0,size*2);
394 //------------------------------------------------------------------------
395 void ContourExtractData::CalculateImageResult()
397 if (okImagesResults==true)
399 ResetImageResult(zImage);
406 minPoint[0] = 999999;
407 minPoint[1] = 999999;
408 maxPoint[0] = -999999;
409 maxPoint[1] = -999999;
411 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
412 InitLstContoursLinesYPoints();
414 for (j=minPoint[1]; j<=maxPoint[1]; j++)
416 for (i=minPoint[0]; i<=maxPoint[0]; i++)
419 //RaC 20-11-09 Changes specified in isInside (C1)
421 imagedata->GetExtent(ext);
423 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
425 if (isInside(i,j,_typeOperation)==true)
427 value = GetDataValue(i,j,zImage);
428 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
430 PutVtkImageDataResultValue(i,j,zImage, value );
439 imagedataValueResult->Modified();
440 imagedataMaskResult->Modified();
441 imagedataValueResult->Update();
442 imagedataMaskResult->Update();
447 //------------------------------------------------------------------------
448 void ContourExtractData::GetValuesInsideCrown( int *numberOfPixels,
449 std::vector<double> *pLstValue,
450 std::vector<double> *pLstValuePosX,
451 std::vector<double> *pLstValuePosY,
452 std::vector<double> *pLstValuePosZ)
455 pLstValuePosX->clear();
456 pLstValuePosY->clear();
457 pLstValuePosZ->clear();
459 // if (okImagesResults==true)
461 // ResetImageResult(zImage);
470 minPoint[0] = 999999;
471 minPoint[1] = 999999;
472 maxPoint[0] = -999999;
473 maxPoint[1] = -999999;
475 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
476 InitLstContoursLinesYPoints();
478 for (j=minPoint[1]; j<=maxPoint[1]; j++)
480 for (i=minPoint[0]; i<=maxPoint[0]; i++)
482 //RaC 20-11-09 Changes specified in isInside (C1)
484 imagedata->GetExtent(ext);
486 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
488 if (isInside(i,j,_typeOperation)==true)
492 value = GetDataValue(i,j,zImage);
493 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
495 pLstValue -> push_back( value );
496 pLstValuePosX -> push_back( i );
497 pLstValuePosY -> push_back( j );
498 pLstValuePosZ -> push_back( -1 );
505 *numberOfPixels = acum;
508 //------------------------------------------------------------------------
510 vtkImageData *ContourExtractData::GetVtkImageValueResult()
512 return imagedataValueResult;
514 //------------------------------------------------------------------------
515 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
517 return imagedataMaskResult;
519 // ------------------------------------------------------------------------
520 void ContourExtractData::InitVtkImagesResult()
527 imagedata->GetSpacing(spc);
528 imagedata->GetExtent(ext);
529 newDim[0]=ext[1]-ext[0]+1;
530 newDim[1]=ext[3]-ext[2]+1;
531 newDim[2]=ext[5]-ext[4]+1;
532 scalartype = imagedata->GetScalarType();
534 if (imagedataValueResult!=NULL)
536 imagedataValueResult->Delete();
538 imagedataValueResult = vtkImageData::New();
539 // imagedataValueResult->SetScalarType(scalartype);
540 imagedataValueResult->SetScalarTypeToUnsignedShort();
541 imagedataValueResult->SetSpacing(spc);
542 imagedataValueResult->SetDimensions( newDim );
543 imagedataValueResult->AllocateScalars();
545 if (imagedataMaskResult!=NULL)
547 imagedataMaskResult->Delete();
549 imagedataMaskResult = vtkImageData::New();
550 // imagedataMaskResult->SetScalarType(scalartype);
551 imagedataMaskResult->SetScalarTypeToUnsignedShort();
552 imagedataMaskResult->SetSpacing(spc);
553 imagedataMaskResult->SetDimensions( newDim );
554 imagedataMaskResult->AllocateScalars();
558 //------------------------------------------------------------------------
559 void ContourExtractData::InitVolumeStatistics()
563 vol_minValue = 9999999;
564 vol_maxValue =-9999999;
565 vol_acum_average = 0;
566 vol_acum_standardeviation = 0;
569 //------------------------------------------------------------------------
570 void ContourExtractData::SetVolumeStatistics(int rCountRange,
575 double acum_standardeviation)
577 vol_rCountRange = vol_rCountRange + rCountRange;
578 vol_rsize = vol_rsize + rsize;
580 if (minValue<vol_minValue){ vol_minValue = minValue; }
581 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
583 vol_acum_average = vol_acum_average + acum_average;
584 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
587 //------------------------------------------------------------------------
588 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
590 double *vol_minValue,
591 double *vol_maxValue,
593 double *vol_standardeviation)
595 *vol_rCountRange = this->vol_rCountRange;
596 *vol_rsize = this->vol_rsize;
597 *vol_minValue = this->vol_minValue;
598 *vol_maxValue = this->vol_maxValue;
599 *vol_average = this->vol_acum_average / this->vol_rsize;
600 *vol_standardeviation = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
604 //------------------------------------------------------------------------
605 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
613 double *rstandardeviation
619 double standardeviation = 0;
620 double acum_average = 0;
621 double acum_standardeviation = 0;
626 if (inputLstValue!=NULL)
628 size=inputLstValue->size();
630 max=(*inputLstValue)[0];
631 min=(*inputLstValue)[0];
632 // Average , countRange
634 for ( i=0; i<size; i++ )
636 ng=(*inputLstValue)[i];
637 acum_average = acum_average + ng;
638 if (max<ng) max=ng; // Max
639 if (min>ng) min=ng; // Min
640 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
642 average = acum_average / size;
645 acum_standardeviation=0;
647 for ( i=0; i<size; i++ )
649 tmp = (*inputLstValue)[i] - average;
650 acum_standardeviation = acum_standardeviation + tmp*tmp;
651 } // for standar deviation
652 standardeviation = sqrt(acum_standardeviation/size);
653 SetVolumeStatistics(countRange, (*rsize),
655 acum_average,acum_standardeviation);
661 *rCountRange = countRange;
665 *rstandardeviation = standardeviation;
668 //------------------------------------------------------------------------
669 void ContourExtractData::SetTypeOperation(int type)
674 //------------------------------------------------------------------------
675 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
678 double x1,y1,z1,x2,y2,z2;
679 manualBaseModel *manualcontourmodel= lstManConMod[iContour];
680 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
681 manualcontourmodel->UpdateSpline();
682 //------------------------------------------------------------------------------------------------------
684 for (y=0;y<sizeY;y++)
686 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
687 x1=x1+0.5; y1=y1+0.5;
688 for (i=1; i<nps; i++)
690 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
691 x2=x2+0.5; y2=y2+0.5;
692 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
694 _lstlstlstVecX1[iContour][y].push_back(x1);
695 _lstlstlstVecY1[iContour][y].push_back(y1);
696 _lstlstlstVecX2[iContour][y].push_back(x2);
697 _lstlstlstVecY2[iContour][y].push_back(y2);
700 } // for i Points in spline
705 void ContourExtractData::InitLstContoursLinesYPoints()
707 // init InInside Optimisation
710 _lstlstlstVecX1.clear();
711 _lstlstlstVecY1.clear();
712 _lstlstlstVecX2.clear();
713 _lstlstlstVecY2.clear();
717 this->imagedata->GetWholeExtent(ext);
718 int sizeY = ext[3]-ext[2]+1;
720 std::vector<double> vecDouble;
721 std::vector< std::vector<double> > vecVecDouble;
722 for ( i=0 ; i<_sizeImageY ; i++ )
724 vecVecDouble.push_back( vecDouble );
727 //Fill structure with points
728 int sizeContours = lstManConMod.size();
729 for( i=0 ; i<sizeContours ; i++ )
731 _lstlstlstVecX1.push_back( vecVecDouble );
732 _lstlstlstVecY1.push_back( vecVecDouble );
733 _lstlstlstVecX2.push_back( vecVecDouble );
734 _lstlstlstVecY2.push_back( vecVecDouble );
735 Fill_lstlstlstVecXY(i,_sizeImageY);
740 void ContourExtractData::SetScalarRange(double min, double max)
746 void ContourExtractData::SetSizeImageY(int pSizeImageY)
748 _sizeImageY=pSizeImageY;