]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/imageUndoRedo/image3DDequeUR.cxx
#3332 creaContours Bug New - Manual Paint UnDo ReDo with vtk update (working)
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / imageUndoRedo / image3DDequeUR.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        image3DDequeUR.cxx
28  * @brief       This file contains the implementation of the Image3DDequeUR 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 "image3DDequeUR.h"
35 #include <iostream>
36 #include <sstream>
37 #include <ctime>
38
39 // ----------------------------------------------------------------------------------
40 Image3DDequeUR::Image3DDequeUR( )
41 {
42 #ifdef _WIN32
43         this->m_GlobalPath = std::getenv("TEMP");
44 #endif
45 #ifdef _WIN64
46         this->m_GlobalPath = std::getenv("TEMP");
47 #endif
48 #ifdef LINUX
49         this->m_GlobalPath = "/tmp/";
50 #endif // MACOSX
51         this->m_CurrentURPos = -1;
52 }
53
54 // ----------------------------------------------------------------------------------
55 //virtual
56 Image3DDequeUR::~Image3DDequeUR( )
57 {
58         this->CleanHardDisk( );
59 }
60
61 // ----------------------------------------------------------------------------------
62 //virtual
63 void Image3DDequeUR::AddImagesToURContainer( VTKImageDataPointerType imgUndo,
64         VTKImageDataPointerType imgRedo, ImageMManager* imMManager )
65 {
66         this->CleanURContainerFromIndex( this->m_CurrentURPos + 1 );
67         //Adding image
68         ImageInfoUR* imageInfo = new ImageInfoUR( );
69         imageInfo->SetImageName( this->GetImageName( this->m_ImgURDeque.size( ) ) );
70         imageInfo->SetImages( imgUndo, imgRedo );
71         imageInfo->SetImageMManager( imMManager );
72         //Adding to deque
73         this->m_ImgURDeque.push_back( imageInfo );
74         this->m_CurrentURPos = this->m_ImgURDeque.size( ) - 1;
75         //Managing memory
76         this->ManageMemory( );
77 }
78
79 // ----------------------------------------------------------------------------------
80 //virtual
81 ImageInfoUR* Image3DDequeUR::Undo( )
82 {
83         ImageInfoUR* imgRet = NULL;
84         int imgURDequeSize = this->m_ImgURDeque.size( );
85         if( ( this->m_ImgURDeque.size( ) > 0 ) && ( this->m_CurrentURPos != -1 )
86                 && ( this->m_CurrentURPos < imgURDequeSize ) )
87         {
88                 imgRet = this->m_ImgURDeque[ m_CurrentURPos ];
89                 this->m_CurrentURPos--;
90         } //fi
91         return ( imgRet );
92 }
93
94 // ----------------------------------------------------------------------------------
95 //virtual
96 ImageInfoUR* Image3DDequeUR::Redo( )
97 {
98         ImageInfoUR* imgRet = NULL;
99         this->m_CurrentURPos++;
100         int imgURDequeSize = this->m_ImgURDeque.size( );
101         if( ( this->m_ImgURDeque.size( ) > 0 ) && ( this->m_CurrentURPos != -1 )
102                 && ( this->m_CurrentURPos < imgURDequeSize ) )
103         {
104                 imgRet = this->m_ImgURDeque[ m_CurrentURPos ];
105         } else {
106                 this->m_CurrentURPos--;
107         }
108         return ( imgRet );
109 }
110
111 // ----------------------------------------------------------------------------------
112 //virtual
113 void Image3DDequeUR::CleanURContainerFromIndex( const int& index )
114 {
115         int count = 0;
116         for( unsigned int i = index; i < this->m_ImgURDeque.size( ); i++ )
117         {
118                 this->m_ImgURDeque[ i ]->RemoveImagesFromMemory( this->m_GlobalPath );
119                 this->m_ImgURDeque[ i ]->RemoveImagesFromDisk( this->m_GlobalPath );
120                 count++;
121         }
122         for( int i = 0; i < count; i++ )
123         {
124                 this->m_ImgURDeque.pop_back( );
125         } //rof
126 }
127
128 // ----------------------------------------------------------------------------------
129 //virtual
130 void Image3DDequeUR::ManageMemory( )
131 {
132         int imgURDequeSize = this->m_ImgURDeque.size( );
133         int i;
134         for( i = 0; i < imgURDequeSize; i++ )
135         {
136                 if( this->m_ImgURDeque[ i ]->GetStatus( ) )
137                 {
138                         this->m_ImgURDeque[ i ]->RemoveImagesFromMemory( this->m_GlobalPath );
139                 } // if Status
140         } // for i
141           //Adding CurrentUndoPos to memory
142         if( ( this->m_CurrentURPos != -1 ) && !( this->m_ImgURDeque[ this->m_CurrentURPos ]->GetStatus( ) ) )
143         {
144                 this->m_ImgURDeque[ this->m_CurrentURPos ]->LoadImagesToMemory( this->m_GlobalPath );
145         }
146         int currentRedoPos = this->m_CurrentURPos + 1;
147         if( ( currentRedoPos < imgURDequeSize ) && !( this->m_ImgURDeque[ currentRedoPos ]->GetStatus( ) ) )
148         {
149                 this->m_ImgURDeque[ currentRedoPos ]->LoadImagesToMemory( this->m_GlobalPath );
150         }
151 }
152
153 // ----------------------------------------------------------------------------------
154 void Image3DDequeUR::SetGlobalPath( const StringType& globalPath )
155 {
156         this->m_GlobalPath = globalPath;
157 }
158
159 // ----------------------------------------------------------------------------------
160 void Image3DDequeUR::CleanHardDisk( )
161 {
162         for( unsigned int i = 0; i < this->m_ImgURDeque.size( ); i++ )
163         {
164                 this->m_ImgURDeque[ i ]->RemoveImagesFromDisk( this->m_GlobalPath );
165         }
166 }
167
168 // ----------------------------------------------------------------------------------
169 Image3DDequeUR::StringType Image3DDequeUR::GetGlobalPath( )
170 {
171         return ( this->m_GlobalPath );
172 }
173
174 // ----------------------------------------------------------------------------------
175 Image3DDequeUR::StringType Image3DDequeUR::GetImageName( const int & pos )
176 {
177 //Giving a name to an image using the date and time
178         if( this->m_IDImages.empty( ) )
179         {
180                 time_t rawtime;
181                 struct tm * timeinfo;
182                 char buffer[ 80 ];
183                 time( &rawtime );
184                 timeinfo = localtime( &rawtime );
185                 strftime( buffer, 80, "%H%M%S_%a_%d_%b_%y_", timeinfo );
186                 StringType date( buffer );
187                 StringType aux( buffer );
188                 this->m_IDImages = "img_" + aux;
189         }
190         std::stringstream ss; //create a stringstream
191         ss << pos; //add number to the stream
192         StringType imageName = this->m_IDImages + ss.str( );
193         return ( imageName );
194 }
195
196 // ----------------------------------------------------------------------------------