]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ModelShowNPoints.cxx
2f9db6267173c2f6f9a8c7c9bb3313a175e0e7c7
[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         idCurrentPoint  = -1;
12 }
13 //----------------------------------------------------------------------
14
15 ModelShowNPoints::~ModelShowNPoints()
16 {
17 }
18
19 //------------------------------------------------------------------------
20 void ModelShowNPoints::SetRadio(double radio)
21 {
22         mradio =  radio;
23 }
24
25 //------------------------------------------------------------------------
26 double ModelShowNPoints::GetRadio()
27 {
28         return mradio;
29 }
30
31 //------------------------------------------------------------------------
32 std::vector<double> ModelShowNPoints::GetLstPointsX()
33 {
34         return lstPointsX;
35 }
36
37 //------------------------------------------------------------------------
38 std::vector<double> ModelShowNPoints::GetLstPointsY()
39 {
40         return lstPointsY;
41 }
42
43 //------------------------------------------------------------------------
44 std::vector<double> ModelShowNPoints::GetLstPointsZ()
45 {
46         return lstPointsZ;
47 }
48
49 //------------------------------------------------------------------------
50 std::vector<std::string> ModelShowNPoints::GetLstLabels()
51 {
52 //      return lstLabels;
53         std::vector<std::string> tmpLst;
54         int i   ,size=lstLabels.size();
55         std::string tmp;
56         for (i=0; i<size; i++) 
57         {
58                 tmp=lstLabels[i];
59                 if (tmp.empty()==true) 
60                 {
61                         tmp="VOID";     
62                 }
63                 tmpLst.push_back( tmp );
64         } // for i
65         return tmpLst;
66 }
67
68 //------------------------------------------------------------------------
69 void ModelShowNPoints::SetReferencePoint(std::vector<double> ppoint)
70 {
71         mReferencePoint = ppoint;
72         if(lstPointsX.size() > 0){
73                 int idPoint = IdInsidePoint();
74                 if(idPoint == -1 && idCurrentPoint >= 0 && idCurrentPoint < lstPointsX.size()){
75             idCurrentPoint = idCurrentPoint;
76                 }
77                 else{
78             idCurrentPoint = idPoint;
79                 }
80         }
81 }
82
83 //------------------------------------------------------------------------
84 std::vector<double> ModelShowNPoints::GetReferencePoint()
85 {
86         return mReferencePoint;
87 }
88
89 //------------------------------------------------------------------------
90 void ModelShowNPoints::SetImage(vtkImageData *image)
91 {
92         this->mimage=image;
93 }
94
95
96 //------------------------------------------------------------------------
97 std::string ModelShowNPoints::CleanSpaces(std::string ss)
98 {
99         int i;
100         while( (i=ss.find(32))>=0 )
101         {
102                 ss.replace(i,1,"_");
103         }
104         return ss;
105 }
106
107 //------------------------------------------------------------------------
108 void ModelShowNPoints::GetIdPoint(int id, double *x, double *y, double *z)
109 {
110         *x = lstPointsX[id];
111         *y = lstPointsY[id];
112         *z = lstPointsZ[id];
113 }
114
115 //------------------------------------------------------------------------
116 std::string ModelShowNPoints::GetIdLabel(int id)
117 {
118         return lstLabels[id];
119 }
120
121 //------------------------------------------------------------------------
122 vtkImageData *ModelShowNPoints::GetImage()
123 {
124         return mimage;
125 }
126
127 //------------------------------------------------------------------------
128 void ModelShowNPoints::AddPoint(double x, double y, double z, std::string label)
129 {
130         lstPointsX.push_back( x );
131         lstPointsY.push_back( y );
132         lstPointsZ.push_back( z );
133         std::string strLabel    = CleanSpaces(  label );
134         lstLabels.push_back( strLabel );
135     idCurrentPoint          =   lstPointsX.size()-1;
136 }
137
138 //------------------------------------------------------------------------
139 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
140 {
141 //    return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
142         return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
143 }
144
145 //------------------------------------------------------------------------
146 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
147 {
148         if(lstPointsX.size()>1)
149         {
150                 std::vector<double> dTotal;
151                 int pos = 1;
152                 double a,b,c,res;
153         int i,j;
154                 //Calcule distance for each pair of points
155                 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
156                 {
157                                 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
158                                 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
159                                 res = a + b;            
160                                 dTotal.push_back (res);         
161                 }
162                 //Gets the smallest distance 
163                 double smallTMP = dTotal[0];
164                 for (j = 0; j < (int) dTotal.size(); j++)
165                 {
166                           if(dTotal[j]<smallTMP)
167                           {
168                                           smallTMP=dTotal[j];
169                                                 pos = j+1;
170                           } // if
171                 } // for j
172                 
173         
174         //  *************** Open contour case Start *******************
175         if (lstPointsX.size()==2)
176         {
177             double cx,cy,cz,r1,r2;
178             i  = 0;
179             cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
180             cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
181             cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
182             r1 = DistanceSQ( cx,cy,cz,x,y,z );
183             r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
184             if (r1<r2) // inside circle
185             {
186                 pos=1;
187             } else {   // outside circle
188                 if (a<b)  // befor first point
189                 {
190                     pos=0;
191                 } else {  // after second point
192                 pos=2;
193                 } // if a<b
194             } // if r1<r2
195         } // lstPointsX.size == 2
196         
197         if (lstPointsX.size()>2)
198         {
199             double r3;
200             if ( pos==1)  // first point of the list
201             {
202                 i = 0;
203                 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
204                 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
205                 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
206                 if (b>r3) // outside circle
207                 {
208                     pos = 0;
209                 } // if b>r3
210             } // if pos
211             if (pos==lstPointsX.size()-1 )  // last point of the list
212             {
213                 i = lstPointsX.size()-2;
214                 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
215                 if (a>r3) // outside circle
216                 {
217                     pos = pos+1;
218                 } // if a>r3
219             } // if pos
220         } // if lstPointsX.size
221         //  *************** Open contour case End *******************
222
223         
224                 std::vector<double>::iterator it;
225                 //Insert the point in the list of points
226                 it = lstPointsX.begin();
227                 lstPointsX.insert( it+pos, x );
228                 it = lstPointsY.begin();
229                 lstPointsY.insert( it+pos, y );
230                 it = lstPointsZ.begin();
231                 lstPointsZ.insert( it+pos, z );
232
233                 std::string strLabel = CleanSpaces(  label );
234         
235                 std::vector<std::string>::iterator itS;
236                 itS = lstLabels.begin();
237                 //Insert Label in list of labels
238                 lstLabels.insert( itS+pos, strLabel );
239         idCurrentPoint=pos;
240         return pos;
241         } else {
242                 return -1;
243         }// if size lst X
244
245 }
246
247
248 //------------------------------------------------------------------------
249 void ModelShowNPoints::SavePoints_(FILE* ff)
250 {
251         std::string tmpLabel;
252         int i , size = (int) (lstPointsX.size());
253         fprintf(ff,"NumberOfPoints %d \n",size);
254         fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
255         double x, y, z;
256         double value;
257         for (i=0; i<size; i++)
258         {
259             x       = lstPointsX[i];
260             y       = lstPointsY[i];
261             z       = lstPointsZ[i];
262             value   = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
263             if (lstLabels[i]!="")
264             {
265                 tmpLabel=lstLabels[i];
266             } else{
267                 tmpLabel="<_VOID_>";
268             }
269             fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
270         } // for
271 }
272
273 //------------------------------------------------------------------------
274 void ModelShowNPoints::SavePoints(std::string filename)
275 {
276         FILE* ff = fopen( filename.c_str() , "w+" );
277     if (ff!=NULL)
278     {
279         SavePoints_(ff);
280                 fclose(ff);
281         } else {   // else ff
282                 printf("ModelShowNPoints::SavePoints  ...Error... creating file\n");
283         } //ff
284 }
285
286 //------------------------------------------------------------------------
287 int ModelShowNPoints::ReadPoints_(FILE* ff)
288 {
289     int i,size;
290     char chartmp[256];
291     fscanf(ff," %s %d",chartmp,&size);
292     fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
293
294     float value;
295     double x,y,z;
296     for (i=0; i<size; i++)
297     {
298         fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
299         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
300         AddPoint(x,y,z, chartmp );
301     }
302     return size;
303 }
304
305 //------------------------------------------------------------------------
306 int ModelShowNPoints::ReadPoints(std::string filename)
307 {
308     int size=0;
309         FILE *ff = fopen( filename.c_str() , "r+" );
310         if (ff!=NULL)
311         {
312         size = ReadPoints_(ff);
313                 fclose(ff);
314         } else {   // else ff
315                 printf("ModelShowNPoints::LoadPoints  ...Error... reading file");
316         } //ff
317         return size;
318 }
319
320 //------------------------------------------------------------------------
321 int ModelShowNPoints::GetNearestPoint()
322 {
323         int id=-1;
324         int i, size=(int)(lstPointsX.size());
325         double radioMin=10000000;       
326         for ( i=0  ; i<size; i++ )
327         {
328                 double rx =  mReferencePoint[0] - lstPointsX [i];
329                 double ry =  mReferencePoint[1] - lstPointsY [i];
330                 double rz =  mReferencePoint[2] - lstPointsZ [i];
331                 double radio = rx*rx + ry*ry + rz*rz;
332                 if ( radio <= radioMin)
333                 {
334                         radioMin=radio;
335                         id=i;
336                 }       // if
337         } // for                        
338         return id;
339 }
340
341 //------------------------------------------------------------------------
342 int ModelShowNPoints::GetLstPointsSize()
343 {
344         return lstPointsX.size();
345 }
346
347 //------------------------------------------------------------------------
348 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
349 {
350         lstPointsX[id] = mReferencePoint[0];
351         lstPointsY[id] = mReferencePoint[1];
352         lstPointsZ[id] = mReferencePoint[2];            
353 }
354
355
356 //------------------------------------------------------------------------      
357 int ModelShowNPoints::IdInsidePoint()
358 {
359         int id=-1;
360         int i, size=(int)( lstPointsX.size() );
361     double spc[3];
362     double radio2=(mradio+1)*(mradio+1);
363         if(mimage ==NULL)
364         {
365                 printf("WidgetShowNPoints::IdInsidePoint  image not set\n");
366                 return -1;
367         }else{
368                 mimage->GetSpacing(spc);
369                 for ( i=0  ; i<size; i++ )
370                 {
371                         double rx =  spc[0]*(mReferencePoint[0] - lstPointsX [i]);
372                         double ry =  spc[1]*(mReferencePoint[1] - lstPointsY [i]);
373                         double rz =  spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
374                         if ( rx*rx + ry*ry + rz*rz <= radio2)
375                         {
376                                 id=i;
377                         }       // if
378                 } // for
379                 return id;
380         } // if
381 }
382 //------------------------------------------------------------------------
383 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
384 {
385         if(id >= 0 && id < lstPointsX.size()){
386                 lstPointsX[id] = point[0];
387                 lstPointsY[id] = point[1];
388                 lstPointsZ[id] = point[2];
389         }
390         else{
391                 printf("WidgetShowNPoints::SetPointById  Invalid pointid, id is outside range\n");
392         }
393 }
394 //------------------------------------------------------------------------      
395 int ModelShowNPoints::RenamePoint(std::string label)
396 {
397         int id=IdInsidePoint();
398         if (id>=0)
399         {
400                 std::string strLabel = CleanSpaces( label );
401                 lstLabels[id] = strLabel;
402         }
403         return id;
404 }
405
406 //----------------------------------------------------------------------
407 void ModelShowNPoints::ErasePoint(int id)
408 {
409         lstPointsX.erase( lstPointsX.begin()+id );
410         lstPointsY.erase( lstPointsY.begin()+id );
411         lstPointsZ.erase( lstPointsZ.begin()+id );
412         lstLabels.erase( lstLabels.begin()+id );
413     SetIdCurrentPoint(id);
414 }
415
416 //----------------------------------------------------------------------
417 void ModelShowNPoints::SetFirstTime(bool value)
418 {
419         firsttime=value;
420 }
421
422 //----------------------------------------------------------------------
423 bool ModelShowNPoints::GetFirstTime()
424 {
425         return firsttime;
426 }
427
428 //----------------------------------------------------------------------
429 void ModelShowNPoints::InversLstPoints()
430 {
431     int i,size      = lstPointsX.size();
432     int i2,size2    = size/2;
433     double dTmp;
434     std::string sTmp;
435     for (i=0 ;  i<size2 ; i++)
436     {
437         i2 = size-1-i;
438         dTmp = lstPointsX[i];   lstPointsX[i] = lstPointsX[i2];     lstPointsX[i2] = dTmp;
439         dTmp = lstPointsY[i];   lstPointsY[i] = lstPointsY[i2];     lstPointsY[i2] = dTmp;
440         dTmp = lstPointsZ[i];   lstPointsZ[i] = lstPointsZ[i2];     lstPointsZ[i2] = dTmp;
441         sTmp = lstLabels[i];    lstLabels[i]  = lstLabels[i2];      lstLabels[i2]  = sTmp;
442     } // for i
443 }
444
445 //----------------------------------------------------------------------
446 int ModelShowNPoints::GetIdCurrentPoint()
447 {
448         return idCurrentPoint;
449 }
450
451 //----------------------------------------------------------------------
452 void ModelShowNPoints::SetIdCurrentPoint(int idPoint)
453 {
454     if (GetLstPointsSize()==0)
455     {
456         idCurrentPoint=-1;
457     } else {
458         idCurrentPoint=idPoint;
459         if (idCurrentPoint<0) {idCurrentPoint=0;}
460         if (idCurrentPoint>=GetLstPointsSize() ) {idCurrentPoint=GetLstPointsSize()-1; }
461     } // if Size==0
462 }