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