]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ModelShowNPoints.cxx
5bbc22337e12eb8b2ef9b1d919827aaa0f46cd32
[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 }
136
137 //------------------------------------------------------------------------
138 double ModelShowNPoints::DistanceSQ(double dX0, double dY0, double dZ0, double dX1, double dY1, double dZ1)//CFT
139 {
140 //    return sqrt((dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0));
141         return (dX1 - dX0)*(dX1 - dX0) + (dY1 - dY0)*(dY1 - dY0) + (dZ1 - dZ0)*(dZ1 - dZ0);
142 }
143
144 //------------------------------------------------------------------------
145 int ModelShowNPoints::InsertPoint(double x, double y, double z, std::string label)
146 {
147         if(lstPointsX.size()>1)
148         {
149                 std::vector<double> dTotal;
150                 int pos = 1;
151                 double a,b,c,res;
152         int i,j;
153                 //Calcule distance for each pair of points
154                 for(i = 0; i<(int)lstPointsX.size()-1 ; i++)
155                 {
156                                 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
157                                 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
158                                 res = a + b;            
159                                 dTotal.push_back (res);         
160                 }
161                 //Gets the smallest distance 
162                 double smallTMP = dTotal[0];
163                 for (j = 0; j < (int) dTotal.size(); j++)
164                 {
165                           if(dTotal[j]<smallTMP)
166                           {
167                                           smallTMP=dTotal[j];
168                                                 pos = j+1;
169                           }
170                 }
171                 
172         
173         //  *************** Open contour case Start *******************
174         if (lstPointsX.size()==2)
175         {
176             double cx,cy,cz,r1,r2;
177             i  = 0;
178             cx = (lstPointsX[i]+lstPointsX[i+1]) / 2;
179             cy = (lstPointsY[i]+lstPointsY[i+1]) / 2;
180             cz = (lstPointsZ[i]+lstPointsZ[i+1]) / 2;
181             r1 = DistanceSQ( cx,cy,cz,x,y,z );
182             r2 = DistanceSQ( cx,cy,cz,lstPointsX[i], lstPointsY[i], lstPointsZ[i] );
183             if (r1<r2) // inside circle
184             {
185                 pos=1;
186             } else {   // outside circle
187                 if (a<b)  // befor first point
188                 {
189                     pos=0;
190                 } else {  // after second point
191                 pos=2;
192                 }
193             }
194         }
195         if (lstPointsX.size()>2)
196         {
197             double r3;
198             if ( pos==1)  // first point of the list
199             {
200                 i = 0;
201                 a = DistanceSQ(x, y, z, lstPointsX[i], lstPointsY[i], lstPointsZ[i]);
202                 b = DistanceSQ(x, y, z, lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1]);
203                 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
204                 if (b>r3) // outside circle
205                 {
206                     pos = 0;
207                 }
208             }
209             if (pos==lstPointsX.size()-1 )  // last point of the list
210             {
211                 i = lstPointsX.size()-2;
212                 r3 = DistanceSQ( lstPointsX[i], lstPointsY[i], lstPointsZ[i],lstPointsX[i+1], lstPointsY[i+1], lstPointsZ[i+1] );
213                 if (a>r3) // outside circle
214                 {
215                     pos = pos+1;
216                 }
217             }
218         }
219         //  *************** Open contour case End *******************
220
221         
222                 std::vector<double>::iterator it;
223                 //Insert the point in the list of points
224                 it = lstPointsX.begin();
225                 lstPointsX.insert( it+pos, x );
226                 it = lstPointsY.begin();
227                 lstPointsY.insert( it+pos, y );
228                 it = lstPointsZ.begin();
229                 lstPointsZ.insert( it+pos, z );
230
231                 std::string strLabel = CleanSpaces(  label );
232         
233                 std::vector<std::string>::iterator itS;
234                 itS = lstLabels.begin();
235                 //Insert Label in list of labels
236                 lstLabels.insert( itS+pos, strLabel );
237                 return pos;
238         } else {
239                 return -1;
240         }// if size lst X
241
242 }
243
244
245 //------------------------------------------------------------------------
246 void ModelShowNPoints::SavePoints_(FILE* ff)
247 {
248         std::string tmpLabel;
249         int i , size = (int) (lstPointsX.size());
250         fprintf(ff,"NumberOfPoints %d \n",size);
251         fprintf(ff," X\tY\tZ\tvalue\tLabel\n");
252         double x, y, z;
253         double value;
254         for (i=0; i<size; i++)
255         {
256             x       = lstPointsX[i];
257             y       = lstPointsY[i];
258             z       = lstPointsZ[i];
259             value   = mimage->GetScalarComponentAsDouble(std::round(x),std::round(y),std::round(z),0);
260             if (lstLabels[i]!="")
261             {
262                 tmpLabel=lstLabels[i];
263             } else{
264                 tmpLabel="<_VOID_>";
265             }
266             fprintf(ff,"%f\t%f\t%f\t%f\t%s\n", x , y , z , value  , tmpLabel.c_str());
267         } // for
268 }
269
270 //------------------------------------------------------------------------
271 void ModelShowNPoints::SavePoints(std::string filename)
272 {
273         FILE* ff = fopen( filename.c_str() , "w+" );
274     if (ff!=NULL)
275     {
276         SavePoints_(ff);
277                 fclose(ff);
278         } else {   // else ff
279                 printf("ModelShowNPoints::SavePoints  ...Error... creating file\n");
280         } //ff
281 }
282
283 //------------------------------------------------------------------------
284 int ModelShowNPoints::ReadPoints_(FILE* ff)
285 {
286     int i,size;
287     char chartmp[256];
288     fscanf(ff," %s %d",chartmp,&size);
289     fscanf(ff," %s %s %s %s %s",chartmp, chartmp,chartmp,chartmp,chartmp );
290
291     float value;
292     double x,y,z;
293     for (i=0; i<size; i++)
294     {
295         fscanf(ff,"%lf%lf%lf%f%s",&x,&y,&z,&value,chartmp );  // x,y,z,value,label
296         if (strcmp(chartmp,"<_VOID_>")==0) { strcpy(chartmp,""); }
297         AddPoint(x,y,z, chartmp );
298     }
299     return size;
300 }
301
302 //------------------------------------------------------------------------
303 int ModelShowNPoints::ReadPoints(std::string filename)
304 {
305     int size=0;
306         FILE *ff = fopen( filename.c_str() , "r+" );
307         if (ff!=NULL)
308         {
309         size = ReadPoints_(ff);
310                 fclose(ff);
311         } else {   // else ff
312                 printf("ModelShowNPoints::LoadPoints  ...Error... reading file");
313         } //ff
314         return size;
315 }
316
317 //------------------------------------------------------------------------
318 int ModelShowNPoints::GetNearestPoint()
319 {
320         int id=-1;
321         int i, size=(int)(lstPointsX.size());
322         double radioMin=10000000;       
323         for ( i=0  ; i<size; i++ )
324         {
325                 double rx =  mReferencePoint[0] - lstPointsX [i];
326                 double ry =  mReferencePoint[1] - lstPointsY [i];
327                 double rz =  mReferencePoint[2] - lstPointsZ [i];
328                 double radio = rx*rx + ry*ry + rz*rz;
329                 if ( radio <= radioMin)
330                 {
331                         radioMin=radio;
332                         id=i;
333                 }       // if
334         } // for                        
335         return id;
336 }
337
338 //------------------------------------------------------------------------
339 int ModelShowNPoints::GetLstPointsSize()
340 {
341         return lstPointsX.size();
342 }
343
344 //------------------------------------------------------------------------
345 void ModelShowNPoints::SetPointId_mReferencePoint(int id)
346 {
347         lstPointsX[id] = mReferencePoint[0];
348         lstPointsY[id] = mReferencePoint[1];
349         lstPointsZ[id] = mReferencePoint[2];            
350 }
351
352
353 //------------------------------------------------------------------------      
354 int ModelShowNPoints::IdInsidePoint()
355 {
356         int id=-1;
357         int i, size=(int)( lstPointsX.size() );
358     double spc[3];
359     double radio2=(mradio+1)*(mradio+1);
360         if(mimage ==NULL)
361         {
362                 printf("WidgetShowNPoints::IdInsidePoint  image not set\n");
363                 return -1;
364         }else{
365                 mimage->GetSpacing(spc);
366                 for ( i=0  ; i<size; i++ )
367                 {
368                         double rx =  spc[0]*(mReferencePoint[0] - lstPointsX [i]);
369                         double ry =  spc[1]*(mReferencePoint[1] - lstPointsY [i]);
370                         double rz =  spc[2]*(mReferencePoint[2] - lstPointsZ [i]);
371                         if ( rx*rx + ry*ry + rz*rz <= radio2)
372                         {
373                                 id=i;
374                         }       // if
375                 } // for
376                 return id;
377         } // if
378 }
379 //------------------------------------------------------------------------
380 void ModelShowNPoints::SetPointById(int id, std::vector<double> point)
381 {
382         if(id >= 0 && id < lstPointsX.size()){
383                 lstPointsX[id] = point[0];
384                 lstPointsY[id] = point[1];
385                 lstPointsZ[id] = point[2];
386         }
387         else{
388                 printf("WidgetShowNPoints::SetPointById  Invalid pointid, id is outside range\n");
389         }
390 }
391 //------------------------------------------------------------------------      
392 int ModelShowNPoints::RenamePoint(std::string label)
393 {
394         int id=IdInsidePoint();
395         if (id>=0)
396         {
397                 std::string strLabel = CleanSpaces( label );
398                 lstLabels[id] = strLabel;
399         }
400         return id;
401 }
402
403 //----------------------------------------------------------------------
404 void ModelShowNPoints::ErasePoint(int id)
405 {
406         lstPointsX.erase( lstPointsX.begin()+id );
407         lstPointsY.erase( lstPointsY.begin()+id );
408         lstPointsZ.erase( lstPointsZ.begin()+id );
409         lstLabels.erase( lstLabels.begin()+id );
410 }
411
412 //----------------------------------------------------------------------
413 void ModelShowNPoints::SetFirstTime(bool value)
414 {
415         firsttime=value;
416 }
417
418 //----------------------------------------------------------------------
419 bool ModelShowNPoints::GetFirstTime()
420 {
421         return firsttime;
422 }
423
424 //----------------------------------------------------------------------
425 void ModelShowNPoints::InversLstPoints()
426 {
427     int i,size      = lstPointsX.size();
428     int i2,size2    = size/2;
429     double dTmp;
430     std::string sTmp;
431     for (i=0 ;  i<size2 ; i++)
432     {
433         i2 = size-1-i;
434         dTmp = lstPointsX[i];   lstPointsX[i] = lstPointsX[i2];     lstPointsX[i2] = dTmp;
435         dTmp = lstPointsY[i];   lstPointsY[i] = lstPointsY[i2];     lstPointsY[i2] = dTmp;
436         dTmp = lstPointsZ[i];   lstPointsZ[i] = lstPointsZ[i2];     lstPointsZ[i2] = dTmp;
437         sTmp = lstLabels[i];    lstLabels[i]  = lstLabels[i2];      lstLabels[i2]  = sTmp;
438     } // for i
439 }
440
441 //----------------------------------------------------------------------
442 int ModelShowNPoints::GetIdCurrentPoint()
443 {
444         return idCurrentPoint;
445 }
446
447 //----------------------------------------------------------------------
448 void ModelShowNPoints::SetIdCurrentPoint(int idPoint)
449 {
450     if (GetLstPointsSize()==0)
451     {
452         idCurrentPoint=-1;
453     } else {
454         idCurrentPoint=idPoint;
455         if (idCurrentPoint<0) {idCurrentPoint=0;}
456         if (idCurrentPoint>=GetLstPointsSize() ) {idCurrentPoint=GetLstPointsSize()-1; }
457     } // if Size==0
458 }