From 5028428b36813a4f690f2ecae8c6669b30ce5d47 Mon Sep 17 00:00:00 2001 From: Jose Luis Guzman Date: Thu, 29 Oct 2015 16:35:44 +0100 Subject: [PATCH] undo redo example Done. MementoState updated. Sphere widget still unsync --- appli/examples/CMakeLists.txt | 2 +- appli/examples/example_SphereWidget.cxx | 6 +- appli/examples/example_Test_Memento.cxx | 272 ++++++++++++------------ lib/cpExtensions/IO/MementoState.cxx | 189 ++++++++-------- lib/cpExtensions/IO/MementoState.h | 34 +-- 5 files changed, 253 insertions(+), 250 deletions(-) diff --git a/appli/examples/CMakeLists.txt b/appli/examples/CMakeLists.txt index c58f673..8a9d5df 100644 --- a/appli/examples/CMakeLists.txt +++ b/appli/examples/CMakeLists.txt @@ -31,7 +31,7 @@ SET( example_HandleWidget example_SeedWidget example_SphereWidget - ##example_Test_Memento + example_Test_Memento ##example_MarchingCubes ##example_OtsuFilter ##example_RGBImageToHSVChannels diff --git a/appli/examples/example_SphereWidget.cxx b/appli/examples/example_SphereWidget.cxx index 8de844a..6ba1e5e 100644 --- a/appli/examples/example_SphereWidget.cxx +++ b/appli/examples/example_SphereWidget.cxx @@ -30,20 +30,20 @@ typedef cpExtensions::Interaction::SphereWidget TSphereWidget; // ------------------------------------------------------------------------- -std::string _path = "C://dev//creatis//cpPlugins//build//Debug//cpPluginsIO.dll"; +std::string _pathIOplugin = "C://dev//creatis//cpPlugins//build//Debug//cpPluginsIO.dll"; std::string _img = "C://img//SphereVolume.mhd"; int main(int argc, char* argv[]) { if (argc >= 3) { - _path = argv[1]; + _pathIOplugin = argv[1]; _img = argv[2]; } // Create interface TInterface plugins; - if (!plugins.Load(_path)) + if (!plugins.Load(_pathIOplugin)) { std::cerr << "Failed to load plugins." << std::endl; return(1); diff --git a/appli/examples/example_Test_Memento.cxx b/appli/examples/example_Test_Memento.cxx index 01f3308..7cbd37a 100644 --- a/appli/examples/example_Test_Memento.cxx +++ b/appli/examples/example_Test_Memento.cxx @@ -1,156 +1,152 @@ #include #include #include +#include -#include #include #include #include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - #include -#include -#include -#include - -typedef itk::Image ImageType; -typedef itk::ImageFileReader ReaderType; -typedef cpPlugins::Interface::Image TPluginImage; +#ifdef WIN32 +#define myWait Sleep(10000); +#else +#define myWait usleep(10000); +#endif + + +// ------------------------------------------------------------------------- +// type definiftions + +typedef cpPlugins::Interface::Interface TInterface; +typedef cpPlugins::Interface::ProcessObject TProcessObject; +typedef cpPlugins::Interface::DataObject TDataObject; +typedef cpPlugins::Interface::Image TImage; +typedef cpPlugins::Interface::Parameters TParameters; +typedef TInterface::TClasses TClasses; + +typedef cpExtensions::Visualization::ImageSliceActors TSliceActors; + +typedef cpExtensions::IO::MementoState TMemento; +typedef cpPlugins::BasicFilters::BinaryErodeImageFilter TBinaryErodeFilter; +// ------------------------------------------------------------------------- + +std::string _pathIOplugin = "C://dev//creatis//cpPlugins//build//Debug//cpPluginsIO.dll"; +std::string _pathBasicFiltersPlugin= "C://dev//creatis//cpPlugins//build//Debug//cpPluginsBasicFilters.dll"; +std::string _img = "C://img//SphereVolume.mhd"; + +int main(int argc, char* argv[]) +{ + if (argc >= 3) + { + _img = argv[1]; + _pathIOplugin = argv[2]; + _pathBasicFiltersPlugin = argv[3]; + } + + // Create interface + TInterface plugins; + if (!plugins.Load(_pathIOplugin) || + !plugins.Load(_pathBasicFiltersPlugin)) + { + std::cerr << "Failed to load plugins." << std::endl; + return(1); + } + + // Create reader + TProcessObject::Pointer reader; + reader = plugins.CreateProcessObject("cpPlugins::IO::ImageReader"); + if (reader.IsNull()) + { + std::cerr + << "No suitable reader found in plugins." << std::endl + << "Reader: " << reader.GetPointer() << std::endl + << std::endl; + return(1); + } + + // Configure reader + TParameters* reader_params = reader->GetParameters(); + reader_params->AddToStringList("FileNames", _img); + + // Execute pipeline + std::string err = reader->Update(); + if (err != "") + { + std::cerr << "ERROR: " << err << std::endl; + return(1); + + } + + TImage* image = reader->GetOutput< TImage >("Output"); -int main() { - auto memento = cpExtensions::IO::MementoState(); - - //here goes the code - - - std::string filePath = "julia_mha.mhd"; - std::string filePathRaw = "julia_mha.raw"; - // Create an image - vtkSmartPointer source = - vtkSmartPointer::New(); - - vtkSmartPointer castFilter = - vtkSmartPointer::New(); - castFilter->SetOutputScalarTypeToUnsignedChar(); - castFilter->SetInputConnection(source->GetOutputPort()); - castFilter->Update(); - - vtkSmartPointer writer = - vtkSmartPointer::New(); - writer->SetInputConnection(castFilter->GetOutputPort()); - writer->SetFileName(filePath.c_str()); - writer->SetRAWFileName(filePathRaw.c_str()); - writer->Write(); - - //ReaderType::Pointer reader = ReaderType::New(); - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filePath.c_str()); - reader->Update(); - //first memento - /* auto obj = TPluginImage::New(); - - auto oo = dynamic_cast (reader->GetOutput()); - obj->SetSource(oo); - memento.SetToMemento(oo);*/ - + TMemento* _memento = new TMemento(); + _memento->SetToMemento(image); + + /* auto retrievedImage = _memento->GetFromMemento(1); + image->SetVTK(retrievedImage); +*/ // filter 1 (state 2) --------------------------------------------------------------- + + TProcessObject::Pointer binaryFilter; + binaryFilter = plugins.CreateProcessObject("cpPlugins::BasicFilters::BinaryErodeImageFilter"); + if (binaryFilter.IsNull()) + { + std::cerr + << "No suitable reader found in plugins." << std::endl + << "Reader: " << binaryFilter.GetPointer() << std::endl + << std::endl; + return(1); + } + + // filter 1 + TDataObject * readerOut = reader->GetOutput< TDataObject >("Output"); + binaryFilter->SetInput("Input", readerOut); + binaryFilter->Update(); + _memento->SetToMemento(binaryFilter->GetOutput< TImage >("Output")); + + myWait; + + vtkSmartPointer retrievedImage = _memento->MementoUndo(); - typedef itk::BinaryBallStructuringElement< - ImageType::PixelType, 2> StructuringElementType; - StructuringElementType structuringElement; - structuringElement.SetRadius(10); - structuringElement.CreateStructuringElement(); - - typedef itk::VTKImageToImageFilter imgToFilter; - imgToFilter::Pointer connectorimg = imgToFilter::New(); - connectorimg->SetInput(reader->GetOutput()); - connectorimg->Update(); - - typedef itk::BinaryErodeImageFilter - BinaryErodeImageFilterType; + // the retrieved image could not be ready if this happens the function returns null + // for the test we made the example wait before try to read but this is the way + // to handle this + if (retrievedImage == nullptr) + { + return (1); + } - BinaryErodeImageFilterType::Pointer erodeFilter - = BinaryErodeImageFilterType::New(); - erodeFilter->SetInput(connectorimg->GetOutput()); - erodeFilter->SetKernel(structuringElement); - - // filter 2 (state 3) --------------------------------------------------------------- - - typedef itk::BinaryThresholdImageFilter - BinaryThresholdImageFilterType; - - BinaryThresholdImageFilterType::Pointer thresholdFilter - = BinaryThresholdImageFilterType::New(); - thresholdFilter->SetInput(erodeFilter->GetOutput()); - thresholdFilter->SetLowerThreshold(10); - thresholdFilter->SetUpperThreshold(30); - thresholdFilter->SetInsideValue(255); - thresholdFilter->SetOutsideValue(0); - - - // filter 3 (state 4) --------------------------------------------------------------- - - typedef itk::InvertIntensityImageFilter - InvertIntensityImageFilterType; - - InvertIntensityImageFilterType::Pointer invertIntensityFilter - = InvertIntensityImageFilterType::New(); - invertIntensityFilter->SetInput(thresholdFilter->GetOutput()); - invertIntensityFilter->SetMaximum(255); - - // undo (state 3) --------------------------------------------------------------- - - // undo (state 2) --------------------------------------------------------------- - - // redo (state 3) --------------------------------------------------------------- - - // visualization -- just for test - - typedef itk::ImageToVTKImageFilter ConnectorType; - ConnectorType::Pointer connector = ConnectorType::New(); - connector->SetInput(invertIntensityFilter->GetOutput()); - connector->Update(); - - vtkSmartPointer actor = - vtkSmartPointer::New(); - actor->GetMapper()->SetInputData(connector->GetOutput()); - - vtkSmartPointer renderer = - vtkSmartPointer::New(); - vtkSmartPointer renderWindow = - vtkSmartPointer::New(); - renderWindow->AddRenderer(renderer); - vtkSmartPointer renderWindowInteractor = - vtkSmartPointer::New(); - renderWindowInteractor->SetRenderWindow(renderWindow); - - renderer->AddActor(actor); - renderer->SetBackground(.2, .3, .4); - - renderWindow->Render(); - renderWindowInteractor->Start(); - - - -//#ifdef _DEBUG -// auto i = 0; -// std::cout << "wait for close"; -// std::cin >> i; -//#endif + //image->SetVTK(retrievedImage.Get()); // this metohd is not implemented yet + + // Configure visualization objects + vtkSmartPointer< vtkRenderer > renderer = + vtkSmartPointer< vtkRenderer >::New(); + renderer->SetBackground(0.1, 0.1, 0.1); + + vtkSmartPointer< vtkRenderWindow > window = + vtkSmartPointer< vtkRenderWindow >::New(); + window->AddRenderer(renderer); + window->SetSize(600, 600); + + // Set up the interaction + vtkSmartPointer< vtkRenderWindowInteractor > interactor = + vtkSmartPointer< vtkRenderWindowInteractor >::New(); + window->SetInteractor(interactor); + + // Create slice actors + vtkSmartPointer< TSliceActors > image_actors = + vtkSmartPointer< TSliceActors >::New(); + //image_actors->AddInputData(image->GetVTK< vtkImageData >(), 2); + image_actors->AddInputData(retrievedImage, 2); + image_actors->PushActorsInto(window); + + // Begin interaction + renderer->ResetCamera(); + window->Render(); + interactor->Start(); return 0; diff --git a/lib/cpExtensions/IO/MementoState.cxx b/lib/cpExtensions/IO/MementoState.cxx index 12045c3..e0dd049 100644 --- a/lib/cpExtensions/IO/MementoState.cxx +++ b/lib/cpExtensions/IO/MementoState.cxx @@ -3,63 +3,26 @@ #include #include +#include + + + // ------------------------------------------------------------------------- cpExtensions::IO::MementoState::MementoState() { - m_Id = 0; + m_maxId = m_Id = 0; + m_stateIt = m_stateReady.begin(); }; // ------------------------------------------------------------------------- -//cpExtensions::IO::MementoState::MementoState(long id, TPluginImage* _img) -//{ -// SetToMemento(_img); -//} -// -//bool -//cpExtensions::IO::MementoState::SetToMemento(TPluginObject * _do) -//{ -// char base[] = "state"; -// std::ostringstream oss; -// this->m_Id++; -// oss << this->m_Id; -// std::string str = oss.str(); -// auto complement = str.c_str(); -// -// std::string fileName(base); -// fileName.append(complement); -// fileName.append(".mhd"); -// -// std::string fileNameRaw(base); -// fileNameRaw.append(complement); -// fileNameRaw.append(".raw"); -//// save(fileName, fileNameRaw, _img); -// -// -// vtkSmartPointer writer = -// vtkSmartPointer::New(); -// -// auto img = TPluginImage::New(); -// img->SetSource(_do); -// -// // writer->SetInputData(_do->GetVTK()); -// writer->SetInputData(img->GetVTK()); -// -// writer->SetFileName(fileName.c_str()); -// writer->SetRAWFileName(fileNameRaw.c_str()); -// writer->Write(); -// -// return true; -//} -// -// bool cpExtensions::IO::MementoState::SetToMemento(cpPlugins::Interface::Image* _img) { char base[] = "state"; std::ostringstream oss; - this->m_Id++; + this->m_Id; oss << this->m_Id; std::string str = oss.str(); auto complement = str.c_str(); @@ -71,53 +34,113 @@ cpExtensions::IO::MementoState::SetToMemento(cpPlugins::Interface::Image* _img) std::string fileNameRaw(base); fileNameRaw.append(complement); fileNameRaw.append(".raw"); - save(fileName, fileNameRaw, _img); + //save(fileName, fileNameRaw, _img); + m_stateReady.insert(m_stateReady.begin() + m_Id, false); + std::async(std::launch::async, &MementoState::Save, this, fileName, fileNameRaw, _img); + + m_stateReady[m_Id] = true; + m_Id++; + + if (m_Id > m_maxId) + { + m_maxId = m_Id; + } return true; } -// -// -//// ------------------------------------------------------------------------- -//vtkSmartPointer -//cpExtensions::IO::MementoState::getMemento(long id) -//{ -// char base[] = "state"; -// std::ostringstream oss; -// oss << id; -// std::string str = oss.str(); -// auto complement = str.c_str(); -// -// std::string fileName(base); -// fileName.append(complement); -// fileName.append(".mhd"); -// return load(fileName); -//} -// + + +// ------------------------------------------------------------------------- +vtkSmartPointer +cpExtensions::IO::MementoState::GetFromMemento(long id) +{ + if (id > m_maxId) + { + return nullptr; + } + + char base[] = "state"; + std::ostringstream oss; + oss << id; + std::string str = oss.str(); + auto complement = str.c_str(); + + std::string fileName(base); + fileName.append(complement); + fileName.append(".mhd"); + return Load(fileName); + /*std::future> result(std::async(std::launch::async, &MementoState::Load, this, fileName)); + return result.get();*/ +} + // ------------------------------------------------------------------------- -void +vtkSmartPointer +cpExtensions::IO::MementoState::MementoUndo() +{ + char base[] = "state"; + std::ostringstream oss; + oss << --this->m_Id; + std::string str = oss.str(); + auto complement = str.c_str(); + + std::string fileName(base); + fileName.append(complement); + fileName.append(".mhd"); + + if (m_stateReady.at(this->m_Id)) + { + return Load(fileName); + } + + return nullptr; +} + + +vtkSmartPointer +cpExtensions::IO::MementoState::MementoRedo() +{ + char base[] = "state"; + std::ostringstream oss; + oss << ++this->m_Id; + std::string str = oss.str(); + auto complement = str.c_str(); + + std::string fileName(base); + fileName.append(complement); + fileName.append(".mhd"); + + if (m_stateReady.at(this->m_Id) && + this->m_Id <= this->m_maxId) + { + return Load(fileName); + } + return nullptr; +} + +// ------------------------------------------------------------------------- +void cpExtensions::IO::MementoState:: -save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img) { - - vtkSmartPointer writer = - vtkSmartPointer::New(); - writer->SetInputData(img->GetVTK< vtkImageData >()); - writer->SetFileName(filename.c_str()); - writer->SetRAWFileName(filenameRaw.c_str()); - writer->Write(); - +Save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img) { + + vtkSmartPointer writer = + vtkSmartPointer::New(); + writer->SetInputData(img->GetVTK< vtkImageData >()); + writer->SetFileName(filename.c_str()); + writer->SetRAWFileName(filenameRaw.c_str()); + writer->Write(); } // ------------------------------------------------------------------------- -vtkSmartPointer -cpExtensions::IO::MementoState::load(const std::string& filename) { - - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filename.c_str()); - reader->Update(); - return reader; - -//return( NULL ); +vtkSmartPointer +cpExtensions::IO::MementoState::Load(const std::string& filename) { + + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(filename.c_str()); + + reader->Update(); + return reader->GetOutput(); + } // eof - $RCSfile$ diff --git a/lib/cpExtensions/IO/MementoState.h b/lib/cpExtensions/IO/MementoState.h index 13a7a4b..2116f36 100644 --- a/lib/cpExtensions/IO/MementoState.h +++ b/lib/cpExtensions/IO/MementoState.h @@ -19,45 +19,29 @@ #include #include - -// ------------------------------------------------------------------------- - - +#include namespace cpExtensions { namespace IO { - //========================================================================== -// Plugins types -//typedef cpPlugins::Interface::Interface TPluginsInterface; -//typedef cpPlugins::Interface::Object TPluginObject; -//typedef cpPlugins::Interface::DataObject TPluginData; -//typedef cpPlugins::Interface::Image TPluginImage; -//typedef cpPlugins::Interface::ImplicitFunction TPluginImplicitFunction; -//typedef cpPlugins::Interface::Mesh TPluginMesh; -//typedef cpPlugins::Interface::ProcessObject TPluginFilter; -//typedef cpPlugins::Interface::Parameters TParameters; -// -//typedef cpExtensions::Visualization::MPRObjects TMPRObjects; - class cpExtensions_EXPORT MementoState { private: long m_Id; - + long m_maxId; + std::vector m_stateReady; + std::vector::iterator m_stateIt; public: MementoState(); -// MementoState(long id, cpPlugins::Interface::Image * _img); - bool SetToMemento(cpPlugins::Interface::Image* _img); -// bool SetToMemento(itk::ProcessObject po); - -// vtkSmartPointer getMemento(long id); + vtkSmartPointer MementoUndo(); + vtkSmartPointer MementoRedo(); + vtkSmartPointer GetFromMemento(long id); private: - void save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img); - vtkSmartPointer load(const std::string& filename); + void Save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img); + vtkSmartPointer Load(const std::string& filename); }; } -- 2.45.2