Developer documentation | Axl-2.5.1

axlOFFReader.cpp
Go to the documentation of this file.
1 /* axlOFFReader.cpp ---
2  *
3  * Author: Anais Ducoffe
4  * Copyright (C) 2013 - Anais Ducoffe, Inria.
5  */
6 
7 /* Commentary:
8  *
9  */
10 
11 /* Change log:
12  *
13  */
14 
15 #include "axlOFFReader.h"
16 
17 #include <axlCore/axlPoint.h>
18 #include <axlCore/axlMesh.h>
20 
21 #include <dtkCoreSupport/dtkAbstractData.h>
22 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
23 
24 // /////////////////////////////////////////////////////////////////
25 // axlOFFReader
26 // /////////////////////////////////////////////////////////////////
27 
29 {
30  this->setObjectName(this->identifier());
31 }
32 
34 {
35 
36 }
37 
38 
40 
43 QString axlOFFReader::identifier(void) const
44 {
45  return "axlOFFReader";
46 }
47 
48 
50 
53 QString axlOFFReader::description(void) const
54 {
55  return "axlOFFReader";
56 }
57 
58 
60 
63 QStringList axlOFFReader::handled(void) const
64 {
65  return QStringList() << ".off" << "off document";
66 }
67 
68 
70 
74 {
75  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlOFFReader", QStringList(), createaxlOFFReader);
76 }
77 
78 
80 
83 bool axlOFFReader::canRead(const QString& file)
84 {
85  QFile filename(file);
86  if (filename.open(QFile::ReadOnly))
87  {
88  QTextStream in(&filename);
89 
90  QString format = "";
91 
92  QVector<int> currentFace;
93 
94  format = in.readLine(75);
95 
96  if(format.startsWith("OFF") || format.startsWith("off") || format.startsWith("Off")){
97  return true;
98  }
99 
100  }
101 
102  return false;
103 }
104 
106 
109 bool axlOFFReader::read(const QString& file)
110 {
111  QFile filename(file);
112  if (filename.open(QFile::ReadOnly))
113  {
114  QTextStream in(&filename);
115 
116  QString format = "";
117  int vertexCount = 0;
118  int faceCount = 0;
119  int edgeCount = 0;
120  bool intConvert = false;
121 
122  double vx = 0.0;
123  double vy = 0.0;
124  double vz = 0.0;
125 
126  QVector<int> currentFace;
127  int currentFaceSize = 0;
128  int currentFaceIndex = 0;
129 
130  format = in.readLine(75);
131 
132  if(format.startsWith("OFF") || format.startsWith("off") || format.startsWith("Off"))
133  {
134  axlMesh *mesh = new axlMesh;
135  //format = in.skipWhiteSpace();
136 
137  // head reading
138  while(!intConvert)
139  {
140  in.skipWhiteSpace();
141  in >> format;// skip commentary
142  vertexCount = format.toInt(&intConvert);
143  }
144  in.skipWhiteSpace();
145  in >> faceCount;
146  in.skipWhiteSpace();
147  in >> edgeCount;
148 
149  // vertices reading
150 
151  for(int i = 0 ; i < vertexCount ; i++)
152  {
153  in.skipWhiteSpace();
154  in >> vx;
155  in.skipWhiteSpace();
156  in >> vy;
157  in.skipWhiteSpace();
158  in >> vz;
159 
160  mesh->push_back_vertex(vx, vy, vz);
161  }// vertices are read
162 
163  // faces reading
164  for(int i = 0 ; i < faceCount+edgeCount ; i++)
165  {
166  currentFace.clear();
167  in.skipWhiteSpace();
168  in >> currentFaceSize;
169 
170  if(currentFaceSize == 1){
171  for(int j = 0 ; j < 2 ; j++)
172  {
173  in.skipWhiteSpace();
174  in >> currentFaceIndex;
175  currentFace << currentFaceIndex;
176  }
177  // skip color information if necessary
178  in.readLine(75);
179  mesh->push_back_edge(currentFace.at(0), currentFace.at(1));
180 
181  }else{
182  // current face reading
183  for(int j = 0 ; j < currentFaceSize ; j++)
184  {
185  in.skipWhiteSpace();
186  in >> currentFaceIndex;
187  currentFace << currentFaceIndex;
188  }
189  // skip color information if necessary
190  in.readLine(75);
191  mesh->push_back_face(currentFace);
192  }
193  }// faces and edges are read
194 
195 
196  if(mesh->face_count() != 0)
197  {
198  mesh->vertex_show() = true;
199  mesh->edge_show() = true;
200  if(mesh->edge_show())
201  mesh->face_show() = true;
202 
203  }
204  else if(mesh->edge_count() != 0)
205  {
206  mesh->face_show() = false;
207  mesh->edge_show() = true;
208  mesh->vertex_show() = true;
209  }
210  else if(mesh->vertex_count() != 0 )
211  {
212  mesh->face_show() = false;
213  mesh->edge_show() = false;
214  mesh->vertex_show() = true;
215  }
216  else
217  {
218  mesh->face_show() = false;
219  mesh->edge_show() = false;
220  mesh->vertex_show() = false;
221  }
222  this->setData(mesh);
223  return true;
224  }
225 
226  }
227 
228  // qDebug()<<"nb element "<<d->dataList.size();
229 
230  // emit dataSetInserted(d->dataList);
231 
232 
233  return false;
234 
235 }
236 
237 dtkAbstractDataReader *createaxlOFFReader(void)
238 {
239  return new axlOFFReader;
240 }
int face_count(void) const
Definition: axlMesh.cpp:138
QString description(void) const
Return a description of the reader.
bool read(const QString &file)
Read the file which is supposed to be of right type OFF and create a data.
bool vertex_show(void) const
Definition: axlMesh.cpp:159
void push_back_edge(int, int)
Definition: axlMesh.cpp:669
static bool registered(void)
Register this reader type in the factory.
bool edge_show(void) const
Definition: axlMesh.cpp:190
~axlOFFReader(void)
int edge_count(void) const
Definition: axlMesh.cpp:143
QString identifier(void) const
Return the identifier "axlOFFReader".
bool face_show(void) const
Definition: axlMesh.cpp:200
void push_back_face(const Face &face)
Definition: axlMesh.cpp:735
axlOFFReader(void)
int vertex_count(void) const
Definition: axlMesh.cpp:122
dtkAbstractDataReader * createaxlOFFReader(void)
QStringList handled(void) const
Return the type of file that the reader is able to read : "OFF files (.OFF, .off)" ...
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41
void push_back_vertex(const double &x, const double &y, const double &z)
Add a new vertex to the mesh.
Definition: axlMesh.cpp:333
bool canRead(const QString &file)
Check that the file can be read by this type of reader.