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 && idCurrentPoint >= 0 && idCurrentPoint < lstPointsX.size()){
75 idCurrentPoint = idCurrentPoint;
78 idCurrentPoint = 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 );
135 idCurrentPoint = lstPointsX.size()-1;
138 //------------------------------------------------------------------------
139 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
141 // return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
142 return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
145 //------------------------------------------------------------------------
146 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
148 if(lstPointsX.size()>1)
150 std::vector<double> dTotal;
154 //Calcule distance for each pair of points
155 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
157 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
158 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
160 dTotal.push_back (res);
162 //Gets the smallest distance
163 double smallTMP = dTotal[0];
164 for (j = 0; j < (int) dTotal.size(); j++)
166 if(dTotal[j]<smallTMP)
174 // *************** Open contour case Start *******************
175 if (lstPointsX.size()==2)
177 double cx,cy,cz,r1,r2;
179 cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
180 cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
181 cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
182 r1 = DistanceSQ( cx,cy,cz,x,y,z );
183 r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
184 if (r1<r2) // inside circle
187 } else { // outside circle
188 if (a<b) // befor first point
191 } else { // after second point
195 } // lstPointsX.size == 2
197 if (lstPointsX.size()>2)
200 if ( pos==1) // first point of the list
203 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
204 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
205 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
206 if (b>r3) // outside circle
211 if (pos==lstPointsX.size()-1 ) // last point of the list
213 i = lstPointsX.size()-2;
214 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
215 if (a>r3) // outside circle
220 } // if lstPointsX.size
221 // *************** Open contour case End *******************
224 std::vector<double>::iterator it;
225 //Insert the point in the list of points
226 it = lstPointsX.begin();
227 lstPointsX.insert( it+pos, x );
228 it = lstPointsY.begin();
229 lstPointsY.insert( it+pos, y );
230 it = lstPointsZ.begin();
231 lstPointsZ.insert( it+pos, z );
233 std::string strLabel = CleanSpaces( label );
235 std::vector<std::string>::iterator itS;
236 itS = lstLabels.begin();
237 //Insert Label in list of labels
238 lstLabels.insert( itS+pos, strLabel );
248 //------------------------------------------------------------------------
249 void ModelShowNPoints::SavePoints_(FILE* ff)
251 std::string tmpLabel;
252 int i , size = (int) (lstPointsX.size());
253 fprintf(ff,"NumberOfPoints %d \n",size);
254 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
257 for (i=0; i<size; i++)
262 value = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
263 if (lstLabels[i]!="")
265 tmpLabel=lstLabels[i];
269 fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
273 //------------------------------------------------------------------------
274 void ModelShowNPoints::SavePoints(std::string filename)
276 FILE* ff = fopen( filename.c_str() , "w+" );
282 printf("ModelShowNPoints::SavePoints ...Error... creating file\n");
286 //------------------------------------------------------------------------
287 int ModelShowNPoints::ReadPoints_(FILE* ff)
291 fscanf(ff," %s %d",chartmp,&size);
292 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
296 for (i=0; i<size; i++)
298 fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
299 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
300 AddPoint(x,y,z, chartmp );
305 //------------------------------------------------------------------------
306 int ModelShowNPoints::ReadPoints(std::string filename)
309 FILE *ff = fopen( filename.c_str() , "r+" );
312 size = ReadPoints_(ff);
315 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
320 //------------------------------------------------------------------------
321 int ModelShowNPoints::GetNearestPoint()
324 int i, size=(int)(lstPointsX.size());
325 double radioMin=10000000;
326 for ( i=0 ; i<size; i++ )
328 double rx = mReferencePoint[0] - lstPointsX [i];
329 double ry = mReferencePoint[1] - lstPointsY [i];
330 double rz = mReferencePoint[2] - lstPointsZ [i];
331 double radio = rx*rx + ry*ry + rz*rz;
332 if ( radio <= radioMin)
341 //------------------------------------------------------------------------
342 int ModelShowNPoints::GetLstPointsSize()
344 return lstPointsX.size();
347 //------------------------------------------------------------------------
348 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
350 lstPointsX[id] = mReferencePoint[0];
351 lstPointsY[id] = mReferencePoint[1];
352 lstPointsZ[id] = mReferencePoint[2];
356 //------------------------------------------------------------------------
357 int ModelShowNPoints::IdInsidePoint()
360 int i, size=(int)( lstPointsX.size() );
362 double radio2=(mradio+1)*(mradio+1);
365 printf("WidgetShowNPoints::IdInsidePoint image not set\n");
368 mimage->GetSpacing(spc);
369 for ( i=0 ; i<size; i++ )
371 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
372 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
373 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
374 if ( rx*rx + ry*ry + rz*rz <= radio2)
382 //------------------------------------------------------------------------
383 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
385 if(id >= 0 && id < lstPointsX.size()){
386 lstPointsX[id] = point[0];
387 lstPointsY[id] = point[1];
388 lstPointsZ[id] = point[2];
391 printf("WidgetShowNPoints::SetPointById Invalid pointid, id is outside range\n");
394 //------------------------------------------------------------------------
395 int ModelShowNPoints::RenamePoint(std::string label)
397 int id=IdInsidePoint();
400 std::string strLabel = CleanSpaces( label );
401 lstLabels[id] = strLabel;
406 //----------------------------------------------------------------------
407 void ModelShowNPoints::ErasePoint(int id)
409 lstPointsX.erase( lstPointsX.begin()+id );
410 lstPointsY.erase( lstPointsY.begin()+id );
411 lstPointsZ.erase( lstPointsZ.begin()+id );
412 lstLabels.erase( lstLabels.begin()+id );
413 SetIdCurrentPoint(id);
416 //----------------------------------------------------------------------
417 void ModelShowNPoints::SetFirstTime(bool value)
422 //----------------------------------------------------------------------
423 bool ModelShowNPoints::GetFirstTime()
428 //----------------------------------------------------------------------
429 void ModelShowNPoints::InversLstPoints()
431 int i,size = lstPointsX.size();
432 int i2,size2 = size/2;
435 for (i=0 ; i<size2 ; i++)
438 dTmp = lstPointsX[i]; lstPointsX[i] = lstPointsX[i2]; lstPointsX[i2] = dTmp;
439 dTmp = lstPointsY[i]; lstPointsY[i] = lstPointsY[i2]; lstPointsY[i2] = dTmp;
440 dTmp = lstPointsZ[i]; lstPointsZ[i] = lstPointsZ[i2]; lstPointsZ[i2] = dTmp;
441 sTmp = lstLabels[i]; lstLabels[i] = lstLabels[i2]; lstLabels[i2] = sTmp;
445 //----------------------------------------------------------------------
446 int ModelShowNPoints::GetIdCurrentPoint()
448 return idCurrentPoint;
451 //----------------------------------------------------------------------
452 void ModelShowNPoints::SetIdCurrentPoint(int idPoint)
454 if (GetLstPointsSize()==0)
458 idCurrentPoint=idPoint;
459 if (idCurrentPoint<0) {idCurrentPoint=0;}
460 if (idCurrentPoint>=GetLstPointsSize() ) {idCurrentPoint=GetLstPointsSize()-1; }