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;
391 imagedataValueResult->SetScalarComponentFromDouble(x,y,z,0,value);
392 imagedataMaskResult->SetScalarComponentFromDouble(x,y,z,0,255);
393 // pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
394 // *pValue = (unsigned short)value;
395 // pMask = (unsigned char *)imagedataMaskResult->GetScalarPointer(x,y,z);
399 //------------------------------------------------------------------------
400 void ContourExtractData::ResetImageResult(int z)
402 if (okImagesResults==true)
404 unsigned short *pValue;
405 unsigned short *pMask;
406 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
407 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
410 imagedataValueResult->GetExtent(ext);
412 int size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
413 memset(pValue,0, size*imagedataValueResult->GetScalarSize() );
414 memset(pMask,0, size*imagedataMaskResult->GetScalarSize() );
419 //------------------------------------------------------------------------
420 void ContourExtractData::CalculateImageResult()
422 if (okImagesResults==true)
424 ResetImageResult(zImage);
431 minPoint[0] = 999999;
432 minPoint[1] = 999999;
433 maxPoint[0] = -999999;
434 maxPoint[1] = -999999;
436 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
437 InitLstContoursLinesYPoints();
439 for (j=minPoint[1]; j<=maxPoint[1]; j++)
441 for (i=minPoint[0]; i<=maxPoint[0]; i++)
444 //RaC 20-11-09 Changes specified in isInside (C1)
446 imagedata->GetExtent(ext);
448 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
450 if (isInside(i,j,_typeOperation)==true)
452 value = GetDataValue(i,j,zImage);
453 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
455 PutVtkImageDataResultValue(i,j,zImage, value );
464 imagedataValueResult->Modified();
465 imagedataMaskResult->Modified();
466 imagedataValueResult->Update();
467 imagedataMaskResult->Update();
472 //------------------------------------------------------------------------
473 void ContourExtractData::GetValuesInsideCrown( int *numberOfPixels,
474 std::vector<double> *pLstValue,
475 std::vector<double> *pLstValuePosX,
476 std::vector<double> *pLstValuePosY,
477 std::vector<double> *pLstValuePosZ)
480 pLstValuePosX->clear();
481 pLstValuePosY->clear();
482 pLstValuePosZ->clear();
484 // if (okImagesResults==true)
486 // ResetImageResult(zImage);
495 minPoint[0] = 999999;
496 minPoint[1] = 999999;
497 maxPoint[0] = -999999;
498 maxPoint[1] = -999999;
500 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
501 InitLstContoursLinesYPoints();
503 for (j=minPoint[1]; j<=maxPoint[1]; j++)
505 for (i=minPoint[0]; i<=maxPoint[0]; i++)
507 //RaC 20-11-09 Changes specified in isInside (C1)
509 imagedata->GetExtent(ext);
511 if ((i>=0) && (i<=ext[1]) && (j>=0) && (j<=ext[3]))
513 if (isInside(i,j,_typeOperation)==true)
517 value = GetDataValue(i,j,zImage);
518 if ( (value>=scalarRange[0]) && (value<=scalarRange[1]) )
520 pLstValue -> push_back( value );
521 pLstValuePosX -> push_back( i );
522 pLstValuePosY -> push_back( j );
523 pLstValuePosZ -> push_back( -1 );
530 *numberOfPixels = acum;
533 //------------------------------------------------------------------------
535 vtkImageData *ContourExtractData::GetVtkImageValueResult()
537 return imagedataValueResult;
539 //------------------------------------------------------------------------
540 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
542 return imagedataMaskResult;
544 // ------------------------------------------------------------------------
545 void ContourExtractData::InitVtkImagesResult()
552 imagedata->GetSpacing(spc);
553 imagedata->GetExtent(ext);
554 newDim[0]=ext[1]-ext[0]+1;
555 newDim[1]=ext[3]-ext[2]+1;
556 newDim[2]=ext[5]-ext[4]+1;
557 scalartype = imagedata->GetScalarType();
559 if (imagedataValueResult!=NULL)
561 imagedataValueResult->Delete();
563 imagedataValueResult = vtkImageData::New();
566 imagedataValueResult->SetScalarType(scalartype);
567 //imagedataValueResult->SetScalarTypeToUnsignedShort();
569 imagedataValueResult->SetSpacing(spc);
570 imagedataValueResult->SetDimensions( newDim );
571 imagedataValueResult->AllocateScalars();
573 if (imagedataMaskResult!=NULL)
575 imagedataMaskResult->Delete();
577 imagedataMaskResult = vtkImageData::New();
580 // imagedataMaskResult->SetScalarType(scalartype);
581 // imagedataMaskResult->SetScalarTypeToUnsignedShort();
582 imagedataMaskResult->SetScalarTypeToUnsignedChar();
584 imagedataMaskResult->SetSpacing(spc);
585 imagedataMaskResult->SetDimensions( newDim );
586 imagedataMaskResult->AllocateScalars();
590 //------------------------------------------------------------------------
591 void ContourExtractData::InitVolumeStatistics()
595 vol_minValue = 9999999;
596 vol_maxValue =-9999999;
597 vol_acum_average = 0;
598 vol_acum_standardeviation = 0;
601 //------------------------------------------------------------------------
602 void ContourExtractData::SetVolumeStatistics(int rCountRange,
607 double acum_standardeviation)
609 vol_rCountRange = vol_rCountRange + rCountRange;
610 vol_rsize = vol_rsize + rsize;
612 if (minValue<vol_minValue){ vol_minValue = minValue; }
613 if (maxValue>vol_maxValue){ vol_maxValue = maxValue; }
615 vol_acum_average = vol_acum_average + acum_average;
616 vol_acum_standardeviation = vol_acum_standardeviation + acum_standardeviation;
619 //------------------------------------------------------------------------
620 void ContourExtractData::GetVolumeStatistics(int *vol_rCountRange,
622 double *vol_minValue,
623 double *vol_maxValue,
625 double *vol_standardeviation)
627 *vol_rCountRange = this->vol_rCountRange;
628 *vol_rsize = this->vol_rsize;
629 *vol_minValue = this->vol_minValue;
630 *vol_maxValue = this->vol_maxValue;
631 *vol_average = this->vol_acum_average / this->vol_rsize;
632 *vol_standardeviation = sqrt(this->vol_acum_standardeviation / this->vol_rsize);
636 //------------------------------------------------------------------------
637 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
645 double *rstandardeviation
651 double standardeviation = 0;
652 double acum_average = 0;
653 double acum_standardeviation = 0;
658 if (inputLstValue!=NULL)
660 size=inputLstValue->size();
662 max=(*inputLstValue)[0];
663 min=(*inputLstValue)[0];
664 // Average , countRange
666 for ( i=0; i<size; i++ )
668 ng=(*inputLstValue)[i];
669 acum_average = acum_average + ng;
670 if (max<ng) max=ng; // Max
671 if (min>ng) min=ng; // Min
672 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
674 average = acum_average / size;
677 acum_standardeviation=0;
679 for ( i=0; i<size; i++ )
681 tmp = (*inputLstValue)[i] - average;
682 acum_standardeviation = acum_standardeviation + tmp*tmp;
683 } // for standar deviation
684 standardeviation = sqrt(acum_standardeviation/size);
685 SetVolumeStatistics(countRange, (*rsize),
687 acum_average,acum_standardeviation);
693 *rCountRange = countRange;
697 *rstandardeviation = standardeviation;
700 //------------------------------------------------------------------------
701 void ContourExtractData::SetTypeOperation(int type)
706 //------------------------------------------------------------------------
707 void ContourExtractData::Fill_lstlstlstVecXY(int iContour, int sizeY)
710 double x1,y1,z1,x2,y2,z2;
711 manualBaseModel *manualcontourmodel= lstManConMod[iContour];
712 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
713 manualcontourmodel->UpdateSpline();
714 //------------------------------------------------------------------------------------------------------
716 for (y=0;y<sizeY;y++)
718 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
719 x1=x1+0.5; y1=y1+0.5;
720 for (i=1; i<nps; i++)
722 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
723 x2=x2+0.5; y2=y2+0.5;
724 if ( ((y1<y2)&&(y>=y1)&&(y<=y2)) || ((y1>y2)&&(y>=y2)&&(y<=y1)) || ((int)y1==y) || ((int)y2==y) )
726 _lstlstlstVecX1[iContour][y].push_back(x1);
727 _lstlstlstVecY1[iContour][y].push_back(y1);
728 _lstlstlstVecX2[iContour][y].push_back(x2);
729 _lstlstlstVecY2[iContour][y].push_back(y2);
732 } // for i Points in spline
737 void ContourExtractData::InitLstContoursLinesYPoints()
739 // init InInside Optimisation
742 _lstlstlstVecX1.clear();
743 _lstlstlstVecY1.clear();
744 _lstlstlstVecX2.clear();
745 _lstlstlstVecY2.clear();
749 this->imagedata->GetWholeExtent(ext);
750 int sizeY = ext[3]-ext[2]+1;
752 std::vector<double> vecDouble;
753 std::vector< std::vector<double> > vecVecDouble;
754 for ( i=0 ; i<_sizeImageY ; i++ )
756 vecVecDouble.push_back( vecDouble );
759 //Fill structure with points
760 int sizeContours = lstManConMod.size();
761 for( i=0 ; i<sizeContours ; i++ )
763 _lstlstlstVecX1.push_back( vecVecDouble );
764 _lstlstlstVecY1.push_back( vecVecDouble );
765 _lstlstlstVecX2.push_back( vecVecDouble );
766 _lstlstlstVecY2.push_back( vecVecDouble );
767 Fill_lstlstlstVecXY(i,_sizeImageY);
772 void ContourExtractData::SetScalarRange(double min, double max)
778 void ContourExtractData::SetSizeImageY(int pSizeImageY)
780 _sizeImageY=pSizeImageY;