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