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 // RaC 20-11-09 Changes in InitLstContoursLinesYPoints
29 this->imagedata->GetWholeExtent(ext);
30 _sizeImageY = ext[3]-ext[2]+1;
32 // init vtk image result : valuesImage maskImage
33 if (this->okImagesResults==true){ InitVtkImagesResult(); }
35 //----------------------------------------------------------------------
36 void ContourExtractData::SetZtoBeAnalys( int z )
41 //------------------------------------------------------------------------
42 void ContourExtractData::SetLstManualContourModel( std::vector<manualBaseModel*> lstManConMod)
44 this->lstManConMod = lstManConMod;
48 //------------------------------------------------------------------------
49 void ContourExtractData::GetMinMaxPoint(int *minPoint,
51 manualBaseModel *manualcontourmodel
55 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
57 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
58 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
59 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
60 //------------------------------------------------------------------------------------------------------
62 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
65 //double delta=( double ) ( np ) / ( double ) ( nps );
66 manualcontourmodel->UpdateSpline();
69 //t= delta * (double)i;
70 //manualcontourmodel->GetSplinePoint(t,x,y,z);
71 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
72 if (x<minPoint[0]){ minPoint[0]=(int)x; }
73 if (y<minPoint[1]){ minPoint[1]=(int)y; }
74 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
75 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
81 //------------------------------------------------------------------------------------------------------
84 //------------------------------------------------------------------------
85 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
90 int i,size = lstManConMod.size();
92 for(i=0 ; i<size ; i++)
94 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
99 //------------------------------------------------------------------------
100 int ContourExtractData::AnalisisContourInsideV2(int x, int y, int iContour )
106 //cout<<"RaC ContourExtractData::AnalisisContourInsideV2-iContour:"<<iContour<<" y:"<<y<<endl;
108 int nps=_lstlstlstVecX1[iContour][y].size();
111 double borderX, borderY;
112 double xx1, yy1,xx2, yy2;
116 for (i=0; i<nps; i++)
118 x1=_lstlstlstVecX1[iContour][y][i];
119 y1=_lstlstlstVecY1[iContour][y][i];
120 x2=_lstlstlstVecX2[iContour][y][i];
121 y2=_lstlstlstVecY2[iContour][y][i];
128 xx1=x1; yy1=y1; xx2=x2; yy2=y2;
130 xx1=x2; yy1=y2; xx2=x1; yy2=y1;
133 double difxx2xx1=fabs(xx2-xx1);
134 if (difxx2xx1==0) difxx2xx1=0.0000000001;
136 // Finding border looking in vertical direction AND verifing if pixel is at right of the line
137 if ( (yy>=yy1)&&(yy<=yy2) )
139 //by triangle similarity
140 d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
141 if ( (xx1<=xx2)&&(x<(xx1+d)) ) { result++; }
142 if ( (xx1>xx2)&&(x<(xx1-d)) ) { result++; }
144 if ( (yy2-yy1)/difxx2xx1 >= 1.0)
155 } // if point inside y
158 // Finding border looking in vertical direction
159 if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
161 if ( (yy2-yy1)/difxx2xx1 <= 1.0)
163 //by triangle similarity
164 d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
170 } // if point inside x
173 //Border verification
174 if ( (x==(int)borderX) && (y==(int)borderY) ) { inBorder=true; }// if point in border
176 // Verification : border in horizontal line
177 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2)) { inBorder=true; }
178 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1)) { inBorder=true; }
180 if (inBorder==true){ i=nps; }
183 if (inBorder==true) { result=1; }
190 //------------------------------------------------------------------------
191 // typeOperation=0 AND
192 // typeOperation=1 OR
193 // typeOperation=2 XOR
194 bool ContourExtractData::isInside(int x, int y, int typeOperation)
198 int i,size = this->lstManConMod.size();
199 int numberInside = 0;
201 /* RaC 20-11-09 (C1) Changes to use the method without the image.
203 imagedata->GetExtent(ext);
205 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
209 if (typeOperation==0) // AND Intersection
213 // To process the statistics of the Points contour the procedure is different
215 manualBaseModel *mbm = lstManConMod[i];
216 if(mbm->GetTypeModel()==7)
218 if(mbm->IsPoint(x,y)==true)
225 numberLeft = AnalisisContourInsideV2(x,y, i );
228 if ( (numberLeft % 2) ==1){ numberInside++; }
230 if ( numberInside == (size) ){ result=true; }
231 } // AND Intersection
235 if (typeOperation==1) // OR All
239 // To process the statistics of the Points contour the procedure is different
241 manualBaseModel *mbm = lstManConMod[i];
242 if(mbm->GetTypeModel()==7)
244 if(mbm->IsPoint(x,y)==true)
251 numberLeft = AnalisisContourInsideV2(x,y, i );
253 if ( (numberLeft % 2) ==1){ result=true; }
259 if (typeOperation==2) // XOR crown
263 // To process the statistics of the Points contour the procedure is different
265 manualBaseModel *mbm = lstManConMod[i];
266 if(mbm->GetTypeModel()==7)
268 if(mbm->IsPoint(x,y)==true)
275 numberLeft = numberLeft + AnalisisContourInsideV2(x,y, i );
279 if ( numberLeft % 2 ==1){ result = true; }
289 //------------------------------------------------------------------------
291 double ContourExtractData::GetDataValue(int x, int y, int z)
293 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
294 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
296 //EED OJO avec JS _zz = z;
300 p = imagedata->GetScalarPointer(x,y,z);
302 if (imagedata->GetScalarType()==VTK_CHAR)
305 result = (double)(*pp);
307 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
309 signed char *pp = (signed char*)p;
310 result = (double)(*pp);
312 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
314 unsigned char *pp = (unsigned char*)p;
315 result = (double)(*pp);
317 else if (imagedata->GetScalarType()==VTK_SHORT)
319 short *pp = (short*)p;
320 result = (double)(*pp);
322 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
324 unsigned short *pp = (unsigned short*)p;
325 result = (double)(*pp);
327 else if (imagedata->GetScalarType()==VTK_INT)
330 result = (double)(*pp);
332 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
334 unsigned int *pp = (unsigned int*)p;
335 result = (double)(*pp);
337 else if (imagedata->GetScalarType()==VTK_LONG)
340 result = (double)(*pp);
342 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
344 unsigned long *pp = (unsigned long*)p;
345 result = (double)(*pp);
347 else if (imagedata->GetScalarType()==VTK_FLOAT)
349 float *pp = (float*)p;
350 result = (double)(*pp);
352 else if (imagedata->GetScalarType()==VTK_DOUBLE)
354 double *pp = (double*)p;
355 result = (double)(*pp);
361 //------------------------------------------------------------------------
363 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
365 unsigned short *pValue;
366 unsigned short *pMask;
367 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
368 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
370 *pValue = (unsigned short)value;
373 //------------------------------------------------------------------------
374 void ContourExtractData::ResetImageResult(int z)
376 if (okImagesResults==true)
378 unsigned short *pValue;
379 unsigned short *pMask;
380 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
381 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
384 imagedataValueResult->GetExtent(ext);
386 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
387 memset(pValue,0,size*2);
388 memset(pMask,0,size*2);
393 //------------------------------------------------------------------------
394 void ContourExtractData::CalculateImageResult()
396 if (okImagesResults==true)
398 ResetImageResult(zImage);
405 minPoint[0] = 999999;
406 minPoint[1] = 999999;
407 maxPoint[0] = -999999;
408 maxPoint[1] = -999999;
410 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
411 InitLstContoursLinesYPoints();
413 for (j=minPoint[1]; j<=maxPoint[1]; j++)
415 for (i=minPoint[0]; i<=maxPoint[0]; i++)
418 //RaC 20-11-09 Changes specified in isInside (C1)
420 imagedata->GetExtent(ext);
422 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
424 if (isInside(i,j,_typeOperation)==true)
426 value = GetDataValue(i,j,zImage);
427 PutVtkImageDataResultValue(i,j,zImage, value );
436 imagedataValueResult->Modified();
437 imagedataMaskResult->Modified();
438 imagedataValueResult->Update();
439 imagedataMaskResult->Update();
444 //------------------------------------------------------------------------
445 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
446 std::vector<double> *pLstValuePosX,
447 std::vector<double> *pLstValuePosY,
448 std::vector<double> *pLstValuePosZ)
451 pLstValuePosX->clear();
452 pLstValuePosY->clear();
453 pLstValuePosZ->clear();
455 // if (okImagesResults==true)
457 // ResetImageResult(zImage);
466 minPoint[0] = 999999;
467 minPoint[1] = 999999;
468 maxPoint[0] = -999999;
469 maxPoint[1] = -999999;
471 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
472 InitLstContoursLinesYPoints();
474 for (j=minPoint[1]; j<=maxPoint[1]; j++)
476 for (i=minPoint[0]; i<=maxPoint[0]; i++)
478 //RaC 20-11-09 Changes specified in isInside (C1)
480 imagedata->GetExtent(ext);
482 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
485 if (isInside(i,j,_typeOperation)==true)
487 value = GetDataValue(i,j,zImage);
490 // if (okImagesResults==true){
491 // PutVtkImageDataResultValue(i,j,zImage, value );
494 pLstValue -> push_back( value );
495 pLstValuePosX -> push_back( i );
496 pLstValuePosY -> push_back( j );
497 pLstValuePosZ -> push_back( -1 );
506 // if (this->okImagesResults==true){
507 // imagedataValueResult->Modified();
508 // imagedataMaskResult->Modified();
514 //------------------------------------------------------------------------
516 vtkImageData *ContourExtractData::GetVtkImageValueResult()
518 return imagedataValueResult;
520 //------------------------------------------------------------------------
521 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
523 return imagedataMaskResult;
525 // ------------------------------------------------------------------------
526 void ContourExtractData::InitVtkImagesResult()
533 imagedata->GetSpacing(spc);
534 imagedata->GetExtent(ext);
535 newDim[0]=ext[1]-ext[0]+1;
536 newDim[1]=ext[3]-ext[2]+1;
537 newDim[2]=ext[5]-ext[4]+1;
538 scalartype = imagedata->GetScalarType();
540 if (imagedataValueResult!=NULL)
542 imagedataValueResult->Delete();
544 imagedataValueResult = vtkImageData::New();
545 // imagedataValueResult->SetScalarType(scalartype);
546 imagedataValueResult->SetScalarTypeToUnsignedShort();
547 imagedataValueResult->SetSpacing(spc);
548 imagedataValueResult->SetDimensions( newDim );
549 imagedataValueResult->AllocateScalars();
551 if (imagedataMaskResult!=NULL)
553 imagedataMaskResult->Delete();
555 imagedataMaskResult = vtkImageData::New();
556 // imagedataMaskResult->SetScalarType(scalartype);
557 imagedataMaskResult->SetScalarTypeToUnsignedShort();
558 imagedataMaskResult->SetSpacing(spc);
559 imagedataMaskResult->SetDimensions( newDim );
560 imagedataMaskResult->AllocateScalars();
564 //------------------------------------------------------------------------
565 void ContourExtractData::InitVolumeStatistics()
569 vol_minValue = 9999999;
570 vol_maxValue =-9999999;
571 vol_acum_average = 0;
572 vol_acum_standardeviation = 0;
575 //------------------------------------------------------------------------
576 void ContourExtractData::SetVolumeStatistics(int rCountRange,
581 double acum_standardeviation)
583 vol_rCountRange = vol_rCountRange + rCountRange;
584 vol_rsize = vol_rsize + rsize;
586 if (minValue<vol_minValue){ vol_minValue = minValue; }
587 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
589 vol_acum_average = vol_acum_average + acum_average;
590 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
593 //------------------------------------------------------------------------
594 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
596 double *vol_minValue,
597 double *vol_maxValue,
599 double *vol_standardeviation)
601 *vol_rCountRange = this->vol_rCountRange;
602 *vol_rsize = this->vol_rsize;
603 *vol_minValue = this->vol_minValue;
604 *vol_maxValue = this->vol_maxValue;
605 *vol_average = this->vol_acum_average / this->vol_rsize;
606 *vol_standardeviation = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
610 //------------------------------------------------------------------------
611 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
619 double *rstandardeviation
625 double standardeviation = 0;
626 double acum_average = 0;
627 double acum_standardeviation = 0;
632 if (inputLstValue!=NULL)
634 size=inputLstValue->size();
636 max=(*inputLstValue)[0];
637 min=(*inputLstValue)[0];
638 // Average , countRange
640 for ( i=0; i<size; i++ )
642 ng=(*inputLstValue)[i];
643 acum_average = acum_average + ng;
644 if (max<ng) max=ng; // Max
645 if (min>ng) min=ng; // Min
646 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
648 average = acum_average / size;
651 acum_standardeviation=0;
653 for ( i=0; i<size; i++ )
655 tmp = (*inputLstValue)[i] - average;
656 acum_standardeviation = acum_standardeviation + tmp*tmp;
657 } // for standar deviation
658 standardeviation = sqrt(acum_standardeviation/size);
659 SetVolumeStatistics(countRange, size,
661 acum_average,acum_standardeviation);
667 *rCountRange = countRange;
671 *rstandardeviation = standardeviation;
674 //------------------------------------------------------------------------
675 void ContourExtractData::SetTypeOperation(int type)
680 //------------------------------------------------------------------------
681 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
684 double x1,y1,z1,x2,y2,z2;
685 manualBaseModel *manualcontourmodel= lstManConMod[iContour];
686 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
687 manualcontourmodel->UpdateSpline();
688 //------------------------------------------------------------------------------------------------------
690 for (y=0;y<sizeY;y++)
692 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
693 x1=x1+0.5; y1=y1+0.5;
694 for (i=1; i<nps; i++)
696 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
697 x2=x2+0.5; y2=y2+0.5;
698 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
700 _lstlstlstVecX1[iContour][y].push_back(x1);
701 _lstlstlstVecY1[iContour][y].push_back(y1);
702 _lstlstlstVecX2[iContour][y].push_back(x2);
703 _lstlstlstVecY2[iContour][y].push_back(y2);
706 } // for i Points in spline
711 void ContourExtractData::InitLstContoursLinesYPoints()
713 // init InInside Optimisation
716 _lstlstlstVecX1.clear();
717 _lstlstlstVecY1.clear();
718 _lstlstlstVecX2.clear();
719 _lstlstlstVecY2.clear();
723 this->imagedata->GetWholeExtent(ext);
724 int sizeY = ext[3]-ext[2]+1;
726 std::vector<double> vecDouble;
727 std::vector< std::vector<double> > vecVecDouble;
728 for ( i=0 ; i<_sizeImageY ; i++ )
730 vecVecDouble.push_back( vecDouble );
733 //Fill structure with points
734 int sizeContours = lstManConMod.size();
735 for( i=0 ; i<sizeContours ; i++ )
737 _lstlstlstVecX1.push_back( vecVecDouble );
738 _lstlstlstVecY1.push_back( vecVecDouble );
739 _lstlstlstVecX2.push_back( vecVecDouble );
740 _lstlstlstVecY2.push_back( vecVecDouble );
741 Fill_lstlstlstVecXY(i,_sizeImageY);
746 void ContourExtractData::SetSizeImageY(int pSizeImageY)
748 _sizeImageY=pSizeImageY;