CustusX  2023.01.05-dev+develop.0da12
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 
96  if(mesh->getColor().rgba())
97  {
98  int intRed = mesh->getColor().red();
99  int intGreen = mesh->getColor().green();
100  int intBlue = mesh->getColor().blue();
101  int intAlpha = mesh->getColor().alpha();
102  int intColor = intRed<<24 | intGreen<<16 | intBlue<<8 | intAlpha;
103  out << (qint32) intColor; // Color
104  }
105  else
106  out << (qint32) 0xFFFFFFFF;//53672537; // Color
107 
108  out << (qint32) polyData->GetNumberOfPoints(); // # vertices
109  out << (qint32) 0; // always 0
110  out << (qint32) -1; // always -1
111  out << (qint32) polys->GetNumberOfCells(); // # primitives
112  out << (qint32) 3; // 3 nodes per primitive (triangle)
113 
114  for (int i=0; i<polyData->GetNumberOfPoints(); i++)
115  {
116  double p[3];
117  polyData->GetPoint(i,p);
118  out << p[0]; // x
119  out << p[1]; // y
120  out << p[2]; // z
121  }
122 
123  vtkIdType n_ids;
124  vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
125  polys->InitTraversal();
126 
127  while(polys->GetNextCell(idlist))
128  {
129  n_ids = idlist->GetNumberOfIds();
130  if (n_ids == 3)
131  {
132  out << (qint32) idlist->GetId(0); // First vertex of triangle
133  out << (qint32) idlist->GetId(1); // Second vertex of triangle
134  out << (qint32) idlist->GetId(2); // Third vertex of triangle
135  }
136  else
137  std::cout << "Warning in .cgeo export: Skipped polygon not containing exactly 3 points." << std::endl;
138  }
139 }
140 
142 {
143  return Mesh::getTypeName();
144 }
145 
146 bool CgeoReaderWriter::canWrite(const QString &type, const QString &filename) const
147 {
148  return this->canWriteInternal(type, filename);
149 }
150 }//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.