]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmickView.cpp
Added boost signal to validate
[creaImageIO.git] / src2 / creaImageIOGimmickView.cpp
1 #include <creaImageIOGimmickView.h>
2 #include <creaImageIOSystem.h>
3
4 #include "boost/filesystem.hpp"
5
6 namespace fs = boost::filesystem;
7
8 namespace creaImageIO
9 {
10   //======================================================================
11   // CTor
12   GimmickView::GimmickView(Gimmick* gimmick, int threads)
13     : mGimmick(gimmick),
14           mReader(threads)
15   {
16     GimmickDebugMessage(1,"GimmickView::GimmickView"
17                         <<std::endl);
18         // Start the threads ...
19     mReader.Start();
20
21   }
22   //======================================================================
23
24   //======================================================================
25   /// Destructor
26   GimmickView::~GimmickView()
27   {
28     GimmickDebugMessage(1,"GimmickView::~GimmickView"
29                         <<std::endl);
30   }
31    //======================================================================
32  
33   //======================================================================  
34   /// Initializes the view : 
35   /// Creates the TreeViews for all the TreeHandler of the Controller
36   /// 
37   void GimmickView::Initialize()
38   {
39   }
40   //======================================================================
41   
42   //======================================================================
43   /// Finalize 
44   void GimmickView::Finalize()
45   {
46   }
47
48   //======================================================================
49
50   //======================================================================
51   /// Create the tree views 
52   void GimmickView::CreateTreeViews()
53   {
54     GimmickMessage(2,"Creating the tree views"<<std::endl);
55     Gimmick::TreeHandlerMapType::const_iterator i;
56     for (i = mGimmick->GetTreeHandlerMap().begin();
57          i!= mGimmick->GetTreeHandlerMap().end();
58          ++i)
59       {
60         this->CreateTreeView(i->second);
61       }
62   }
63   //======================================================================
64
65   //======================================================================
66   /// Updates the TreeView of given name from level l to bottom
67   /// (calls the virtual method TreeView::Update())
68   void GimmickView::UpdateTreeViewLevel(const std::string& t, int l)
69   {
70     TreeViewMapType::iterator i;
71     i = GetTreeViewMap().find(t);
72     if ( i == GetTreeViewMap().end() )
73       {
74         GimmickError("INTERNAL ERROR : GimmickView::UpdateTreeView : '"
75                      <<t<<"' is not in TreeViewMap");
76       }
77     i->second->UpdateLevel(l);    
78   }
79   //======================================================================
80   ///Validates the dimension compliance of the images with the maximum and 
81   ///minimum given, and between their sizes
82   bool GimmickView::ValidateSelected (std::vector<tree::Node*>& sel, int min_dim, int max_dim)
83   {
84         GimmickMessage(2,"Validating selected"<<std::endl);
85         bool valid=true;
86         int level;
87         std::string mMessage;
88         if(sel.size()>0)
89         {
90                 std::vector<tree::Node*>::iterator i;
91                 std::string row;
92                 std::string col;
93                 std::string plane;
94                 
95                 //Validation between image sizes
96                 for (i=sel.begin(); i!=sel.end() && valid; ++i)
97                 {
98                         if(i==sel.begin())
99                         {
100                                 row=(*i)->GetAttribute("D0028_0010");
101                                 col=(*i)->GetAttribute("D0028_0011");
102                                 plane=(*i)->GetAttribute("D0028_0012");
103                                 level=(*i)->GetLevel();
104                                 
105                         }
106                         else
107                         {
108                                 if(((*i)->GetAttribute("D0028_0010"))!=row ||
109                                         ((*i)->GetAttribute("D0028_0011"))!=col ||
110                                         ((*i)->GetAttribute("D0028_0012"))!=plane)
111                                         {
112                                                 mMessage="The selected images are not compatible.";
113                                                 valid=false;
114                                         }
115                         }
116                 }
117
118                 //Dimention validation
119                 //Compatibility with maximum and minimum
120                         if(valid)
121                         {       
122                                 
123                                 int rows;
124                                 int cols;
125                                 int planes;
126                                 std::string s;
127                                 std::istringstream t(s);
128                                 s=row;
129                                 t >> rows;
130                                 if(row==""){rows=1;}
131                                 s=col;
132                                 t >> cols;
133                                 if(col==""){cols=1;}
134                                 s=plane;
135                                 t >> planes;
136                                 if(plane==""){planes=1;}
137
138                                 int dim = 0;
139                                 if (planes>1) dim=3;
140                                 else if (cols>1) dim=2;
141                                 else if (rows>1) dim=1;
142                             
143                                 if (dim == 0) 
144                                 {
145                                         mMessage="Unknown image dimension : cannot select !";
146                                         valid= false;
147                                 }
148                                 else if (dim>max_dim)
149                                 {
150                                         mMessage="Selecting ";
151                                         mMessage+=dim;
152                                         mMessage+="D images is not allowed !";
153                                         valid= false;
154                                 }
155                                 if ( dim == max_dim )
156                                 {
157                                         mMessage="Cannot add this image to selection : would result in a ";
158                                         mMessage+=(dim+1);
159                                         mMessage+="D image!";
160                                         
161                                         valid= false;
162                                 }
163                                 if ( dim < min_dim && sel.size()<2 )
164                                 {
165                                         GimmickDebugMessage(2, "State Check: Dim: "
166                                                 <<dim
167                                                 <<" Dim min:"
168                                                 <<min_dim
169                                                 <<std::endl);
170                                         mMessage="Cannot build the selection as it would result in a ";
171                                         mMessage+=dim;
172                                         mMessage+="D image, and the minimum is ";
173                                         mMessage+=min_dim;
174                                         mMessage+="D!";
175                                         valid= false;
176                                 }
177                         }
178           }
179         else
180         {
181                 mMessage="Cannot have 0 images selected";
182                 valid=false;
183         }
184         if(valid)
185         {
186                 mMessage="Selection OK !";
187         }
188         mValidationSignal(valid);
189         SetMessage(mMessage);
190         return valid;
191   }
192
193    //======================================================================
194
195    //======================================================================
196   ///Reads Images (Non Threaded)
197   void GimmickView::ReadImagesNotThreaded(std::vector<vtkImageData*>& s,std::vector<tree::Node*> im, int dimension)
198   {
199           
200         // Create the output data
201         if (im.size()==1) 
202       {
203                   
204                 // Only one image : give it
205                 vtkImageData* out = vtkImageData::New();
206                 GimmickMessage(1, "State Check: Full Filename: "
207                                                 <<im.front()->GetAttribute("FullFileName")
208                                                 <<std::endl);
209                 out->ShallowCopy(mReader.GetImage(im.front()->GetAttribute("FullFileName")));
210                 s.push_back( out );
211       }
212           
213     else if (im.size()>1)
214       {
215         vtkImageData* first = mReader.GetImage( im.front()->GetAttribute("FullFileName"));
216         if (dimension==2) 
217           {     
218             // n2D to 3D
219             vtkImageData* out = vtkImageData::New();
220             out->CopyStructure(first);  
221             out->SetScalarType(first->GetScalarType());
222             int ext[6];
223             first->GetExtent(ext);
224             ext[5] = im.size();
225             out->SetExtent(ext);
226             // LG : TODO : Z Spacing  ?
227             
228             out->AllocateScalars();
229             
230             //first->Print(std::cout);
231             //      out->Print(std::cout);
232             
233             int dim[3];
234             first->GetDimensions(dim);
235             unsigned long imsize = 
236               ( (unsigned long)first->GetScalarPointer(0,1,0)
237                 - (unsigned long)first->GetScalarPointer(0,0,0))
238               *dim[1];
239
240             int slice = 0;
241                 std::vector<tree::Node*>::iterator it;
242             for (it=im.begin(); it!=im.end(); ++it) 
243               {
244                 //std::cout << "copying slice "<<slice <<std::endl;
245                           vtkImageData* cur = mReader.GetImage( (*it)->GetAttribute("FullFileName"));
246                 
247                 void* src = cur->GetScalarPointer(0,0,0);
248                 void* dst = out->GetScalarPointer(0,0,slice);
249                 //              std::cout << "src="<<src<<std::endl;
250                 //              std::cout << "dst="<<dst<<std::endl;
251                 //              std::cout << "siz="<<imsize<<std::endl;
252                 memcpy(dst,src,imsize);
253
254                 slice++;
255               }
256             s.push_back(out);
257           }
258         else 
259           {
260             // n3D
261                   std::vector<tree::Node*>::iterator it;
262             for (it=im.begin(); it!=im.end(); ++it) 
263               {
264                 vtkImageData* out = vtkImageData::New();
265                 out->ShallowCopy(mReader.GetImage((*it)->GetAttribute("FullFileName")));
266                 s.push_back(out);
267               }
268           }
269       }
270         
271   }
272
273
274   //======================================================================
275
276   //======================================================================
277   void GimmickView::
278   OnMultiThreadImageReaderEvent(const std::string& filename,
279                                 MultiThreadImageReaderUser::EventType e,
280                                 vtkImageData* image)
281   {
282     GimmickDebugMessage(5,
283                         "MultiThreadImageReader event : "<<e<<std::endl);
284     if (filename.size()==0)
285       {
286         GimmickDebugMessage(5,
287                             "Pushing unknown image in queue"
288                             <<std::endl);
289         mImageEventQueue.push_back(ImageEventType(image));
290         return;
291       }
292     std::map<std::string,tree::Node*>::iterator i;
293     i = mImageFileNameToNode.find(filename);
294     if (i!=mImageFileNameToNode.end())
295       {
296         GimmickDebugMessage(5,
297                             "Pushing image of file '"<<i->second<<"' in queue"
298                             <<std::endl);
299         mImageEventQueue.push_back(ImageEventType(i->second,image));
300       }
301   }
302
303   //====================================================================
304
305   //====================================================================
306   void GimmickView::ConnectValidationObserver(ValidationCallbackType callback)
307   {
308     mValidationSignal.connect(callback);
309   }
310           
311   
312 } // EO namespace creaImageIO
313
314