]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ModelShowNPoints.cxx
#3485 ShowNPionts for Multiple Groups
[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_(FILE* ff)
186 {
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");
191         int x, y, z;
192         double value;
193         for (i=0; i<size; i++)
194         {
195             x       = lstPointsX[i];
196             y       = lstPointsY[i];
197             z       = lstPointsZ[i];
198             value   = mimage->GetScalarComponentAsDouble(x,y,z,0);
199             if (lstLabels[i]!="")
200             {
201                 tmpLabel=lstLabels[i];
202             } else{
203                 tmpLabel="<_VOID_>";
204             }
205             fprintf(ff,"%d\t%d\t%d\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
206         } // for
207 }
208
209 //------------------------------------------------------------------------
210 void ModelShowNPoints::SavePoints(std::string filename)
211 {
212         FILE* ff = fopen( filename.c_str() , "w+" );
213     if (ff!=NULL)
214     {
215         SavePoints_(ff);
216                 fclose(ff);
217         } else {   // else ff
218                 printf("ModelShowNPoints::SavePoints  ...Error... creating file\n");
219         } //ff
220 }
221
222 //------------------------------------------------------------------------
223 int ModelShowNPoints::ReadPoints_(FILE* ff)
224 {
225     int i,size;
226     char chartmp[256];
227     fscanf(ff," %s %d",chartmp,&size);
228     fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
229
230     float value;
231     int x,y,z;
232     for (i=0; i<size; i++)
233     {
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 );
237     }
238     return size;
239 }
240
241 //------------------------------------------------------------------------
242 int ModelShowNPoints::ReadPoints(std::string filename)
243 {
244     int size=0;
245         FILE *ff = fopen( filename.c_str() , "r+" );
246         if (ff!=NULL)
247         {
248         size = ReadPoints_(ff);
249                 fclose(ff);
250         } else {   // else ff
251                 printf("ModelShowNPoints::LoadPoints  ...Error... reading file");
252         } //ff
253         return size;
254 }
255
256 //------------------------------------------------------------------------
257 int ModelShowNPoints::GetNearestPoint()
258 {
259         int id=-1;
260         int i, size=(int)(lstPointsX.size());
261         double radioMin=10000000;       
262         for ( i=0  ; i<size; i++ )
263         {
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)
269                 {
270                         radioMin=radio;
271                         id=i;
272                 }       // if
273         } // for                        
274         return id;
275 }
276
277 //------------------------------------------------------------------------
278 int ModelShowNPoints::GetLstPointsSize()
279 {
280         return lstPointsX.size();
281 }
282
283 //------------------------------------------------------------------------
284 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
285 {
286         lstPointsX[id] = mReferencePoint[0];
287         lstPointsY[id] = mReferencePoint[1];
288         lstPointsZ[id] = mReferencePoint[2];            
289 }
290
291
292 //------------------------------------------------------------------------      
293 int ModelShowNPoints::IdInsidePoint()
294 {
295         int id=-1;
296         int i, size=(int)( lstPointsX.size() );
297     double spc[3];
298         if(mimage ==NULL)
299         {
300                 printf("WidgetShowNPoints::IdInsidePoint  image not set\n");
301                 return -1;
302         }else{
303                 mimage->GetSpacing(spc);
304                 for ( i=0  ; i<size; i++ )
305                 {
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)
310                         {
311                                 id=i;
312                         }       // if
313                 } // for
314                 return id;
315         } // if
316 }
317
318 //------------------------------------------------------------------------      
319 int ModelShowNPoints::RenamePoint(std::string label)
320 {
321         int id=IdInsidePoint();
322         if (id>=0)
323         {
324                 std::string strLabel = CleanSpaces( label );
325                 lstLabels[id] = strLabel;
326         }
327         return id;
328 }
329
330 //----------------------------------------------------------------------
331 void ModelShowNPoints::ErasePoint(int id)
332 {
333         lstPointsX.erase( lstPointsX.begin()+id );
334         lstPointsY.erase( lstPointsY.begin()+id );
335         lstPointsZ.erase( lstPointsZ.begin()+id );
336         lstLabels.erase( lstLabels.begin()+id );
337 }
338
339 //----------------------------------------------------------------------
340 void ModelShowNPoints::SetFirstTime(bool value)
341 {
342         firsttime=value;
343 }
344
345 //----------------------------------------------------------------------
346 bool ModelShowNPoints::GetFirstTime()
347 {
348         return firsttime;
349 }
350
351