]> 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,flag)
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()
71         {
72                 delete histogram;
73                 delete plotter;
74
75         }
76         /*
77         Draw the histogram in the plotter
78         */
79         void HistogramWidget::drawHistogram()
80         {
81                 //int xValues[MAX],yValues[MAX],extent[6];
82                 double* xValues;
83                 double* yValues;
84                 
85                 vtkImageData* histogramImageData=histogram->getHistogram();
86                 
87                 //size
88                 histogramSize=histogram->getSize();
89
90                 //plotting
91                 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
92                 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
93                 
94                 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
95                 
96                 for(int i=0; i< histogramSize; i++)
97                 {
98                         xValues[i]=i;
99                         yValues[i]=log( (double) histogramPointer[i]);
100                 }
101
102                 
103                 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
104                 
105                 if (histogramFunction)
106                 {
107                         histogramFunction->setEditable(false);
108                         //smooth function
109                         histogramFunction->setType(2);
110                         //it is an histogram
111                         histogramFunction->setmType(2);
112                         idHistogram=plotter->addFunction(histogramFunction);
113                         //for setting range the vision
114                         plotter->addFunctionToMove(histogramFunction);
115                         wxPen mypen1(*wxBLUE, 1, wxSOLID );
116                         mypen1.SetWidth(2);
117                         histogramFunction->SetPen( mypen1 );
118                 }
119                         
120                         free(xValues);
121                         free(yValues);
122                 
123         }
124         /*
125         Draw the transference function in the plotter
126         one  for default
127         */
128         void HistogramWidget::drawTransferenceFunction()
129         {
130         
131                         double xValues[5],yValues[5];
132                         //xValues
133                         int maxValueGrey=histogram->getMaximumLevelOfGrey();
134                         xValues[0]=0;
135                         xValues[1]=maxValueGrey/16;
136                         xValues[2]=maxValueGrey/8;
137                         xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
138                         xValues[4]=maxValueGrey;
139                         
140                         //yValues
141                         yValues[0]=0;
142                         yValues[1]=25;
143                         yValues[2]=100;
144                         yValues[3]=25;
145                         yValues[4]=0;
146                         
147                         pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 ); 
148                         // Including and drawing the created function in the plotter
149                         if (tf)
150                         {
151                                 tf->setType(1);
152                                 //is the actual Function
153                                 tf->setActual(true);
154                                 //show points
155                                 tf->SetShowPoints(true);
156                                 idTransferenceFunction=plotter->addFunction( tf );
157                                 if(type==1)
158                                         plotter->addFunctionToMove(tf);
159                                 wxPen mypen(*wxBLACK,0.5, wxSOLID  );
160                                 mypen.SetWidth(2);
161                                 tf->SetPen( mypen );
162                         }
163         }
164
165         /*
166                 if the user resize the window   
167         */
168         void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
169         {
170                 int scrX,scrY;
171                 GetClientSize(&scrX,&scrY);
172                 plotter->SetSize(scrX,scrY);
173                 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
174                 if(actual!=NULL){
175                         actual->setScreens(scrX,scrY);
176                         actual->setScales();
177                 }
178                 
179         }
180         /*
181                 get number of points of the transference function
182         */
183         int HistogramWidget::getSizeTransferenceFunction()
184         {
185                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
186                 return tf->getSizePoints();
187
188         }
189         /*
190                 get a point of the transference function
191         */
192         void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
193         {
194                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
195                 if(tf)
196                 {
197                         wxNode* pnode=tf->GetPointAt(index);
198                         pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
199                         x=point->getRealX();
200                         y=point->getRealY();
201                 }
202         }
203         /*
204                 get a point of the Histogram
205                 given the grey value
206         */
207         int HistogramWidget::getHistogramPoint(int gValue)
208         {
209                 return histogram->getHistogramPoint(gValue);
210         }
211         /*
212                 get number of points of the barColor
213         */
214         int HistogramWidget::getSizeBarColor()
215         {
216                 return  plotter->getColorPointsSize();
217         }
218         /*
219                 get a  color int the bqr color
220         */
221         void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
222         {
223                 double tmp=x;
224                 plotter->getBarColorDataAt(index,tmp,red,green,blue);
225                 x=(int)tmp;
226         }
227         /*
228          Returns the  maximum value ot the histogram that is show to the user 
229         */
230         float HistogramWidget::getMaxShowedPorcentage()
231         {
232                 float  porcentageMaxX=plotter->getMaxShowedPorcentage();
233                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
234                 int min=histogramFunction->getMinX();
235                 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
236                 return min+ x;
237         }
238         /*
239          Returns the  minimum value ot the histogram that is show to the user 
240         */
241         float HistogramWidget::getMinShowedPorcentage()
242         {
243                 float  porcentageMinX=plotter->getMinShowedPorcentage();
244                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
245                 int min=histogramFunction->getMinX();
246                 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
247                 return min+ x;
248         }
249         /*
250          Returns the  minimum value ot the histogram that is show to the user 
251         */
252         float HistogramWidget::getActualShowedPorcentage()
253         {
254                 float  porcentageActualX=plotter->getMinShowedPorcentage();
255                 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
256                 int min=histogramFunction->getMinX();
257                 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
258                 return min+ x;
259         }
260         //---------------------------------------
261         // setting data in transferences function
262         // and in bar color
263         //----------------------------------------
264         /*
265           Adds a point to the transference function
266         */
267         bool  HistogramWidget::addPointToTransferenceFunction(double x, double y)
268         {
269                 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
270                 return tf->AddPoint(x,y);
271         }
272         /*
273                 add a color point to the histogram
274                 @param x the level of grey to which the color is assigned
275                 @param red the level of red for the color
276                 @param green the level of red for the color
277                 @param blue the level of red for the color
278         */
279         bool  HistogramWidget::addColorPoint(double x,int red,int green, int blue)
280         {
281                 return plotter->addColorPoint(x,red,green,blue);
282         }
283          
284         //------------------------
285         //Erase data
286         //------------------------
287         /*
288          Erase all the points that are in the transference function
289         */
290
291         void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
292         {
293                 if(transferenceFunctionHasPoints)
294                 {
295                         // we have to erase the points
296                         pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
297                         int numOfPoints=tf->getSizePoints();
298                         int i=numOfPoints-1;//-2;
299                         while(i>=0)
300                         {
301                                 tf->deletePointAt(i);
302                                 i--;
303                         }
304                         
305                 }
306                 
307         //we set for actual the histogram
308                 //plotter->setActual()
309         }
310         /*
311         Erase the  color points in the plotter
312         */
313         void HistogramWidget::eraseColorPoints()
314         {
315                 plotter->eraseColorPoints();
316         }
317         //------------------
318         //Updating plotter
319         //------------------
320         void HistogramWidget::updatePlotter()
321         {
322                 plotter->update();
323         }
324
325         //-------------------
326         // Getter and setters
327         //-------------------
328
329         void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
330         {
331                 transferenceFunctionHasPoints=hasPoints;
332         }
333
334         void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
335         {
336                 transferenceFunctionHasPoints=hasColorPoints;
337         }
338         int HistogramWidget::getHistogramSize()
339         {
340                 return histogramSize;
341         }
342         void HistogramWidget::setType(int type)
343         {
344                 this->type=type;
345         }
346
347