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