NorMIT-nav  22.09
An IGT application
cxPolyDataMeshReader.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 "cxPolyDataMeshReader.h"
13 
14 #include <QFileInfo>
15 #include <vtkPolyDataReader.h>
16 #include <vtkPolyDataWriter.h>
17 #include <vtkPolyData.h>
18 #include <ctkPluginContext.h>
19 #include "cxMesh.h"
20 #include "cxErrorObserver.h"
21 #include "cxTypeConversions.h"
22 #include "cxLogger.h"
23 
24 namespace cx
25 {
26 
27 bool PolyDataMeshReader::readInto(DataPtr data, QString filename)
28 {
29  return this->readInto(boost::dynamic_pointer_cast<Mesh>(data), filename);
30 }
31 
32 bool PolyDataMeshReader::readInto(MeshPtr mesh, QString filename)
33 {
34  if (!mesh)
35  return false;
36  vtkPolyDataPtr raw = this->loadVtkPolyData(filename);
37  if(!raw)
38  return false;
39  mesh->setVtkPolyData(raw);
40  return true;
41 }
42 
44 {
45  vtkPolyDataReaderPtr reader = vtkPolyDataReaderPtr::New();
46  reader->SetFileName(cstring_cast(fileName));
47 
48  if (!ErrorObserver::checkedRead(reader, fileName))
49  return vtkPolyDataPtr();
50 
51  vtkPolyDataPtr polyData = reader->GetOutput();
52  return polyData;
53 }
54 
56 {
57  return Mesh::getTypeName();
58 }
59 
60 DataPtr PolyDataMeshReader::read(const QString& uid, const QString& filename)
61 {
62  MeshPtr mesh(new Mesh(uid));
63  this->readInto(mesh, filename);
64  return mesh;
65 }
66 
67 std::vector<DataPtr> PolyDataMeshReader::read(const QString &filename)
68 {
69  std::vector<DataPtr> retval;
70  MeshPtr mesh = boost::dynamic_pointer_cast<Mesh>(this->createData(Mesh::getTypeName(), filename));
71 
72  vtkPolyDataPtr raw = this->loadVtkPolyData(filename);
73  if(!raw)
74  return retval;
75  mesh->setVtkPolyData(raw);
76 
77  retval.push_back(mesh);
78  return retval;
79 }
80 
81 void PolyDataMeshReader::write(DataPtr data, const QString &filename)
82 {
83  MeshPtr mesh = boost::dynamic_pointer_cast<Mesh>(data);
84  if(!mesh)
85  reportError("Could not cast data to mesh");
86  vtkPolyDataWriterPtr writer = vtkPolyDataWriterPtr::New();
87  writer->SetInputData(mesh->getVtkPolyData());
88  writer->SetFileName(cstring_cast(filename));
89 
90  writer->Update();
91  writer->Write();
92 }
93 
95  FileReaderWriterImplService("PolyDataMeshReader", Mesh::getTypeName(), Mesh::getTypeName(), "vtk", patientModelService)
96 {
97 }
98 
99 bool PolyDataMeshReader::canRead(const QString &type, const QString &filename)
100 {
101  QString fileType = QFileInfo(filename).suffix();
102  return ( fileType.compare("vtk", Qt::CaseInsensitive) == 0);
103 }
104 
105 
106 }
107 
108 
110 {
111  return Mesh::getTypeName();
112 }
113 
114 bool cx::PolyDataMeshReader::canWrite(const QString &type, const QString &filename) const
115 {
116  return this->canWriteInternal(type, filename);
117 }
cxLogger.h
cx::PolyDataMeshReader::canWrite
bool canWrite(const QString &type, const QString &filename) const
Definition: cxPolyDataMeshReader.cpp:114
cx::PolyDataMeshReader::readInto
virtual bool readInto(DataPtr data, QString path)
Definition: cxPolyDataMeshReader.cpp:27
cx::PolyDataMeshReader::read
virtual DataPtr read(const QString &uid, const QString &filename)
Definition: cxPolyDataMeshReader.cpp:60
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::PolyDataMeshReader::loadVtkPolyData
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
Definition: cxPolyDataMeshReader.cpp:43
cstring_cast
cstring_cast_Placeholder cstring_cast(const T &val)
Definition: cxTypeConversions.h:69
vtkPolyDataWriterPtr
vtkSmartPointer< class vtkPolyDataWriter > vtkPolyDataWriterPtr
Definition: vtkForwardDeclarations.h:117
cx::Mesh::getTypeName
static QString getTypeName()
Definition: cxMesh.h:67
cx::MeshPtr
boost::shared_ptr< class Mesh > MeshPtr
Definition: cxForwardDeclarations.h:48
cxErrorObserver.h
cx::PatientModelServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Definition: cxLogicManager.h:25
cxTypeConversions.h
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cx::PolyDataMeshReader::canReadDataType
virtual QString canReadDataType() const
Definition: cxPolyDataMeshReader.cpp:55
cx::PolyDataMeshReader::canWriteDataType
QString canWriteDataType() const
Definition: cxPolyDataMeshReader.cpp:109
cx::PolyDataMeshReader::write
virtual void write(DataPtr data, const QString &filename)
Definition: cxPolyDataMeshReader.cpp:81
vtkPolyDataReaderPtr
vtkSmartPointer< class vtkPolyDataReader > vtkPolyDataReaderPtr
Definition: vtkForwardDeclarations.h:114
cx::vtkPolyDataPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Definition: cxCenterlineRegistration.h:42
cx::ErrorObserver::checkedRead
static bool checkedRead(vtkSmartPointer< vtkAlgorithm > reader, QString filename)
Definition: cxErrorObserver.h:60
cx::FileReaderWriterImplService
Definition: cxFileReaderWriterService.h:71
cx::Mesh
A mesh data set.
Definition: cxMesh.h:45
cx::FileReaderWriterImplService::createData
DataPtr createData(QString type, QString filename, QString name="") const
Definition: cxFileReaderWriterService.cpp:75
cxPolyDataMeshReader.h
cx::PolyDataMeshReader::PolyDataMeshReader
PolyDataMeshReader(PatientModelServicePtr patientModelService)
Definition: cxPolyDataMeshReader.cpp:94
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cxMesh.h
cx::PolyDataMeshReader::canRead
virtual bool canRead(const QString &type, const QString &filename)
Definition: cxPolyDataMeshReader.cpp:99