Fraxinus  17.12-rc2
An IGT application
cxDataReaderWriter.h
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 #ifndef CXDATAREADERWRITER_H_
34 #define CXDATAREADERWRITER_H_
35 
36 #include "cxResourceExport.h"
37 #include "cxPrecompiledHeader.h"
38 
39 #include <map>
40 #include <set>
41 #include <string>
42 #include <QMutex>
43 #include <vector>
44 #include "cxImage.h"
45 #include "cxMesh.h"
46 #include <QFileInfo>
47 #include "boost/scoped_ptr.hpp"
48 #include "cxLogger.h"
49 
50 namespace cx
51 {
52 typedef boost::shared_ptr<class SpaceProvider> SpaceProviderPtr;
53 
64 class cxResource_EXPORT DataReader
65 {
66 public:
67  virtual ~DataReader()
68  {
69  }
70  virtual bool canLoad(const QString& type, const QString& filename) = 0;
71  virtual DataPtr load(const QString& uid, const QString& filename) = 0;
72  virtual vtkImageDataPtr loadVtkImageData(QString filename) { return vtkImageDataPtr(); }
73  virtual vtkPolyDataPtr loadVtkPolyData(QString filename) { return vtkPolyDataPtr(); }
74  virtual QString canLoadDataType() const =0;
75  virtual bool readInto(DataPtr data, QString path) = 0;
76 
77 };
78 typedef boost::shared_ptr<DataReader> DataReaderPtr;
79 
83 class cxResource_EXPORT MetaImageReader: public DataReader
84 {
85 public:
86  virtual ~MetaImageReader()
87  {
88  }
89  virtual bool canLoad(const QString& type, const QString& filename)
90  {
91  QString fileType = QFileInfo(filename).suffix();
92  return (fileType.compare("mhd", Qt::CaseInsensitive) == 0 || fileType.compare("mha", Qt::CaseInsensitive) == 0);
93  }
94  virtual QString canLoadDataType() const { return "image"; }
95  virtual bool readInto(DataPtr data, QString filename);
96  bool readInto(ImagePtr image, QString path);
97  virtual DataPtr load(const QString& uid, const QString& filename);
98  virtual vtkImageDataPtr loadVtkImageData(QString filename);
99  void saveImage(ImagePtr image, const QString& filename);
100 };
101 
105 class cxResource_EXPORT NIfTIReader: public DataReader
106 {
107 public:
108  NIfTIReader();
109  virtual ~NIfTIReader()
110  {
111  }
112  virtual bool canLoad(const QString& type, const QString& filename)
113  {
114  QString fileType = QFileInfo(filename).suffix();
115  return (fileType.compare("nii", Qt::CaseInsensitive) == 0);
116  }
117  virtual bool readInto(DataPtr data, QString path);
118  bool readInto(ImagePtr image, QString filename);
119  virtual QString canLoadDataType() const { return "image"; }
120  virtual DataPtr load(const QString& uid, const QString& filename);
121  virtual vtkImageDataPtr loadVtkImageData(QString filename);
122 
123 private:
124  vtkMatrix4x4Ptr sform_matrix;
125 };
126 
130 class cxResource_EXPORT PNGImageReader: public DataReader
131 {
132 public:
133  virtual ~PNGImageReader() {}
134  virtual bool canLoad(const QString& type, const QString& filename)
135  {
136  QString fileType = QFileInfo(filename).suffix();
137  return (fileType.compare("png", Qt::CaseInsensitive) == 0);
138  }
139  virtual bool readInto(DataPtr data, QString path);
140  bool readInto(ImagePtr image, QString filename);
141  virtual QString canLoadDataType() const { return "image"; }
142  virtual DataPtr load(const QString& uid, const QString& filename);
143  virtual vtkImageDataPtr loadVtkImageData(QString filename);
144 };
145 
146 
150 class cxResource_EXPORT PolyDataMeshReader: public DataReader
151 {
152 public:
154  {
155  }
156  virtual bool canLoad(const QString& type, const QString& filename)
157  {
158  QString fileType = QFileInfo(filename).suffix();
159  return ( fileType.compare("vtk", Qt::CaseInsensitive) == 0);
160  }
161  virtual bool readInto(DataPtr data, QString path);
162  bool readInto(MeshPtr mesh, QString filename);
163 
164  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
165  virtual QString canLoadDataType() const { return "mesh"; }
166  virtual DataPtr load(const QString& uid, const QString& filename);
167 };
168 
172 class cxResource_EXPORT XMLPolyDataMeshReader: public DataReader
173 {
174 public:
176  {
177  }
178  virtual bool canLoad(const QString& type, const QString& filename)
179  {
180  QString fileType = QFileInfo(filename).suffix();
181  return ( fileType.compare("vtp", Qt::CaseInsensitive) == 0);
182  }
183  virtual bool readInto(DataPtr data, QString path);
184  bool readInto(MeshPtr mesh, QString filename);
185 
186  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
187  virtual QString canLoadDataType() const { return "mesh"; }
188  virtual DataPtr load(const QString& uid, const QString& filename);
189 };
190 
194 class cxResource_EXPORT StlMeshReader: public DataReader
195 {
196 public:
197  virtual ~StlMeshReader()
198  {
199  }
200  virtual bool canLoad(const QString& type, const QString& filename)
201  {
202  QString fileType = QFileInfo(filename).suffix();
203  return (fileType.compare("stl", Qt::CaseInsensitive) == 0);
204  }
205  virtual bool readInto(DataPtr data, QString path);
206  bool readInto(MeshPtr mesh, QString filename);
207  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
208  virtual QString canLoadDataType() const { return "mesh"; }
209  virtual DataPtr load(const QString& uid, const QString& filename);
210 };
211 
212 
216 class cxResource_EXPORT DataReaderWriter
217 {
218 public:
219  explicit DataReaderWriter();
220 
221  vtkImageDataPtr loadVtkImageData(QString filename);
222  vtkPolyDataPtr loadVtkPolyData(QString filename);
223  QString findDataTypeFromFile(QString filename);
224  void readInto(DataPtr data, QString path);
225 
226 private:
227  DataReaderPtr findReader(const QString& path, const QString& type="unknown");
228  typedef std::set<DataReaderPtr> DataReadersType;
229  DataReadersType mDataReaders;
230 };
231 
237 } // namespace cx
238 
239 #endif // CXDATAREADERWRITER_H_
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:58
virtual QString canLoadDataType() const
virtual bool canLoad(const QString &type, const QString &filename)
virtual QString canLoadDataType() const
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
virtual bool canLoad(const QString &type, const QString &filename)
boost::shared_ptr< DataReader > DataReaderPtr
Reader for NIfTI files.
virtual bool canLoad(const QString &type, const QString &filename)
virtual QString canLoadDataType() const
virtual vtkImageDataPtr loadVtkImageData(QString filename)
boost::shared_ptr< class Data > DataPtr
virtual QString canLoadDataType() const
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
Read or write vtk or ssc data objects from/to file.
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
virtual QString canLoadDataType() const
virtual bool canLoad(const QString &type, const QString &filename)
Reader for portable network graphics .png files.
Reader for .vtp files.
virtual bool canLoad(const QString &type, const QString &filename)
Reader for .vtk files.
virtual bool canLoad(const QString &type, const QString &filename)
boost::shared_ptr< class Mesh > MeshPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Reader for metaheader .mhd files.
Reader for STL files.
Interface for Data file readers.
virtual QString canLoadDataType() const
Namespace for all CustusX production code.