]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/imageUndoRedo.cxx
DFCH: ManualPaint + imageUndoRedo: Undo/Redo functionality its now working =) =)
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / imageUndoRedo / imageUndoRedo.cxx
1 /*!
2  * @file        imageUndoRedo.cxx
3  * @brief       This file contains the implementation of the ImageUndoRedo class.
4  * @author      Info-Dev
5  * @author      Diego CACERES (diego.caceres[AT]creatis.insa-lyon.fr)
6  * @date        2011-11-15
7  */
8
9 #include "imageUndoRedo.h"
10
11 // ----------------------------------------------------------------------------------
12 ImageUndoRedo::ImageUndoRedo() {
13         this->m_ImagesDeque = new IDequeType();
14 }
15 // ----------------------------------------------------------------------------------
16 //virtual
17 ImageUndoRedo::~ImageUndoRedo() {
18
19 }
20 // ----------------------------------------------------------------------------------
21 //virtual
22 void ImageUndoRedo::Undo() {
23         ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo();
24         if (imageInfo != NULL) {
25                 this->DrawUR(imageInfo, true);
26                 this->UpdateUndoImage();
27         }
28 }
29 // ----------------------------------------------------------------------------------
30 //virtual
31 void ImageUndoRedo::Redo() {
32         ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo();
33         if (imageInfo != NULL) {
34                 this->DrawUR(imageInfo, false);
35                 this->UpdateUndoImage();
36         }
37 }
38 // ----------------------------------------------------------------------------------
39 void ImageUndoRedo::SetImage(VTKImageDataPointerType image) {
40         this->m_CurrentImage = image;
41         this->m_CurrentImage->Update();
42         this->UpdateUndoImage();
43 }
44 // ----------------------------------------------------------------------------------
45 //virtual
46 void ImageUndoRedo::SetURImages(ImageMManagerType* imMManager) {
47         ImageMManagerType* newImageManager = new ImageMManagerType(imMManager);
48         if (newImageManager->ValidateRegion()) {
49                 RegionSType region = newImageManager->GetModifiedRegion();
50                 VTKImageDataPointerType imgUndo = this->GetImageRegion(region,
51                                 this->m_UndoImage);
52                 VTKImageDataPointerType imgRedo = this->GetImageRegion(region,
53                                 this->m_CurrentImage);
54                 this->m_ImagesDeque->AddImagesToURContainer(imgUndo, imgRedo,
55                                 newImageManager);
56                 this->UpdateUndoImage();
57         } else {
58                 std::cerr << "INVALID REGION" << std::endl;
59         }
60 }
61 // ----------------------------------------------------------------------------------
62 void ImageUndoRedo::UpdateUndoImage() {
63         this->m_CurrentImage->Update();
64         this->m_UndoImage = VTKImageDataPointerType::New();
65         this->m_UndoImage->DeepCopy(m_CurrentImage);
66         this->m_UndoImage->Update();
67 }
68 // ----------------------------------------------------------------------------------
69 ImageUndoRedo::VTKImageDataPointerType ImageUndoRedo::GetImageRegion(
70                 const RegionSType& region, VTKImageDataPointerType img) {
71         VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New();
72         extract->SetVOI(region.minX, region.maxX, region.minY, region.maxY,
73                         region.minZ, region.maxZ);
74         extract->SetSampleRate(1, 1, 1);
75         extract->SetInput(img);
76         VTKImageDataPointerType imgResult = extract->GetOutput();
77         imgResult->Update();
78         return (imgResult);
79 }
80 // ----------------------------------------------------------------------------------
81 void ImageUndoRedo::SetCurrentImage(VTKImageDataPointerType img) {
82         this->m_CurrentImage = img;
83 }
84 // ----------------------------------------------------------------------------------
85 //virtual
86 void ImageUndoRedo::DrawUR(ImageInfoUR* imageInfo, const bool& undo) {
87         VTKImageDataPointerType img;
88         if (undo) {
89                 img = imageInfo->GetUndoImage();
90         } //fi
91         else {
92                 img = imageInfo->GetRedoImage();
93         } //else
94         RegionSType region = imageInfo->GetImageMManager()->GetModifiedRegion();
95         if (img != NULL) {
96                 for (int i = region.minX, x = 0; i <= region.maxX; i++, x++) {
97                         for (int j = region.minY, y = 0; j <= region.maxY; j++, y++) {
98                                 for (int k = region.minZ, z = 0; k <= region.maxZ; k++, z++) {
99                                         float value = img->GetScalarComponentAsFloat(x, y, z, 0);
100                                         this->m_CurrentImage->SetScalarComponentFromFloat(i, j, k,
101                                                         0, value);
102                                 } //rof
103                         } //rof
104                 } //rof
105                 this->m_CurrentImage->Modified();
106         }
107         this->m_ImagesDeque->ManageMemory();
108 }
109 // ----------------------------------------------------------------------------------