Fraxinus  18.10
An IGT application
cxImageDataContainer.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxImageDataContainer.h"
13 #include <QDir>
14 #include <vtkImageImport.h>
15 #include <vtkImageData.h>
16 #include "cxDataReaderWriter.h"
17 #include "cxLogger.h"
18 #include "cxTypeConversions.h"
19 #include "cxUtilHelpers.h"
20 
21 typedef vtkSmartPointer<class vtkImageImport> vtkImageImportPtr;
22 
23 namespace cx
24 {
25 
27 {
28  mFilename = filename;
29  mImageData = image;
30 }
31 
33 {
34 }
35 
36 
37 
39 {
40  if (!mImageData)
41  {
42  mImageData = MetaImageReader().loadVtkImageData(mFilename);
43  }
44  return mImageData;
45 }
46 
48 {
49  mImageData = vtkImageDataPtr();
50  return true;
51 }
52 
56 
58 {
59  int count = 0;
60  for (unsigned i=0; i<this->size(); ++i)
61  {
62  if (this->purge(i))
63  count++;
64  }
65 // double fraction = double(count)/this->size();
66 // std::cout << QString("PurgeAll imagecache: purged %1\% of %2 frames.").arg(fraction).arg(this->size()) << std::endl;
67 }
68 
72 
73 
74 CachedImageDataContainer::CachedImageDataContainer(QString baseFilename, int size) :
75  mDeleteFilesOnRelease(false)
76 {
77  QFileInfo info(baseFilename);
78 
79  for (int i=0; true; ++i)
80  {
81  QString file = info.absolutePath()+"/"+info.completeBaseName()+QString("_%1.mhd").arg(i);
82 
83  if (!QFileInfo(file).exists())
84  break;
85 
86  CachedImageDataPtr cache(new CachedImageData(file));
87  mImages.push_back(cache);
88 
89  if (size>=0 && i>=size)
90  break;
91  }
92 
93  if (size>=0)
94  {
95  CX_ASSERT(size == mImages.size());
96  }
97 }
98 
100  mDeleteFilesOnRelease(false)
101 {
102 }
103 
105  mDeleteFilesOnRelease(false)
106 {
107  for (unsigned i=0; i<frames.size(); ++i)
108  this->append(frames[i]);
109 }
110 
111 void CachedImageDataContainer::append(QString filename)
112 {
113  mImages.push_back(CachedImageDataPtr(new CachedImageData(filename)));
114 }
115 
117 {
118  if (mDeleteFilesOnRelease)
119  {
120  for (unsigned i=0; i<mImages.size(); ++i)
121  {
122  QDir().remove(mImages[i]->getFilename());
123  QDir().remove(changeExtension(mImages[i]->getFilename(), "raw"));
124  QDir().remove(changeExtension(mImages[i]->getFilename(), "zraw"));
125  }
126  }
127 }
128 
130 {
131  CX_ASSERT(index < this->size());
132  CX_ASSERT(mImages[index]);
133 
134  if (index >= this->size())
135  {
136  std::cout << QString("Attempt to call index %1, size=%2").arg(index).arg(this->size()) << std::endl;
137  }
138 // int* a = NULL;
139 // *a = 5;
140  vtkImageDataPtr retval = mImages[index]->getImage();
141  mImages[index]->purge();
142  return retval;
143 }
144 
146 {
147  return mImages[index]->getFilename();
148 }
149 
150 
152 {
153  return (unsigned)mImages.size();
154 }
155 
159 
161 {
162  mOptionalWholeBase = image3D;
163  mImages.resize(image3D->GetDimensions()[2]);
164 
165  for (int i=0; i<mImages.size(); ++i)
166  {
167  vtkImageImportPtr import = vtkImageImportPtr::New();
168 
169  import->SetImportVoidPointer(image3D->GetScalarPointer(0,0,i));
170  import->SetDataScalarType(image3D->GetScalarType());
171  import->SetDataSpacing(image3D->GetSpacing());
172  import->SetNumberOfScalarComponents(image3D->GetNumberOfScalarComponents());
173 // int* extent = image3D->GetWholeExtent();
174 // int* extent = image3D->GetExtent();
175  IntBoundingBox3D extent(image3D->GetExtent());
176  extent[4] = 0;
177  extent[5] = 0;
178  import->SetWholeExtent(extent.data());
179  import->SetDataExtentToWholeExtent();
180 
181  import->Update();
182  mImages[i] = import->GetOutput();
183 
184 // std::cout << "SplitFramesContainer " << i << std::endl;
185 // std::cout << "===================================" << std::endl;
186 // mImages[i]->Print(std::cout);
187  }
188 }
189 
191 {
192  return mImages[index];
193 }
194 
196 {
197  return (unsigned)mImages.size();
198 }
199 
200 
204 
205 FramesDataContainer::FramesDataContainer(std::vector<vtkImageDataPtr> images)
206 {
207  mImages = images;
208 }
209 
211 {
212  return mImages[index];
213 }
214 
216 {
217  return mImages.size();
218 }
219 
220 } // namespace cx
vtkImageDataPtr getImage()
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
boost::shared_ptr< CachedImageData > CachedImageDataPtr
virtual vtkImageDataPtr loadVtkImageData(QString filename)
virtual QString getFilename(unsigned index)
virtual vtkImageDataPtr get(unsigned index)
CachedImageData(QString filename, vtkImageDataPtr image=NULL)
virtual unsigned size() const
virtual unsigned size() const
virtual unsigned size() const
Representation of an integer bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
QString changeExtension(QString name, QString ext)
virtual vtkImageDataPtr get(unsigned index)
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Reader for metaheader .mhd files.
SplitFramesContainer(vtkImageDataPtr image3D)
virtual vtkImageDataPtr get(unsigned index)
FramesDataContainer(std::vector< vtkImageDataPtr > images)
Namespace for all CustusX production code.