00001
00002
00003
00004
00005
00006
00007
00008 #include "HistogramWidget.h"
00009 #include "vtkImageCast.h"
00010 #include <math.h>
00011
00012
00013
00014
00015
00016
00017 #ifndef WX_PRECOMP
00018 #include <wx/wx.h>
00019
00020 #endif
00021 #include <wx/bitmap.h>
00022
00023
00024
00025
00026 IMPLEMENT_CLASS(HistogramWidget, wxWindow)
00027
00028
00029
00030
00031 BEGIN_EVENT_TABLE(HistogramWidget, wxWindow)
00032 EVT_SIZE (HistogramWidget::OnSize)
00033 END_EVENT_TABLE()
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag,vtkImageData* imageData,int type):
00046 wxPanel(parent,id,wxDefaultPosition,wxDefaultSize)
00047 {
00048 SetBackgroundColour(wxColour(255,255,255));
00049
00050 histogram= new pHistogram(imageData);
00051
00052
00053
00054 plotter=new pPlotter(this, 400,350);
00055
00056
00057 plotter->setType(2);
00058
00059 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
00060 histogramSize=0;
00061 idTransferenceFunction=-1;
00062 idHistogram=-1;
00063 transferenceFunctionHasColor=true;
00064 transferenceFunctionHasPoints=true;
00065 this->type=type;
00066
00067 this->SetAutoLayout(true);
00068 this->Refresh();
00069
00070
00071 drawHistogram();
00072 drawTransferenceFunction();
00073
00074 }
00075
00076 HistogramWidget::HistogramWidget( wxWindow *parent, wxWindowID id)
00077 : wxPanel(parent,id){
00078
00079
00080 SetBackgroundColour(wxColour(255,255,255));
00081 histogram = NULL;
00082
00083
00084
00085 plotter=new pPlotter(this, 400,350);
00086
00087
00088 plotter->setType(2);
00089
00090 plotter->setPopUpMenu(true,true,true,true,true,true,false,false,false,false,false,false,false);
00091 histogramSize=0;
00092 idTransferenceFunction=-1;
00093 idHistogram=-1;
00094 transferenceFunctionHasColor=true;
00095 transferenceFunctionHasPoints=true;
00096 this->type=type;;
00097 this->SetAutoLayout(true);
00098 this->Refresh();
00099
00100
00101 }
00102
00103
00104
00105 void HistogramWidget::initializeHistogram(vtkImageData* img){
00106 if(histogram ==NULL){
00107 histogram= new pHistogram(img);
00108 }
00109
00110 drawHistogram();
00111 drawTransferenceFunction();
00112 }
00113
00114 HistogramWidget::~HistogramWidget()
00115 {
00116 delete histogram;
00117 delete plotter;
00118
00119 }
00120
00121
00122
00123 void HistogramWidget::drawHistogram()
00124 {
00125
00126 double* xValues;
00127 double* yValues;
00128
00129 vtkImageData* histogramImageData=histogram->getHistogram();
00130
00131
00132 histogramSize=histogram->getSize();
00133
00134
00135 xValues=(double*)malloc(NUM_POINTS*sizeof(double));
00136 yValues=(double*)malloc(NUM_POINTS*sizeof(double));
00137
00138 unsigned short* histogramPointer=(unsigned short*)histogramImageData->GetScalarPointer(0,0,0);
00139
00140 for(int i=0; i< histogramSize; i++)
00141 {
00142 xValues[i]=i;
00143 yValues[i]=log( (double) histogramPointer[i]);
00144 }
00145
00146
00147 pGraphicalFunction* histogramFunction=plotter->getFunctionForVectors(xValues,histogramSize,yValues,histogramSize);
00148
00149 if (histogramFunction)
00150 {
00151 histogramFunction->setEditable(false);
00152
00153 histogramFunction->setType(2);
00154
00155 histogramFunction->setmType(2);
00156 idHistogram=plotter->addFunction(histogramFunction);
00157
00158 plotter->addFunctionToMove(histogramFunction);
00159 wxPen mypen1(*wxBLUE, 1, wxSOLID );
00160 mypen1.SetWidth(2);
00161 histogramFunction->SetPen( mypen1 );
00162 }
00163
00164 free(xValues);
00165 free(yValues);
00166
00167 }
00168
00169
00170
00171
00172 void HistogramWidget::drawTransferenceFunction()
00173 {
00174
00175 double xValues[5],yValues[5];
00176
00177 int maxValueGrey=histogram->getMaximumLevelOfGrey();
00178 xValues[0]=0;
00179 xValues[1]=maxValueGrey/16;
00180 xValues[2]=maxValueGrey/8;
00181 xValues[3]=maxValueGrey/16+(maxValueGrey-maxValueGrey/2)/2;
00182 xValues[4]=maxValueGrey;
00183
00184
00185 yValues[0]=0;
00186 yValues[1]=25;
00187 yValues[2]=100;
00188 yValues[3]=25;
00189 yValues[4]=0;
00190
00191 pGraphicalFunction * tf = plotter ->getFunctionForVectors( xValues, 5, yValues, 5 );
00192 printf("EED %p HistogramWidget::drawTransferenceFunction %p\n", this , tf);
00193
00194 if (tf)
00195 {
00196 tf->setType(1);
00197
00198 tf->setActual(true);
00199
00200 tf->SetShowPoints(true);
00201 idTransferenceFunction=plotter->addFunction( tf );
00202 if(type==1)
00203 plotter->addFunctionToMove(tf);
00204 wxPen mypen(*wxBLACK,0.5, wxSOLID );
00205 mypen.SetWidth(2);
00206 tf->SetPen( mypen );
00207 }
00208 }
00209
00210
00211
00212
00213 void HistogramWidget::OnSize(wxSizeEvent &WXUNUSED(event))
00214 {
00215 int scrX,scrY;
00216 GetClientSize(&scrX,&scrY);
00217 plotter->SetSize(scrX,scrY);
00218 pGraphicalFunction* actual=plotter->getFunction(idTransferenceFunction);
00219 if(actual!=NULL){
00220 actual->setScreens(scrX,scrY);
00221 actual->setScales();
00222 }
00223
00224 }
00225
00226
00227
00228 int HistogramWidget::getSizeTransferenceFunction()
00229 {
00230 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00231 return tf->getSizePoints();
00232
00233 }
00234
00235
00236
00237 void HistogramWidget::getTransferenceFunctionPoint(int index,int& x,int& y)
00238 {
00239 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00240 if(tf)
00241 {
00242 wxNode* pnode=tf->GetPointAt(index);
00243 pFunctionPoint* point=(pFunctionPoint*)pnode->GetData();
00244 x=point->getRealX();
00245 y=point->getRealY();
00246 }
00247 }
00248
00249
00250
00251
00252 int HistogramWidget::getHistogramPoint(int gValue)
00253 {
00254 return histogram->getHistogramPoint(gValue);
00255 }
00256
00257
00258
00259 int HistogramWidget::getSizeBarColor()
00260 {
00261 return plotter->getColorPointsSize();
00262 }
00263
00264
00265
00266 void HistogramWidget:: getDataBarColorPoint(int index,int&x, int& red,int& green,int& blue)
00267 {
00268 double tmp=x;
00269 plotter->getBarColorDataAt(index,tmp,red,green,blue);
00270 x=(int)tmp;
00271 }
00272
00273
00274
00275 float HistogramWidget::getMaxShowedPorcentage()
00276 {
00277 float porcentageMaxX=plotter->getMaxShowedPorcentage();
00278 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00279 int min=histogramFunction->getMinX();
00280 float x=porcentageMaxX*(histogramFunction->getMaxX()-min);
00281 return min+ x;
00282 }
00283
00284
00285
00286 float HistogramWidget::getMinShowedPorcentage()
00287 {
00288 float porcentageMinX=plotter->getMinShowedPorcentage();
00289 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00290 int min=histogramFunction->getMinX();
00291 float x=porcentageMinX*(histogramFunction->getMaxX()-min);
00292 return min+ x;
00293 }
00294
00295
00296
00297 float HistogramWidget::getActualShowedPorcentage()
00298 {
00299 float porcentageActualX=plotter->getMinShowedPorcentage();
00300 pGraphicalFunction* histogramFunction=plotter->getFunction(idHistogram);
00301 int min=histogramFunction->getMinX();
00302 float x=porcentageActualX*(histogramFunction->getMaxX()-min);
00303 return min+ x;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312 bool HistogramWidget::addPointToTransferenceFunction(double x, double y)
00313 {
00314 bool result=false;
00315 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00316
00317 if (tf!=NULL) { result=tf->AddPoint(x,y); }
00318
00319 return result;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328 bool HistogramWidget::addColorPoint(double x,int red,int green, int blue)
00329 {
00330 return plotter->addColorPoint(x,red,green,blue);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340 void HistogramWidget::erasePointsTransferenceFunction()
00341 {
00342 if(transferenceFunctionHasPoints)
00343 {
00344
00345 pGraphicalFunction* tf=plotter->getFunction(idTransferenceFunction);
00346 if (tf!=NULL){
00347 int numOfPoints=tf->getSizePoints();
00348 int i=numOfPoints-1;
00349 while(i>=0)
00350 {
00351 tf->deletePointAt(i);
00352 i--;
00353 }
00354 }
00355 }
00356
00357
00358
00359 }
00360
00361
00362
00363 void HistogramWidget::eraseColorPoints()
00364 {
00365 plotter->eraseColorPoints();
00366 }
00367
00368
00369
00370 void HistogramWidget::updatePlotter()
00371 {
00372 plotter->update();
00373 }
00374
00375
00376
00377
00378
00379 void HistogramWidget::setTransferenceFunctionHasPoints(bool hasPoints)
00380 {
00381 transferenceFunctionHasPoints=hasPoints;
00382 }
00383
00384 void HistogramWidget::setTransferenceFunctionHasColor(bool hasColorPoints)
00385 {
00386 transferenceFunctionHasPoints=hasColorPoints;
00387 }
00388 int HistogramWidget::getHistogramSize()
00389 {
00390 return histogramSize;
00391 }
00392 void HistogramWidget::setType(int type)
00393 {
00394 this->type=type;
00395 }
00396
00400 void HistogramWidget::GetValuesPointsFunction(std::vector<double>& greylevel,std::vector<double>& value){
00401 plotter->GetValuesPointsFunction(greylevel,value,histogramSize);
00402 }
00403
00408 void HistogramWidget::GetValuesColorPointsFunction(std::vector<double>& greylevel,
00409 std::vector<double>& red,
00410 std::vector<double>& green,
00411 std::vector<double>& blue)
00412 {
00413 plotter->GetValuesColorPointsFunction(greylevel,red,green,blue);
00414 }