00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkTransferFunctionGeneratorWidget.h"
00019
00020 #include <mitkTransferFunctionProperty.h>
00021 #include <mitkRenderingManager.h>
00022 #include <mitkTransferFunctionInitializer.h>
00023 #include <QFileDialog>
00024
00025 #include <SceneSerializationExports.h>
00026 #include <mitkTransferFunctionPropertySerializer.h>
00027 #include <mitkTransferFunctionPropertyDeserializer.h>
00028
00029
00030 static const char* presetNames[] = { "choose an internal transferfunction preset",
00031 "CT Generic",
00032 "CT Black & White",
00033 "CT Thorax large",
00034 "CT Thorax small",
00035 "CT Bone",
00036 "CT Bone (with Gradient)",
00037 "CT Cardiac",
00038 "MR Generic" };
00039
00040 static int numPresetNames = sizeof( presetNames ) / sizeof( char * );
00041
00042 QmitkTransferFunctionGeneratorWidget::QmitkTransferFunctionGeneratorWidget(QWidget* parent,
00043 Qt::WindowFlags f) :
00044 QWidget(parent, f)
00045 {
00046 histoGramm = NULL;
00047
00048 this->setupUi(this);
00049
00050
00051 {
00052 connect( m_CrossLevelWindow, SIGNAL( SignalDeltaMove( int, int ) ), this, SLOT( OnDeltaLevelWindow( int, int ) ) );
00053 }
00054
00055
00056 {
00057 connect( m_CrossThreshold, SIGNAL( SignalDeltaMove( int, int ) ), this, SLOT( OnDeltaThreshold( int, int ) ) );
00058 thDelta = 100;
00059 }
00060
00061
00062 {
00063 for(int r=0; r< numPresetNames; r++)
00064 m_TransferFunctionComboBox->addItem( QString::fromLocal8Bit(presetNames[r]));
00065
00066
00067 connect( m_TransferFunctionComboBox, SIGNAL( activated( int ) ), this, SLOT( OnMitkInternalPreset( int ) ) );
00068
00069 connect( m_SavePreset, SIGNAL( clicked() ), this, SLOT( OnSavePreset() ) );
00070
00071 connect( m_LoadPreset, SIGNAL( clicked() ), this, SLOT( OnLoadPreset() ) );
00072 }
00073
00074 presetFileName = ".";
00075 }
00076
00077
00078
00079
00080 void QmitkTransferFunctionGeneratorWidget::OnSavePreset( )
00081 {
00082 if(tfpToChange.IsNull())
00083 return;
00084
00085 mitk::TransferFunction::Pointer tf = tfpToChange->GetValue();
00086
00087 std::string fileName;
00088 std::string fileNameOutput;
00089
00090 presetFileName = QFileDialog::getSaveFileName( this,"Choose a filename to save the transferfunction",presetFileName, "Transferfunction (*.xml)" );
00091
00092
00093 fileName=presetFileName.toLocal8Bit().constData();
00094
00095 MITK_INFO << "Saving Transferfunction under path: " << fileName;
00096
00097 fileNameOutput= ReduceFileName(fileName);
00098
00099 if ( mitk::TransferFunctionPropertySerializer::SerializeTransferFunction( fileName.c_str(), tf ))
00100 m_InfoPreset->setText( QString( (std::string("saved ")+ fileNameOutput).c_str() ) );
00101 else
00102 m_InfoPreset->setText( QString( std::string("saving failed").c_str() ) );
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 }
00152
00153
00154 void QmitkTransferFunctionGeneratorWidget::OnLoadPreset( )
00155 {
00156 if(tfpToChange.IsNull())
00157 return;
00158
00159 std::string fileName;
00160 std::string fileNameOutput;
00161
00162 presetFileName = QFileDialog::getOpenFileName( this,"Choose a file to open the transferfunction from",presetFileName, "Transferfunction (*.xml)" );
00163
00164 fileName=presetFileName.toLocal8Bit().constData();
00165
00166 MITK_INFO << "Loading Transferfunction from path: " << fileName;
00167
00168 fileNameOutput= ReduceFileName(fileName);
00169
00170 mitk::TransferFunction::Pointer tf = mitk::TransferFunctionPropertyDeserializer::DeserializeTransferFunction(fileName.c_str());
00171
00172 if(tf.IsNotNull())
00173 {
00174
00175
00176
00177
00178
00179 tfpToChange->SetValue( tf );
00180
00181 m_InfoPreset->setText( QString( (std::string("loaded ")+ fileNameOutput).c_str() ) );
00182 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00183 emit SignalUpdateCanvas();
00184
00185
00186
00187
00188
00189
00190
00191
00192 }
00193 }
00194
00195 void QmitkTransferFunctionGeneratorWidget::OnMitkInternalPreset( int mode )
00196 {
00197 if(tfpToChange.IsNull())
00198 return;
00199
00200
00201 if( --mode == -1 )
00202 return;
00203
00204
00205 m_TransferFunctionComboBox->setCurrentIndex( 0 );
00206
00207
00208 mitk::TransferFunctionInitializer::Pointer tfInit = mitk::TransferFunctionInitializer::New(tfpToChange->GetValue());
00209 tfInit->SetTransferFunctionMode(mode);
00210 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00211 emit SignalUpdateCanvas();
00212 m_InfoPreset->setText( QString( (std::string("selected ")+ std::string(presetNames[mode+1])).c_str() ) );
00213 }
00214
00215
00216 static double transformationGlocke ( double x )
00217 {
00218 double z = 0.1;
00219
00220 double a = 2 - 2 * z;
00221
00222 double b = 2 * z - 1;
00223
00224 x = a * x + b;
00225
00226 return x;
00227 }
00228
00229 static double stepFunctionGlocke ( double x )
00230 {
00231 x = 1-(2*x -1.0);
00232 x = x * ( 3*x - 2*x*x );
00233
00234 x = x * x;
00235
00236 return x;
00237 }
00238
00239 void QmitkTransferFunctionGeneratorWidget::OnDeltaLevelWindow(int dx, int dy)
00240 {
00241
00242
00243
00244
00245 if(tfpToChange.IsNull())
00246 return;
00247
00248 thPos += dx;
00249 thDelta -= dy;
00250
00251 if(thDelta < 1)
00252 thDelta = 1;
00253
00254 if(thDelta > 1024)
00255 thDelta = 1024;
00256
00257 if(thPos < histoMinimum)
00258 thPos = histoMinimum;
00259
00260 if(thPos > histoMaximum)
00261 thPos = histoMaximum;
00262
00263 std::stringstream ss;
00264
00265 ss << "Click on the cross and move the mouse"<<"\n"
00266 <<"\n"
00267 << "center at " << thPos << "\n"
00268 << "width " << thDelta * 2;
00269
00270 m_InfoLevelWindow->setText( QString( ss.str().c_str() ) );
00271
00272 mitk::TransferFunction::Pointer tf = tfpToChange->GetValue();
00273
00274
00275 {
00276 vtkPiecewiseFunction *f=tf->GetScalarOpacityFunction();
00277 f->RemoveAllPoints();
00278
00279 for( int r = 0; r<= 6; r++)
00280 {
00281 double relPos = (r / 6.0) * 0.5 + 0.5;
00282 f->AddPoint(thPos+thDelta*(-transformationGlocke(relPos)),stepFunctionGlocke(relPos));
00283 f->AddPoint(thPos+thDelta*( transformationGlocke(relPos)),stepFunctionGlocke(relPos));
00284 }
00285
00286 f->Modified();
00287 }
00288
00289
00290 {
00291 vtkPiecewiseFunction *f=tf->GetGradientOpacityFunction();
00292 f->RemoveAllPoints();
00293
00294
00295 f->AddPoint( 0, 1.0 );
00296 f->Modified();
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 tf->Modified();
00308
00309 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00310 emit SignalUpdateCanvas();
00311 }
00312
00313 static double stepFunctionThreshold ( double x )
00314 {
00315 x = 0.5*x + 0.5;
00316 x = x * ( 3*x - 2*x*x );
00317
00318 x = x * x;
00319
00320 return x;
00321 }
00322
00323 void QmitkTransferFunctionGeneratorWidget::OnDeltaThreshold(int dx, int dy)
00324 {
00325
00326 if(tfpToChange.IsNull())
00327 return;
00328
00329 thPos += dx;
00330 thDelta += dy;
00331
00332 if(thDelta < 1)
00333 thDelta = 1;
00334
00335 if(thDelta > 1024)
00336 thDelta = 1024;
00337
00338 if(thPos < histoMinimum)
00339 thPos = histoMinimum;
00340
00341 if(thPos > histoMaximum)
00342 thPos = histoMaximum;
00343
00344
00345
00346
00347
00348
00349
00350 std::stringstream ss;
00351
00352 ss << "Click on the cross and move the mouse"<<"\n"
00353 <<"\n"
00354 << "threshold at " << thPos << "\n"
00355 << "width " << thDelta * 2;
00356
00357 m_InfoThreshold->setText( QString( ss.str().c_str() ) );
00358
00359 mitk::TransferFunction::Pointer tf = tfpToChange->GetValue();
00360
00361
00362 {
00363 vtkPiecewiseFunction *f=tf->GetScalarOpacityFunction();
00364 f->RemoveAllPoints();
00365
00366 for( int r = 1; r<= 4; r++)
00367 {
00368 double relPos = r / 4.0;
00369 f->AddPoint(thPos+thDelta*(-relPos),stepFunctionThreshold(-relPos));
00370 f->AddPoint(thPos+thDelta*( relPos),stepFunctionThreshold( relPos));
00371 }
00372 f->Modified();
00373 }
00374
00375
00376 {
00377 vtkPiecewiseFunction *f=tf->GetGradientOpacityFunction();
00378 f->RemoveAllPoints();
00379 f->AddPoint( 0, 1.0 );
00380 f->Modified();
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392 tf->Modified();
00393 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00394 emit SignalUpdateCanvas();
00395
00396 }
00397
00398 std::string QmitkTransferFunctionGeneratorWidget::ReduceFileName(std::string fileNameLong )
00399 {
00400 if (fileNameLong.length()< 40)
00401 return fileNameLong;
00402
00403
00404
00405 std::string fileNameShort;
00406 std::string fileNameRevert;
00407
00408 for(unsigned int i=0; i< fileNameLong.length(); i++)
00409 {
00410 if(i<3)
00411 {
00412 char x= fileNameLong[i];
00413 fileNameShort= fileNameShort+x;
00414 }
00415 if(i==3)
00416 {
00417 fileNameShort= fileNameShort+"...";
00418 break;
00419 }
00420 }
00421
00422 unsigned int len( fileNameLong.length() );
00423 for(unsigned int i=len-1; i <= len; i--)
00424 {
00425 std::string x=std::string("")+fileNameLong[i];
00426
00427 if ( x.compare("/")==0 || x.compare("\\")==0)
00428 {
00429 fileNameRevert= "/" + fileNameRevert;
00430 break;
00431 }
00432
00433 if (i>=fileNameLong.length()-24)
00434 {
00435 fileNameRevert= x+ fileNameRevert;
00436
00437 }
00438 else
00439 {
00440 fileNameRevert= "/..." + fileNameRevert;
00441 break;
00442 }
00443
00444 }
00445
00446 return fileNameShort+fileNameRevert;
00447 }
00448
00449 QmitkTransferFunctionGeneratorWidget::~QmitkTransferFunctionGeneratorWidget()
00450 {
00451 }
00452
00453
00454 void QmitkTransferFunctionGeneratorWidget::SetDataNode(mitk::DataNode* node)
00455 {
00456 histoGramm = NULL;
00457
00458 if (node)
00459 {
00460 tfpToChange = dynamic_cast<mitk::TransferFunctionProperty*>(node->GetProperty("TransferFunction"));
00461
00462 if(!tfpToChange)
00463 {
00464 if (! dynamic_cast<mitk::Image*>(node->GetData()))
00465 {
00466 MITK_WARN << "QmitkTransferFunctionGeneratorWidget::SetDataNode called with non-image node";
00467 return;
00468 }
00469
00470 node->SetProperty("TransferFunction", tfpToChange = mitk::TransferFunctionProperty::New() );
00471 dynamic_cast<mitk::TransferFunctionProperty*>(node->GetProperty("TransferFunction"));
00472 }
00473
00474 mitk::TransferFunction::Pointer tf = tfpToChange->GetValue();
00475
00476 if( mitk::Image* image = dynamic_cast<mitk::Image*>( node->GetData() ) )
00477 {
00478
00479 histoMinimum= image->GetScalarValueMin();
00480 histoMaximum= image->GetScalarValueMax();
00481 }
00482
00483 thPos = ( histoMinimum + histoMaximum ) / 2;
00484
00485 }
00486 else
00487 {
00488 tfpToChange = 0;
00489 m_InfoPreset->setText( QString( "" ) );
00490
00491 }
00492 }
00493
00494
00495