]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/IO/MementoState.cxx
sphere widget done, finally
[cpPlugins.git] / lib / cpExtensions / IO / MementoState.cxx
index 12045c3d28c87e8ad351b1ffccf3835b0fbf969a..e0dd0493287518489bf013fdb16de1e08deeff7f 100644 (file)
@@ -3,63 +3,26 @@
 
 #include <vtkMetaImageReader.h>
 #include <vtkMetaImageWriter.h>
+#include <future>
+
+
+
 
 
 // -------------------------------------------------------------------------
 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<vtkMetaImageWriter> writer =
-//    vtkSmartPointer<vtkMetaImageWriter>::New();
-//
-//  auto img = TPluginImage::New();
-//  img->SetSource(_do);
-//
-// // writer->SetInputData(_do->GetVTK<vtkDataObject>());
-//  writer->SetInputData(img->GetVTK<vtkDataObject>());
-//  
-//  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<vtkMetaImageReader>
-//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<vtkImageData>
+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<vtkSmartPointer<vtkImageData>> result(std::async(std::launch::async, &MementoState::Load, this, fileName));
+  return result.get();*/
+}
+
 // -------------------------------------------------------------------------
-void 
+vtkSmartPointer<vtkImageData>
+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<vtkImageData>
+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<vtkMetaImageWriter> writer =
-               vtkSmartPointer<vtkMetaImageWriter>::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<vtkMetaImageWriter> writer =
+    vtkSmartPointer<vtkMetaImageWriter>::New();
+  writer->SetInputData(img->GetVTK< vtkImageData >());
+  writer->SetFileName(filename.c_str());
+  writer->SetRAWFileName(filenameRaw.c_str());
+  writer->Write();
 }
 
 // -------------------------------------------------------------------------
-vtkSmartPointer<vtkMetaImageReader>
-cpExtensions::IO::MementoState::load(const std::string& filename) {
-  
-       vtkSmartPointer<vtkMetaImageReader> reader =
-               vtkSmartPointer<vtkMetaImageReader>::New();
-       reader->SetFileName(filename.c_str());
-       reader->Update();
-       return reader;
-  
-//return( NULL );
+vtkSmartPointer<vtkImageData>
+cpExtensions::IO::MementoState::Load(const std::string& filename) {
+
+  vtkSmartPointer<vtkMetaImageReader> reader =
+    vtkSmartPointer<vtkMetaImageReader>::New();
+  reader->SetFileName(filename.c_str());
+
+  reader->Update();
+  return reader->GetOutput();
+
 }
 
 // eof - $RCSfile$