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)
74 int idPoint = IdInsidePoint();
75 if(idPoint == -1 && idCurrentPoint >= 0 && idCurrentPoint < lstPointsX.size())
77 idCurrentPoint = idCurrentPoint;
79 idCurrentPoint = idPoint;
81 } // if lstPointsX.size
84 //------------------------------------------------------------------------
85 std::vector<double> ModelShowNPoints::GetReferencePoint()
87 return mReferencePoint;
90 //------------------------------------------------------------------------
91 void ModelShowNPoints::SetImage(vtkImageData *image)
97 //------------------------------------------------------------------------
98 std::string ModelShowNPoints::CleanSpaces(std::string ss)
101 while( (i=ss.find(32))>=0 )
108 //------------------------------------------------------------------------
109 void ModelShowNPoints::GetIdPoint(int id, double *x, double *y, double *z)
116 //------------------------------------------------------------------------
117 std::string ModelShowNPoints::GetIdLabel(int id)
119 return lstLabels[id];
122 //------------------------------------------------------------------------
123 vtkImageData *ModelShowNPoints::GetImage()
128 //------------------------------------------------------------------------
129 void ModelShowNPoints::AddPoint(double x, double y, double z, std::string label)
131 lstPointsX.push_back( x );
132 lstPointsY.push_back( y );
133 lstPointsZ.push_back( z );
134 std::string strLabel = CleanSpaces( label );
135 lstLabels.push_back( strLabel );
136 idCurrentPoint = lstPointsX.size()-1;
139 //------------------------------------------------------------------------
140 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
142 // return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
143 return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
146 //------------------------------------------------------------------------
147 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
149 if(lstPointsX.size()>1)
151 std::vector<double> dTotal;
155 //Calcule distance for each pair of points
156 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
158 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
159 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
161 dTotal.push_back (res);
163 //Gets the smallest distance
164 double smallTMP = dTotal[0];
165 for (j = 0; j < (int) dTotal.size(); j++)
167 if(dTotal[j]<smallTMP)
175 // *************** Open contour case Start *******************
176 if (lstPointsX.size()==2)
178 double cx,cy,cz,r1,r2;
180 cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
181 cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
182 cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
183 r1 = DistanceSQ( cx,cy,cz,x,y,z );
184 r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
185 if (r1<r2) // inside circle
188 } else { // outside circle
189 if (a<b) // befor first point
192 } else { // after second point
196 } // lstPointsX.size == 2
198 if (lstPointsX.size()>2)
201 if ( pos==1) // first point of the list
204 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
205 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
206 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
207 if (b>r3) // outside circle
212 if (pos==lstPointsX.size()-1 ) // last point of the list
214 i = lstPointsX.size()-2;
215 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
216 if (a>r3) // outside circle
221 } // if lstPointsX.size
222 // *************** Open contour case End *******************
225 std::vector<double>::iterator it;
226 //Insert the point in the list of points
227 it = lstPointsX.begin();
228 lstPointsX.insert( it+pos, x );
229 it = lstPointsY.begin();
230 lstPointsY.insert( it+pos, y );
231 it = lstPointsZ.begin();
232 lstPointsZ.insert( it+pos, z );
234 std::string strLabel = CleanSpaces( label );
236 std::vector<std::string>::iterator itS;
237 itS = lstLabels.begin();
238 //Insert Label in list of labels
239 lstLabels.insert( itS+pos, strLabel );
249 //------------------------------------------------------------------------
250 void ModelShowNPoints::SavePoints_(FILE* ff)
252 std::string tmpLabel;
253 int i , size = (int) (lstPointsX.size());
254 fprintf(ff,"NumberOfPoints %d \n",size);
255 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
258 for (i=0; i<size; i++)
263 value = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
264 if (lstLabels[i]!="")
266 tmpLabel=lstLabels[i];
270 fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
274 //------------------------------------------------------------------------
275 void ModelShowNPoints::SavePoints(std::string filename)
277 FILE* ff = fopen( filename.c_str() , "w+" );
283 printf("ModelShowNPoints::SavePoints ...Error... creating file\n");
287 //------------------------------------------------------------------------
288 int ModelShowNPoints::ReadPoints_(FILE* ff)
292 fscanf(ff," %s %d",chartmp,&size);
293 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
297 for (i=0; i<size; i++)
299 fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
300 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
301 AddPoint(x,y,z, chartmp );
306 //------------------------------------------------------------------------
307 int ModelShowNPoints::ReadPoints(std::string filename)
310 FILE *ff = fopen( filename.c_str() , "r+" );
313 size = ReadPoints_(ff);
316 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
321 //------------------------------------------------------------------------
322 int ModelShowNPoints::GetNearestPoint()
325 int i, size=(int)(lstPointsX.size());
326 double radioMin=10000000;
327 for ( i=0 ; i<size; i++ )
329 double rx = mReferencePoint[0] - lstPointsX [i];
330 double ry = mReferencePoint[1] - lstPointsY [i];
331 double rz = mReferencePoint[2] - lstPointsZ [i];
332 double radio = rx*rx + ry*ry + rz*rz;
333 if ( radio <= radioMin)
342 //------------------------------------------------------------------------
343 int ModelShowNPoints::GetLstPointsSize()
345 return lstPointsX.size();
348 //------------------------------------------------------------------------
349 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
351 lstPointsX[id] = mReferencePoint[0];
352 lstPointsY[id] = mReferencePoint[1];
353 lstPointsZ[id] = mReferencePoint[2];
357 //------------------------------------------------------------------------
358 int ModelShowNPoints::IdInsidePoint()
361 int i, size=(int)( lstPointsX.size() );
363 double tmpRadio = mradio/3;
364 double radio2=(tmpRadio+1)*(tmpRadio+1);
365 if ( (mimage==NULL) || (mReferencePoint.size()!=3) )
367 printf("WidgetShowNPoints::IdInsidePoint image not set or ReferencePoint not set\n");
370 mimage->GetSpacing(spc);
371 for ( i=0 ; i<size; i++ )
373 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
374 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
375 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
376 if ( rx*rx + ry*ry + rz*rz <= radio2)
385 //------------------------------------------------------------------------
386 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
388 if(id >= 0 && id < lstPointsX.size()){
389 lstPointsX[id] = point[0];
390 lstPointsY[id] = point[1];
391 lstPointsZ[id] = point[2];
394 printf("WidgetShowNPoints::SetPointById Invalid pointid, id is outside range\n");
398 std::vector<double> ModelShowNPoints::GetPointById( int id )
400 std::vector<double> point;
401 if(id >= 0 && id < lstPointsX.size())
403 point.push_back ( lstPointsX[id] );
404 point.push_back ( lstPointsY[id] );
405 point.push_back ( lstPointsZ[id] );
408 printf("WidgetShowNPoints::GetPointById Invalid pointid, id is outside range\n");
414 //------------------------------------------------------------------------
415 int ModelShowNPoints::RenamePoint(std::string label)
417 int id=IdInsidePoint();
420 std::string strLabel = CleanSpaces( label );
421 lstLabels[id] = strLabel;
426 //----------------------------------------------------------------------
427 void ModelShowNPoints::ErasePoint(int id)
429 lstPointsX.erase( lstPointsX.begin()+id );
430 lstPointsY.erase( lstPointsY.begin()+id );
431 lstPointsZ.erase( lstPointsZ.begin()+id );
432 lstLabels.erase( lstLabels.begin()+id );
433 SetIdCurrentPoint(id);
436 //----------------------------------------------------------------------
437 void ModelShowNPoints::SetFirstTime(bool value)
442 //----------------------------------------------------------------------
443 bool ModelShowNPoints::GetFirstTime()
448 //----------------------------------------------------------------------
449 void ModelShowNPoints::InversLstPoints()
451 int i,size = lstPointsX.size();
452 int i2,size2 = size/2;
455 for (i=0 ; i<size2 ; i++)
458 dTmp = lstPointsX[i]; lstPointsX[i] = lstPointsX[i2]; lstPointsX[i2] = dTmp;
459 dTmp = lstPointsY[i]; lstPointsY[i] = lstPointsY[i2]; lstPointsY[i2] = dTmp;
460 dTmp = lstPointsZ[i]; lstPointsZ[i] = lstPointsZ[i2]; lstPointsZ[i2] = dTmp;
461 sTmp = lstLabels[i]; lstLabels[i] = lstLabels[i2]; lstLabels[i2] = sTmp;
465 //----------------------------------------------------------------------
466 int ModelShowNPoints::GetIdCurrentPoint()
468 return idCurrentPoint;
471 //----------------------------------------------------------------------
472 void ModelShowNPoints::SetIdCurrentPoint(int idPoint)
474 if (GetLstPointsSize()==0)
478 idCurrentPoint=idPoint;
479 if (idCurrentPoint<0) {idCurrentPoint=0;}
480 if (idCurrentPoint>=GetLstPointsSize() ) {idCurrentPoint=GetLstPointsSize()-1; }