]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/HistogramWidget.cxx
*** empty log message ***
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / HistogramWidget.cxx
1 /*
2  This class plots image's histograms in a plotter
3 */
4
5  //---------------------
6  // Includes
7  //----------------------
8  #include "HistogramWidget.h"
9  #include  "vtkImageCast.h"
10  #include  <math.h>
11 // ----------------------------------------------------------------------------
12 // WX headers inclusion.
13 // For compilers that support precompilation, includes <wx/wx.h>.
14 // ----------------------------------------------------------------------------
15
16         #ifndef WX_PRECOMP
17         #include <wx/wx.h>
18         #endif
19
20 //----------------------------------------------------------------------------
21 // Class implementation
22 //----------------------------------------------------------------------------
23
24 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
25 //----------------------------------------------------------------------------
26 // Event Table
27 //----------------------------------------------------------------------------
28
29 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
30         EVT_SIZE  (HistogramWidget::OnSize)
31 END_EVENT_TABLE()
32  //---------------------
33  // Constructor
34  //----------------------
35 //wxScrolledWindow(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxHSCROLL | wxVSCROLL, const wxString& name = "scrolledWindow")
36 //wxWindow(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxString& name = wxPanelNameStr)
37 //pPlotterWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
38 /*
39 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag):wxWindow(parent,id,pos,size,flag)
40         {
41         }
42  */
43         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag,vtkImageData* imageData,int type):
44         wxPanel(parent,id,pos,size)
45         {
46                  SetBackgroundColour(wxColour(255,255,255));
47                 //histogram
48                 histogram= new pHistogram(imageData);
49                 
50                 //plotter
51                 plotter=new pPlotter(this, 400,350);
52
53                 //is a plotter of histograms
54                 plotter->setType(2);
55                 //setting the popMenu
56                 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
57                 histogramSize=0;
58                 idTransferenceFunction=-1;
59                 idHistogram=-1;
60                 transferenceFunctionHasColor=true;
61                 transferenceFunctionHasPoints=true;
62                 this->type=type;;
63                 
64                 //drawing
65                 drawHistogram();
66                 drawTransferenceFunction();
67                 
68         }
69
70         HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
71                 : wxPanel(parent,id){
72
73
74                         SetBackgroundColour(wxColour(255,255,255));
75                         histogram = NULL;       
76                         
77                         //plotter
78                         plotter=new pPlotter(this, 400,350);
79
80                         //is a plotter of histograms
81                         plotter->setType(2);
82                         //setting the popMenu
83                         plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
84                         histogramSize=0;
85                         idTransferenceFunction=-1;
86                         idHistogram=-1;
87                         transferenceFunctionHasColor=true;
88                         transferenceFunctionHasPoints=true;
89                         this->type=type;;
90                 
91                                 
92         }
93
94         void HistogramWidget::initializeHistogram(vtkImageData* img){
95                 if(histogram ==NULL){
96                         histogram= new pHistogram(img);
97                 }               
98                 //draw
99                 drawHistogram();
100                 drawTransferenceFunction();
101         }
102
103         HistogramWidget::~HistogramWidget()
104         {
105                 delete histogram;
106                 delete plotter;
107
108         }
109         /*
110         Draw the histogram in the plotter
111         */
112         void HistogramWidget::drawHistogram()
113         {
114                 //int xValues[MAX],yValues[MAX],extent[6];
115                 double* xValues;
116                 double* yValues;
117                 
118                 vtkImageData* histogramImageData=histogram->getHistogram();
119                 
120                 //size
121                 histogramSize=histogram->getSize();
122
123                 //plotting
124                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
125                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
126                 
127                 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
128                 
129                 for(int i=0; i< histogramSize; i++)
130                 {
131                         xValues[i]=i;
132                         yValues[i]=log( (double) histogramPointer[i]);
133                 }
134
135                 
136                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
137                 
138                 if (histogramFunction)
139                 {
140                         histogramFunction->setEditable(false);
141                         //smooth function
142                         histogramFunction->setType(2);
143                         //it is an histogram
144                         histogramFunction->setmType(2);
145                         idHistogram=plotter->addFunction(histogramFunction);
146                         //for setting range the vision
147                         plotter->addFunctionToMove(histogramFunction);
148                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
149                         mypen1.SetWidth(2);
150                         histogramFunction->SetPen( mypen1 );
151                 }
152                         
153                         free(xValues);
154                         free(yValues);
155                 
156         }
157         /*
158         Draw the transference function in the plotter
159         one  for default
160         */
161         void HistogramWidget::drawTransferenceFunction()
162         {
163         
164                         double xValues[5],yValues[5];
165                         //xValues
166                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
167                         xValues[0]=0;
168                         xValues[1]=maxValueGrey/16;
169                         xValues[2]=maxValueGrey/8;
170                         xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
171                         xValues[4]=maxValueGrey;
172                         
173                         //yValues
174                         yValues[0]=0;
175                         yValues[1]=25;
176                         yValues[2]=100;
177                         yValues[3]=25;
178                         yValues[4]=0;
179                         
180                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
181                 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);             
182                         // Including and drawing the created function in the plotter
183                         if (tf)
184                         {
185                                 tf->setType(1);
186                                 //is the actual Function
187                                 tf->setActual(true);
188                                 //show points
189                                 tf->SetShowPoints(true);
190                                 idTransferenceFunction=plotter->addFunction( tf );
191                                 if(type==1)
192                                         plotter->addFunctionToMove(tf);
193                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
194                                 mypen.SetWidth(2);
195                                 tf->SetPen( mypen );
196                         }
197         }
198
199         /*
200                 if the user resize the window   
201         */
202         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
203         {
204                 int scrX,scrY;
205                 GetClientSize(&scrX,&scrY);
206                 plotter->SetSize(scrX,scrY);
207                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
208                 if(actual!=NULL){
209                         actual->setScreens(scrX,scrY);
210                         actual->setScales();
211                 }
212                 
213         }
214         /*
215                 get number of points of the transference function
216         */
217         int HistogramWidget::getSizeTransferenceFunction()
218         {
219                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
220                 return tf->getSizePoints();
221
222         }
223         /*
224                 get a point of the transference function
225         */
226         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
227         {
228                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
229                 if(tf)
230                 {
231                         wxNode* pnode=tf->GetPointAt(index);
232                         pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
233                         x=point->getRealX();
234                         y=point->getRealY();
235                 }
236         }
237         /*
238                 get a point of the Histogram
239                 given the grey value
240         */
241         int HistogramWidget::getHistogramPoint(int gValue)
242         {
243                 return histogram->getHistogramPoint(gValue);
244         }
245         /*
246                 get number of points of the barColor
247         */
248         int HistogramWidget::getSizeBarColor()
249         {
250                 return  plotter->getColorPointsSize();
251         }
252         /*
253                 get a  color int the bqr color
254         */
255         void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
256         {
257                 double tmp=x;
258                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
259                 x=(int)tmp;
260         }
261         /*
262          Returns the  maximum value ot the histogram that is show to the user 
263         */
264         float HistogramWidget::getMaxShowedPorcentage()
265         {
266                 float  porcentageMaxX=plotter->getMaxShowedPorcentage();
267                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
268                 int min=histogramFunction->getMinX();
269                 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
270                 return min+ x;
271         }
272         /*
273          Returns the  minimum value ot the histogram that is show to the user 
274         */
275         float HistogramWidget::getMinShowedPorcentage()
276         {
277                 float  porcentageMinX=plotter->getMinShowedPorcentage();
278                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
279                 int min=histogramFunction->getMinX();
280                 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
281                 return min+ x;
282         }
283         /*
284          Returns the  minimum value ot the histogram that is show to the user 
285         */
286         float HistogramWidget::getActualShowedPorcentage()
287         {
288                 float  porcentageActualX=plotter->getMinShowedPorcentage();
289                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
290                 int min=histogramFunction->getMinX();
291                 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
292                 return min+ x;
293         }
294         //---------------------------------------
295         // setting data in transferences function
296         // and in bar color
297         //----------------------------------------
298         /*
299           Adds a point to the transference function
300         */
301         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
302         {
303                 bool result=false;
304                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
305 printf("EED %p HistogramWidget::addPointToTransferenceFunction tp%p x%f y%f %d\n",this, tf, x ,y, idTransferenceFunction);
306                 if (tf!=NULL) { result=tf->AddPoint(x,y); }
307                 
308                 return result;
309         }
310         /*
311                 add a color point to the histogram
312                 @param x the level of grey to which the color is assigned
313                 @param red the level of red for the color
314                 @param green the level of red for the color
315                 @param blue the level of red for the color
316         */
317         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
318         {
319                 return plotter->addColorPoint(x,red,green,blue);
320         }
321          
322         //------------------------
323         //Erase data
324         //------------------------
325         /*
326          Erase all the points that are in the transference function
327         */
328
329         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
330         {
331                 if(transferenceFunctionHasPoints)
332                 {
333                         // we have to erase the points
334                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
335                         if (tf!=NULL){
336                                 int numOfPoints=tf->getSizePoints();
337                                 int i=numOfPoints-1;//-2;
338                                 while(i>=0)
339                                 {
340                                         tf->deletePointAt(i);
341                                         i--;
342                                 } // while
343                         } // if tf
344                 } // if transferenceFunctionHasPoints
345                 
346         //we set for actual the histogram
347                 //plotter->setActual()
348         }
349         /*
350         Erase the  color points in the plotter
351         */
352         void HistogramWidget::eraseColorPoints()
353         {
354                 plotter->eraseColorPoints();
355         }
356         //------------------
357         //Updating plotter
358         //------------------
359         void HistogramWidget::updatePlotter()
360         {
361                 plotter->update();
362         }
363
364         //-------------------
365         // Getter and setters
366         //-------------------
367
368         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
369         {
370                 transferenceFunctionHasPoints=hasPoints;
371         }
372
373         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
374         {
375                 transferenceFunctionHasPoints=hasColorPoints;
376         }
377         int HistogramWidget::getHistogramSize()
378         {
379                 return histogramSize;
380         }
381         void HistogramWidget::setType(int type)
382         {
383                 this->type=type;
384         }
385
386 /**
387 **      Returns two vectors, the grey level of the point and its value, the value is between [0,1]
388 **/
389         void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
390                 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
391         }
392
393 /**
394 **      Returns two vectors, the grey level of the point and its value, the red, green
395 **      and blue value is between [0,1]
396 **/
397         void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
398                                                                 std::vector<double>& red,
399                                                                 std::vector<double>& green,
400                                                                 std::vector<double>& blue)
401         {
402                 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);
403         }