CustusX  2023.01.05-dev+develop.0da12
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 }
void reportError(QString msg)
Definition: cxLogger.cpp:71
A mesh data set.
Definition: cxMesh.h:45
DataPtr createData(QString type, QString filename, QString name="") const
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
virtual bool readInto(DataPtr data, QString path)
cstring_cast_Placeholder cstring_cast(const T &val)
virtual QString canReadDataType() const
PolyDataMeshReader(PatientModelServicePtr patientModelService)
virtual DataPtr read(const QString &uid, const QString &filename)
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual void write(DataPtr data, const QString &filename)
vtkSmartPointer< class vtkPolyDataWriter > vtkPolyDataWriterPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
static bool checkedRead(vtkSmartPointer< vtkAlgorithm > reader, QString filename)
vtkSmartPointer< class vtkPolyDataReader > vtkPolyDataReaderPtr
static QString getTypeName()
Definition: cxMesh.h:67
virtual bool canRead(const QString &type, const QString &filename)
boost::shared_ptr< class Mesh > MeshPtr
bool canWrite(const QString &type, const QString &filename) const
bool canWriteInternal(const QString &type, const QString &filename) const
Namespace for all CustusX production code.