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