]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ModelShowNPoints.cxx
7468bfdc2bef2de9d43921ef2da7e89ff5ad17b8
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ModelShowNPoints.cxx
1
2 #include <cmath>
3
4 #include "ModelShowNPoints.h"
5
6
7 //----------------------------------------------------------------------
8 ModelShowNPoints::ModelShowNPoints()
9 {
10         firsttime=true;
11 }
12 //----------------------------------------------------------------------
13
14 ModelShowNPoints::~ModelShowNPoints()
15 {
16 }
17
18 //------------------------------------------------------------------------
19 void ModelShowNPoints::SetRadio(double radio)
20 {
21         mradio =  radio;
22 }
23
24 //------------------------------------------------------------------------
25 double ModelShowNPoints::GetRadio()
26 {
27         return mradio;
28 }
29
30 //------------------------------------------------------------------------
31 std::vector<int> ModelShowNPoints::GetLstPointsX()
32 {
33         return lstPointsX;
34 }
35
36 //------------------------------------------------------------------------
37 std::vector<int> ModelShowNPoints::GetLstPointsY()
38 {
39         return lstPointsY;
40 }
41
42 //------------------------------------------------------------------------
43 std::vector<int> ModelShowNPoints::GetLstPointsZ()
44 {
45         return lstPointsZ;
46 }
47
48 //------------------------------------------------------------------------
49 std::vector<std::string> ModelShowNPoints::GetLstLabels()
50 {
51 //      return lstLabels;
52         std::vector<std::string> tmpLst;
53         int i   ,size=lstLabels.size();
54         std::string tmp;
55         for (i=0; i<size; i++) 
56         {
57                 tmp=lstLabels[i];
58                 if (tmp.empty()==true) 
59                 {
60                         tmp="VOID";     
61                 }
62                 tmpLst.push_back( tmp );
63         } // for i
64         return tmpLst;
65 }
66
67 //------------------------------------------------------------------------
68 void ModelShowNPoints::SetReferencePoint(std::vector<int> ppoint)
69 {
70         mReferencePoint = ppoint;
71 }
72
73 //------------------------------------------------------------------------
74 std::vector<int> ModelShowNPoints::GetReferencePoint()
75 {
76         return mReferencePoint;
77 }
78
79 //------------------------------------------------------------------------
80 void ModelShowNPoints::SetImage(vtkImageData *image)
81 {
82         this->mimage=image;
83 }
84
85
86 //------------------------------------------------------------------------
87 std::string ModelShowNPoints::CleanSpaces(std::string ss)
88 {
89         int i;
90         while( (i=ss.find(32))>=0 )
91         {
92                 ss.replace(i,1,"_");
93         }
94         return ss;
95 }
96
97 //------------------------------------------------------------------------
98 void ModelShowNPoints::GetIdPoint(int id, int *x, int *y, int *z)
99 {
100         *x=lstPointsX[id];
101         *y=lstPointsY[id];
102         *z=lstPointsZ[id];
103 }
104
105 //------------------------------------------------------------------------
106 std::string ModelShowNPoints::GetIdLabel(int id)
107 {
108         return lstLabels[id];
109 }
110
111 //------------------------------------------------------------------------
112 vtkImageData *ModelShowNPoints::GetImage()
113 {
114         return mimage;
115 }
116
117 //------------------------------------------------------------------------
118 void ModelShowNPoints::AddPoint(int x, int y, int z, std::string label)
119 {
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 );
125 }
126
127 //------------------------------------------------------------------------
128 double ModelShowNPoints::Distance(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
129 {
130     return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
131 }
132
133 //------------------------------------------------------------------------
134 int ModelShowNPoints::InsertPoint(int x, int y, int z, std::string label)
135 {
136         if(lstPointsX.size()>1)
137         {
138                 std::vector<int> dTotal;
139                 int pos = 1;
140                 int a,b,res;
141                 
142                 //Calcule distance for each pair of points
143                 for(int i = 0; i<(int)lstPointsX.size()-1 ; i++)
144                 {
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]);
147                                 res = a + b;            
148                                 dTotal.push_back (res);         
149                 }
150                 //Gets the smallest distance 
151                 int smallTMP = dTotal[0];
152                 for (int j = 0; j < (int) dTotal.size(); j++)
153                 {
154                           if(dTotal[j]<smallTMP)
155                           {
156                                           smallTMP=dTotal[j];
157                                                 pos = j+1;
158                           }
159                 }
160                 
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 );
169
170                 std::string strLabel = CleanSpaces(  label );
171         
172                 std::vector<std::string>::iterator itS;
173                 itS = lstLabels.begin();
174                 //Insert Label in list of labels
175                 lstLabels.insert( itS+pos, strLabel );
176                 return pos;
177         } else {
178                 return -1;
179         }// if size lst X
180
181 }
182
183
184 //------------------------------------------------------------------------
185 void ModelShowNPoints::SavePoints(std::string filename)
186 {
187         std::string tmpLabel;
188         FILE *ff;
189         ff = fopen( filename.c_str() , "w+" );
190         if (ff!=NULL)
191         {
192                 int i , size = (int) (lstPointsX.size());
193                 fprintf(ff,"NumberOfPoints %d \n",size);
194                 fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
195                 int x, y, z;
196                 double value;
197                 for (i=0; i<size; i++)
198                 {
199                         x=lstPointsX[i];
200                         y=lstPointsY[i];
201                         z=lstPointsZ[i];
202                         value= mimage->GetScalarComponentAsDouble(x,y,z,0);
203                         if (lstLabels[i]!="") 
204                         {
205                                 tmpLabel=lstLabels[i];
206                         } else{
207                                 tmpLabel="<_VOID_>";
208                         }
209                         fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
210                 } // for
211                 fclose(ff);
212         } else {   // else ff
213                 printf("ModelShowNPoints::SavePoints  ...Error... creating file");
214         } //ff
215 }
216
217 //------------------------------------------------------------------------
218 int ModelShowNPoints::ReadPoints(std::string filename)
219 {
220         int i,size;
221         char chartmp[256];
222         FILE *ff;
223         ff = fopen( filename.c_str() , "r+" );
224         if (ff!=NULL)
225         {
226                 fscanf(ff," %s %d",chartmp,&size);
227                 fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
228
229                 float value;
230                 int x,y,z;
231                 for (i=0; i<size; i++)
232                 {
233                         fscanf(ff,"%d%d%d%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
234                         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
235                         AddPoint(x,y,z, chartmp );
236                 }
237                 fclose(ff);
238         } else {   // else ff
239                 printf("ModelShowNPoints::LoadPoints  ...Error... reading file");
240         } //ff
241         return size;
242 }
243
244 //------------------------------------------------------------------------
245 int ModelShowNPoints::GetNearestPoint()
246 {
247         int id=-1;
248         int i, size=(int)(lstPointsX.size());
249         double radioMin=10000000;       
250         for ( i=0  ; i<size; i++ )
251         {
252                 double rx =  mReferencePoint[0] - lstPointsX [i];
253                 double ry =  mReferencePoint[1] - lstPointsY [i];
254                 double rz =  mReferencePoint[2] - lstPointsZ [i];
255                 double radio = rx*rx + ry*ry + rz*rz;
256                 if ( radio <= radioMin)
257                 {
258                         radioMin=radio;
259                         id=i;
260                 }       // if
261         } // for                        
262         return id;
263 }
264
265 //------------------------------------------------------------------------
266 int ModelShowNPoints::GetLstPointsSize()
267 {
268         return lstPointsX.size();
269 }
270
271 //------------------------------------------------------------------------
272 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
273 {
274         lstPointsX[id] = mReferencePoint[0];
275         lstPointsY[id] = mReferencePoint[1];
276         lstPointsZ[id] = mReferencePoint[2];            
277 }
278
279
280 //------------------------------------------------------------------------      
281 int ModelShowNPoints::IdInsidePoint()
282 {
283         int id=-1;
284         int i, size=(int)(lstPointsX.size());
285         double spc[3];
286         if(mimage ==NULL)
287         {
288                 printf("WidgetShowNPoints::IdInsidePoint  image not set\n");
289                 return -1;
290         }else{
291                 mimage->GetSpacing(spc);
292                 for ( i=0  ; i<size; i++ )
293                 {
294                         double rx =  spc[0]*(mReferencePoint[0] - lstPointsX [i]);
295                         double ry =  spc[1]*(mReferencePoint[1] - lstPointsY [i]);
296                         double rz =  spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
297                         if ( rx*rx + ry*ry + rz*rz <= mradio*mradio)
298                         {
299                                 id=i;
300                         }       // if
301                 } // for
302                 return id;
303         } // if
304 }
305
306 //------------------------------------------------------------------------      
307 int ModelShowNPoints::RenamePoint(std::string label)
308 {
309         int id=IdInsidePoint();
310         if (id>=0)
311         {
312                 std::string strLabel = CleanSpaces( label );
313                 lstLabels[id] = strLabel;
314         }
315         return id;
316 }
317
318 //----------------------------------------------------------------------
319 void ModelShowNPoints::ErasePoint(int id)
320 {
321         lstPointsX.erase( lstPointsX.begin()+id );
322         lstPointsY.erase( lstPointsY.begin()+id );
323         lstPointsZ.erase( lstPointsZ.begin()+id );
324         lstLabels.erase( lstLabels.begin()+id );
325 }
326
327 //----------------------------------------------------------------------
328 void ModelShowNPoints::SetFirstTime(bool value)
329 {
330         firsttime=value;
331 }
332
333 //----------------------------------------------------------------------
334 bool ModelShowNPoints::GetFirstTime()
335 {
336         return firsttime;
337 }
338
339