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