CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
49 namespace cx
50 {
51 typedef boost::shared_ptr<class SpaceProvider> SpaceProviderPtr;
52 
71 class cxResource_EXPORT StaticMutexVtkLocker
72 {
73 public:
76 private:
77  static boost::shared_ptr<QMutex> mMutex;
78 };
79 
80 
84 class cxResource_EXPORT DataReader
85 {
86 public:
87  virtual ~DataReader()
88  {
89  }
90  virtual bool canLoad(const QString& type, const QString& filename) = 0;
91  virtual DataPtr load(const QString& uid, const QString& filename) = 0;
92  virtual vtkImageDataPtr loadVtkImageData(QString filename) { return vtkImageDataPtr(); }
93  virtual vtkPolyDataPtr loadVtkPolyData(QString filename) { return vtkPolyDataPtr(); }
94  virtual QString canLoadDataType() const =0;
95  virtual bool readInto(DataPtr data, QString path) = 0;
96 
97 };
98 typedef boost::shared_ptr<DataReader> DataReaderPtr;
99 
103 class cxResource_EXPORT MetaImageReader: public DataReader
104 {
105 public:
107  {
108  }
109  virtual bool canLoad(const QString& type, const QString& filename)
110  {
111  QString fileType = QFileInfo(filename).suffix();
112  return (fileType.compare("mhd", Qt::CaseInsensitive) == 0 || fileType.compare("mha", Qt::CaseInsensitive) == 0);
113  }
114  virtual QString canLoadDataType() const { return "image"; }
115  virtual bool readInto(DataPtr data, QString path);
116  bool readInto(ImagePtr image, QString filename);
117  virtual DataPtr load(const QString& uid, const QString& filename);
118 // vtkImageDataPtr load(const QString& filename) { return this->loadVtkImageData(filename); }
119  virtual vtkImageDataPtr loadVtkImageData(QString filename);
120  void saveImage(ImagePtr image, const QString& filename);
121 };
122 
126 class cxResource_EXPORT PNGImageReader: public DataReader
127 {
128 public:
129  virtual ~PNGImageReader() {}
130  virtual bool canLoad(const QString& type, const QString& filename)
131  {
132  QString fileType = QFileInfo(filename).suffix();
133  return (fileType.compare("png", Qt::CaseInsensitive) == 0);
134  }
135  virtual bool readInto(DataPtr data, QString path);
136  bool readInto(ImagePtr image, QString filename);
137  virtual QString canLoadDataType() const { return "image"; }
138  virtual DataPtr load(const QString& uid, const QString& filename);
139  virtual vtkImageDataPtr loadVtkImageData(QString filename);
140 };
141 
142 
146 class cxResource_EXPORT PolyDataMeshReader: public DataReader
147 {
148 public:
150  {
151  }
152  virtual bool canLoad(const QString& type, const QString& filename)
153  {
154  QString fileType = QFileInfo(filename).suffix();
155  return (fileType.compare("vtk", Qt::CaseInsensitive) == 0);
156  }
157  virtual bool readInto(DataPtr data, QString path);
158  bool readInto(MeshPtr mesh, QString filename);
159 
160  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
161  virtual QString canLoadDataType() const { return "mesh"; }
162  virtual DataPtr load(const QString& uid, const QString& filename);
163 };
164 
168 class cxResource_EXPORT StlMeshReader: public DataReader
169 {
170 public:
171  virtual ~StlMeshReader()
172  {
173  }
174  virtual bool canLoad(const QString& type, const QString& filename)
175  {
176  QString fileType = QFileInfo(filename).suffix();
177  return (fileType.compare("stl", Qt::CaseInsensitive) == 0);
178  }
179  virtual bool readInto(DataPtr data, QString path);
180  bool readInto(MeshPtr mesh, QString filename);
181  virtual vtkPolyDataPtr loadVtkPolyData(QString filename);
182  virtual QString canLoadDataType() const { return "mesh"; }
183  virtual DataPtr load(const QString& uid, const QString& filename);
184 };
185 
191 class cxResource_EXPORT DataReaderWriter
192 {
193 public:
194  explicit DataReaderWriter();
195 
196  vtkImageDataPtr loadVtkImageData(QString filename);
197  vtkPolyDataPtr loadVtkPolyData(QString filename);
198  QString findDataTypeFromFile(QString filename);
199  void readInto(DataPtr data, QString path);
200 
201 private:
202  DataReaderPtr findReader(const QString& path, const QString& type="unknown");
203  typedef std::set<DataReaderPtr> DataReadersType;
204  DataReadersType mDataReaders;
205 };
206 
212 } // namespace cx
213 
214 #endif // CXDATAREADERWRITER_H_
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
virtual QString canLoadDataType() const
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
virtual vtkImageDataPtr loadVtkImageData(QString filename)
boost::shared_ptr< class Data > DataPtr
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
virtual QString canLoadDataType() const
virtual bool canLoad(const QString &type, const QString &filename)
Reader for portable network graphics .png 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