00001
00002 #include "ContourExtractData.h"
00003
00004
00005
00006 ContourExtractData::ContourExtractData( bool okImagesResults)
00007 {
00008 this->imagedata = NULL;
00009 imagedataValueResult = NULL;
00010 imagedataMaskResult = NULL;
00011 this->okImagesResults = okImagesResults;
00012 _typeOperation = 0;
00013 }
00014
00015
00016
00017 ContourExtractData::~ContourExtractData()
00018 {
00019 }
00020
00021
00022
00023 void ContourExtractData::SetImage( vtkImageData* imagedata)
00024 {
00025 this->imagedata = imagedata;
00026 if (this->okImagesResults==true){
00027 InitVtkImagesResult();
00028 }
00029 }
00030
00031 void ContourExtractData::SetZtoBeAnalys( int z )
00032 {
00033 this->zImage = z;
00034 }
00035
00036
00037 void ContourExtractData::SetLstManualContourModel( std::vector<manualContourModel*> lstManConMod)
00038 {
00039 this->lstManConMod = lstManConMod;
00040 }
00041
00042
00043
00044 void ContourExtractData::GetMinMaxPoint(int *minPoint,
00045 int *maxPoint,
00046 manualContourModel *manualcontourmodel
00047 )
00048 {
00049 int i;
00050
00051
00052
00053
00054 int nps = manualcontourmodel->GetNumberOfPointsSpline();
00055
00056
00057
00058
00059 double x,y,z;
00060
00061 manualcontourmodel->UpdateSpline();
00062 for (i=0; i<nps; i++)
00063 {
00064
00065
00066 manualcontourmodel->GetSpline_i_Point(i,&x,&y,&z);
00067 if (x<minPoint[0]){ minPoint[0]=(int)x; }
00068 if (y<minPoint[1]){ minPoint[1]=(int)y; }
00069 if (x>maxPoint[0]){ maxPoint[0]=(int)x; }
00070 if (y>maxPoint[1]){ maxPoint[1]=(int)y; }
00071 }
00072
00073 }
00074
00075
00076 void ContourExtractData::GetMinMaxPoint_Of_LstManConMod( int *minPoint,
00077 int *maxPoint
00078 )
00079
00080 {
00081 int i,size = lstManConMod.size();
00082 for(i=0 ; i<size ; i++)
00083 {
00084 GetMinMaxPoint(minPoint,maxPoint,lstManConMod[i]);
00085 }
00086 }
00087
00088
00089 int ContourExtractData::AnalisisContourInside(int x,
00090 int y,
00091 manualContourModel *manualcontourmodel
00092 )
00093 {
00094 int result = 0;
00095 int i;
00096
00097
00098
00099
00100 int nps = manualcontourmodel->GetNumberOfPointsSpline();
00101
00102 double x1,y1,z1,x2,y2,z2;
00103 double xx1, yy1,xx2, yy2;
00104
00105 manualcontourmodel->UpdateSpline();
00106
00107 double d;
00108 bool ok;
00109
00110
00111
00112 nps--;
00113
00114 manualcontourmodel->GetSpline_i_Point(0,&x1,&y1,&z1);
00115 for (i=1; i<=nps; i++)
00116 {
00117 ok=false;
00118
00119
00120 manualcontourmodel->GetSpline_i_Point(i,&x2,&y2,&z2);
00121
00122
00123 if ( ((y1<y2)&&(y>=y1)&&(y<y2)) || ((y1>y2)&&(y<=y1)&&(y>y2)) )
00124 {
00125 if (y1<y2) { xx1=x1; yy1=y1; xx2=x2; yy2=y2;} else { xx1=x2; yy1=y2; xx2=x1; yy2=y1; }
00126 d = ( fabs(xx2-xx1)*(y-yy1) ) / (yy2-yy1) ;
00127 if ( ((xx1<xx2)&&(x<(xx1+d))) || ((xx1>xx2)&&(x<(xx1-d))) ) { result++; }
00128 }
00129 x1=x2; y1=y2; z1=z2;
00130 }
00131
00132 return result;
00133 }
00134
00135
00136
00137
00138 bool ContourExtractData::isInside(int x, int y, int typeOperation)
00139 {
00140 bool result = false;
00141 int numberLeft = 0;
00142 int i,size = this->lstManConMod.size();
00143 int numberInside = 0;
00144
00145 int ext[6];
00146 imagedata->GetExtent(ext);
00147
00148 if ((x>=0) && (x<=ext[1]) && (y>=0) && (y<=ext[3]))
00149 {
00150
00151 if (typeOperation==0)
00152 {
00153 for (i=0;i<size;i++)
00154 {
00155 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
00156 if ( (numberLeft % 2) ==1){ numberInside++; }
00157 }
00158 if ( numberInside == (size) ){ result=true; }
00159 }
00160
00161
00162 if (typeOperation==1)
00163 {
00164 for (i=0;i<size;i++)
00165 {
00166 numberLeft = AnalisisContourInside(x,y, lstManConMod[i] );
00167 if ( (numberLeft % 2) ==1){ result=true; }
00168 }
00169 }
00170
00171 if (typeOperation==2)
00172 {
00173 for (i=0;i<size;i++)
00174 {
00175 numberLeft = numberLeft + AnalisisContourInside(x,y, lstManConMod[i] );
00176 }
00177 if ( numberLeft % 2 ==1){ result = true; }
00178 }
00179
00180
00181
00182 }
00183
00184 return result;
00185 }
00186
00187
00188
00189 double ContourExtractData::GetDataValue(int x, int y, int z)
00190 {
00191
00192
00193
00194
00195
00196 double result;
00197 void *p;
00198 p = imagedata->GetScalarPointer(x,y,z);
00199
00200 if (imagedata->GetScalarType()==VTK_CHAR)
00201 {
00202 char *pp = (char*)p;
00203 result = (double)(*pp);
00204 }
00205 else if (imagedata->GetScalarType()==VTK_SIGNED_CHAR)
00206 {
00207 signed char *pp = (signed char*)p;
00208 result = (double)(*pp);
00209 }
00210 else if (imagedata->GetScalarType()==VTK_UNSIGNED_CHAR)
00211 {
00212 unsigned char *pp = (unsigned char*)p;
00213 result = (double)(*pp);
00214 }
00215 else if (imagedata->GetScalarType()==VTK_SHORT)
00216 {
00217 short *pp = (short*)p;
00218 result = (double)(*pp);
00219 }
00220 else if (imagedata->GetScalarType()==VTK_UNSIGNED_SHORT)
00221 {
00222 unsigned short *pp = (unsigned short*)p;
00223 result = (double)(*pp);
00224 }
00225 else if (imagedata->GetScalarType()==VTK_INT)
00226 {
00227 int *pp = (int*)p;
00228 result = (double)(*pp);
00229 }
00230 else if (imagedata->GetScalarType()==VTK_UNSIGNED_INT)
00231 {
00232 unsigned int *pp = (unsigned int*)p;
00233 result = (double)(*pp);
00234 }
00235 else if (imagedata->GetScalarType()==VTK_LONG)
00236 {
00237 long *pp = (long*)p;
00238 result = (double)(*pp);
00239 }
00240 else if (imagedata->GetScalarType()==VTK_UNSIGNED_LONG)
00241 {
00242 unsigned long *pp = (unsigned long*)p;
00243 result = (double)(*pp);
00244 }
00245 else if (imagedata->GetScalarType()==VTK_FLOAT)
00246 {
00247 float *pp = (float*)p;
00248 result = (double)(*pp);
00249 }
00250 else if (imagedata->GetScalarType()==VTK_DOUBLE)
00251 {
00252 double *pp = (double*)p;
00253 result = (double)(*pp);
00254 }
00255
00256 return result;
00257 }
00258
00259
00260
00261 void ContourExtractData::PutVtkImageDataResultValue( int x, int y, int z, double value )
00262 {
00263 unsigned short *pValue;
00264 unsigned short *pMask;
00265 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(x,y,z);
00266 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(x,y,z);
00267 *pMask = 255;
00268 *pValue = (unsigned short)value;
00269 }
00270
00271
00272 void ContourExtractData::ResetImageResult(int z)
00273 {
00274 if (okImagesResults==true)
00275 {
00276 unsigned short *pValue;
00277 unsigned short *pMask;
00278 pValue = (unsigned short *)imagedataValueResult->GetScalarPointer(0,0,z);
00279 pMask = (unsigned short *)imagedataMaskResult->GetScalarPointer(0,0,z);
00280
00281 int ext[6];
00282 imagedataValueResult->GetExtent(ext);
00283
00284 int i,size = (ext[1]-ext[0]+1) * (ext[3]-ext[2]+1);
00285 for(i=0; i<size; i++)
00286 {
00287 *pMask = 0;
00288 *pValue = 0;
00289 pMask++;
00290 pValue++;
00291 }
00292 }
00293 }
00294
00295
00296
00297 void ContourExtractData::CalculateImageResult()
00298 {
00299 if (okImagesResults==true)
00300 {
00301 ResetImageResult(zImage);
00302
00303 int minPoint[2];
00304 int maxPoint[2];
00305 int i,j;
00306 double value;
00307
00308 minPoint[0] = 999999;
00309 minPoint[1] = 999999;
00310 maxPoint[0] = -999999;
00311 maxPoint[1] = -999999;
00312
00313 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
00314 for (j=minPoint[1]; j<maxPoint[1]; j++)
00315 {
00316 for (i=minPoint[0]; i<maxPoint[0]; i++)
00317 {
00318 if (isInside(i,j,_typeOperation)==true)
00319 {
00320 value = GetDataValue(i,j,zImage);
00321 PutVtkImageDataResultValue(i,j,zImage, value );
00322 }
00323 }
00324 }
00325
00326
00327 imagedataValueResult->Modified();
00328 imagedataMaskResult->Modified();
00329 }
00330
00331 }
00332
00333
00334 void ContourExtractData::GetValuesInsideCrown(std::vector<double> *pLstValue,
00335 std::vector<double> *pLstValuePosX,
00336 std::vector<double> *pLstValuePosY,
00337 std::vector<double> *pLstValuePosZ)
00338 {
00339 pLstValue->clear();
00340 pLstValuePosX->clear();
00341 pLstValuePosY->clear();
00342 pLstValuePosZ->clear();
00343
00344
00345
00346
00347
00348
00349 int minPoint[2];
00350 int maxPoint[2];
00351 int i,j;
00352 double value;
00353
00354
00355 minPoint[0] = 999999;
00356 minPoint[1] = 999999;
00357 maxPoint[0] = -999999;
00358 maxPoint[1] = -999999;
00359
00360 GetMinMaxPoint_Of_LstManConMod(minPoint,maxPoint);
00361
00362
00363 for (j=minPoint[1]; j<maxPoint[1]; j++)
00364 {
00365 for (i=minPoint[0]; i<maxPoint[0]; i++)
00366 {
00367 if (isInside(i,j,_typeOperation)==true)
00368 {
00369 value = GetDataValue(i,j,zImage);
00370
00371
00372
00373
00374
00375
00376 pLstValue -> push_back( value );
00377 pLstValuePosX -> push_back( i );
00378 pLstValuePosY -> push_back( j );
00379 pLstValuePosZ -> push_back( -1 );
00380 }
00381 }
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 }
00393
00394
00395
00396 vtkImageData *ContourExtractData::GetVtkImageValueResult()
00397 {
00398 return imagedataValueResult;
00399 }
00400
00401 vtkImageData *ContourExtractData::GetVtkImageMaskResult()
00402 {
00403 return imagedataMaskResult;
00404 }
00405
00406 void ContourExtractData::InitVtkImagesResult()
00407 {
00408 int ext[6];
00409 int newDim[3];
00410 double spc[3];
00411 int scalartype;
00412
00413 imagedata->GetSpacing(spc);
00414 imagedata->GetExtent(ext);
00415 newDim[0]=ext[1]-ext[0]+1;
00416 newDim[1]=ext[3]-ext[2]+1;
00417 newDim[2]=ext[5]-ext[4]+1;
00418 scalartype = imagedata->GetScalarType();
00419
00420 if (imagedataValueResult!=NULL)
00421 {
00422 imagedataValueResult->Delete();
00423 }
00424 imagedataValueResult = vtkImageData::New();
00425
00426 imagedataValueResult->SetScalarTypeToUnsignedShort();
00427 imagedataValueResult->SetSpacing(spc);
00428 imagedataValueResult->SetDimensions( newDim );
00429 imagedataValueResult->AllocateScalars();
00430
00431 if (imagedataMaskResult!=NULL)
00432 {
00433 imagedataMaskResult->Delete();
00434 }
00435 imagedataMaskResult = vtkImageData::New();
00436
00437 imagedataMaskResult->SetScalarTypeToUnsignedShort();
00438 imagedataMaskResult->SetSpacing(spc);
00439 imagedataMaskResult->SetDimensions( newDim );
00440 imagedataMaskResult->AllocateScalars();
00441 }
00442
00443
00444
00445
00446 void ContourExtractData::Statistics( std::vector<double> *inputLstValue,
00447 int grayRangeMin,
00448 int grayRangeMax,
00449 int *rCountRange,
00450 int *rsize,
00451 double *rmin,
00452 double *rmax,
00453 double *raverage,
00454 double *rstandardeviation)
00455 {
00456 double min = 0;
00457 double max = 0;
00458 double average = 0;
00459 double standardeviation = 0;
00460 double acum = 0;
00461 int size = 0;
00462 int countRange = 0;
00463 double ng;
00464
00465 if (inputLstValue!=NULL)
00466 {
00467 size=inputLstValue->size();
00468 if (size>0){
00469 max=(*inputLstValue)[0];
00470 min=(*inputLstValue)[0];
00471
00472 int i;
00473 for ( i=0; i<size; i++ )
00474 {
00475 ng=(*inputLstValue)[i];
00476 acum = acum + ng;
00477 if (max<ng) max=ng;
00478 if (min>ng) min=ng;
00479 if ((ng>=grayRangeMin) && (ng<=grayRangeMax)) countRange++;
00480 }
00481 average = acum / size;
00482
00483
00484 acum=0;
00485 double tmp;
00486 for ( i=0; i<size; i++ )
00487 {
00488 tmp = (*inputLstValue)[i] - average;
00489 acum = acum + tmp*tmp;
00490 }
00491 standardeviation = sqrt(acum/size);
00492
00493 }
00494 }
00495
00496
00497 *rsize = size;
00498 *rCountRange = countRange;
00499 *rmin = min;
00500 *rmax = max;
00501 *raverage = average;
00502 *rstandardeviation = standardeviation;
00503 }
00504
00505
00506 void ContourExtractData::SetTypeOperation(int type)
00507 {
00508 _typeOperation=type;
00509 }