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