1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 #include "ContourExtractData.h"
29 //----------------------------------------------------------------------
30 ContourExtractData::ContourExtractData( bool okImagesResults)
32 this->imagedata = NULL;
33 imagedataValueResult = NULL;
34 imagedataMaskResult = NULL;
35 this->okImagesResults = okImagesResults;
39 // ------------------------------------------------------------------------
41 ContourExtractData::~ContourExtractData()
46 //----------------------------------------------------------------------
47 void ContourExtractData::SetImage( vtkImageData* imagedata)
49 this->imagedata = imagedata;
50 this->imagedata->GetScalarRange(scalarRange);
52 // RaC 20-11-09 Changes in InitLstContoursLinesYPoints
54 this->imagedata->GetWholeExtent(ext);
55 _sizeImageY = ext[3]-ext[2]+1;
57 // init vtk image result : valuesImage maskImage
58 if (this->okImagesResults==true){ InitVtkImagesResult(); }
60 //----------------------------------------------------------------------
61 void ContourExtractData::SetZtoBeAnalys( int z )
66 //------------------------------------------------------------------------
67 void ContourExtractData::SetLstManualContourModel( std::vector<manualBaseModel*> lstManConMod)
69 this->lstManConMod = lstManConMod;
73 //------------------------------------------------------------------------
74 void ContourExtractData::GetMinMaxPoint(int *minPoint,
76 manualBaseModel *manualcontourmodel
80 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
82 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
83 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
84 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
85 //------------------------------------------------------------------------------------------------------
87 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
90 //double delta=( double ) ( np ) / ( double ) ( nps );
91 manualcontourmodel->UpdateSpline();
94 //t= delta * (double)i;
95 //manualcontourmodel->GetSplinePoint(t,x,y,z);
96 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
97 if (x<minPoint[0]){ minPoint[0]=(int)x; }
98 if (y<minPoint[1]){ minPoint[1]=(int)y; }
99 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
100 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
106 //------------------------------------------------------------------------------------------------------
109 //------------------------------------------------------------------------
110 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
115 int i,size = lstManConMod.size();
117 for(i=0 ; i<size ; i++)
119 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
124 //------------------------------------------------------------------------
125 int ContourExtractData::AnalisisContourInsideV2(int x, int y, int iContour )
131 int nps=_lstlstlstVecX1[iContour][y].size();
134 double borderX, borderY;
135 double xx1, yy1,xx2, yy2;
139 for (i=0; i<nps; i++)
141 x1=_lstlstlstVecX1[iContour][y][i];
142 y1=_lstlstlstVecY1[iContour][y][i];
143 x2=_lstlstlstVecX2[iContour][y][i];
144 y2=_lstlstlstVecY2[iContour][y][i];
151 xx1=x1; yy1=y1; xx2=x2; yy2=y2;
153 xx1=x2; yy1=y2; xx2=x1; yy2=y1;
156 double difxx2xx1=fabs(xx2-xx1);
157 if (difxx2xx1==0) difxx2xx1=0.0000000001;
159 // Finding border looking in vertical direction AND verifing if pixel is at right of the line
160 if ( (yy>=yy1)&&(yy<=yy2) )
162 //by triangle similarity
163 d = ( fabs(xx2-xx1)*(yy-yy1) ) / (yy2-yy1) ;
164 if ( (xx1<=xx2)&&(x<(xx1+d)) ) { result++; }
165 if ( (xx1>xx2)&&(x<(xx1-d)) ) { result++; }
167 if ( (yy2-yy1)/difxx2xx1 >= 1.0)
178 } // if point inside y
181 // Finding border looking in vertical direction
182 if ( ((xx1<=xx2)&&(xx>=xx1)&&(xx<xx2)) || ((xx1>xx2)&&(xx>=xx2)&&(xx<xx1)) )
184 if ( (yy2-yy1)/difxx2xx1 <= 1.0)
186 //by triangle similarity
187 d = ( fabs(xx1-xx)*(yy2-yy1) ) / difxx2xx1;
193 } // if point inside x
196 //Border verification
197 if ( (x==(int)borderX) && (y==(int)borderY) ) { inBorder=true; }// if point in border
199 // Verification : border in horizontal line
200 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x1<x2) && (x>=x1) && (x<=x2)) { inBorder=true; }
201 if ( ((int)y1==(int)y2) && ((int)y1==y) && (x2<x1) && (x>=x2) && (x<=x1)) { inBorder=true; }
203 if (inBorder==true){ i=nps; }
206 if (inBorder==true) { result=1; }
213 //------------------------------------------------------------------------
214 // typeOperation=0 AND
215 // typeOperation=1 OR
216 // typeOperation=2 XOR
217 bool ContourExtractData::isInside(int x, int y, int typeOperation)
221 int i,size = this->lstManConMod.size();
222 int numberInside = 0;
224 /* RaC 20-11-09 (C1) Changes to use the method without the image.
226 imagedata->GetExtent(ext);
228 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
232 if (typeOperation==0) // AND Intersection
236 // To process the statistics of the Points contour the procedure is different
238 manualBaseModel *mbm = lstManConMod[i];
239 if(mbm->GetTypeModel()==7)
241 if(mbm->IsPoint(x,y)==true)
248 numberLeft = AnalisisContourInsideV2(x,y, i );
251 if ( (numberLeft % 2) ==1){ numberInside++; }
253 if ( numberInside == (size) ){ result=true; }
254 } // AND Intersection
258 if (typeOperation==1) // OR All
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 = AnalisisContourInsideV2(x,y, i );
276 if ( (numberLeft % 2) ==1){ result=true; }
282 if (typeOperation==2) // XOR crown
286 // To process the statistics of the Points contour the procedure is different
288 manualBaseModel *mbm = lstManConMod[i];
289 if(mbm->GetTypeModel()==7)
291 if(mbm->IsPoint(x,y)==true)
298 numberLeft = numberLeft + AnalisisContourInsideV2(x,y, i );
302 if ( numberLeft % 2 ==1){ result = true; }
312 //------------------------------------------------------------------------
314 double ContourExtractData::GetDataValue(int x, int y, int z)
316 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
317 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
319 //EED OJO avec JS _zz = z;
323 p = imagedata->GetScalarPointer(x,y,z);
325 if (imagedata->GetScalarType()==VTK_CHAR)
328 result = (double)(*pp);
330 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
332 signed char *pp = (signed char*)p;
333 result = (double)(*pp);
335 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
337 unsigned char *pp = (unsigned char*)p;
338 result = (double)(*pp);
340 else if (imagedata->GetScalarType()==VTK_SHORT)
342 short *pp = (short*)p;
343 result = (double)(*pp);
345 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
347 unsigned short *pp = (unsigned short*)p;
348 result = (double)(*pp);
350 else if (imagedata->GetScalarType()==VTK_INT)
353 result = (double)(*pp);
355 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
357 unsigned int *pp = (unsigned int*)p;
358 result = (double)(*pp);
360 else if (imagedata->GetScalarType()==VTK_LONG)
363 result = (double)(*pp);
365 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
367 unsigned long *pp = (unsigned long*)p;
368 result = (double)(*pp);
370 else if (imagedata->GetScalarType()==VTK_FLOAT)
372 float *pp = (float*)p;
373 result = (double)(*pp);
375 else if (imagedata->GetScalarType()==VTK_DOUBLE)
377 double *pp = (double*)p;
378 result = (double)(*pp);
384 //------------------------------------------------------------------------
386 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
388 unsigned short *pValue;
389 unsigned short *pMask;
390 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
391 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
393 *pValue = (unsigned short)value;
396 //------------------------------------------------------------------------
397 void ContourExtractData::ResetImageResult(int z)
399 if (okImagesResults==true)
401 unsigned short *pValue;
402 unsigned short *pMask;
403 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
404 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
407 imagedataValueResult->GetExtent(ext);
409 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
410 memset(pValue,0,size*2);
411 memset(pMask,0,size*2);
416 //------------------------------------------------------------------------
417 void ContourExtractData::CalculateImageResult()
419 if (okImagesResults==true)
421 ResetImageResult(zImage);
428 minPoint[0] = 999999;
429 minPoint[1] = 999999;
430 maxPoint[0] = -999999;
431 maxPoint[1] = -999999;
433 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
434 InitLstContoursLinesYPoints();
436 for (j=minPoint[1]; j<=maxPoint[1]; j++)
438 for (i=minPoint[0]; i<=maxPoint[0]; i++)
441 //RaC 20-11-09 Changes specified in isInside (C1)
443 imagedata->GetExtent(ext);
445 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
447 if (isInside(i,j,_typeOperation)==true)
449 value = GetDataValue(i,j,zImage);
450 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
452 PutVtkImageDataResultValue(i,j,zImage, value );
461 imagedataValueResult->Modified();
462 imagedataMaskResult->Modified();
463 imagedataValueResult->Update();
464 imagedataMaskResult->Update();
469 //------------------------------------------------------------------------
470 void ContourExtractData::GetValuesInsideCrown( int *numberOfPixels,
471 std::vector<double> *pLstValue,
472 std::vector<double> *pLstValuePosX,
473 std::vector<double> *pLstValuePosY,
474 std::vector<double> *pLstValuePosZ)
477 pLstValuePosX->clear();
478 pLstValuePosY->clear();
479 pLstValuePosZ->clear();
481 // if (okImagesResults==true)
483 // ResetImageResult(zImage);
492 minPoint[0] = 999999;
493 minPoint[1] = 999999;
494 maxPoint[0] = -999999;
495 maxPoint[1] = -999999;
497 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
498 InitLstContoursLinesYPoints();
500 for (j=minPoint[1]; j<=maxPoint[1]; j++)
502 for (i=minPoint[0]; i<=maxPoint[0]; i++)
504 //RaC 20-11-09 Changes specified in isInside (C1)
506 imagedata->GetExtent(ext);
508 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
510 if (isInside(i,j,_typeOperation)==true)
514 value = GetDataValue(i,j,zImage);
515 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
517 pLstValue -> push_back( value );
518 pLstValuePosX -> push_back( i );
519 pLstValuePosY -> push_back( j );
520 pLstValuePosZ -> push_back( -1 );
527 *numberOfPixels = acum;
530 //------------------------------------------------------------------------
532 vtkImageData *ContourExtractData::GetVtkImageValueResult()
534 return imagedataValueResult;
536 //------------------------------------------------------------------------
537 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
539 return imagedataMaskResult;
541 // ------------------------------------------------------------------------
542 void ContourExtractData::InitVtkImagesResult()
549 imagedata->GetSpacing(spc);
550 imagedata->GetExtent(ext);
551 newDim[0]=ext[1]-ext[0]+1;
552 newDim[1]=ext[3]-ext[2]+1;
553 newDim[2]=ext[5]-ext[4]+1;
554 scalartype = imagedata->GetScalarType();
556 if (imagedataValueResult!=NULL)
558 imagedataValueResult->Delete();
560 imagedataValueResult = vtkImageData::New();
561 // imagedataValueResult->SetScalarType(scalartype);
562 imagedataValueResult->SetScalarTypeToUnsignedShort();
563 imagedataValueResult->SetSpacing(spc);
564 imagedataValueResult->SetDimensions( newDim );
565 imagedataValueResult->AllocateScalars();
567 if (imagedataMaskResult!=NULL)
569 imagedataMaskResult->Delete();
571 imagedataMaskResult = vtkImageData::New();
572 // imagedataMaskResult->SetScalarType(scalartype);
573 imagedataMaskResult->SetScalarTypeToUnsignedShort();
574 imagedataMaskResult->SetSpacing(spc);
575 imagedataMaskResult->SetDimensions( newDim );
576 imagedataMaskResult->AllocateScalars();
580 //------------------------------------------------------------------------
581 void ContourExtractData::InitVolumeStatistics()
585 vol_minValue = 9999999;
586 vol_maxValue =-9999999;
587 vol_acum_average = 0;
588 vol_acum_standardeviation = 0;
591 //------------------------------------------------------------------------
592 void ContourExtractData::SetVolumeStatistics(int rCountRange,
597 double acum_standardeviation)
599 vol_rCountRange = vol_rCountRange + rCountRange;
600 vol_rsize = vol_rsize + rsize;
602 if (minValue<vol_minValue){ vol_minValue = minValue; }
603 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
605 vol_acum_average = vol_acum_average + acum_average;
606 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
609 //------------------------------------------------------------------------
610 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
612 double *vol_minValue,
613 double *vol_maxValue,
615 double *vol_standardeviation)
617 *vol_rCountRange = this->vol_rCountRange;
618 *vol_rsize = this->vol_rsize;
619 *vol_minValue = this->vol_minValue;
620 *vol_maxValue = this->vol_maxValue;
621 *vol_average = this->vol_acum_average / this->vol_rsize;
622 *vol_standardeviation = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
626 //------------------------------------------------------------------------
627 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
635 double *rstandardeviation
641 double standardeviation = 0;
642 double acum_average = 0;
643 double acum_standardeviation = 0;
648 if (inputLstValue!=NULL)
650 size=inputLstValue->size();
652 max=(*inputLstValue)[0];
653 min=(*inputLstValue)[0];
654 // Average , countRange
656 for ( i=0; i<size; i++ )
658 ng=(*inputLstValue)[i];
659 acum_average = acum_average + ng;
660 if (max<ng) max=ng; // Max
661 if (min>ng) min=ng; // Min
662 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
664 average = acum_average / size;
667 acum_standardeviation=0;
669 for ( i=0; i<size; i++ )
671 tmp = (*inputLstValue)[i] - average;
672 acum_standardeviation = acum_standardeviation + tmp*tmp;
673 } // for standar deviation
674 standardeviation = sqrt(acum_standardeviation/size);
675 SetVolumeStatistics(countRange, (*rsize),
677 acum_average,acum_standardeviation);
683 *rCountRange = countRange;
687 *rstandardeviation = standardeviation;
690 //------------------------------------------------------------------------
691 void ContourExtractData::SetTypeOperation(int type)
696 //------------------------------------------------------------------------
697 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
700 double x1,y1,z1,x2,y2,z2;
701 manualBaseModel *manualcontourmodel= lstManConMod[iContour];
702 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
703 manualcontourmodel->UpdateSpline();
704 //------------------------------------------------------------------------------------------------------
706 for (y=0;y<sizeY;y++)
708 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
709 x1=x1+0.5; y1=y1+0.5;
710 for (i=1; i<nps; i++)
712 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
713 x2=x2+0.5; y2=y2+0.5;
714 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
716 _lstlstlstVecX1[iContour][y].push_back(x1);
717 _lstlstlstVecY1[iContour][y].push_back(y1);
718 _lstlstlstVecX2[iContour][y].push_back(x2);
719 _lstlstlstVecY2[iContour][y].push_back(y2);
722 } // for i Points in spline
727 void ContourExtractData::InitLstContoursLinesYPoints()
729 // init InInside Optimisation
732 _lstlstlstVecX1.clear();
733 _lstlstlstVecY1.clear();
734 _lstlstlstVecX2.clear();
735 _lstlstlstVecY2.clear();
739 this->imagedata->GetWholeExtent(ext);
740 int sizeY = ext[3]-ext[2]+1;
742 std::vector<double> vecDouble;
743 std::vector< std::vector<double> > vecVecDouble;
744 for ( i=0 ; i<_sizeImageY ; i++ )
746 vecVecDouble.push_back( vecDouble );
749 //Fill structure with points
750 int sizeContours = lstManConMod.size();
751 for( i=0 ; i<sizeContours ; i++ )
753 _lstlstlstVecX1.push_back( vecVecDouble );
754 _lstlstlstVecY1.push_back( vecVecDouble );
755 _lstlstlstVecX2.push_back( vecVecDouble );
756 _lstlstlstVecY2.push_back( vecVecDouble );
757 Fill_lstlstlstVecXY(i,_sizeImageY);
762 void ContourExtractData::SetScalarRange(double min, double max)
768 void ContourExtractData::SetSizeImageY(int pSizeImageY)
770 _sizeImageY=pSizeImageY;