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<int> ModelShowNPoints::GetLstPointsX()
36 //------------------------------------------------------------------------
37 std::vector<int> ModelShowNPoints::GetLstPointsY()
42 //------------------------------------------------------------------------
43 std::vector<int> 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<int> ppoint)
70 mReferencePoint = ppoint;
73 //------------------------------------------------------------------------
74 std::vector<int> 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, int *x, int *y, int *z)
105 //------------------------------------------------------------------------
106 std::string ModelShowNPoints::GetIdLabel(int id)
108 return lstLabels[id];
111 //------------------------------------------------------------------------
112 vtkImageData *ModelShowNPoints::GetImage()
117 //------------------------------------------------------------------------
118 void ModelShowNPoints::AddPoint(int x, int y, int 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::Distance(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));
133 //------------------------------------------------------------------------
134 int ModelShowNPoints::InsertPoint(int x, int y, int z, std::string label)
136 if(lstPointsX.size()>1)
138 std::vector<int> dTotal;
142 //Calcule distance for each pair of points
143 for(int i = 0; i<(int)lstPointsX.size()-1 ; i++)
145 a = Distance(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
146 b = Distance(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
148 dTotal.push_back (res);
150 //Gets the smallest distance
151 int smallTMP = dTotal[0];
152 for (int j = 0; j < (int) dTotal.size(); j++)
154 if(dTotal[j]<smallTMP)
161 std::vector<int>::iterator it;
162 //Insert the point in the list of points
163 it = lstPointsX.begin();
164 lstPointsX.insert( it+pos, x );
165 it = lstPointsY.begin();
166 lstPointsY.insert( it+pos, y );
167 it = lstPointsZ.begin();
168 lstPointsZ.insert( it+pos, z );
170 std::string strLabel = CleanSpaces( label );
172 std::vector<std::string>::iterator itS;
173 itS = lstLabels.begin();
174 //Insert Label in list of labels
175 lstLabels.insert( itS+pos, strLabel );
184 //------------------------------------------------------------------------
185 void ModelShowNPoints::SavePoints_(FILE* ff)
187 std::string tmpLabel;
188 int i , size = (int) (lstPointsX.size());
189 fprintf(ff,"NumberOfPoints %d \n",size);
190 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
193 for (i=0; i<size; i++)
198 value = mimage->GetScalarComponentAsDouble(x,y,z,0);
199 if (lstLabels[i]!="")
201 tmpLabel=lstLabels[i];
205 fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
209 //------------------------------------------------------------------------
210 void ModelShowNPoints::SavePoints(std::string filename)
212 FILE* ff = fopen( filename.c_str() , "w+" );
218 printf("ModelShowNPoints::SavePoints ...Error... creating file\n");
222 //------------------------------------------------------------------------
223 int ModelShowNPoints::ReadPoints_(FILE* ff)
227 fscanf(ff," %s %d",chartmp,&size);
228 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
232 for (i=0; i<size; i++)
234 fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
235 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
236 AddPoint(x,y,z, chartmp );
241 //------------------------------------------------------------------------
242 int ModelShowNPoints::ReadPoints(std::string filename)
245 FILE *ff = fopen( filename.c_str() , "r+" );
248 size = ReadPoints_(ff);
251 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
256 //------------------------------------------------------------------------
257 int ModelShowNPoints::GetNearestPoint()
260 int i, size=(int)(lstPointsX.size());
261 double radioMin=10000000;
262 for ( i=0 ; i<size; i++ )
264 double rx = mReferencePoint[0] - lstPointsX [i];
265 double ry = mReferencePoint[1] - lstPointsY [i];
266 double rz = mReferencePoint[2] - lstPointsZ [i];
267 double radio = rx*rx + ry*ry + rz*rz;
268 if ( radio <= radioMin)
277 //------------------------------------------------------------------------
278 int ModelShowNPoints::GetLstPointsSize()
280 return lstPointsX.size();
283 //------------------------------------------------------------------------
284 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
286 lstPointsX[id] = mReferencePoint[0];
287 lstPointsY[id] = mReferencePoint[1];
288 lstPointsZ[id] = mReferencePoint[2];
292 //------------------------------------------------------------------------
293 int ModelShowNPoints::IdInsidePoint()
296 int i, size=(int)(lstPointsX.size());
300 printf("WidgetShowNPoints::IdInsidePoint image not set\n");
303 mimage->GetSpacing(spc);
304 for ( i=0 ; i<size; i++ )
306 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
307 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
308 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
309 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
318 //------------------------------------------------------------------------
319 int ModelShowNPoints::RenamePoint(std::string label)
321 int id=IdInsidePoint();
324 std::string strLabel = CleanSpaces( label );
325 lstLabels[id] = strLabel;
330 //----------------------------------------------------------------------
331 void ModelShowNPoints::ErasePoint(int id)
333 lstPointsX.erase( lstPointsX.begin()+id );
334 lstPointsY.erase( lstPointsY.begin()+id );
335 lstPointsZ.erase( lstPointsZ.begin()+id );
336 lstLabels.erase( lstLabels.begin()+id );
339 //----------------------------------------------------------------------
340 void ModelShowNPoints::SetFirstTime(bool value)
345 //----------------------------------------------------------------------
346 bool ModelShowNPoints::GetFirstTime()