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