]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/IO/MementoState.cxx
Merge ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / lib / cpExtensions / IO / MementoState.cxx
1 #include <cpExtensions/IO/MementoState.h>
2
3
4 #include <vtkMetaImageReader.h>
5 #include <vtkMetaImageWriter.h>
6 #include <future>
7
8
9
10
11
12 // -------------------------------------------------------------------------
13 cpExtensions::IO::MementoState::MementoState()
14 {
15   m_maxId = m_Id = 0;
16   m_stateIt = m_stateReady.begin();
17 };
18
19 // -------------------------------------------------------------------------
20 bool
21 cpExtensions::IO::MementoState::SetToMemento(cpPlugins::Interface::Image* _img)
22 {
23   char base[] = "state";
24   std::ostringstream oss;
25   this->m_Id;
26   oss << this->m_Id;
27   std::string str = oss.str();
28   auto complement = str.c_str();
29
30   std::string fileName(base);
31   fileName.append(complement);
32   fileName.append(".mhd");
33
34   std::string fileNameRaw(base);
35   fileNameRaw.append(complement);
36   fileNameRaw.append(".raw");
37   //save(fileName, fileNameRaw, _img);
38   m_stateReady.insert(m_stateReady.begin() + m_Id, false);
39   std::async(std::launch::async, &MementoState::Save, this, fileName, fileNameRaw, _img);
40
41   m_stateReady[m_Id] = true;
42   m_Id++;
43
44   if (m_Id > m_maxId)
45   {
46     m_maxId = m_Id;
47   }
48
49   return true;
50 }
51
52
53 // -------------------------------------------------------------------------
54 vtkSmartPointer<vtkImageData>
55 cpExtensions::IO::MementoState::GetFromMemento(long id)
56 {
57   if (id > m_maxId)
58   {
59     return nullptr;
60   }
61
62   char base[] = "state";
63   std::ostringstream oss;
64   oss << id;
65   std::string str = oss.str();
66   auto complement = str.c_str();
67
68   std::string fileName(base);
69   fileName.append(complement);
70   fileName.append(".mhd");
71   return Load(fileName);
72   /*std::future<vtkSmartPointer<vtkImageData>> result(std::async(std::launch::async, &MementoState::Load, this, fileName));
73   return result.get();*/
74 }
75
76 // -------------------------------------------------------------------------
77 vtkSmartPointer<vtkImageData>
78 cpExtensions::IO::MementoState::MementoUndo()
79 {
80   char base[] = "state";
81   std::ostringstream oss;
82   oss << --this->m_Id;
83   std::string str = oss.str();
84   auto complement = str.c_str();
85
86   std::string fileName(base);
87   fileName.append(complement);
88   fileName.append(".mhd");
89
90   if (m_stateReady.at(this->m_Id))
91   {
92     return Load(fileName);
93   }
94
95   return nullptr;
96 }
97
98
99 vtkSmartPointer<vtkImageData>
100 cpExtensions::IO::MementoState::MementoRedo()
101 {
102   char base[] = "state";
103   std::ostringstream oss;
104   oss << ++this->m_Id;
105   std::string str = oss.str();
106   auto complement = str.c_str();
107
108   std::string fileName(base);
109   fileName.append(complement);
110   fileName.append(".mhd");
111
112   if (m_stateReady.at(this->m_Id) &&
113     this->m_Id <= this->m_maxId)
114   {
115     return Load(fileName);
116   }
117   return nullptr;
118 }
119
120 // -------------------------------------------------------------------------
121 void
122 cpExtensions::IO::MementoState::
123 Save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img) {
124
125   vtkSmartPointer<vtkMetaImageWriter> writer =
126     vtkSmartPointer<vtkMetaImageWriter>::New();
127   writer->SetInputData(img->GetVTK< vtkImageData >());
128   writer->SetFileName(filename.c_str());
129   writer->SetRAWFileName(filenameRaw.c_str());
130   writer->Write();
131 }
132
133 // -------------------------------------------------------------------------
134 vtkSmartPointer<vtkImageData>
135 cpExtensions::IO::MementoState::Load(const std::string& filename) {
136
137   vtkSmartPointer<vtkMetaImageReader> reader =
138     vtkSmartPointer<vtkMetaImageReader>::New();
139   reader->SetFileName(filename.c_str());
140
141   reader->Update();
142   return reader->GetOutput();
143
144 }
145
146 // eof - $RCSfile$