NorMIT-nav  22.09
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 }
cx::StlMeshReader::loadVtkPolyData
virtual vtkPolyDataPtr loadVtkPolyData(QString filename)
Definition: cxStlMeshReader.cpp:59
cxLogger.h
cx::StlMeshReader::canReadDataType
virtual QString canReadDataType() const
Definition: cxStlMeshReader.cpp:54
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
vtkSTLReaderPtr
vtkSmartPointer< class vtkSTLReader > vtkSTLReaderPtr
Definition: vtkForwardDeclarations.h:131
cx::StlMeshReader::read
virtual DataPtr read(const QString &uid, const QString &filename)
Definition: cxStlMeshReader.cpp:71
cx::StlMeshReader::canWriteDataType
QString canWriteDataType() const
Definition: cxStlMeshReader.cpp:107
cx::StlMeshReader::readInto
virtual bool readInto(DataPtr data, QString path)
Definition: cxStlMeshReader.cpp:38
cstring_cast
cstring_cast_Placeholder cstring_cast(const T &val)
Definition: cxTypeConversions.h:69
cx::Mesh::getTypeName
static QString getTypeName()
Definition: cxMesh.h:67
cx::MeshPtr
boost::shared_ptr< class Mesh > MeshPtr
Definition: cxForwardDeclarations.h:48
cx::StlMeshReader::canWrite
bool canWrite(const QString &type, const QString &filename) const
Definition: cxStlMeshReader.cpp:112
cxErrorObserver.h
cx::StlMeshReader::write
void write(DataPtr data, const QString &filename)
Definition: cxStlMeshReader.cpp:92
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
vtkSTLWriterPtr
vtkSmartPointer< class vtkSTLWriter > vtkSTLWriterPtr
Definition: vtkForwardDeclarations.h:132
cxStlMeshReader.h
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::StlMeshReader::StlMeshReader
StlMeshReader(PatientModelServicePtr patientModelService)
Definition: cxStlMeshReader.cpp:27
cx::FileReaderWriterImplService::createData
DataPtr createData(QString type, QString filename, QString name="") const
Definition: cxFileReaderWriterService.cpp:75
cx::StlMeshReader::canRead
virtual bool canRead(const QString &type, const QString &filename)
Definition: cxStlMeshReader.cpp:32
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cxMesh.h