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 if (this->okImagesResults==true){
27 InitVtkImagesResult();
30 //----------------------------------------------------------------------
31 void ContourExtractData::SetZtoBeAnalys( int z )
36 //------------------------------------------------------------------------
37 void ContourExtractData::SetLstManualContourModel( std::vector<manualContourModel*> lstManConMod)
39 this->lstManConMod = lstManConMod;
43 //------------------------------------------------------------------------
44 void ContourExtractData::GetMinMaxPoint(int *minPoint,
46 manualContourModel *manualcontourmodel
50 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
52 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
53 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
54 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
55 //------------------------------------------------------------------------------------------------------
57 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
60 //double delta=( double ) ( np ) / ( double ) ( nps );
61 manualcontourmodel->UpdateSpline();
64 //t= delta * (double)i;
65 //manualcontourmodel->GetSplinePoint(t,x,y,z);
66 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
67 if (x<minPoint[0]){ minPoint[0]=(int)x; }
68 if (y<minPoint[1]){ minPoint[1]=(int)y; }
69 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
70 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
72 //------------------------------------------------------------------------------------------------------
75 //------------------------------------------------------------------------
76 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
81 int i,size = lstManConMod.size();
82 for(i=0 ; i<size ; i++)
84 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
87 //------------------------------------------------------------------------
89 int ContourExtractData::AnalisisContourInside(int x,
91 manualContourModel *manualcontourmodel
96 //int np = manualcontourmodel->GetSizeLstPoints( ); // number of control points // JPRx
98 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
99 //int nps = manualviewbaseecontour->GetNumberOfPointsSpline(); // number of points in the spline
100 int nps = manualcontourmodel->GetNumberOfPointsSpline(); // number of points in the spline
101 //double x1,y1,z1,x2,y2,z2,t;
102 double x1,y1,z1,x2,y2,z2;
103 double xx1, yy1,xx2, yy2;
104 //double delta=( double ) ( np ) / ( double ) ( nps );
105 manualcontourmodel->UpdateSpline();
106 //------------------------------------------------------------------------------------------------------
111 // JSTG 26-02-08 ---------------------------------------------------------------------------------------
113 //manualcontourmodel->GetSplinePoint(0,x1,y1,z1);
114 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
115 for (i=1; i<=nps; i++)
118 //t= delta * (double)(i%nps);
119 //manualcontourmodel->GetSplinePoint(t,x2,y2,z2);
120 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
121 //------------------------------------------------------------------------------------------------------
122 //by triangle similarity
123 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
125 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
126 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
127 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
135 //------------------------------------------------------------------------
138 bool ContourExtractData::isInside(int x, int y, int typeOperation)
142 int i,size = this->lstManConMod.size();
143 int numberInside = 0;
146 imagedata->GetExtent(ext);
148 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
151 if (typeOperation==0) // AND Intersection
155 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
156 if ( (numberLeft % 2) ==1){ numberInside++; }
158 if ( numberInside == (size) ){ result=true; }
159 } // AND Intersection
162 if (typeOperation==1) // OR All
166 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
167 if ( (numberLeft % 2) ==1){ result=true; }
171 if (typeOperation==2) // XOR crown
175 numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
177 if ( numberLeft % 2 ==1){ result = true; }
187 //------------------------------------------------------------------------
189 double ContourExtractData::GetDataValue(int x, int y, int z)
191 // wxVtk2DBaseView *wxvtk2dbaseview = (wxVtk2DBaseView*)wxvtkbaseview;
192 // int z = (int)wxvtk2dbaseview->GetVtkBaseData()->GetZ();
194 //EED OJO avec JS _zz = z;
198 p = imagedata->GetScalarPointer(x,y,z);
200 if (imagedata->GetScalarType()==VTK_CHAR)
203 result = (double)(*pp);
205 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
207 signed char *pp = (signed char*)p;
208 result = (double)(*pp);
210 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
212 unsigned char *pp = (unsigned char*)p;
213 result = (double)(*pp);
215 else if (imagedata->GetScalarType()==VTK_SHORT)
217 short *pp = (short*)p;
218 result = (double)(*pp);
220 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
222 unsigned short *pp = (unsigned short*)p;
223 result = (double)(*pp);
225 else if (imagedata->GetScalarType()==VTK_INT)
228 result = (double)(*pp);
230 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
232 unsigned int *pp = (unsigned int*)p;
233 result = (double)(*pp);
235 else if (imagedata->GetScalarType()==VTK_LONG)
238 result = (double)(*pp);
240 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
242 unsigned long *pp = (unsigned long*)p;
243 result = (double)(*pp);
245 else if (imagedata->GetScalarType()==VTK_FLOAT)
247 float *pp = (float*)p;
248 result = (double)(*pp);
250 else if (imagedata->GetScalarType()==VTK_DOUBLE)
252 double *pp = (double*)p;
253 result = (double)(*pp);
259 //------------------------------------------------------------------------
261 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
263 unsigned short *pValue;
264 unsigned short *pMask;
265 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
266 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
268 *pValue = (unsigned short)value;
271 //------------------------------------------------------------------------
272 void ContourExtractData::ResetImageResult(int z)
274 if (okImagesResults==true)
276 unsigned short *pValue;
277 unsigned short *pMask;
278 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
279 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
282 imagedataValueResult->GetExtent(ext);
284 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
285 for(i=0; i<size; i++)
296 //------------------------------------------------------------------------
297 void ContourExtractData::CalculateImageResult()
299 if (okImagesResults==true)
301 ResetImageResult(zImage);
308 minPoint[0] = 999999;
309 minPoint[1] = 999999;
310 maxPoint[0] = -999999;
311 maxPoint[1] = -999999;
313 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
314 for (j=minPoint[1]; j<maxPoint[1]; j++)
316 for (i=minPoint[0]; i<maxPoint[0]; i++)
318 if (isInside(i,j,_typeOperation)==true)
320 value = GetDataValue(i,j,zImage);
321 PutVtkImageDataResultValue(i,j,zImage, value );
327 imagedataValueResult->Modified();
328 imagedataMaskResult->Modified();
333 //------------------------------------------------------------------------
334 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
335 std::vector<double> *pLstValuePosX,
336 std::vector<double> *pLstValuePosY,
337 std::vector<double> *pLstValuePosZ)
340 pLstValuePosX->clear();
341 pLstValuePosY->clear();
342 pLstValuePosZ->clear();
344 // if (okImagesResults==true)
346 // ResetImageResult(zImage);
355 minPoint[0] = 999999;
356 minPoint[1] = 999999;
357 maxPoint[0] = -999999;
358 maxPoint[1] = -999999;
360 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
363 for (j=minPoint[1]; j<maxPoint[1]; j++)
365 for (i=minPoint[0]; i<maxPoint[0]; i++)
367 if (isInside(i,j,_typeOperation)==true)
369 value = GetDataValue(i,j,zImage);
372 // if (okImagesResults==true){
373 // PutVtkImageDataResultValue(i,j,zImage, value );
376 pLstValue -> push_back( value );
377 pLstValuePosX -> push_back( i );
378 pLstValuePosY -> push_back( j );
379 pLstValuePosZ -> push_back( -1 );
386 // if (this->okImagesResults==true){
387 // imagedataValueResult->Modified();
388 // imagedataMaskResult->Modified();
394 //------------------------------------------------------------------------
396 vtkImageData *ContourExtractData::GetVtkImageValueResult()
398 return imagedataValueResult;
400 //------------------------------------------------------------------------
401 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
403 return imagedataMaskResult;
405 // ------------------------------------------------------------------------
406 void ContourExtractData::InitVtkImagesResult()
413 imagedata->GetSpacing(spc);
414 imagedata->GetExtent(ext);
415 newDim[0]=ext[1]-ext[0]+1;
416 newDim[1]=ext[3]-ext[2]+1;
417 newDim[2]=ext[5]-ext[4]+1;
418 scalartype = imagedata->GetScalarType();
420 if (imagedataValueResult!=NULL)
422 imagedataValueResult->Delete();
424 imagedataValueResult = vtkImageData::New();
425 // imagedataValueResult->SetScalarType(scalartype);
426 imagedataValueResult->SetScalarTypeToUnsignedShort();
427 imagedataValueResult->SetSpacing(spc);
428 imagedataValueResult->SetDimensions( newDim );
429 imagedataValueResult->AllocateScalars();
431 if (imagedataMaskResult!=NULL)
433 imagedataMaskResult->Delete();
435 imagedataMaskResult = vtkImageData::New();
436 // imagedataMaskResult->SetScalarType(scalartype);
437 imagedataMaskResult->SetScalarTypeToUnsignedShort();
438 imagedataMaskResult->SetSpacing(spc);
439 imagedataMaskResult->SetDimensions( newDim );
440 imagedataMaskResult->AllocateScalars();
445 //------------------------------------------------------------------------
446 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
454 double *rstandardeviation)
459 double standardeviation = 0;
465 if (inputLstValue!=NULL)
467 size=inputLstValue->size();
469 max=(*inputLstValue)[0];
470 min=(*inputLstValue)[0];
471 // Average , countRange
473 for ( i=0; i<size; i++ )
475 ng=(*inputLstValue)[i];
477 if (max<ng) max=ng; // Max
478 if (min>ng) min=ng; // Min
479 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
481 average = acum / size;
486 for ( i=0; i<size; i++ )
488 tmp = (*inputLstValue)[i] - average;
489 acum = acum + tmp*tmp;
491 standardeviation = sqrt(acum/size);
498 *rCountRange = countRange;
502 *rstandardeviation = standardeviation;
505 //------------------------------------------------------------------------
506 void ContourExtractData::SetTypeOperation(int type)