1 #include <cpExtensions/IO/MementoState.h>
4 #include <vtkMetaImageReader.h>
5 #include <vtkMetaImageWriter.h>
12 // -------------------------------------------------------------------------
13 cpExtensions::IO::MementoState::MementoState()
16 m_stateIt = m_stateReady.begin();
19 // -------------------------------------------------------------------------
21 cpExtensions::IO::MementoState::SetToMemento(cpPlugins::Interface::Image* _img)
23 char base[] = "state";
24 std::ostringstream oss;
27 std::string str = oss.str();
28 auto complement = str.c_str();
30 std::string fileName(base);
31 fileName.append(complement);
32 fileName.append(".mhd");
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);
41 m_stateReady[m_Id] = true;
53 // -------------------------------------------------------------------------
54 vtkSmartPointer<vtkImageData>
55 cpExtensions::IO::MementoState::GetFromMemento(long id)
62 char base[] = "state";
63 std::ostringstream oss;
65 std::string str = oss.str();
66 auto complement = str.c_str();
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();*/
76 // -------------------------------------------------------------------------
77 vtkSmartPointer<vtkImageData>
78 cpExtensions::IO::MementoState::MementoUndo()
80 char base[] = "state";
81 std::ostringstream oss;
83 std::string str = oss.str();
84 auto complement = str.c_str();
86 std::string fileName(base);
87 fileName.append(complement);
88 fileName.append(".mhd");
90 if (m_stateReady.at(this->m_Id))
92 return Load(fileName);
99 vtkSmartPointer<vtkImageData>
100 cpExtensions::IO::MementoState::MementoRedo()
102 char base[] = "state";
103 std::ostringstream oss;
105 std::string str = oss.str();
106 auto complement = str.c_str();
108 std::string fileName(base);
109 fileName.append(complement);
110 fileName.append(".mhd");
112 if (m_stateReady.at(this->m_Id) &&
113 this->m_Id <= this->m_maxId)
115 return Load(fileName);
120 // -------------------------------------------------------------------------
122 cpExtensions::IO::MementoState::
123 Save(const std::string& filename, const std::string& filenameRaw, cpPlugins::Interface::Image* img) {
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());
133 // -------------------------------------------------------------------------
134 vtkSmartPointer<vtkImageData>
135 cpExtensions::IO::MementoState::Load(const std::string& filename) {
137 vtkSmartPointer<vtkMetaImageReader> reader =
138 vtkSmartPointer<vtkMetaImageReader>::New();
139 reader->SetFileName(filename.c_str());
142 return reader->GetOutput();