]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/imageUndoRedo.cxx
#3332 creaContours Bug New - Manual Paint UnDo ReDo with vtk update (working)
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / imageUndoRedo / imageUndoRedo.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*!
27  * @file        imageUndoRedo.cxx
28  * @brief       This file contains the implementation of the ImageUndoRedo class.
29  * @author      Info-Dev
30  * @author      Diego CACERES (diego.caceres[AT]creatis.insa-lyon.fr)
31  * @date        2011-11-15
32  */
33
34 #include "imageUndoRedo.h"
35
36 // ----------------------------------------------------------------------------------
37 ImageUndoRedo::ImageUndoRedo( )
38 {
39         this->m_ImagesDeque = new IDequeType( );
40 }
41 // ----------------------------------------------------------------------------------
42 //virtual
43 ImageUndoRedo::~ImageUndoRedo( )
44 {
45
46 }
47 // ----------------------------------------------------------------------------------
48 //virtual
49 void ImageUndoRedo::Undo( )
50 {
51 printf("EED ImageUndoRedo::Undo Start\n");
52         ImageInfoUR* imageInfo = this->m_ImagesDeque->Undo( );
53         if( imageInfo != NULL)
54         {
55                 this->DrawUR( imageInfo, true );
56                 this->UpdateUndoImage( );
57         } //if  imageInfo
58 printf("EED ImageUndoRedo::Undo End\n");
59
60 }
61 // ----------------------------------------------------------------------------------
62 //virtual
63 void ImageUndoRedo::Redo( )
64 {
65         ImageInfoUR* imageInfo = this->m_ImagesDeque->Redo( );
66         if( imageInfo != NULL)
67         {
68                 this->DrawUR( imageInfo, false );
69                 this->UpdateUndoImage( );
70         }
71 }
72 // ----------------------------------------------------------------------------------
73 void ImageUndoRedo::SetImage( VTKImageDataPointerType image )
74 {
75         this->m_CurrentImage = image;
76
77 //EED 2017-01-01 Migration VTK7
78 #if VTK_MAJOR_VERSION <= 5
79         this->m_CurrentImage->Update( );
80 #else
81         this->m_CurrentImage->Modified( );
82 #endif
83
84         this->UpdateUndoImage( );
85 }
86
87 // ----------------------------------------------------------------------------------
88 //virtual
89 void ImageUndoRedo::SetURImages( ImageMManagerType* imMManager )
90 {
91         ImageMManagerType* newImageManager = new ImageMManagerType( imMManager );
92         if( newImageManager->ValidateRegion( ) )
93         {
94                 RegionSType region = newImageManager->GetModifiedRegion( );
95                 VTKImageDataPointerType imgUndo = this->GetImageRegion( region,this->m_UndoImage );
96                 VTKImageDataPointerType imgRedo = this->GetImageRegion( region,this->m_CurrentImage );
97                 this->m_ImagesDeque->AddImagesToURContainer( imgUndo, imgRedo,newImageManager );
98                 this->UpdateUndoImage( );
99         } else {
100                 std::cerr << "INVALID REGION" << std::endl;
101         }
102 }
103
104 // ----------------------------------------------------------------------------------
105 void ImageUndoRedo::UpdateUndoImage( )
106 {
107 //EED 2017-01-01 Migration VTK7
108 #if VTK_MAJOR_VERSION <= 5
109         this->m_CurrentImage->Update( );
110 #else
111         this->m_CurrentImage->Modified( );
112 #endif
113         this->m_UndoImage = VTKImageDataPointerType::New( );
114         this->m_UndoImage->DeepCopy( m_CurrentImage );
115 //EED 2017-01-01 Migration VTK7
116 #if VTK_MAJOR_VERSION <= 5
117         this->m_UndoImage->Update( );
118 #else
119         this->m_UndoImage->Modified( );
120 #endif
121 }
122
123 // ----------------------------------------------------------------------------------
124 ImageUndoRedo::VTKImageDataPointerType ImageUndoRedo::GetImageRegion( const RegionSType& region, VTKImageDataPointerType img )
125 {
126 printf("EED ImageUndoRedo::GetImageRegion Start\n");
127         VTKExtractVOIPointerType extract = VTKExtractVOIPointerType::New( );
128         extract->SetVOI( region.minX, region.maxX, region.minY, region.maxY,region.minZ, region.maxZ );
129
130 printf("EED ImageUndoRedo::GetImageRegion region %d %d   %d %d   %d %d\n", region.minX, region.maxX, region.minY, region.maxY,region.minZ, region.maxZ );
131
132         extract->SetSampleRate( 1, 1, 1 );
133 //EED 2017-01-01 Migration VTK7
134 #if VTK_MAJOR_VERSION <= 5
135         extract->SetInput( img );
136 #else
137         extract->SetInputData( img );
138         extract->Update();
139 #endif
140         VTKImageDataPointerType imgResult = extract->GetOutput( );
141
142 //EED 2017-01-01 Migration VTK7
143 #if VTK_MAJOR_VERSION <= 5
144         imgResult->Update( );
145 #else
146         imgResult->Modified( );
147 #endif
148
149 printf("EED ImageUndoRedo::GetImageRegion End\n");
150         return ( imgResult );
151 }
152 // ----------------------------------------------------------------------------------
153 void ImageUndoRedo::SetCurrentImage( VTKImageDataPointerType img )
154 {
155         this->m_CurrentImage = img;
156 }
157 // ----------------------------------------------------------------------------------
158 //virtual
159 void ImageUndoRedo::DrawUR( ImageInfoUR* imageInfo, const bool& undo )
160 {
161 printf("EED ImageUndoRedo::DrawUR Start\n");
162         VTKImageDataPointerType img;
163         if( undo )
164         {
165                 img = imageInfo->GetUndoImage( );
166         } else {
167                 img = imageInfo->GetRedoImage( );
168         } //else
169         RegionSType region = imageInfo->GetImageMManager( )->GetModifiedRegion( );
170         if( img != NULL)
171         {
172                 int *dim                        = img->GetDimensions( );
173 printf("EED ImageUndoRedo::DrawUR dim = %d %d %d\n", dim[0],dim[1],dim[2]);
174                 int ext[6]; img->GetExtent( ext );
175 printf("EED ImageUndoRedo::DrawUR ext = %d %d   %d %d    %d %d\n", ext[0],ext[1],ext[2],ext[3],ext[4],ext[5] );
176         
177                 int sizeXImageIn        = dim[ 0 ];
178                 size_t linesize         = sizeXImageIn * sizeof(unsigned short);
179                 for( int j = region.minY, y = 0; j <= region.maxY; j++, y++ )
180                 {
181                         for( int k = region.minZ, z = 0; k <= region.maxZ; k++, z++ )
182                         {
183                                 void* ptrsrc    = img->GetScalarPointer( 0, y, z );
184                                 void* ptrdest   = this->m_CurrentImage->GetScalarPointer( region.minX, j, k );
185                                 memcpy( ptrdest, ptrsrc, linesize );
186                         } // for k
187                 } // for j
188                 this->m_CurrentImage->Modified( );
189         } // if img
190         this->m_ImagesDeque->ManageMemory( );
191 printf("EED ImageUndoRedo::DrawUR End\n");
192 }
193 // ----------------------------------------------------------------------------------