From 43b426a4b3555d2210ac65ec1585ce856541fc8b Mon Sep 17 00:00:00 2001 From: apo Date: Tue, 6 Dec 2005 07:14:42 +0000 Subject: [PATCH] To introduce caching for textures --- src/VISU_I/VISU_GaussPoints_i.cc | 26 +----- src/VVTK/VVTK_SegmentationCursorDlg.cxx | 109 ++++++++++++++++++------ src/VVTK/VVTK_SegmentationCursorDlg.h | 16 +++- 3 files changed, 98 insertions(+), 53 deletions(-) diff --git a/src/VISU_I/VISU_GaussPoints_i.cc b/src/VISU_I/VISU_GaussPoints_i.cc index 18097a65..96b02726 100644 --- a/src/VISU_I/VISU_GaussPoints_i.cc +++ b/src/VISU_I/VISU_GaussPoints_i.cc @@ -30,6 +30,7 @@ #include "VISU_Result_i.hh" #include "VISU_GaussPtsAct.h" #include "VISU_GaussPointsPL.hxx" +#include "VVTK_SegmentationCursorDlg.h" #include "VISU_OpenGLPointSpriteMapper.hxx" #include "VISU_ScalarBarCtrl.hxx" @@ -725,28 +726,9 @@ VISU::GaussPoints_i if( !updateMainTexture && !updateAlphaTexture ) return; - QString mainTextureFormat = theMainTexture.section( '.', -1 ); - QString mainTextureVTI = theMainTexture.section( '.', 0, -2 ) + ".vti"; - QString command1 = QString( "VISU_img2vti " ) + mainTextureFormat + - " " + theMainTexture + " " + mainTextureVTI; - //cout << command1.latin1() << endl; - bool convertMainTexture = system( command1.latin1() ) == 0; - - QString alphaTextureFormat = theAlphaTexture.section( '.', -1 ); - QString alphaTextureVTI = theAlphaTexture.section( '.', 0, -2 ) + ".vti"; - QString command2 = QString( "VISU_img2vti " ) + alphaTextureFormat + - " " + theAlphaTexture + " " + alphaTextureVTI; - //cout << command2.latin1() << endl; - bool convertAlphaTexture = system( command2.latin1() ) == 0; - - if( convertMainTexture && convertAlphaTexture ){ - vtkImageData* anImageData = - VISU_GaussPointsPL::MakeTexture( mainTextureVTI.latin1(), - alphaTextureVTI.latin1()); - myGaussPointsPL->SetImageData( anImageData ); - if( anImageData ) - anImageData->Delete(); - } + using namespace VISU; + TTextureValue aTextureValue = GetTexture(theMainTexture,theAlphaTexture); + myGaussPointsPL->SetImageData( aTextureValue.GetPointer() ); } diff --git a/src/VVTK/VVTK_SegmentationCursorDlg.cxx b/src/VVTK/VVTK_SegmentationCursorDlg.cxx index c81d39cb..1a62c496 100644 --- a/src/VVTK/VVTK_SegmentationCursorDlg.cxx +++ b/src/VVTK/VVTK_SegmentationCursorDlg.cxx @@ -31,6 +31,7 @@ #include #include #include +#include #include "utilities.h" @@ -51,6 +52,73 @@ using namespace std; + +//---------------------------------------------------------------- +namespace VISU +{ + inline + QString + Image2VTI(const QString& theImageFileName) + { + QFileInfo aFileInfo(theImageFileName); + QString aFormat = aFileInfo.extension(FALSE); + QString aVTIName = QString("/tmp/") + getenv("USER") + "-" + aFileInfo.baseName(TRUE) + ".vti"; + QString aCommand = QString( "VISU_img2vti " ) + aFormat + " " + theImageFileName + " " + aVTIName; + + if(system( aCommand.latin1() ) == 0) + return aVTIName; + + return QString::null; + } + + inline + void + RemoveFile(const QString& theFileName) + { + if( !theFileName.isNull() ){ + QString aCommand = QString( "rm -fr " ) + theFileName; + system( aCommand.latin1() ); + } + } + + + TTextureValue + GetTexture(const QString& theMainTexture, + const QString& theAlphaTexture) + { + typedef std::pair TTextureKey; + typedef std::map TTextureMap; + + static TTextureMap aTextureMap; + + TTextureValue aTextureValue; + TTextureKey aTextureKey(theMainTexture,theAlphaTexture); + TTextureMap::const_iterator anIter = aTextureMap.find(aTextureKey); + if(anIter != aTextureMap.end()){ + aTextureValue = anIter->second; + }else{ + QString aMainTextureVTI = Image2VTI(theMainTexture); + QString anAlphaTextureVTI = Image2VTI(theAlphaTexture); + + if( !aMainTextureVTI.isNull() && !anAlphaTextureVTI.isNull() ){ + aTextureValue = + VISU_GaussPointsPL::MakeTexture( aMainTextureVTI.latin1(), + anAlphaTextureVTI.latin1()); + + if( aTextureValue.GetPointer() ) + aTextureMap[aTextureKey] = aTextureValue; + } + + RemoveFile(aMainTextureVTI); + RemoveFile(anAlphaTextureVTI); + } + + return aTextureValue; + } +} + + +//---------------------------------------------------------------- VVTK_SegmentationCursorDlg::VVTK_SegmentationCursorDlg( QWidget* parent, const char* name ) :QDialog( parent, name, false, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ), myEventCallbackCommand( vtkCallbackCommand::New() ), @@ -488,9 +556,11 @@ VISU_OutsideCursorSettings* VVTK_SegmentationCursorDlg::GetOutsideCursorSettings return myOutsideCursorSettings.GetPointer(); } -vtkImageData* VVTK_SegmentationCursorDlg::MakeImageData( bool theInside, - const QString& theMainTexture, - const QString& theAlphaTexture ) +VISU::TTextureValue +VVTK_SegmentationCursorDlg +::MakeImageData( bool theInside, + const QString& theMainTexture, + const QString& theAlphaTexture ) { if( theInside ) { @@ -513,25 +583,8 @@ vtkImageData* VVTK_SegmentationCursorDlg::MakeImageData( bool theInside, myOutsideAlphaTexture = theAlphaTexture; } - QString mainTextureFormat = theMainTexture.section( '.', -1 ); - QString mainTextureVTI = theMainTexture.section( '.', 0, -2 ) + ".vti"; - QString command1 = QString( "VISU_img2vti " ) + mainTextureFormat + - " " + theMainTexture + " " + mainTextureVTI; - //cout << command1.latin1() << endl; - bool convertMainTexture = system( command1.latin1() ) == 0; - - QString alphaTextureFormat = theAlphaTexture.section( '.', -1 ); - QString alphaTextureVTI = theAlphaTexture.section( '.', 0, -2 ) + ".vti"; - QString command2 = QString( "VISU_img2vti " ) + alphaTextureFormat + - " " + theAlphaTexture + " " + alphaTextureVTI; - //cout << command2.latin1() << endl; - bool convertAlphaTexture = system( command2.latin1() ) == 0; - - if( !convertMainTexture || !convertAlphaTexture ) - return 0; - - return VISU_GaussPointsPL::MakeTexture( mainTextureVTI.latin1(), - alphaTextureVTI.latin1()); + return VISU::GetTexture( theMainTexture.latin1(), + theAlphaTexture.latin1()); } void VVTK_SegmentationCursorDlg::onClickApply() @@ -594,10 +647,10 @@ void VVTK_SegmentationCursorDlg::ApplyInsideGaussPoints() { QString anInsideMainTexture = myInsidePrimitiveBox->getMainTexture(); QString anInsideAlphaTexture = myInsidePrimitiveBox->getAlphaTexture(); - vtkImageData* aTexture = this->MakeImageData( true, anInsideMainTexture, anInsideAlphaTexture ); + VISU::TTextureValue aTexture = MakeImageData( true, anInsideMainTexture, anInsideAlphaTexture ); - if( aTexture ) - myInsideCursorSettings->SetTexture( aTexture ); + if( aTexture.GetPointer() ) + myInsideCursorSettings->SetTexture( aTexture.GetPointer() ); myInsideCursorSettings->SetInitial( false ); myInsideCursorSettings->SetPrimitiveType( myInsidePrimitiveBox->getPrimitiveType() ); @@ -617,10 +670,10 @@ void VVTK_SegmentationCursorDlg::ApplyOutsideGaussPoints() { QString anOutsideMainTexture = myOutsidePrimitiveBox->getMainTexture(); QString anOutsideAlphaTexture = myOutsidePrimitiveBox->getAlphaTexture(); - vtkImageData* aTexture = this->MakeImageData( false, anOutsideMainTexture, anOutsideAlphaTexture ); + VISU::TTextureValue aTexture = MakeImageData( false, anOutsideMainTexture, anOutsideAlphaTexture ); - if( aTexture ) - myOutsideCursorSettings->SetTexture( aTexture ); + if( aTexture.GetPointer() ) + myOutsideCursorSettings->SetTexture( aTexture.GetPointer() ); myOutsideCursorSettings->SetInitial( false ); myOutsideCursorSettings->SetPrimitiveType( myOutsidePrimitiveBox->getPrimitiveType() ); diff --git a/src/VVTK/VVTK_SegmentationCursorDlg.h b/src/VVTK/VVTK_SegmentationCursorDlg.h index 39c0c118..29f5fed2 100644 --- a/src/VVTK/VVTK_SegmentationCursorDlg.h +++ b/src/VVTK/VVTK_SegmentationCursorDlg.h @@ -39,6 +39,15 @@ class VISU_WidgetCtrl; class VVTK_PrimitiveBox; class VVTK_SizeBox; +namespace VISU +{ + typedef vtkSmartPointer TTextureValue; + + TTextureValue + GetTexture(const QString& theMainTexture, + const QString& theAlphaTexture); +} + //! Segmentation Cursor Dialog. /*! * Uses for set up Segmentation and Outside Cursor Gauss Points preferenses @@ -75,9 +84,10 @@ protected: void ApplyInsideGaussPoints(); void ApplyOutsideGaussPoints(); - vtkImageData* MakeImageData( bool theInside, - const QString& theMainTexture, - const QString& theAlphaTexture ); + VISU::TTextureValue + MakeImageData( bool theInside, + const QString& theMainTexture, + const QString& theAlphaTexture ); private: static void ProcessEvents(vtkObject* theObject, -- 2.39.2