Developer documentation | Axl-2.5.1

axlOFFWriter.cpp
Go to the documentation of this file.
1 /* axlOFFWriter.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 "axlOFFWriter.h"
16 
17 #include <dtkCoreSupport/dtkAbstractData.h>
18 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
19 
21 #include<axlCore/axlMesh.h>
22 #include<axlCore/axlPoint.h>
24 
26 
27 #include <QtWidgets>
28 
29 // /////////////////////////////////////////////////////////////////
30 // axlOFFWriter
31 // /////////////////////////////////////////////////////////////////
32 
33 axlOFFWriter::axlOFFWriter(void) : dtkAbstractDataWriter()
34 {
35  this->setObjectName(this->description());
36 
37 }
38 
40 {
41 
42 }
43 
44 
45 
47 
50 QString axlOFFWriter::identifier(void) const
51 {
52  return "axlOFFWriter";
53 }
54 
55 
57 
60 QString axlOFFWriter::description(void) const
61 {
62  return "axlOFFWriter";
63 }
64 
65 
67 
70 QStringList axlOFFWriter::handled(void) const
71 {
72  return QStringList() <<".off" << "off document";
73 }
74 
75 
76 
78 
82 {
83  return dtkAbstractDataFactory::instance()->registerDataWriterType("axlOFFWriter", QStringList(), createaxlOFFWriter);
84 }
85 
86 
88 
91 bool axlOFFWriter::canWrite(const QString& file){
92  return (file.endsWith(".off")); //Qt::CaseInsensitive(
93 }
94 
95 
97 
100 bool axlOFFWriter::write(const QString& file)
101 {
102  QFile filename(file);
103  QTextStream os( &filename);
104 
105  if( !filename.open(QIODevice::WriteOnly | QIODevice::Text)) {
106  QMessageBox::information(0, "Axel","Error of writing","&Ok",0) ;
107  return false;
108  }
109 
110  // construct the axlMesh;
111  axlMesh *mesh = new axlMesh;
112 
113  //dtkAbstractData *data = d->dataList.at(i);
114  axlAbstractDataComposite *data = dynamic_cast<axlAbstractDataComposite *>(this->data());
115  int numberElement = data->count();
116 
117  for(int i = 0 ; i < numberElement ; i++)
118  {
119  bool founded = false;
120 
121  axlMesh *currentMesh = NULL;
122 
123 
124  // test for all converters registered in the factory
125 
126  foreach(QString converter, dtkAbstractDataFactory::instance()->converters())
127  {
128  if(!founded)
129  {
130  axlAbstractDataConverter *axl_converter = dynamic_cast<axlAbstractDataConverter *>(dtkAbstractDataFactory::instance()->converter(converter));
131  axl_converter->setData(data->get(i));
132  currentMesh = axl_converter->toMesh();
133  if(currentMesh)
134  founded = true;
135  }
136 
137  }
138 
139 
140  // //Atomics data writers
141 
142  // if(!founded)
143  // {
144  // axlPointConverter *axl_converter = new axlPointConverter;
145  // axl_converter->setData(data);
146  // currentMesh = axl_converter->toMesh();
147  // if(currentMesh)
148  // founded = true;
149  // }
150 
151  if(!founded)
152  {
153  if ( (currentMesh = dynamic_cast<axlMesh*>(data->get(i))) ) {
154  founded = true;
155  }
156  }
157 
158  if(currentMesh)
159  mesh->append(currentMesh);
160 
161 
162  if(!founded)
163  return false;
164  }
165 
166  if(mesh->vertex_count() == 0){
167  return false;
168  }
169 
170  os << "OFF\n";
171  os << "#\n";
172  os << "# "<< file << "\n";
173  os << "# This file was created with axelModeler.\n";
174  os << "# Go to http://axl.inria.fr for more information.\n";
175  os << "#\n";
176 
177  //write of the mesh
178 
179  os << mesh->vertex_count() << " " << mesh->face_count() << " "<< mesh->edge_count()<< "\n";
180 
181  for(int i = 0 ; i < mesh->vertex_count() ; i++)
182  {
183  axlPoint p;
184  mesh->vertex(i, &p);
185  os << p.x() << " " << p.y() << " " << p.z()<< "\n";
186  }
187 
188  for(int i = 0 ; i < mesh->face_count() ; i++)
189  {
190  QVector<int> face = mesh->face(i);
191 
192  os << face.size();
193  for(int j = 0 ; j < face.size(); j++)
194  {
195  os << " " << face.at(j);
196  }
197 
198  os << "\n";
199  }
200 
201  // for(int i = 0 ; i < mesh->edge_count() ; i++)
202  // {
203  // axlMesh::Edge edge = mesh->edge(i);
204  // os << 1 << " " << edge.first << " " << edge.second << "\n";
205  // }
206 
207  filename.close();
208 
209  delete mesh;
210 
211  return true;
212 }
213 
214 
215 
216 dtkAbstractDataWriter *createaxlOFFWriter(void)
217 {
218  return new axlOFFWriter;
219 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
QString identifier(void) const
Return the identifier "axlOFFWriter".
int face_count(void) const
Definition: axlMesh.cpp:138
static bool registered(void)
Register the writer in the factory.
void append(axlMesh *mesh)
Definition: axlMesh.cpp:814
QString description(void) const
Return a description of the writer.
bool canWrite(const QString &file)
Check if the writer can write the file.
int edge_count(void) const
Definition: axlMesh.cpp:143
void vertex(const int &ind, double vertex[3])
set vertex values of vertices with index ind.
Definition: axlMesh.cpp:322
QStringList handled(void) const
Return the appropriate extension of files (.off) written by this writer.
double y
Definition: axlPoint.h:37
double z
Definition: axlPoint.h:38
axlOFFWriter(void)
~axlOFFWriter(void)
dtkAbstractDataWriter * createaxlOFFWriter(void)
bool write(const QString &file)
Write the .off file.
double x
Definition: axlPoint.h:37
virtual axlMesh * toMesh(void)
Mesh conversion.
int vertex_count(void) const
Definition: axlMesh.cpp:122
Face face(int ind) const
Definition: axlMesh.cpp:709
bool get(dtkAbstractData *data)
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41