4 #include "ModelShowNPoints.h"
7 //----------------------------------------------------------------------
8 ModelShowNPoints::ModelShowNPoints()
12 //----------------------------------------------------------------------
14 ModelShowNPoints::~ModelShowNPoints()
18 //------------------------------------------------------------------------
19 void ModelShowNPoints::SetRadio(double radio)
24 //------------------------------------------------------------------------
25 double ModelShowNPoints::GetRadio()
30 //------------------------------------------------------------------------
31 std::vector<double> ModelShowNPoints::GetLstPointsX()
36 //------------------------------------------------------------------------
37 std::vector<double> ModelShowNPoints::GetLstPointsY()
42 //------------------------------------------------------------------------
43 std::vector<double> ModelShowNPoints::GetLstPointsZ()
48 //------------------------------------------------------------------------
49 std::vector<std::string> ModelShowNPoints::GetLstLabels()
52 std::vector<std::string> tmpLst;
53 int i ,size=lstLabels.size();
55 for (i=0; i<size; i++)
58 if (tmp.empty()==true)
62 tmpLst.push_back( tmp );
67 //------------------------------------------------------------------------
68 void ModelShowNPoints::SetReferencePoint(std::vector<double> ppoint)
70 mReferencePoint = ppoint;
73 //------------------------------------------------------------------------
74 std::vector<double> ModelShowNPoints::GetReferencePoint()
76 return mReferencePoint;
79 //------------------------------------------------------------------------
80 void ModelShowNPoints::SetImage(vtkImageData *image)
86 //------------------------------------------------------------------------
87 std::string ModelShowNPoints::CleanSpaces(std::string ss)
90 while( (i=ss.find(32))>=0 )
97 //------------------------------------------------------------------------
98 void ModelShowNPoints::GetIdPoint(int id, double *x, double *y, double *z)
105 //------------------------------------------------------------------------
106 std::string ModelShowNPoints::GetIdLabel(int id)
108 return lstLabels[id];
111 //------------------------------------------------------------------------
112 vtkImageData *ModelShowNPoints::GetImage()
117 //------------------------------------------------------------------------
118 void ModelShowNPoints::AddPoint(double x, double y, double z, std::string label)
120 lstPointsX.push_back( x );
121 lstPointsY.push_back( y );
122 lstPointsZ.push_back( z );
123 std::string strLabel = CleanSpaces( label );
124 lstLabels.push_back( strLabel );
127 //------------------------------------------------------------------------
128 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
130 // return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
131 return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
134 //------------------------------------------------------------------------
135 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
137 if(lstPointsX.size()>1)
139 std::vector<double> dTotal;
143 //Calcule distance for each pair of points
144 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
146 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
147 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
149 dTotal.push_back (res);
151 //Gets the smallest distance
152 double smallTMP = dTotal[0];
153 for (j = 0; j < (int) dTotal.size(); j++)
155 if(dTotal[j]<smallTMP)
163 // *************** Open contour case Start *******************
164 if (lstPointsX.size()==2)
166 double cx,cy,cz,r1,r2;
168 cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
169 cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
170 cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
171 r1 = DistanceSQ( cx,cy,cz,x,y,z );
172 r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
173 if (r1<r2) // inside circle
176 } else { // outside circle
177 if (a<b) // befor first point
180 } else { // after second point
185 if (lstPointsX.size()>2)
188 if ( pos==1) // first point of the list
191 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
192 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
193 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
194 if (b>r3) // outside circle
199 if (pos==lstPointsX.size()-1 ) // last point of the list
201 i = lstPointsX.size()-2;
202 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
203 if (a>r3) // outside circle
209 // *************** Open contour case End *******************
212 std::vector<double>::iterator it;
213 //Insert the point in the list of points
214 it = lstPointsX.begin();
215 lstPointsX.insert( it+pos, x );
216 it = lstPointsY.begin();
217 lstPointsY.insert( it+pos, y );
218 it = lstPointsZ.begin();
219 lstPointsZ.insert( it+pos, z );
221 std::string strLabel = CleanSpaces( label );
223 std::vector<std::string>::iterator itS;
224 itS = lstLabels.begin();
225 //Insert Label in list of labels
226 lstLabels.insert( itS+pos, strLabel );
235 //------------------------------------------------------------------------
236 void ModelShowNPoints::SavePoints_(FILE* ff)
238 std::string tmpLabel;
239 int i , size = (int) (lstPointsX.size());
240 fprintf(ff,"NumberOfPoints %d \n",size);
241 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
244 for (i=0; i<size; i++)
249 value = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
250 if (lstLabels[i]!="")
252 tmpLabel=lstLabels[i];
256 fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
260 //------------------------------------------------------------------------
261 void ModelShowNPoints::SavePoints(std::string filename)
263 FILE* ff = fopen( filename.c_str() , "w+" );
269 printf("ModelShowNPoints::SavePoints ...Error... creating file\n");
273 //------------------------------------------------------------------------
274 int ModelShowNPoints::ReadPoints_(FILE* ff)
278 fscanf(ff," %s %d",chartmp,&size);
279 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
283 for (i=0; i<size; i++)
285 fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
286 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
287 AddPoint(x,y,z, chartmp );
292 //------------------------------------------------------------------------
293 int ModelShowNPoints::ReadPoints(std::string filename)
296 FILE *ff = fopen( filename.c_str() , "r+" );
299 size = ReadPoints_(ff);
302 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
307 //------------------------------------------------------------------------
308 int ModelShowNPoints::GetNearestPoint()
311 int i, size=(int)(lstPointsX.size());
312 double radioMin=10000000;
313 for ( i=0 ; i<size; i++ )
315 double rx = mReferencePoint[0] - lstPointsX [i];
316 double ry = mReferencePoint[1] - lstPointsY [i];
317 double rz = mReferencePoint[2] - lstPointsZ [i];
318 double radio = rx*rx + ry*ry + rz*rz;
319 if ( radio <= radioMin)
328 //------------------------------------------------------------------------
329 int ModelShowNPoints::GetLstPointsSize()
331 return lstPointsX.size();
334 //------------------------------------------------------------------------
335 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
337 lstPointsX[id] = mReferencePoint[0];
338 lstPointsY[id] = mReferencePoint[1];
339 lstPointsZ[id] = mReferencePoint[2];
343 //------------------------------------------------------------------------
344 int ModelShowNPoints::IdInsidePoint()
347 int i, size=(int)( lstPointsX.size() );
349 double radio2=(mradio+1)*(mradio+1);
352 printf("WidgetShowNPoints::IdInsidePoint image not set\n");
355 mimage->GetSpacing(spc);
356 for ( i=0 ; i<size; i++ )
358 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
359 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
360 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
361 if ( rx*rx + ry*ry + rz*rz <= radio2)
369 //------------------------------------------------------------------------
370 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
372 if(id >= 0 && id < lstPointsX.size()){
373 lstPointsX[id] = point[0];
374 lstPointsY[id] = point[1];
375 lstPointsZ[id] = point[2];
378 printf("WidgetShowNPoints::SetPointById Invalid pointid, id is outside range\n");
381 //------------------------------------------------------------------------
382 int ModelShowNPoints::RenamePoint(std::string label)
384 int id=IdInsidePoint();
387 std::string strLabel = CleanSpaces( label );
388 lstLabels[id] = strLabel;
393 //----------------------------------------------------------------------
394 void ModelShowNPoints::ErasePoint(int id)
396 lstPointsX.erase( lstPointsX.begin()+id );
397 lstPointsY.erase( lstPointsY.begin()+id );
398 lstPointsZ.erase( lstPointsZ.begin()+id );
399 lstLabels.erase( lstLabels.begin()+id );
402 //----------------------------------------------------------------------
403 void ModelShowNPoints::SetFirstTime(bool value)
408 //----------------------------------------------------------------------
409 bool ModelShowNPoints::GetFirstTime()
414 //----------------------------------------------------------------------
415 void ModelShowNPoints::InversLstPoints()
417 int i,size = lstPointsX.size();
418 int i2,size2 = size/2;
421 for (i=0 ; i<size2 ; i++)
424 dTmp = lstPointsX[i]; lstPointsX[i] = lstPointsX[i2]; lstPointsX[i2] = dTmp;
425 dTmp = lstPointsY[i]; lstPointsY[i] = lstPointsY[i2]; lstPointsY[i2] = dTmp;
426 dTmp = lstPointsZ[i]; lstPointsZ[i] = lstPointsZ[i2]; lstPointsZ[i2] = dTmp;
427 sTmp = lstLabels[i]; lstLabels[i] = lstLabels[i2]; lstLabels[i2] = sTmp;