Fraxinus  2023.01.05-dev+develop.0da12
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 "cxFileManagerService.h"
17 #include "cxLogger.h"
18 #include "cxTypeConversions.h"
19 #include "cxUtilHelpers.h"
20 #include "cxBoundingBox3D.h"
21 
22 typedef vtkSmartPointer<class vtkImageImport> vtkImageImportPtr;
23 
24 namespace cx
25 {
26 
28 {
29  mFilename = filename;
30  mImageData = image;
31 }
32 
34 {
35 }
36 
37 
38 
40 {
41  if (!mImageData)
42  {
43  //mImageData = MetaImageReader().loadVtkImageData(mFilename);
44  mImageData = filemanager->loadVtkImageData(mFilename);
45  }
46  return mImageData;
47 }
48 
50 {
51  mImageData = vtkImageDataPtr();
52  return true;
53 }
54 
58 
60 {
61  int count = 0;
62  for (unsigned i=0; i<this->size(); ++i)
63  {
64  if (this->purge(i))
65  count++;
66  }
67 // double fraction = double(count)/this->size();
68 // std::cout << QString("PurgeAll imagecache: purged %1\% of %2 frames.").arg(fraction).arg(this->size()) << std::endl;
69 }
70 
74 
75 
76 CachedImageDataContainer::CachedImageDataContainer(QString baseFilename, int size, FileManagerServicePtr filemanagerservice) :
77  mDeleteFilesOnRelease(false),
78  mFileManagerService(filemanagerservice)
79 {
80  QFileInfo info(baseFilename);
81 
82  for (int i=0; true; ++i)
83  {
84  QString file = info.absolutePath()+"/"+info.completeBaseName()+QString("_%1.mhd").arg(i);
85 
86  if (!QFileInfo(file).exists())
87  break;
88 
89  CachedImageDataPtr cache(new CachedImageData(file));
90  mImages.push_back(cache);
91 
92  if (size>=0 && i>=size)
93  break;
94  }
95 
96  if (size>=0)
97  {
98  CX_ASSERT(size == mImages.size());
99  }
100 }
101 
102 CachedImageDataContainer::CachedImageDataContainer(FileManagerServicePtr filemanagerservice) :
103  mDeleteFilesOnRelease(false),
104  mFileManagerService(filemanagerservice)
105 {
106 }
107 
108 CachedImageDataContainer::CachedImageDataContainer(std::vector<QString> frames, FileManagerServicePtr filemanagerservice) :
109  mDeleteFilesOnRelease(false),
110  mFileManagerService(filemanagerservice)
111 {
112  for (unsigned i=0; i<frames.size(); ++i)
113  this->append(frames[i]);
114 }
115 
116 void CachedImageDataContainer::append(QString filename)
117 {
118  mImages.push_back(CachedImageDataPtr(new CachedImageData(filename)));
119 }
120 
122 {
123  if (mDeleteFilesOnRelease)
124  {
125  for (unsigned i=0; i<mImages.size(); ++i)
126  {
127  QDir().remove(mImages[i]->getFilename());
128  QDir().remove(changeExtension(mImages[i]->getFilename(), "raw"));
129  QDir().remove(changeExtension(mImages[i]->getFilename(), "zraw"));
130  }
131  }
132 }
133 
135 {
136  CX_ASSERT(index < this->size());
137  CX_ASSERT(mImages[index]);
138 
139  if (index >= this->size())
140  {
141  std::cout << QString("Attempt to call index %1, size=%2").arg(index).arg(this->size()) << std::endl;
142  }
143 // int* a = NULL;
144 // *a = 5;
145  vtkImageDataPtr retval = mImages[index]->getImage(mFileManagerService);
146  mImages[index]->purge();
147  return retval;
148 }
149 
151 {
152  return mImages[index]->getFilename();
153 }
154 
155 
157 {
158  return (unsigned)mImages.size();
159 }
160 
164 
166 {
167  mOptionalWholeBase = image3D;
168  mImages.resize(image3D->GetDimensions()[2]);
169 
170  for (int i=0; i<mImages.size(); ++i)
171  {
172  vtkImageImportPtr import = vtkImageImportPtr::New();
173 
174  import->SetImportVoidPointer(image3D->GetScalarPointer(0,0,i));
175  import->SetDataScalarType(image3D->GetScalarType());
176  import->SetDataSpacing(image3D->GetSpacing());
177  import->SetNumberOfScalarComponents(image3D->GetNumberOfScalarComponents());
178 // int* extent = image3D->GetWholeExtent();
179 // int* extent = image3D->GetExtent();
180  IntBoundingBox3D extent(image3D->GetExtent());
181  extent[4] = 0;
182  extent[5] = 0;
183  import->SetWholeExtent(extent.data());
184  import->SetDataExtentToWholeExtent();
185 
186  import->Update();
187  mImages[i] = import->GetOutput();
188 
189 // std::cout << "SplitFramesContainer " << i << std::endl;
190 // std::cout << "===================================" << std::endl;
191 // mImages[i]->Print(std::cout);
192  }
193 }
194 
196 {
197  return mImages[index];
198 }
199 
201 {
202  return (unsigned)mImages.size();
203 }
204 
205 
209 
210 FramesDataContainer::FramesDataContainer(std::vector<vtkImageDataPtr> images)
211 {
212  mImages = images;
213 }
214 
216 {
217  return mImages[index];
218 }
219 
221 {
222  return mImages.size();
223 }
224 
225 } // namespace cx
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
#define CX_ASSERT(statement)
Definition: cxLogger.h:116
boost::shared_ptr< CachedImageData > CachedImageDataPtr
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)
vtkImageDataPtr getImage(FileManagerServicePtr filemanager)
virtual vtkImageDataPtr get(unsigned index)
vtkSmartPointer< class vtkImageImport > vtkImageImportPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
SplitFramesContainer(vtkImageDataPtr image3D)
virtual vtkImageDataPtr get(unsigned index)
FramesDataContainer(std::vector< vtkImageDataPtr > images)
Namespace for all CustusX production code.