]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramDialog.cxx
BUG view MPR
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / HistogramDialog.cxx
1 #include "HistogramDialog.h"
2 #include <vector>
3
4 // IMPLEMENT_CLASS(HistogramDialog, wxDialog)
5
6
7 //id for events
8 enum {
9     ID_SAVE  = 108,
10         ID_LOAD,
11         ID_REFRESH
12 };
13
14 //-------------------------
15 //Constructor
16 //-------------------------
17 HistogramDialog::HistogramDialog(wxWindow *parent,wxString title,vtkImageData* imageData,int type)
18 :wxDialog(parent,-1,title,wxDefaultPosition,wxDefaultSize,wxRESIZE_BORDER|wxDEFAULT_DIALOG_STYLE   ,_T("dialogBox"))
19 {
20
21         _ctfun=NULL;
22         _tfun=NULL;
23
24         SetBackgroundColour(wxColour(255,255,255));
25         /*
26         Pointers
27         */
28         wxvtkmpr3Dview=NULL;
29         wxvtkclipping3Dview=NULL;
30         
31         /*
32          Histogram
33         */
34         
35         histogramW=new HistogramWidget(this, -1, wxPoint(0,0), wxSize(400,400),wxNO_BORDER ,imageData,type);
36         
37         refreshed=false;
38
39         /*
40         Buttons
41         */
42         okBtn = new wxButton(this,wxID_OK ,_T("OK"));
43         cancelBtn = new wxButton(this,wxID_CANCEL,_T("Cancel"));
44         
45         saveDataBtn = new wxButton(this,ID_SAVE,_T("Save"));
46         loadDataBtn = new wxButton(this,ID_LOAD,_T("Load"));
47         refreshBtn = new wxButton(this,ID_REFRESH,_T("Refresh"));
48         
49         Connect(saveDataBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnSaveData       );
50         Connect(loadDataBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnLoadData       );
51         Connect(refreshBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnRefreshBtn      );
52         
53         //-------------------
54         //GUI
55         //-------------------
56         
57         //Upper Box
58         wxBoxSizer * upper_box = new wxBoxSizer( wxHORIZONTAL );
59         //Including components for plotter control
60         upper_box->Add( histogramW, 4, wxGROW);
61         
62
63         //bottom Sizer
64         wxBoxSizer *bottomBox = new wxBoxSizer( wxHORIZONTAL );
65         bottomBox->Add( okBtn, wxSizerFlags().Center());
66         bottomBox->AddSpacer(40);
67         bottomBox->Add( saveDataBtn,wxSizerFlags().Center() );
68         bottomBox->AddSpacer(40);
69         bottomBox->Add( loadDataBtn,wxSizerFlags().Center() );
70         bottomBox->AddSpacer(40);
71         bottomBox->Add( refreshBtn,wxSizerFlags().Center() );
72         bottomBox->AddSpacer(40);
73         bottomBox->Add( cancelBtn,wxSizerFlags().Center() );
74         //Sizer
75         wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
76         sizer->Add(upper_box,1,wxEXPAND);       
77     sizer->Add(bottomBox,0,wxCENTER);
78         SetAutoLayout( TRUE );
79         SetSizer( sizer );
80         SetBestSize(wxSize(600,600));
81 }
82
83 //-----------------------
84 //Handling events
85 //-----------------------
86
87 void HistogramDialog::OnSaveData(wxCommandEvent& event)
88 {
89         wxString nameF=wxFileSelector(_T("Save Data"), _T(" "),_T(""),_T(""),_T("*.*"),wxSAVE, NULL, -1,  -1);//wxFileSelector(_T("Save Data"),wxSAVE);
90         std::ofstream file;
91         if(nameF.CompareTo( _T("/0") )>0)
92                 file.open( (const char*)(nameF.mb_str()) );
93
94         if(file.is_open())
95         {
96                 file << "<histogram Data>" << std::endl;
97                 //histograms points
98                 /*
99                 file << "       <histogramPoints>" << std::endl;
100                 int histogramS=histogramW->getHistogramSize();
101                 int i=0;        
102                 while(i<histogramS)
103                 {
104                         file << "               <HistogramPoint>" << std::endl;
105                         file << "                       <greyValue>" << i << "</greyValue>" <<std::endl;
106                         file << "                       <number>" << histogramW->getHistogramPoint(i) << "</number>"<<std::endl;
107                         file << "               </HistogramPoint>" << std::endl;
108                         i++;
109                 }
110                 file << "       </histogramPoints>" << std::endl;
111                 */
112                 //points transference Function
113                 file << " <TransferenceFunction>"<< std::endl;
114                 int tfPointS=histogramW->getSizeTransferenceFunction();
115                 int i=0;
116                 int gv,in;
117                 while(i<tfPointS)
118                 {
119                         
120                         histogramW->getTransferenceFunctionPoint(i,gv,in);
121                         file << "               <Transferencepoint>" << std::endl;
122                         file << "                       <greyValue>" << gv << "</greyValue>" <<std::endl;
123                         file << "                       <intensity>" << in << "</intensity> "<<std::endl;
124                         file << "               </Transferencepoint>" << std::endl;
125                         i++;
126                 }
127                 file << "       </TransferenceFunction>"<< std::endl;
128                 //colorPoints
129                 file << "       <Colors>"<< std::endl;
130                 int ctfPointS=histogramW->getSizeBarColor();
131
132                 i=0;
133                 int red,green,blue;
134                 while(i<ctfPointS)
135                 {
136                         
137                         histogramW->getDataBarColorPoint(i,gv,red,green,blue);
138                         file << "               <greyValueRGBpoint>" << std::endl;
139                         file << "                       <RGBgreyValue>" << gv << "</RGBgreyValue>" <<std::endl;
140                         file << "                       <red>" << red << "</red>" <<std::endl;
141                         file << "                       <green>" << green << "</green>" <<std::endl;
142                         file << "                       <blue>" << blue << "</blue>" <<std::endl;
143                         file << "               </greyValueRGBpoint>" << std::endl;
144                         i++;
145                 }
146
147                 file << "       </Colors>"<< std::endl;
148                 file << "</histogram Data>" << std::endl;
149         }
150         file.close();
151 }
152
153 void HistogramDialog::OnLoadData(wxCommandEvent& event)
154 {
155         wxString nameF=wxFileSelector(_T("Load Data") );
156         //std::string line;
157         std::ifstream file;
158         if(nameF.CompareTo( _T("/0") )>0)
159                 file.open(  (const char*) (nameF.mb_str()) );
160         
161         bool histogramReading=false,/*histogramPoints=false,histogramPoint=false,*/tf=false,ctf=false,tfp=false,ctfp=false;  // JPRx
162         int gv=-1,in=-1,red=-1,gr=-1,bl=-1/*,nPoints=-1*/; // JPRx
163         if(file.is_open())
164         {
165                 //-------------
166                 erasePointsTransferenceFunction();
167                 eraseColorPoints();
168                 //--------------
169         
170                 //std::getline(file,line);
171                 //int i=0;  // JPRx
172                 while(!file.eof())
173                 {
174                         std::string line;
175                         std::getline(file,line);
176                         //int a=line.find("histogram Data");
177                         if( (int)(line.find("histogram Data"))!=-1)
178                         {
179                                 histogramReading=true;
180                         
181                         }
182                         /*
183                         else if(line.find("histogramPoints")!=0 && histogramReading)
184                         {
185                                 histogramPoints=true;
186                         }
187                         else if(line.find("histogramPoint")!=0 && histogramReading && histogramPoints)
188                         {
189
190                         }
191                         else if(line.find("greyValue")!=0 && histogramReading && histogramPoints)
192                         {
193                                 int pos1=line.find(">");
194                                 int pos2=line.find("<",pos+1);
195                                 std::string x=line.substr(pos1+1,pos2-1);
196                                 gv=atoi(x.c_str());
197                         }
198                         else if(line.find("number")!=0 && histogramReading && histogramPoints)
199                         {
200                                 int pos1=line.find(">");
201                                 int pos2=line.find("<",pos+1);
202                                 std::string x=line.substr(pos1+1,pos2-1);
203                                 nP=atoi(x.c_str());
204                                 //add to the histogram
205                                 //histogramW->addPointToTransferenceFunction(gc,in);
206                         }
207                         */
208                         else if( (int)(line.find("TransferenceFunction"))!=-1 && histogramReading)
209                         {
210                                 tf=true;
211                         }
212                         else if( (int)(line.find("<Transferencepoint>"))!=-1 && histogramReading && tf)
213                         {
214                                 tfp=true;
215                         }
216                         else if( (int)(line.find("greyValue"))!=-1 && histogramReading && tf && tfp)
217                         {
218                                 int pos1=line.find(">");
219                                 int pos2=line.find("<",pos1+1);
220                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
221                                 gv=atoi(x.c_str());
222                                 
223                         }
224                         else if( (int)(line.find("intensity"))!=-1 && histogramReading && tf && tfp)
225                         {
226                                 int pos1=line.find(">");
227                                 int pos2=line.find("<",pos1+1);
228                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
229                                 in=atoi(x.c_str());
230                                 histogramW->addPointToTransferenceFunction(gv,in);
231                                 tfp=false;
232                                 
233                         }
234                         else if( (int)(line.find("Colors"))!=-1 && histogramReading)
235                         {
236                                 ctf=true;
237                         }
238                         
239                         else if( (int)(line.find("greyValueRGBpoint"))!=-1 && histogramReading && ctf )
240                         {
241                                 ctfp=true;
242                                 
243                         }
244                         else if( (int)(line.find("RGBgreyValue"))!=-1 && histogramReading && ctf && ctfp)
245                         {
246                                 int pos1=line.find(">");
247                                 int pos2=line.find("<",pos1+1);
248                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
249                                 gv=atoi(x.c_str());
250                                 
251                         }
252                         else if( (int)(line.find("red"))!=-1 && histogramReading && ctf && ctfp)
253                         {
254                                 int pos1=line.find(">");
255                                 int pos2=line.find("<",pos1+1);
256                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
257                                 red=atoi(x.c_str());
258                                 
259                         }
260                         else if( (int)(line.find("green"))!=-1 && histogramReading && ctf && ctfp)
261                         {
262                                 int pos1=line.find(">");
263                                 int pos2=line.find("<",pos1+1);
264                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
265                                 gr=atoi(x.c_str());
266                         }
267                         else if( (int)(line.find("blue"))!=-1 && histogramReading && ctf && ctfp)
268                         {
269                                 int pos1=line.find(">");
270                                 int pos2=line.find("<",pos1+1);
271                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
272                                 bl=atoi(x.c_str());
273                                 histogramW->addColorPoint(gv,red,gr,bl);
274                                 ctfp=false;
275                         }
276                         line.clear();
277              }
278      }
279         
280         file.close();
281         histogramW->updatePlotter();
282 }
283 /*
284  On refresh btn
285 */
286
287 void HistogramDialog::OnRefreshBtn(wxCommandEvent &event)
288 {
289         refreshed=true;
290                         
291                         int i=0,xi,yi,r,g,b;
292                         
293                         // -- TransferenceFunction --
294                         int nPointsTF=getSizeTransferenceFunction();
295
296                         if(nPointsTF>0)
297                         {
298
299                                 int nTFPoints=getSizeTransferenceFunction();
300                                 i=0;
301                                 while(i<nTFPoints)
302                                 {
303                                         getTransferenceFunctionPoint(i,xi,yi);
304                                         _tfun->AddPoint( xi , yi/100.0 );
305                                         i++;
306                                 }
307                         }
308                         //-- Color bar --
309                         //clean colors
310                         int nPointsCTF=getSizeBarColor();
311                         if(nPointsCTF>0)
312                         {                       
313                                 _ctfun->RemoveAllPoints();
314                                 
315                                 int nCTFpoints=getSizeBarColor();
316                                 i=0;    
317                                 while(i<nCTFpoints)
318                                 {
319                                         getDataBarColorPoint(i,xi,r,g,b);
320                                         _ctfun->AddRGBPoint(xi,r/255.0,g/255.0,b/255.0 );
321                                         i++;
322                                 }
323                         }
324
325 //EED Borrame
326
327         /*
328          NOTE I NEED THE UPDATE
329         */
330         //if it was call from the clipping ctnrl panel
331         if(wxvtkclipping3Dview!=NULL)
332         {
333                 wxvtkclipping3Dview->Refresh();
334
335         }
336         else if(wxvtkmpr3Dview!=NULL)
337         {
338                 wxvtkmpr3Dview->Refresh();
339
340         }
341 }
342
343
344 //-----------------------
345 //Methods
346 //-----------------------
347
348 /*
349                 get number of points of the transference function
350         */
351         int HistogramDialog::getSizeTransferenceFunction()
352         {
353                 return histogramW->getSizeTransferenceFunction();
354                 
355         }
356         /*
357                 get number of points of the barColor
358         */
359         int HistogramDialog::getSizeBarColor()
360         {
361                 return histogramW->getSizeBarColor();
362         }
363         /*
364                 get a point of the transference function
365         */
366         void HistogramDialog::getTransferenceFunctionPoint(int index,int& x,int& y)
367         {
368                 histogramW->getTransferenceFunctionPoint(index,x,y);
369         }
370         /*
371                 get a  color int the bqr color
372         */
373         void HistogramDialog::getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
374         {
375                 histogramW->getDataBarColorPoint(index,x,red,green,blue);
376         }
377         /*
378                 get a point of the Histogram
379                 given the grey value
380         */
381         int HistogramDialog::getHistogramPoint(int gValue)
382         {
383                 return histogramW->getHistogramPoint(gValue);
384         }
385
386         //--------------------
387         // bar Information
388         //---------------------
389         /*
390          Get the porcentage of the positions of 
391          the min,max and actual in the bar range
392         */
393         float HistogramDialog::getMaxShowedPorcentage()
394         {
395                 return histogramW->getMaxShowedPorcentage();
396         }
397         float HistogramDialog::getMinShowedPorcentage()
398         {
399                 return histogramW->getMinShowedPorcentage();
400         }
401         float HistogramDialog::getActualShowedPorcentage()
402         {
403                 return histogramW->getActualShowedPorcentage();
404         }
405         //---------------------------------------
406         // setting data in transferences function
407         // and in bar color
408         //----------------------------------------
409         bool  HistogramDialog::addPointToTransferenceFunction(double x, double y)
410         {
411                 return histogramW->addPointToTransferenceFunction(x,y);
412         }
413         bool  HistogramDialog::addColorPoint(double x,int red,int green, int blue)
414         {
415                 return histogramW->addColorPoint(x,red,green,blue);
416         }
417         //------------------------
418         //Erase data
419         //------------------------
420         
421         /*
422          Erase all the points that are in the transference function
423         */
424         void HistogramDialog::erasePointsTransferenceFunction()
425         {
426                 histogramW->erasePointsTransferenceFunction();
427         }
428         /*
429         Erase the  color points in the plotter
430         */
431         void HistogramDialog::eraseColorPoints()
432         {
433                 histogramW->eraseColorPoints();
434         }
435
436         //-------------------
437         // Getter and setters
438         //-------------------
439
440         void HistogramDialog::setCTF(vtkColorTransferFunction *cf)
441         {
442                 _ctfun=cf;
443         
444         }
445         void HistogramDialog:: setTF(vtkPiecewiseFunction *tf)
446         {
447                 _tfun=tf;
448         }
449
450         bool HistogramDialog::getRefreshed()
451         {
452                 return refreshed;
453         }
454         
455         void HistogramDialog::setVolumeMapper(vtkVolumeRayCastMapper* volMapper)
456         {
457                 volumeMapper=volMapper;
458         }
459         void HistogramDialog::setVolume(vtkVolume* vol)
460         {
461                 newvol=vol;     
462         }
463         void HistogramDialog::setMPR3Dview(wxVtkMPR3DView *wxvtkmpr3Dview1)
464         {
465                 wxvtkmpr3Dview=wxvtkmpr3Dview1;
466         }
467         void HistogramDialog::setClipping3DView(wxVtkClipping3DView *wxvtkclipping3Dview1)
468         {
469                 wxvtkclipping3Dview=wxvtkclipping3Dview1;
470         }
471
472
473         void  HistogramDialog::setTransferenceFunctionHasPoints(bool hasPoints)
474         {
475                 histogramW->setTransferenceFunctionHasPoints(hasPoints);
476         }
477         void  HistogramDialog::setTransferenceFunctionHasColor(bool hasColorPoints)
478         {
479                 histogramW->setTransferenceFunctionHasColor(hasColorPoints);
480         }
481
482         //--------------------
483         // plotter Information
484         //---------------------
485         void  HistogramDialog::updatePlotter()
486         {
487                 histogramW->updatePlotter();
488         }
489
490