4 #include "ModelShowNPoints.h"
7 //----------------------------------------------------------------------
8 ModelShowNPoints::ModelShowNPoints()
12 //----------------------------------------------------------------------
14 ModelShowNPoints::~ModelShowNPoints()
18 //------------------------------------------------------------------------
19 void ModelShowNPoints::SetRadio(double radio)
25 //------------------------------------------------------------------------
26 double ModelShowNPoints::GetRadio()
32 //------------------------------------------------------------------------
33 std::vector<int> ModelShowNPoints::GetLstPointsX()
38 //------------------------------------------------------------------------
39 std::vector<int> ModelShowNPoints::GetLstPointsY()
44 //------------------------------------------------------------------------
45 std::vector<int> ModelShowNPoints::GetLstPointsZ()
50 //------------------------------------------------------------------------
51 std::vector<std::string> ModelShowNPoints::GetLstLabels()
56 //------------------------------------------------------------------------
57 void ModelShowNPoints::SetReferencePoint(std::vector<int> ppoint)
59 mReferencePoint = ppoint;
62 //------------------------------------------------------------------------
63 std::vector<int> ModelShowNPoints::GetReferencePoint()
65 return mReferencePoint;
68 //------------------------------------------------------------------------
69 void ModelShowNPoints::SetImage(vtkImageData *image)
75 //------------------------------------------------------------------------
76 std::string ModelShowNPoints::CleanSpaces(std::string ss)
79 while( (i=ss.find(32))>=0 )
86 //------------------------------------------------------------------------
87 void ModelShowNPoints::GetIdPoint(int id, int *x, int *y, int *z)
94 //------------------------------------------------------------------------
95 std::string ModelShowNPoints::GetIdLabel(int id)
100 //------------------------------------------------------------------------
101 vtkImageData *ModelShowNPoints::GetImage()
106 //------------------------------------------------------------------------
107 void ModelShowNPoints::AddPoint(int x, int y, int z, std::string label)
109 lstPointsX.push_back( x );
110 lstPointsY.push_back( y );
111 lstPointsZ.push_back( z );
112 std::string strLabel = CleanSpaces( label );
113 lstLabels.push_back( strLabel );
116 //------------------------------------------------------------------------
117 double ModelShowNPoints::Distance(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
119 return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
122 //------------------------------------------------------------------------
123 int ModelShowNPoints::InsertPoint(int x, int y, int z, std::string label)
125 if(lstPointsX.size()>1)
127 std::vector<int> dTotal;
131 //Calcule distance for each pair of points
132 for(int i = 0; i<(int)lstPointsX.size()-1 ; i++)
134 a = Distance(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
135 b = Distance(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
137 dTotal.push_back (res);
139 //Gets the smallest distance
140 int smallTMP = dTotal[0];
141 for (int j = 0; j < (int) dTotal.size(); j++)
143 if(dTotal[j]<smallTMP)
150 std::vector<int>::iterator it;
151 //Insert the point in the list of points
152 it = lstPointsX.begin();
153 lstPointsX.insert( it+pos, x );
154 it = lstPointsY.begin();
155 lstPointsY.insert( it+pos, y );
156 it = lstPointsZ.begin();
157 lstPointsZ.insert( it+pos, z );
159 std::string strLabel = CleanSpaces( label );
161 std::vector<std::string>::iterator itS;
162 itS = lstLabels.begin();
163 //Insert Label in list of labels
164 lstLabels.insert( itS+pos, strLabel );
173 //------------------------------------------------------------------------
174 void ModelShowNPoints::SavePoints(std::string filename)
176 std::string tmpLabel;
178 ff = fopen( filename.c_str() , "w+" );
181 int i , size = (int) (lstPointsX.size());
182 fprintf(ff,"NumberOfPoints %d \n",size);
183 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
186 for (i=0; i<size; i++)
191 value= mimage->GetScalarComponentAsDouble(x,y,z,0);
192 if (lstLabels[i]!="")
194 tmpLabel=lstLabels[i];
198 fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value , tmpLabel.c_str());
202 printf("ModelShowNPoints::SavePoints ...Error... creating file");
206 //------------------------------------------------------------------------
207 int ModelShowNPoints::ReadPoints(std::string filename)
212 ff = fopen( filename.c_str() , "r+" );
215 fscanf(ff," %s %d",chartmp,&size);
216 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
220 for (i=0; i<size; i++)
222 fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp ); // x,y,z,value,label
223 if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
224 AddPoint(x,y,z, chartmp );
228 printf("ModelShowNPoints::LoadPoints ...Error... reading file");
233 //------------------------------------------------------------------------
234 int ModelShowNPoints::GetNearestPoint()
237 int i, size=(int)(lstPointsX.size());
238 double radioMin=10000000;
239 for ( i=0 ; i<size; i++ )
241 double rx = mReferencePoint[0] - lstPointsX [i];
242 double ry = mReferencePoint[1] - lstPointsY [i];
243 double rz = mReferencePoint[2] - lstPointsZ [i];
244 double radio = rx*rx + ry*ry + rz*rz;
245 if ( radio <= radioMin)
254 //------------------------------------------------------------------------
255 int ModelShowNPoints::GetLstPointsSize()
257 return lstPointsX.size();
260 //------------------------------------------------------------------------
261 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
263 lstPointsX[id] = mReferencePoint[0];
264 lstPointsY[id] = mReferencePoint[1];
265 lstPointsZ[id] = mReferencePoint[2];
269 //------------------------------------------------------------------------
270 int ModelShowNPoints::IdInsidePoint()
273 int i, size=(int)(lstPointsX.size());
277 printf("WidgetShowNPoints::IdInsidePoint image not set\n");
280 mimage->GetSpacing(spc);
281 for ( i=0 ; i<size; i++ )
283 double rx = spc[0]*(mReferencePoint[0] - lstPointsX [i]);
284 double ry = spc[1]*(mReferencePoint[1] - lstPointsY [i]);
285 double rz = spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
286 if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
295 //------------------------------------------------------------------------
296 int ModelShowNPoints::RenamePoint(std::string label)
298 int id=IdInsidePoint();
301 std::string strLabel = CleanSpaces( label );
302 lstLabels[id] = strLabel;
307 //----------------------------------------------------------------------
308 void ModelShowNPoints::ErasePoint(int id)
310 lstPointsX.erase( lstPointsX.begin()+id );
311 lstPointsY.erase( lstPointsY.begin()+id );
312 lstPointsZ.erase( lstPointsZ.begin()+id );
313 lstLabels.erase( lstLabels.begin()+id );
316 //----------------------------------------------------------------------
317 void ModelShowNPoints::SetFirstTime(bool value)
322 //----------------------------------------------------------------------
323 bool ModelShowNPoints::GetFirstTime()