4 #include "ModelShowNPoints.h"
7 //----------------------------------------------------------------------
8 ModelShowNPoints::ModelShowNPoints()
13 //----------------------------------------------------------------------
15 ModelShowNPoints::~ModelShowNPoints()
19 //------------------------------------------------------------------------
20 void ModelShowNPoints::SetRadio(double radio)
25 //------------------------------------------------------------------------
26 double ModelShowNPoints::GetRadio()
31 //------------------------------------------------------------------------
32 std::vector<double> ModelShowNPoints::GetLstPointsX()
37 //------------------------------------------------------------------------
38 std::vector<double> ModelShowNPoints::GetLstPointsY()
43 //------------------------------------------------------------------------
44 std::vector<double> ModelShowNPoints::GetLstPointsZ()
49 //------------------------------------------------------------------------
50 std::vector<std::string> ModelShowNPoints::GetLstLabels()
53 std::vector<std::string> tmpLst;
54 int i ,size=lstLabels.size();
56 for (i=0; i<size; i++)
59 if (tmp.empty()==true)
63 tmpLst.push_back( tmp );
68 //------------------------------------------------------------------------
69 void ModelShowNPoints::SetReferencePoint(std::vector<double> ppoint)
71 mReferencePoint = ppoint;
72 if(lstPointsX.size() > 0){
73 int idPoint = IdInsidePoint();
74 if(idPoint == -1 && currentPoint >= 0 && currentPoint < lstPointsX.size()){
75 currentPoint = currentPoint;
78 currentPoint = idPoint;
83 //------------------------------------------------------------------------
84 std::vector<double> ModelShowNPoints::GetReferencePoint()
86 return mReferencePoint;
89 //------------------------------------------------------------------------
90 void ModelShowNPoints::SetImage(vtkImageData *image)
96 //------------------------------------------------------------------------
97 std::string ModelShowNPoints::CleanSpaces(std::string ss)
100 while( (i=ss.find(32))>=0 )
107 //------------------------------------------------------------------------
108 void ModelShowNPoints::GetIdPoint(int id, double *x, double *y, double *z)
115 //------------------------------------------------------------------------
116 std::string ModelShowNPoints::GetIdLabel(int id)
118 return lstLabels[id];
121 //------------------------------------------------------------------------
122 vtkImageData *ModelShowNPoints::GetImage()
127 //------------------------------------------------------------------------
128 void ModelShowNPoints::AddPoint(double x, double y, double z, std::string label)
130 lstPointsX.push_back( x );
131 lstPointsY.push_back( y );
132 lstPointsZ.push_back( z );
133 std::string strLabel = CleanSpaces( label );
134 lstLabels.push_back( strLabel );
137 //------------------------------------------------------------------------
138 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
140 // return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
141 return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
144 //------------------------------------------------------------------------
145 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
147 if(lstPointsX.size()>1)
149 std::vector<double> dTotal;
153 //Calcule distance for each pair of points
154 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
156 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
157 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
159 dTotal.push_back (res);
161 //Gets the smallest distance
162 double smallTMP = dTotal[0];
163 for (j = 0; j < (int) dTotal.size(); j++)
165 if(dTotal[j]<smallTMP)
173 // *************** Open contour case Start *******************
174 if (lstPointsX.size()==2)
176 double cx,cy,cz,r1,r2;
178 cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
179 cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
180 cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
181 r1 = DistanceSQ( cx,cy,cz,x,y,z );
182 r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
183 if (r1<r2) // inside circle
186 } else { // outside circle
187 if (a<b) // befor first point
190 } else { // after second point
195 if (lstPointsX.size()>2)
198 if ( pos==1) // first point of the list
201 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
202 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
203 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
204 if (b>r3) // outside circle
209 if (pos==lstPointsX.size()-1 ) // last point of the list
211 i = lstPointsX.size()-2;
212 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
213 if (a>r3) // outside circle
219 // *************** Open contour case End *******************
222 std::vector<double>::iterator it;
223 //Insert the point in the list of points
224 it = lstPointsX.begin();
225 lstPointsX.insert( it+pos, x );
226 it = lstPointsY.begin();
227 lstPointsY.insert( it+pos, y );
228 it = lstPointsZ.begin();
229 lstPointsZ.insert( it+pos, z );
231 std::string strLabel = CleanSpaces( label );
233 std::vector<std::string>::iterator itS;
234 itS = lstLabels.begin();
235 //Insert Label in list of labels
236 lstLabels.insert( itS+pos, strLabel );
245 //------------------------------------------------------------------------
246 void ModelShowNPoints::SavePoints_(FILE* ff)
248 std::string tmpLabel;
249 int i , size = (int) (lstPointsX.size());
250 fprintf(ff,"NumberOfPoints %d \n",size);
251 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
254 for (i=0; i<size; i++)
259 value = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
260 if (lstLabels[i]!="")
262 tmpLabel=lstLabels[i];
266 fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
270 //------------------------------------------------------------------------
271 void ModelShowNPoints::SavePoints(std::string filename)
273 FILE* ff = fopen( filename.c_str() , "w+" );
279 printf("ModelShowNPoints::SavePoints ...Error... creating file\n");
283 //------------------------------------------------------------------------
284 int ModelShowNPoints::ReadPoints_(FILE* ff)
288 fscanf(ff," %s %d",chartmp,&size);
289 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
293 for (i=0; i<size; i++)
295 fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
296 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
297 AddPoint(x,y,z, chartmp );
302 //------------------------------------------------------------------------
303 int ModelShowNPoints::ReadPoints(std::string filename)
306 FILE *ff = fopen( filename.c_str() , "r+" );
309 size = ReadPoints_(ff);
312 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
317 //------------------------------------------------------------------------
318 int ModelShowNPoints::GetNearestPoint()
321 int i, size=(int)(lstPointsX.size());
322 double radioMin=10000000;
323 for ( i=0 ; i<size; i++ )
325 double rx = mReferencePoint[0] - lstPointsX [i];
326 double ry = mReferencePoint[1] - lstPointsY [i];
327 double rz = mReferencePoint[2] - lstPointsZ [i];
328 double radio = rx*rx + ry*ry + rz*rz;
329 if ( radio <= radioMin)
338 //------------------------------------------------------------------------
339 int ModelShowNPoints::GetLstPointsSize()
341 return lstPointsX.size();
344 //------------------------------------------------------------------------
345 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
347 lstPointsX[id] = mReferencePoint[0];
348 lstPointsY[id] = mReferencePoint[1];
349 lstPointsZ[id] = mReferencePoint[2];
353 //------------------------------------------------------------------------
354 int ModelShowNPoints::IdInsidePoint()
357 int i, size=(int)( lstPointsX.size() );
359 double radio2=(mradio+1)*(mradio+1);
362 printf("WidgetShowNPoints::IdInsidePoint image not set\n");
365 mimage->GetSpacing(spc);
366 for ( i=0 ; i<size; i++ )
368 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
369 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
370 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
371 if ( rx*rx + ry*ry + rz*rz <= radio2)
379 //------------------------------------------------------------------------
380 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
382 if(id >= 0 && id < lstPointsX.size()){
383 lstPointsX[id] = point[0];
384 lstPointsY[id] = point[1];
385 lstPointsZ[id] = point[2];
388 printf("WidgetShowNPoints::SetPointById Invalid pointid, id is outside range\n");
391 //------------------------------------------------------------------------
392 int ModelShowNPoints::RenamePoint(std::string label)
394 int id=IdInsidePoint();
397 std::string strLabel = CleanSpaces( label );
398 lstLabels[id] = strLabel;
403 //----------------------------------------------------------------------
404 void ModelShowNPoints::ErasePoint(int id)
406 lstPointsX.erase( lstPointsX.begin()+id );
407 lstPointsY.erase( lstPointsY.begin()+id );
408 lstPointsZ.erase( lstPointsZ.begin()+id );
409 lstLabels.erase( lstLabels.begin()+id );
412 //----------------------------------------------------------------------
413 void ModelShowNPoints::SetFirstTime(bool value)
418 //----------------------------------------------------------------------
419 bool ModelShowNPoints::GetFirstTime()
424 //----------------------------------------------------------------------
425 void ModelShowNPoints::InversLstPoints()
427 int i,size = lstPointsX.size();
428 int i2,size2 = size/2;
431 for (i=0 ; i<size2 ; i++)
434 dTmp = lstPointsX[i]; lstPointsX[i] = lstPointsX[i2]; lstPointsX[i2] = dTmp;
435 dTmp = lstPointsY[i]; lstPointsY[i] = lstPointsY[i2]; lstPointsY[i2] = dTmp;
436 dTmp = lstPointsZ[i]; lstPointsZ[i] = lstPointsZ[i2]; lstPointsZ[i2] = dTmp;
437 sTmp = lstLabels[i]; lstLabels[i] = lstLabels[i2]; lstLabels[i2] = sTmp;
441 //----------------------------------------------------------------------
442 int ModelShowNPoints::GetCurrentPoint()