CustusX  20.03-rc1
An IGT application
cxCgeoReaderWriter.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 "cxCgeoReaderWriter.h"
13 
14 #include <QDir>
15 #include "vtkPolyData.h"
16 #include <vtkCellArray.h>
17 #include "vtkIdList.h"
18 #include "cxMesh.h"
19 #include "cxLogger.h"
20 
21 namespace cx
22 {
23 
25  FileReaderWriterImplService("CgeoReaderWriter", "", Mesh::getTypeName(), "cgeo", patientModelService)
26 {
27 }
28 
30 {
31  return false;
32 }
33 
34 bool CgeoReaderWriter::canRead(const QString &type, const QString &filename)
35 {
36  return false;
37 }
38 
39 std::vector<DataPtr> CgeoReaderWriter::read(const QString &filename)
40 {
41  return std::vector<DataPtr>();
42 }
43 
44 DataPtr CgeoReaderWriter::read(const QString &uid, const QString &filename)
45 {
46  return DataPtr();
47 }
48 
50 {
51  return "";
52 }
53 
54 bool CgeoReaderWriter::readInto(DataPtr data, QString path)
55 {
56  return false;
57 }
58 
59 void CgeoReaderWriter::write(DataPtr data, const QString &filename)
60 {
61  QFile exportFile(filename);
62  exportFile.open(QIODevice::WriteOnly);
63  QDataStream out(&exportFile);
64  this->writeToStream(data, out);
65 }
66 
68 {
69  QByteArray retval;
70  QDataStream out(&retval, QIODevice::WriteOnly);
71  this->writeToStream(data, out);
72 
73  return retval;
74 }
75 
76 void CgeoReaderWriter::writeToStream(DataPtr data, QDataStream &out)
77 {
78  MeshPtr mesh = boost::dynamic_pointer_cast<Mesh>(data);
79  if(!mesh)
80  {
81  CX_LOG_ERROR() << "Couldn't find mesh.";
82  return;
83  }
84  vtkPolyDataPtr polyData = mesh->getTransformedPolyDataCopy(mesh->get_rMd());
85  vtkCellArrayPtr polys = polyData->GetPolys();
86 
87  out.setByteOrder(QDataStream::LittleEndian);
88  out.setFloatingPointPrecision(QDataStream::SinglePrecision);
89 
90  out << (qint32) 12072001; // Magic (12072001)
91  out << (qint32) 0; // TextureCount
92  out << (qint32) 1; // PartCount
93 
94  out << (qint32) 0; // Component ID
95  out << (qint32) 53672537; // Color
96 
97  out << (qint32) polyData->GetNumberOfPoints(); // # vertices
98  out << (qint32) 0; // always 0
99  out << (qint32) -1; // always -1
100  out << (qint32) polys->GetNumberOfCells(); // # primitives
101  out << (qint32) 3; // 3 nodes per primitive (triangle)
102 
103  for (int i=0; i<polyData->GetNumberOfPoints(); i++)
104  {
105  double p[3];
106  polyData->GetPoint(i,p);
107  out << p[0]; // x
108  out << p[1]; // y
109  out << p[2]; // z
110  }
111 
112  vtkIdType n_ids;
113  vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
114  polys->InitTraversal();
115 
116  while(polys->GetNextCell(idlist))
117  {
118  n_ids = idlist->GetNumberOfIds();
119  if (n_ids == 3)
120  {
121  out << (qint32) idlist->GetId(0); // First vertex of triangle
122  out << (qint32) idlist->GetId(1); // Second vertex of triangle
123  out << (qint32) idlist->GetId(2); // Third vertex of triangle
124  }
125  else
126  std::cout << "Warning in .cgeo export: Skipped polygon not containing exactly 3 points." << std::endl;
127  }
128 }
129 
131 {
132  return Mesh::getTypeName();
133 }
134 
135 bool CgeoReaderWriter::canWrite(const QString &type, const QString &filename) const
136 {
137  return this->canWriteInternal(type, filename);
138 }
139 }//cx
bool canWrite(const QString &type, const QString &filename) const
A mesh data set.
Definition: cxMesh.h:45
bool readInto(DataPtr data, QString path)
QString canWriteDataType() const
vtkSmartPointer< class vtkCellArray > vtkCellArrayPtr
QByteArray convertToQByteArray(DataPtr data)
bool canRead(const QString &type, const QString &filename)
boost::shared_ptr< class Data > DataPtr
std::vector< DataPtr > read(const QString &filename)
#define CX_LOG_ERROR
Definition: cxLogger.h:99
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
CgeoReaderWriter(PatientModelServicePtr patientModelService)
QString canReadDataType() const
void write(DataPtr data, const QString &filename)
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
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.