Fraxinus  18.10
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) 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 #ifndef CXDATAREADERWRITER_H_
13 #define CXDATAREADERWRITER_H_
14 
15 #include "cxResourceExport.h"
16 #include "cxPrecompiledHeader.h"
17 
18 #include <map>
19 #include <set>
20 #include <string>
21 #include <QMutex>
22 #include <vector>
23 #include "cxImage.h"
24 #include "cxMesh.h"
25 #include <QFileInfo>
26 #include "boost/scoped_ptr.hpp"
27 #include "cxLogger.h"
28 
29 namespace cx
30 {
31 typedef boost::shared_ptr<class SpaceProvider> SpaceProviderPtr;
32 
43 class cxResource_EXPORT DataReader
44 {
45 public:
46  virtual ~DataReader()
47  {
48  }
49  virtual bool canLoad(const QString& type, const QString& filename) = 0;
50  virtual DataPtr load(const QString& uid, const QString& filename) = 0;
51  virtual vtkImageDataPtr loadVtkImageData(QString filename) { return vtkImageDataPtr(); }
52  virtual vtkPolyDataPtr loadVtkPolyData(QString filename) { return vtkPolyDataPtr(); }
53  virtual QString canLoadDataType() const =0;
54  virtual bool readInto(DataPtr data, QString path) = 0;
55 
56 };
57 typedef boost::shared_ptr<DataReader> DataReaderPtr;
58 
62 class cxResource_EXPORT MetaImageReader: public DataReader
63 {
64 public:
65  virtual ~MetaImageReader()
66  {
67  }
68  virtual bool canLoad(const QString& type, const QString& filename)
69  {
70  QString fileType = QFileInfo(filename).suffix();
71  return (fileType.compare("mhd", Qt::CaseInsensitive) == 0 || fileType.compare("mha", Qt::CaseInsensitive) == 0);
72  }
73  virtual QString canLoadDataType() const { return "image"; }
74  virtual bool readInto(DataPtr data, QString filename);
75  bool readInto(ImagePtr image, QString path);
76  virtual DataPtr load(const QString& uid, const QString& filename);
77  virtual vtkImageDataPtr loadVtkImageData(QString filename);
78  void saveImage(ImagePtr image, const QString& filename);
79 };
80 
84 class cxResource_EXPORT NIfTIReader: public DataReader
85 {
86 public:
87  NIfTIReader();
88  virtual ~NIfTIReader()
89  {
90  }
91  virtual bool canLoad(const QString& type, const QString& filename)
92  {
93  QString fileType = QFileInfo(filename).suffix();
94  return (fileType.compare("nii", Qt::CaseInsensitive) == 0);
95  }
96  virtual bool readInto(DataPtr data, QString path);
97  bool readInto(ImagePtr image, QString filename);
98  virtual QString canLoadDataType() const { return "image"; }
99  virtual DataPtr load(const QString& uid, const QString& filename);
100  virtual vtkImageDataPtr loadVtkImageData(QString filename);
101 
102 private:
103  vtkMatrix4x4Ptr sform_matrix;
104 };
105 
109 class cxResource_EXPORT PNGImageReader: public DataReader
110 {
111 public:
112  virtual ~PNGImageReader() {}
113  virtual bool canLoad(const QString& type, const QString& filename)
114  {
115  QString fileType = QFileInfo(filename).suffix();
116  return (fileType.compare("png", Qt::CaseInsensitive) == 0);
117  }
118  virtual bool readInto(DataPtr data, QString path);
119  bool readInto(ImagePtr image, QString filename);
120  virtual QString canLoadDataType() const { return "image"; }
121  virtual DataPtr load(const QString& uid, const QString& filename);
122  virtual vtkImageDataPtr loadVtkImageData(QString filename);
123 };
124 
125 
129 class cxResource_EXPORT PolyDataMeshReader: public DataReader
130 {
131 public:
133  {
134  }
135  virtual bool canLoad(const QString& type, const QString& filename)
136  {
137  QString fileType = QFileInfo(filename).suffix();
138  return ( fileType.compare("vtk", Qt::CaseInsensitive) == 0);
139  }
140  virtual bool readInto(DataPtr data, QString path);
141  bool readInto(MeshPtr mesh, QString filename);
142 
143  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
144  virtual QString canLoadDataType() const { return "mesh"; }
145  virtual DataPtr load(const QString& uid, const QString& filename);
146 };
147 
151 class cxResource_EXPORT XMLPolyDataMeshReader: public DataReader
152 {
153 public:
155  {
156  }
157  virtual bool canLoad(const QString& type, const QString& filename)
158  {
159  QString fileType = QFileInfo(filename).suffix();
160  return ( fileType.compare("vtp", Qt::CaseInsensitive) == 0);
161  }
162  virtual bool readInto(DataPtr data, QString path);
163  bool readInto(MeshPtr mesh, QString filename);
164 
165  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
166  virtual QString canLoadDataType() const { return "mesh"; }
167  virtual DataPtr load(const QString& uid, const QString& filename);
168 };
169 
173 class cxResource_EXPORT StlMeshReader: public DataReader
174 {
175 public:
176  virtual ~StlMeshReader()
177  {
178  }
179  virtual bool canLoad(const QString& type, const QString& filename)
180  {
181  QString fileType = QFileInfo(filename).suffix();
182  return (fileType.compare("stl", Qt::CaseInsensitive) == 0);
183  }
184  virtual bool readInto(DataPtr data, QString path);
185  bool readInto(MeshPtr mesh, QString filename);
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 
191 
195 class cxResource_EXPORT DataReaderWriter
196 {
197 public:
198  explicit DataReaderWriter();
199 
200  vtkImageDataPtr loadVtkImageData(QString filename);
201  vtkPolyDataPtr loadVtkPolyData(QString filename);
202  QString findDataTypeFromFile(QString filename);
203  void readInto(DataPtr data, QString path);
204 
205 private:
206  DataReaderPtr findReader(const QString& path, const QString& type="unknown");
207  typedef std::set<DataReaderPtr> DataReadersType;
208  DataReadersType mDataReaders;
209 };
210 
216 } // namespace cx
217 
218 #endif // CXDATAREADERWRITER_H_
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:37
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:27
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.