00001
00002
00003 #include "CutModelPolygon.h"
00004
00005
00006 CutModelPolygon::CutModelPolygon()
00007 {
00008 _cutInsideOutside=0;
00009 _inImage=NULL;
00010 _transform = vtkTransform::New();
00011 }
00012
00013 CutModelPolygon::~CutModelPolygon()
00014 {
00015 if(_transform!=NULL)
00016 {
00017 _transform->Delete();
00018 }
00019 if(_points!=NULL)
00020 {
00021 _points->Delete();
00022 }
00023 }
00024
00025 void CutModelPolygon::processOutImage(int cutInsideOutside)
00026 {
00027 _cutInsideOutside=cutInsideOutside;
00028
00029 int numPoints = _points->GetNumberOfPoints();
00030
00031 double v1[3],v2[3];
00032
00033
00034 calculateOrthogonalVectors(v1,v2);
00035
00036 cout<<"PolyCutter::processOutImage"<<endl;
00037 cout<<"V1:"<<" X:"<<v1[0]<<" Y:"<<v1[1]<<" Z:"<<v1[2]<<endl;
00038 cout<<"V2:"<<" X:"<<v2[0]<<" Y:"<<v2[1]<<" Z:"<<v2[2]<<endl;
00039 cout<<"Direction:"<<" X:"<<_direction[0]<<" Y:"<<_direction[1]<<" Z:"<<_direction[2]<<endl;
00040 cout<<endl;
00041
00042
00043
00044 updateTransform(v1,v2,_direction);
00045
00046
00047 std::vector<double> vectorOutX;
00048 std::vector<double> vectorOutY;
00049 std::vector<double> vectorOutZ;
00050
00051 transformContourPoints(&vectorOutX,&vectorOutY,&vectorOutZ);
00052
00053
00055 int num = vectorOutX.size();
00056 for(int t=0;t<num;t++)
00057 {
00058 cout<<"Final Point"<<t<<"-X:"<<vectorOutX[t]<<" Y:"<<vectorOutY[t]<<" Z:"<<vectorOutZ[t]<<endl;
00059 }
00060
00061 cutInputImage(vectorOutX,vectorOutY,vectorOutZ);
00062
00063 }
00064
00065
00066 void CutModelPolygon::cutInputImage(std::vector<double> vectorOutX,std::vector<double> vectorOutY,std::vector<double> vectorOutZ)
00067 {
00068
00069
00070 int i;
00071 double minY=vectorOutY[0];
00072 for(i=1;i<vectorOutY.size();i++)
00073 {
00074 if(vectorOutY[i]<minY)
00075 {
00076 minY=vectorOutY[i];
00077 }
00078 }
00079
00080
00082 for(i=0;i<vectorOutY.size();i++)
00083 {
00084
00085 vectorOutY[i]=vectorOutY[i]-minY;
00086 }
00087
00088
00089
00090 manualBaseModel *model = InitializeContourModel(vectorOutX,vectorOutY,vectorOutZ);
00091
00092 initializeOutputImage();
00093
00094 int ext[6];
00095 _inImage->GetWholeExtent(ext);
00096 int dimX=ext[1]-ext[0]+1;
00097 int dimY=ext[3]-ext[2]+1;
00098 int dimZ=ext[5]-ext[4]+1;
00099
00100
00101
00102 ContourExtractData *extract = new ContourExtractData(false);
00103
00104
00105 std::vector<manualBaseModel*> list;
00106 list.push_back(model);
00107
00108
00109 extract->SetSizeImageY(500);
00110
00111
00112 extract->SetLstManualContourModel(list);
00113 extract->InitLstContoursLinesYPoints();
00114
00115 cout<<"Processing..."<<endl;
00116
00117 for (int i=0; i<dimX; i++)
00118 {
00119 if(i%10==0)
00120 cout<<"Print "<<i<<" of "<<dimX<<endl;
00121
00122 for (int j=0; j<dimY; j++)
00123 {
00124 for (int k=0; k<dimZ; k++)
00125 {
00126 unsigned short *pImage=(unsigned short*)_inImage->GetScalarPointer(i,j,k);
00127
00128 double in[4],out[4];
00129 in[0]=i;
00130 in[1]=j;
00131 in[2]=k;
00132 in[3]=1;
00133
00134
00135 _transform->MultiplyPoint(in,out);
00136
00137
00138 out[1]=out[1]- minY;
00139
00140
00141 if ((out[1]>=0 && out[1]<500) && (extract->isInside(out[0],out[1],0)==true))
00142 {
00143
00144 if(_cutInsideOutside==0)
00145 {
00146 *pImage=0;
00147 }
00148
00149 }
00150 else
00151 {
00152 if(_cutInsideOutside==1)
00153 {
00154 *pImage=0;
00155 }
00156 }
00157
00158 }
00159 }
00160 }
00161
00162 _inImage->Modified();
00163 _inImage->Update();
00164
00165 }
00166
00167
00168 void CutModelPolygon::transformContourPoints(std::vector<double> *vectorOutX,std::vector<double> *vectorOutY,std::vector<double> *vectorOutZ)
00169 {
00170 int numPoints = _points->GetNumberOfPoints();
00171
00172 for(int t=0;t<numPoints;t++)
00173 {
00174 double point[3],in[4],out[4];
00175 _points->GetPoint(t,point);
00176 in[0]=point[0];
00177 in[1]=point[1];
00178 in[2]=point[2];
00179 in[3]=1;
00180
00181
00182
00183
00184 double _spc[3];
00185 _inImage->GetSpacing(_spc);
00186 in[0]=in[0]*(1/_spc[0]);
00187 in[1]=in[1]*(1/_spc[1]);
00188 in[2]=in[2]*(1/_spc[2]);
00189
00190 _transform->MultiplyPoint(in,out);
00191 vectorOutX->push_back(out[0]);
00192 vectorOutY->push_back(out[1]);
00193 vectorOutZ->push_back(out[2]);
00194 }
00195
00196 }
00197
00198 void CutModelPolygon::updateTransform(double* v1, double* v2, double* v3)
00199 {
00200
00201
00202 int i, numPoints=_points->GetNumberOfPoints();
00203 double centerX=0,centerY=0,centerZ=0;
00204 for(i =0; i<numPoints;i++)
00205 {
00206 double point[3];
00207 _points->GetPoint(i,point);
00208 centerX+=point[0];
00209 centerY+=point[1];
00210 centerZ+=point[2];
00211 }
00212
00213 centerX=centerX/numPoints;
00214 centerY=centerY/numPoints;
00215 centerZ=centerZ/numPoints;
00216
00217
00218 vtkMatrix4x4 *matrix = vtkMatrix4x4::New();
00219
00220
00221 matrix->SetElement(0,0,v1[0]);
00222 matrix->SetElement(1,0,v1[1]);
00223 matrix->SetElement(2,0,v1[2]);
00224 matrix->SetElement(3,0,0);
00225
00226
00227
00228 matrix->SetElement(0,1,v2[0]);
00229 matrix->SetElement(1,1,v2[1]);
00230 matrix->SetElement(2,1,v2[2]);
00231 matrix->SetElement(3,1,0);
00232
00233
00234 matrix->SetElement(0,2,v3[0]);
00235 matrix->SetElement(1,2,v3[1]);
00236 matrix->SetElement(2,2,v3[2]);
00237 matrix->SetElement(3,2,0);
00238
00239
00240 matrix->SetElement(0,3,centerX);
00241 matrix->SetElement(1,3,centerY);
00242 matrix->SetElement(2,3,centerZ);
00243 matrix->SetElement(3,3,1);
00244
00245 _transform->SetMatrix(matrix);
00246 _transform->Update();
00247
00248 _transform->Inverse();
00249 _transform->Update();
00250 matrix->Delete();
00251 }
00252
00253 void CutModelPolygon::calculateOrthogonalVectors(double* v1, double* v2)
00254 {
00255 double p1[3],p2[3];
00256 _points->GetPoint(0,p1);
00257 _points->GetPoint(1,p2);
00258
00259
00260 v1[0]=p1[0]-p2[0];
00261 v1[1]=p1[1]-p2[1];
00262 v1[2]=p1[2]-p2[2];
00263
00264
00265 double magV1 = sqrt( v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2]);
00266 v1[0]=v1[0]/magV1;
00267 v1[1]=v1[1]/magV1;
00268 v1[2]=v1[2]/magV1;
00269
00270 v2[0] = v1[1]*_direction[2] - v1[2]*_direction[1];
00271 v2[1] = -(v1[0]*_direction[2] - v1[2]*_direction[0]);
00272 v2[2] = v1[0]*_direction[1] - v1[1]*_direction[0];
00273
00274 double magV2 = sqrt( v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2]);
00275 v2[0]=v2[0]/magV2;
00276 v2[1]=v2[1]/magV2;
00277 v2[2]=v2[2]/magV2;
00278
00279 double magDir = sqrt( _direction[0]*_direction[0] + _direction[1]*_direction[1] + _direction[2]*_direction[2]);
00280 _direction[0]=_direction[0]/magDir;
00281 _direction[1]=_direction[1]/magDir;
00282 _direction[2]=_direction[2]/magDir;
00283 }
00284
00285
00286 void CutModelPolygon::initializeOutputImage()
00287 {
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 }
00300
00301
00302
00303 manualBaseModel* CutModelPolygon::InitializeContourModel(std::vector<double> pointsX, std::vector<double> pointsY, std::vector<double> pointsZ)
00304 {
00305 manualBaseModel *model = new manualContourModelPolygon();
00306
00307 int numPoints = pointsX.size();
00308 for(int i = 0; i<numPoints;i++)
00309 {
00310 model->AddPoint(pointsX[i],pointsY[i],pointsZ[i]);
00311 }
00312
00313 return model;
00314 }
00315
00316
00317
00319
00321
00322 vtkImageData* CutModelPolygon::getInImage()
00323 {
00324 return _inImage;
00325 }
00326
00327 vtkImageData* CutModelPolygon::getOutImage()
00328 {
00329 return _inImage;
00330 }
00331
00332 vtkPoints* CutModelPolygon::getPoints()
00333 {
00334 return _points;
00335 }
00336
00337 double* CutModelPolygon::getDirection()
00338 {
00339 return _direction;
00340 }
00341
00342 void CutModelPolygon::setInImage(vtkImageData* pImage)
00343 {
00344 _inImage=pImage;
00345 }
00346
00347 void CutModelPolygon::setOutImage(vtkImageData* pImage)
00348 {
00349 _inImage=pImage;
00350 }
00351
00352 void CutModelPolygon::setPoints(vtkPoints *pPoints)
00353 {
00354 _points=pPoints;
00355 }
00356
00357 void CutModelPolygon::setDirection(double *pDirection)
00358 {
00359 _direction=pDirection;
00360 }