]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramDialog.cxx
*** empty log message ***
[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 HistogramDialog::HistogramDialog(wxWindow *parent,wxString title)
84 :wxDialog(parent,-1,title,wxDefaultPosition,wxDefaultSize,wxRESIZE_BORDER|wxDEFAULT_DIALOG_STYLE   ,_T("dialogBox")){
85         _ctfun=NULL;
86         _tfun=NULL;
87
88         SetBackgroundColour(wxColour(255,255,255));
89         /*
90         Pointers
91         */
92         wxvtkmpr3Dview=NULL;
93         wxvtkclipping3Dview=NULL;
94         
95         /*
96          Histogram
97         */
98         
99         histogramW=new HistogramWidget(this, -1);
100         
101         refreshed=false;
102
103         /*
104         Buttons
105         */
106         okBtn = new wxButton(this,wxID_OK ,_T("OK"));
107         cancelBtn = new wxButton(this,wxID_CANCEL,_T("Cancel"));
108         
109         saveDataBtn = new wxButton(this,ID_SAVE,_T("Save"));
110         loadDataBtn = new wxButton(this,ID_LOAD,_T("Load"));
111         refreshBtn = new wxButton(this,ID_REFRESH,_T("Refresh"));
112         
113         Connect(saveDataBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnSaveData       );
114         Connect(loadDataBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnLoadData       );
115         Connect(refreshBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &HistogramDialog::OnRefreshBtn      );
116         
117         //-------------------
118         //GUI
119         //-------------------
120         
121         //Upper Box
122         wxBoxSizer * upper_box = new wxBoxSizer( wxHORIZONTAL );
123         //Including components for plotter control
124         upper_box->Add( histogramW, 4, wxGROW);
125         
126
127         //bottom Sizer
128         wxBoxSizer *bottomBox = new wxBoxSizer( wxHORIZONTAL );
129         bottomBox->Add( okBtn, wxSizerFlags().Center());
130         bottomBox->AddSpacer(40);
131         bottomBox->Add( saveDataBtn,wxSizerFlags().Center() );
132         bottomBox->AddSpacer(40);
133         bottomBox->Add( loadDataBtn,wxSizerFlags().Center() );
134         bottomBox->AddSpacer(40);
135         bottomBox->Add( refreshBtn,wxSizerFlags().Center() );
136         bottomBox->AddSpacer(40);
137         bottomBox->Add( cancelBtn,wxSizerFlags().Center() );
138         //Sizer
139         wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
140         sizer->Add(upper_box,1,wxEXPAND);       
141     sizer->Add(bottomBox,0,wxCENTER);
142         SetAutoLayout( TRUE );
143         SetSizer( sizer );
144         SetBestSize(wxSize(600,600));
145 }
146
147 //-----------------------
148 //Handling events
149 //-----------------------
150
151 void HistogramDialog::OnSaveData(wxCommandEvent& event)
152 {
153         wxString nameF=wxFileSelector(_T("Save Data"), _T(" "),_T(""),_T(""),_T("*.*"),wxSAVE, NULL, -1,  -1);//wxFileSelector(_T("Save Data"),wxSAVE);
154         std::ofstream file;
155         if(nameF.CompareTo( _T("/0") )>0)
156                 file.open( (const char*)(nameF.mb_str()) );
157
158         if(file.is_open())
159         {
160                 file << "<histogram Data>" << std::endl;
161                 //histograms points
162                 /*
163                 file << "       <histogramPoints>" << std::endl;
164                 int histogramS=histogramW->getHistogramSize();
165                 int i=0;        
166                 while(i<histogramS)
167                 {
168                         file << "               <HistogramPoint>" << std::endl;
169                         file << "                       <greyValue>" << i << "</greyValue>" <<std::endl;
170                         file << "                       <number>" << histogramW->getHistogramPoint(i) << "</number>"<<std::endl;
171                         file << "               </HistogramPoint>" << std::endl;
172                         i++;
173                 }
174                 file << "       </histogramPoints>" << std::endl;
175                 */
176                 //points transference Function
177                 file << " <TransferenceFunction>"<< std::endl;
178                 int tfPointS=histogramW->getSizeTransferenceFunction();
179                 int i=0;
180                 int gv,in;
181                 while(i<tfPointS)
182                 {
183                         
184                         histogramW->getTransferenceFunctionPoint(i,gv,in);
185                         file << "               <Transferencepoint>" << std::endl;
186                         file << "                       <greyValue>" << gv << "</greyValue>" <<std::endl;
187                         file << "                       <intensity>" << in << "</intensity> "<<std::endl;
188                         file << "               </Transferencepoint>" << std::endl;
189                         i++;
190                 }
191                 file << "       </TransferenceFunction>"<< std::endl;
192                 //colorPoints
193                 file << "       <Colors>"<< std::endl;
194                 int ctfPointS=histogramW->getSizeBarColor();
195
196                 i=0;
197                 int red,green,blue;
198                 while(i<ctfPointS)
199                 {
200                         
201                         histogramW->getDataBarColorPoint(i,gv,red,green,blue);
202                         file << "               <greyValueRGBpoint>" << std::endl;
203                         file << "                       <RGBgreyValue>" << gv << "</RGBgreyValue>" <<std::endl;
204                         file << "                       <red>" << red << "</red>" <<std::endl;
205                         file << "                       <green>" << green << "</green>" <<std::endl;
206                         file << "                       <blue>" << blue << "</blue>" <<std::endl;
207                         file << "               </greyValueRGBpoint>" << std::endl;
208                         i++;
209                 }
210
211                 file << "       </Colors>"<< std::endl;
212                 file << "</histogram Data>" << std::endl;
213         }
214         file.close();
215 }
216
217 void HistogramDialog::OnLoadData(wxCommandEvent& event)
218 {
219         wxString nameF=wxFileSelector(_T("Load Data") );
220         //std::string line;
221         std::ifstream file;
222         if(nameF.CompareTo( _T("/0") )>0)
223                 file.open(  (const char*) (nameF.mb_str()) );
224         
225         bool histogramReading=false,/*histogramPoints=false,histogramPoint=false,*/tf=false,ctf=false,tfp=false,ctfp=false;  // JPRx
226         int gv=-1,in=-1,red=-1,gr=-1,bl=-1/*,nPoints=-1*/; // JPRx
227         if(file.is_open())
228         {
229                 //-------------
230                 erasePointsTransferenceFunction();
231                 eraseColorPoints();
232                 //--------------
233         
234                 //std::getline(file,line);
235                 //int i=0;  // JPRx
236                 while(!file.eof())
237                 {
238                         std::string line;
239                         std::getline(file,line);
240                         //int a=line.find("histogram Data");
241                         if( (int)(line.find("histogram Data"))!=-1)
242                         {
243                                 histogramReading=true;
244                         
245                         }
246                         /*
247                         else if(line.find("histogramPoints")!=0 && histogramReading)
248                         {
249                                 histogramPoints=true;
250                         }
251                         else if(line.find("histogramPoint")!=0 && histogramReading && histogramPoints)
252                         {
253
254                         }
255                         else if(line.find("greyValue")!=0 && histogramReading && histogramPoints)
256                         {
257                                 int pos1=line.find(">");
258                                 int pos2=line.find("<",pos+1);
259                                 std::string x=line.substr(pos1+1,pos2-1);
260                                 gv=atoi(x.c_str());
261                         }
262                         else if(line.find("number")!=0 && histogramReading && histogramPoints)
263                         {
264                                 int pos1=line.find(">");
265                                 int pos2=line.find("<",pos+1);
266                                 std::string x=line.substr(pos1+1,pos2-1);
267                                 nP=atoi(x.c_str());
268                                 //add to the histogram
269                                 //histogramW->addPointToTransferenceFunction(gc,in);
270                         }
271                         */
272                         else if( (int)(line.find("TransferenceFunction"))!=-1 && histogramReading)
273                         {
274                                 tf=true;
275                         }
276                         else if( (int)(line.find("<Transferencepoint>"))!=-1 && histogramReading && tf)
277                         {
278                                 tfp=true;
279                         }
280                         else if( (int)(line.find("greyValue"))!=-1 && histogramReading && tf && tfp)
281                         {
282                                 int pos1=line.find(">");
283                                 int pos2=line.find("<",pos1+1);
284                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
285                                 gv=atoi(x.c_str());
286                                 
287                         }
288                         else if( (int)(line.find("intensity"))!=-1 && histogramReading && tf && tfp)
289                         {
290                                 int pos1=line.find(">");
291                                 int pos2=line.find("<",pos1+1);
292                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
293                                 in=atoi(x.c_str());
294                                 histogramW->addPointToTransferenceFunction(gv,in);
295                                 tfp=false;
296                                 
297                         }
298                         else if( (int)(line.find("Colors"))!=-1 && histogramReading)
299                         {
300                                 ctf=true;
301                         }
302                         
303                         else if( (int)(line.find("greyValueRGBpoint"))!=-1 && histogramReading && ctf )
304                         {
305                                 ctfp=true;
306                                 
307                         }
308                         else if( (int)(line.find("RGBgreyValue"))!=-1 && histogramReading && ctf && ctfp)
309                         {
310                                 int pos1=line.find(">");
311                                 int pos2=line.find("<",pos1+1);
312                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
313                                 gv=atoi(x.c_str());
314                                 
315                         }
316                         else if( (int)(line.find("red"))!=-1 && histogramReading && ctf && ctfp)
317                         {
318                                 int pos1=line.find(">");
319                                 int pos2=line.find("<",pos1+1);
320                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
321                                 red=atoi(x.c_str());
322                                 
323                         }
324                         else if( (int)(line.find("green"))!=-1 && histogramReading && ctf && ctfp)
325                         {
326                                 int pos1=line.find(">");
327                                 int pos2=line.find("<",pos1+1);
328                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
329                                 gr=atoi(x.c_str());
330                         }
331                         else if( (int)(line.find("blue"))!=-1 && histogramReading && ctf && ctfp)
332                         {
333                                 int pos1=line.find(">");
334                                 int pos2=line.find("<",pos1+1);
335                                 std::string x=line.substr(pos1+1,pos2-pos1-1);
336                                 bl=atoi(x.c_str());
337                                 histogramW->addColorPoint(gv,red,gr,bl);
338                                 ctfp=false;
339                         }
340                         line.clear();
341              }
342      }
343         
344         file.close();
345         histogramW->updatePlotter();
346 }
347 /*
348  On refresh btn
349 */
350
351 void HistogramDialog::OnRefreshBtn(wxCommandEvent &event)
352 {
353         refreshed=true;
354                         
355                         int i=0,xi,yi,r,g,b;
356                         
357                         // -- TransferenceFunction --
358                         int nPointsTF=getSizeTransferenceFunction();
359
360                         if(nPointsTF>0)
361                         {
362
363                                 int nTFPoints=getSizeTransferenceFunction();
364                                 i=0;
365                                 while(i<nTFPoints)
366                                 {
367                                         getTransferenceFunctionPoint(i,xi,yi);
368                                         _tfun->AddPoint( xi , yi/100.0 );
369                                         i++;
370                                 }
371                         }
372                         //-- Color bar --
373                         //clean colors
374                         int nPointsCTF=getSizeBarColor();
375                         if(nPointsCTF>0)
376                         {                       
377                                 _ctfun->RemoveAllPoints();
378                                 
379                                 int nCTFpoints=getSizeBarColor();
380                                 i=0;    
381                                 while(i<nCTFpoints)
382                                 {
383                                         getDataBarColorPoint(i,xi,r,g,b);
384                                         _ctfun->AddRGBPoint(xi,r/255.0,g/255.0,b/255.0 );
385                                         i++;
386                                 }
387                         }
388
389 //EED Borrame
390
391         /*
392          NOTE I NEED THE UPDATE
393         */
394         //if it was call from the clipping ctnrl panel
395         if(wxvtkclipping3Dview!=NULL)
396         {
397                 wxvtkclipping3Dview->Refresh();
398
399         }
400         else if(wxvtkmpr3Dview!=NULL)
401         {
402                 wxvtkmpr3Dview->Refresh();
403
404         }
405 }
406
407
408 //-----------------------
409 //Methods
410 //-----------------------
411
412 /*
413                 get number of points of the transference function
414         */
415         int HistogramDialog::getSizeTransferenceFunction()
416         {
417                 return histogramW->getSizeTransferenceFunction();
418                 
419         }
420         /*
421                 get number of points of the barColor
422         */
423         int HistogramDialog::getSizeBarColor()
424         {
425                 return histogramW->getSizeBarColor();
426         }
427         /*
428                 get a point of the transference function
429         */
430         void HistogramDialog::getTransferenceFunctionPoint(int index,int& x,int& y)
431         {
432                 histogramW->getTransferenceFunctionPoint(index,x,y);
433         }
434         /*
435                 get a  color int the bqr color
436         */
437         void HistogramDialog::getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
438         {
439                 histogramW->getDataBarColorPoint(index,x,red,green,blue);
440         }
441         /*
442                 get a point of the Histogram
443                 given the grey value
444         */
445         int HistogramDialog::getHistogramPoint(int gValue)
446         {
447                 return histogramW->getHistogramPoint(gValue);
448         }
449
450         //--------------------
451         // bar Information
452         //---------------------
453         /*
454          Get the porcentage of the positions of 
455          the min,max and actual in the bar range
456         */
457         float HistogramDialog::getMaxShowedPorcentage()
458         {
459                 return histogramW->getMaxShowedPorcentage();
460         }
461         float HistogramDialog::getMinShowedPorcentage()
462         {
463                 return histogramW->getMinShowedPorcentage();
464         }
465         float HistogramDialog::getActualShowedPorcentage()
466         {
467                 return histogramW->getActualShowedPorcentage();
468         }
469         //---------------------------------------
470         // setting data in transferences function
471         // and in bar color
472         //----------------------------------------
473         bool  HistogramDialog::addPointToTransferenceFunction(double x, double y)
474         {
475                 return histogramW->addPointToTransferenceFunction(x,y);
476         }
477         bool  HistogramDialog::addColorPoint(double x,int red,int green, int blue)
478         {
479                 return histogramW->addColorPoint(x,red,green,blue);
480         }
481         //------------------------
482         //Erase data
483         //------------------------
484         
485         /*
486          Erase all the points that are in the transference function
487         */
488         void HistogramDialog::erasePointsTransferenceFunction()
489         {
490                 histogramW->erasePointsTransferenceFunction();
491         }
492         /*
493         Erase the  color points in the plotter
494         */
495         void HistogramDialog::eraseColorPoints()
496         {
497                 histogramW->eraseColorPoints();
498         }
499
500         //-------------------
501         // Getter and setters
502         //-------------------
503
504         void HistogramDialog::setCTF(vtkColorTransferFunction *cf)
505         {
506                 _ctfun=cf;
507         
508         }
509         void HistogramDialog:: setTF(vtkPiecewiseFunction *tf)
510         {
511                 _tfun=tf;
512         }
513
514         bool HistogramDialog::getRefreshed()
515         {
516                 return refreshed;
517         }
518         
519         void HistogramDialog::setVolumeMapper(vtkVolumeRayCastMapper* volMapper)
520         {
521                 volumeMapper=volMapper;
522         }
523         void HistogramDialog::setVolume(vtkVolume* vol)
524         {
525                 newvol=vol;     
526         }
527         void HistogramDialog::setMPR3Dview(wxVtkMPR3DView *wxvtkmpr3Dview1)
528         {
529                 wxvtkmpr3Dview=wxvtkmpr3Dview1;
530         }
531         void HistogramDialog::setClipping3DView(wxVtkClipping3DView *wxvtkclipping3Dview1)
532         {
533                 wxvtkclipping3Dview=wxvtkclipping3Dview1;
534         }
535
536
537         void  HistogramDialog::setTransferenceFunctionHasPoints(bool hasPoints)
538         {
539                 histogramW->setTransferenceFunctionHasPoints(hasPoints);
540         }
541         void  HistogramDialog::setTransferenceFunctionHasColor(bool hasColorPoints)
542         {
543                 histogramW->setTransferenceFunctionHasColor(hasColorPoints);
544         }
545
546         //--------------------
547         // plotter Information
548         //---------------------
549         void  HistogramDialog::updatePlotter()
550         {
551                 histogramW->updatePlotter();
552         }
553
554
555 /**
556 **      Initialize the histogram 
557 */
558         void HistogramDialog::initializeHistogram(vtkImageData* img){
559                 histogramW->initializeHistogram(img);
560         }
561 /**
562 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
563 **/
564         void HistogramDialog::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
565
566                 histogramW->GetValuesPointsFunction(greylevel, value);
567         }
568
569 /**
570 **      Returns two vectors, the grey level of the point and its value, the red, green
571 **      and blue value is between [0,1]
572 **/
573 void HistogramDialog::GetValuesColorPointsFunction(std::vector<double>& greylevel,
574                                                                 std::vector<double>& red,
575                                                                 std::vector<double>& green,
576                                                                 std::vector<double>& blue)
577 {
578         histogramW->GetValuesColorPointsFunction(greylevel, red, green, blue);
579 }
580
581 void HistogramDialog::SetFunctions(vtkPiecewiseFunction* _opac, vtkColorTransferFunction* _color){
582         _tfun = _opac;
583         _ctfun = _color;        
584 }