Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxImageDataContainer.h"
34 #include <QDir>
35 #include <vtkImageImport.h>
36 #include <vtkImageData.h>
37 #include "cxDataReaderWriter.h"
38 #include "cxLogger.h"
39 #include "cxTypeConversions.h"
40 #include "cxUtilHelpers.h"
41 
42 typedef vtkSmartPointer<class vtkImageImport> vtkImageImportPtr;
43 
44 namespace cx
45 {
46 
48 {
49  mFilename = filename;
50  mImageData = image;
51 }
52 
54 {
55 }
56 
57 
58 
60 {
61  if (!mImageData)
62  {
63  mImageData = MetaImageReader().loadVtkImageData(mFilename);
64  }
65  return mImageData;
66 }
67 
69 {
70  mImageData = vtkImageDataPtr();
71  return true;
72 }
73 
77 
79 {
80  int count = 0;
81  for (unsigned i=0; i<this->size(); ++i)
82  {
83  if (this->purge(i))
84  count++;
85  }
86 // double fraction = double(count)/this->size();
87 // std::cout << QString("PurgeAll imagecache: purged %1\% of %2 frames.").arg(fraction).arg(this->size()) << std::endl;
88 }
89 
93 
94 
95 CachedImageDataContainer::CachedImageDataContainer(QString baseFilename, int size) :
96  mDeleteFilesOnRelease(false)
97 {
98  QFileInfo info(baseFilename);
99 
100  for (int i=0; true; ++i)
101  {
102  QString file = info.absolutePath()+"/"+info.completeBaseName()+QString("_%1.mhd").arg(i);
103 
104  if (!QFileInfo(file).exists())
105  break;
106 
107  CachedImageDataPtr cache(new CachedImageData(file));
108  mImages.push_back(cache);
109 
110  if (size>=0 && i>=size)
111  break;
112  }
113 
114  if (size>=0)
115  {
116  CX_ASSERT(size == mImages.size());
117  }
118 }
119 
121  mDeleteFilesOnRelease(false)
122 {
123 }
124 
126  mDeleteFilesOnRelease(false)
127 {
128  for (unsigned i=0; i<frames.size(); ++i)
129  this->append(frames[i]);
130 }
131 
132 void CachedImageDataContainer::append(QString filename)
133 {
134  mImages.push_back(CachedImageDataPtr(new CachedImageData(filename)));
135 }
136 
138 {
139  if (mDeleteFilesOnRelease)
140  {
141  for (unsigned i=0; i<mImages.size(); ++i)
142  {
143  QDir().remove(mImages[i]->getFilename());
144  QDir().remove(changeExtension(mImages[i]->getFilename(), "raw"));
145  QDir().remove(changeExtension(mImages[i]->getFilename(), "zraw"));
146  }
147  }
148 }
149 
151 {
152  CX_ASSERT(index < this->size());
153  CX_ASSERT(mImages[index]);
154 
155  if (index >= this->size())
156  {
157  std::cout << QString("Attempt to call index %1, size=%2").arg(index).arg(this->size()) << std::endl;
158  }
159 // int* a = NULL;
160 // *a = 5;
161  vtkImageDataPtr retval = mImages[index]->getImage();
162  mImages[index]->purge();
163  return retval;
164 }
165 
167 {
168  return mImages[index]->getFilename();
169 }
170 
171 
173 {
174  return (unsigned)mImages.size();
175 }
176 
180 
182 {
183  mOptionalWholeBase = image3D;
184  mImages.resize(image3D->GetDimensions()[2]);
185 
186  for (int i=0; i<mImages.size(); ++i)
187  {
188  vtkImageImportPtr import = vtkImageImportPtr::New();
189 
190  import->SetImportVoidPointer(image3D->GetScalarPointer(0,0,i));
191  import->SetDataScalarType(image3D->GetScalarType());
192  import->SetDataSpacing(image3D->GetSpacing());
193  import->SetNumberOfScalarComponents(image3D->GetNumberOfScalarComponents());
194 // int* extent = image3D->GetWholeExtent();
195 // int* extent = image3D->GetExtent();
196  IntBoundingBox3D extent(image3D->GetExtent());
197  extent[4] = 0;
198  extent[5] = 0;
199  import->SetWholeExtent(extent.data());
200  import->SetDataExtentToWholeExtent();
201 
202  import->Update();
203  mImages[i] = import->GetOutput();
204 
205 // std::cout << "SplitFramesContainer " << i << std::endl;
206 // std::cout << "===================================" << std::endl;
207 // mImages[i]->Print(std::cout);
208  }
209 }
210 
212 {
213  return mImages[index];
214 }
215 
217 {
218  return (unsigned)mImages.size();
219 }
220 
221 
225 
226 FramesDataContainer::FramesDataContainer(std::vector<vtkImageDataPtr> images)
227 {
228  mImages = images;
229 }
230 
232 {
233  return mImages[index];
234 }
235 
237 {
238  return mImages.size();
239 }
240 
241 } // namespace cx
vtkImageDataPtr getImage()
#define CX_ASSERT(statement)
Definition: cxLogger.h:131
boost::shared_ptr< CachedImageData > CachedImageDataPtr
virtual vtkImageDataPtr loadVtkImageData(QString filename)
virtual QString getFilename(unsigned index)
virtual bool purge(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.
virtual unsigned size() const =0
SplitFramesContainer(vtkImageDataPtr image3D)
virtual vtkImageDataPtr get(unsigned index)
FramesDataContainer(std::vector< vtkImageDataPtr > images)