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_UNSIGNED_CHAR)
201 { unsigned char *pp = (unsigned char*)p;
202 result = (double)(*pp);
204 if (imagedata->GetScalarType()==VTK_FLOAT)
205 { float *pp = (float*)p;
206 result = (double)(*pp);
208 if (imagedata->GetScalarType()==VTK_SHORT)
209 { short *pp = (short*)p;
210 result = (double)(*pp);
212 if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
213 { unsigned short *pp = (unsigned short*)p;
214 result = (double)(*pp);
219 //------------------------------------------------------------------------
221 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
223 unsigned short *pValue;
224 unsigned short *pMask;
225 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
226 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
228 *pValue = (unsigned short)value;
231 //------------------------------------------------------------------------
232 void ContourExtractData::ResetImageResult(int z)
234 if (okImagesResults==true)
236 unsigned short *pValue;
237 unsigned short *pMask;
238 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
239 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
242 imagedataValueResult->GetExtent(ext);
244 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
245 for(i=0; i<size; i++)
256 //------------------------------------------------------------------------
257 void ContourExtractData::CalculateImageResult()
259 if (okImagesResults==true)
261 ResetImageResult(zImage);
268 minPoint[0] = 999999;
269 minPoint[1] = 999999;
270 maxPoint[0] = -999999;
271 maxPoint[1] = -999999;
273 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
274 for (j=minPoint[1]; j<maxPoint[1]; j++)
276 for (i=minPoint[0]; i<maxPoint[0]; i++)
278 if (isInside(i,j,_typeOperation)==true)
280 value = GetDataValue(i,j,zImage);
281 PutVtkImageDataResultValue(i,j,zImage, value );
287 imagedataValueResult->Modified();
288 imagedataMaskResult->Modified();
293 //------------------------------------------------------------------------
294 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
295 std::vector<double> *pLstValuePosX,
296 std::vector<double> *pLstValuePosY,
297 std::vector<double> *pLstValuePosZ)
300 pLstValuePosX->clear();
301 pLstValuePosY->clear();
302 pLstValuePosZ->clear();
304 // if (okImagesResults==true)
306 // ResetImageResult(zImage);
315 minPoint[0] = 999999;
316 minPoint[1] = 999999;
317 maxPoint[0] = -999999;
318 maxPoint[1] = -999999;
320 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
323 for (j=minPoint[1]; j<maxPoint[1]; j++)
325 for (i=minPoint[0]; i<maxPoint[0]; i++)
327 if (isInside(i,j,_typeOperation)==true)
329 value = GetDataValue(i,j,zImage);
332 // if (okImagesResults==true){
333 // PutVtkImageDataResultValue(i,j,zImage, value );
336 pLstValue -> push_back( value );
337 pLstValuePosX -> push_back( i );
338 pLstValuePosY -> push_back( j );
339 pLstValuePosZ -> push_back( -1 );
346 // if (this->okImagesResults==true){
347 // imagedataValueResult->Modified();
348 // imagedataMaskResult->Modified();
354 //------------------------------------------------------------------------
356 vtkImageData *ContourExtractData::GetVtkImageValueResult()
358 return imagedataValueResult;
360 //------------------------------------------------------------------------
361 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
363 return imagedataMaskResult;
365 // ------------------------------------------------------------------------
366 void ContourExtractData::InitVtkImagesResult()
373 imagedata->GetSpacing(spc);
374 imagedata->GetExtent(ext);
375 newDim[0]=ext[1]-ext[0]+1;
376 newDim[1]=ext[3]-ext[2]+1;
377 newDim[2]=ext[5]-ext[4]+1;
378 scalartype = imagedata->GetScalarType();
380 if (imagedataValueResult!=NULL)
382 imagedataValueResult->Delete();
384 imagedataValueResult = vtkImageData::New();
385 // imagedataValueResult->SetScalarType(scalartype);
386 imagedataValueResult->SetScalarTypeToUnsignedShort();
387 imagedataValueResult->SetSpacing(spc);
388 imagedataValueResult->SetDimensions( newDim );
389 imagedataValueResult->AllocateScalars();
391 if (imagedataMaskResult!=NULL)
393 imagedataMaskResult->Delete();
395 imagedataMaskResult = vtkImageData::New();
396 // imagedataMaskResult->SetScalarType(scalartype);
397 imagedataMaskResult->SetScalarTypeToUnsignedShort();
398 imagedataMaskResult->SetSpacing(spc);
399 imagedataMaskResult->SetDimensions( newDim );
400 imagedataMaskResult->AllocateScalars();
405 //------------------------------------------------------------------------
406 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
414 double *rstandardeviation)
419 double standardeviation = 0;
425 if (inputLstValue!=NULL)
427 size=inputLstValue->size();
429 max=(*inputLstValue)[0];
430 min=(*inputLstValue)[0];
431 // Average , countRange
433 for ( i=0; i<size; i++ )
435 ng=(*inputLstValue)[i];
437 if (max<ng) max=ng; // Max
438 if (min>ng) min=ng; // Min
439 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++; // countRange
441 average = acum / size;
446 for ( i=0; i<size; i++ )
448 tmp = (*inputLstValue)[i] - average;
449 acum = acum + tmp*tmp;
451 standardeviation = sqrt(acum/size);
458 *rCountRange = countRange;
462 *rstandardeviation = standardeviation;
465 //------------------------------------------------------------------------
466 void ContourExtractData::SetTypeOperation(int type)