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