2 This class plots image's histograms in a plotter
5 //---------------------
7 //----------------------
8 #include "HistogramWidget.h"
9 #include "vtkImageCast.h"
11 // ----------------------------------------------------------------------------
12 // WX headers inclusion.
13 // For compilers that support precompilation, includes <wx/wx.h>.
14 // ----------------------------------------------------------------------------
20 //----------------------------------------------------------------------------
21 // Class implementation
22 //----------------------------------------------------------------------------
24 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
25 //----------------------------------------------------------------------------
27 //----------------------------------------------------------------------------
29 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
30 EVT_SIZE (HistogramWidget::OnSize)
32 //---------------------
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 )
39 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag):wxWindow(parent,id,pos,size,flag)
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)
46 SetBackgroundColour(wxColour(255,255,255));
48 histogram= new pHistogram(imageData);
51 plotter=new pPlotter(this, 400,350);
53 //is a plotter of histograms
56 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
58 idTransferenceFunction=-1;
60 transferenceFunctionHasColor=true;
61 transferenceFunctionHasPoints=true;
66 drawTransferenceFunction();
70 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
74 SetBackgroundColour(wxColour(255,255,255));
78 plotter=new pPlotter(this, 400,350);
80 //is a plotter of histograms
83 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
85 idTransferenceFunction=-1;
87 transferenceFunctionHasColor=true;
88 transferenceFunctionHasPoints=true;
94 void HistogramWidget::initializeHistogram(vtkImageData* img){
96 histogram= new pHistogram(img);
100 drawTransferenceFunction();
103 HistogramWidget::~HistogramWidget()
110 Draw the histogram in the plotter
112 void HistogramWidget::drawHistogram()
114 //int xValues[MAX],yValues[MAX],extent[6];
118 vtkImageData* histogramImageData=histogram->getHistogram();
121 histogramSize=histogram->getSize();
124 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
125 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
127 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
129 for(int i=0; i< histogramSize; i++)
132 yValues[i]=log( (double) histogramPointer[i]);
136 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
138 if (histogramFunction)
140 histogramFunction->setEditable(false);
142 histogramFunction->setType(2);
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 );
150 histogramFunction->SetPen( mypen1 );
158 Draw the transference function in the plotter
161 void HistogramWidget::drawTransferenceFunction()
164 double xValues[5],yValues[5];
166 int maxValueGrey=histogram->getMaximumLevelOfGrey();
168 xValues[1]=maxValueGrey/16;
169 xValues[2]=maxValueGrey/8;
170 xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
171 xValues[4]=maxValueGrey;
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
186 //is the actual Function
189 tf->SetShowPoints(true);
190 idTransferenceFunction=plotter->addFunction( tf );
192 plotter->addFunctionToMove(tf);
193 wxPen mypen(*wxBLACK,0.5, wxSOLID );
200 if the user resize the window
202 void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
205 GetClientSize(&scrX,&scrY);
206 plotter->SetSize(scrX,scrY);
207 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
209 actual->setScreens(scrX,scrY);
215 get number of points of the transference function
217 int HistogramWidget::getSizeTransferenceFunction()
219 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
220 return tf->getSizePoints();
224 get a point of the transference function
226 void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
228 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
231 wxNode* pnode=tf->GetPointAt(index);
232 pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
238 get a point of the Histogram
241 int HistogramWidget::getHistogramPoint(int gValue)
243 return histogram->getHistogramPoint(gValue);
246 get number of points of the barColor
248 int HistogramWidget::getSizeBarColor()
250 return plotter->getColorPointsSize();
253 get a color int the bqr color
255 void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
258 plotter->getBarColorDataAt(index,tmp,red,green,blue);
262 Returns the maximum value ot the histogram that is show to the user
264 float HistogramWidget::getMaxShowedPorcentage()
266 float porcentageMaxX=plotter->getMaxShowedPorcentage();
267 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
268 int min=histogramFunction->getMinX();
269 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
273 Returns the minimum value ot the histogram that is show to the user
275 float HistogramWidget::getMinShowedPorcentage()
277 float porcentageMinX=plotter->getMinShowedPorcentage();
278 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
279 int min=histogramFunction->getMinX();
280 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
284 Returns the minimum value ot the histogram that is show to the user
286 float HistogramWidget::getActualShowedPorcentage()
288 float porcentageActualX=plotter->getMinShowedPorcentage();
289 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
290 int min=histogramFunction->getMinX();
291 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
294 //---------------------------------------
295 // setting data in transferences function
297 //----------------------------------------
299 Adds a point to the transference function
301 bool HistogramWidget::addPointToTransferenceFunction(double x, double y)
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); }
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
317 bool HistogramWidget::addColorPoint(double x,int red,int green, int blue)
319 return plotter->addColorPoint(x,red,green,blue);
322 //------------------------
324 //------------------------
326 Erase all the points that are in the transference function
329 void HistogramWidget::erasePointsTransferenceFunction(/*bool allPoints*/)
331 if(transferenceFunctionHasPoints)
333 // we have to erase the points
334 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
336 int numOfPoints=tf->getSizePoints();
337 int i=numOfPoints-1;//-2;
340 tf->deletePointAt(i);
344 } // if transferenceFunctionHasPoints
346 //we set for actual the histogram
347 //plotter->setActual()
350 Erase the color points in the plotter
352 void HistogramWidget::eraseColorPoints()
354 plotter->eraseColorPoints();
359 void HistogramWidget::updatePlotter()
364 //-------------------
365 // Getter and setters
366 //-------------------
368 void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
370 transferenceFunctionHasPoints=hasPoints;
373 void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
375 transferenceFunctionHasPoints=hasColorPoints;
377 int HistogramWidget::getHistogramSize()
379 return histogramSize;
381 void HistogramWidget::setType(int type)
387 ** Returns two vectors, the grey level of the point and its value, the value is between [0,1]
389 void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
390 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
394 ** Returns two vectors, the grey level of the point and its value, the red, green
395 ** and blue value is between [0,1]
397 void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
398 std::vector<double>& red,
399 std::vector<double>& green,
400 std::vector<double>& blue)
402 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);