Developer documentation | Axl-2.5.1

axlMeshWriter.cpp
Go to the documentation of this file.
1 /* axlMeshWriter.cpp ---
2  *
3  * Author: Meriadeg Perrinel
4  * Copyright (C) 2008-2011 - Meriadeg Perrinel, Inria.
5  * Created: Wed Sep 21 11:36:52 2011 (+0200)
6  * Version: $Id$
7  * Last-Updated: Wed Sep 21 12:11:29 2011 (+0200)
8  * By: Meriadeg Perrinel
9  * Update #: 88
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlMeshWriter.h"
21 
24 #include <axlCore/axlPoint.h>
25 #include <axlCore/axlMesh.h>
26 
27 #include <dtkCoreSupport/dtkAbstractData.h>
28 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlMeshWriter
33 // /////////////////////////////////////////////////////////////////
34 
36 {
37  this->setObjectName(this->description());
38 }
39 
41 {
42 
43 }
44 
45 QString axlMeshWriter::identifier(void) const
46 {
47  return "axlMeshWriter";
48 }
49 
50 QString axlMeshWriter::description(void) const
51 {
52  return "axlMeshWriter";
53 }
54 
55 QStringList axlMeshWriter::handled(void) const
56 {
57  return QStringList() << "axlMesh";
58 }
59 
61 {
62  return dtkAbstractDataFactory::instance()->registerDataWriterType("axlMeshWriter", QStringList(), createaxlMeshWriter);
63 }
64 
65 bool axlMeshWriter::accept(dtkAbstractData *data)
66 {
67  axlMesh *mesh = dynamic_cast<axlMesh *>(data);
68  if(mesh)
69  return true;
70 
71  return false;
72 }
73 
74 bool axlMeshWriter::reject(dtkAbstractData *data)
75 {
76  return !this->accept(data);
77 }
78 
79 QDomElement axlMeshWriter::write(QDomDocument *doc, dtkAbstractData *data)
80 {
81  axlMesh *mesh = dynamic_cast<axlMesh *>(data);
82 
84  QDomElement meshElement = doc->createElement("mesh");
85  QString name = data->name();
86  if(name.size()>0)
87  meshElement.setAttribute("name",name);
88 
90  meshElement.setAttribute("size", QString::number(mesh->size()));
91 
93  QString color ;
94 
95  QColor qcolor = mesh->color();
96  QTextStream(&color) << QString::number(qcolor.red()) << " "
97  << QString::number(qcolor.green()) << " "
98  << QString::number(qcolor.blue()) << " "
99  << QString::number(mesh->opacity());
100 
101  meshElement.setAttribute("color", color);
102 
104  QString shader = mesh->shader();
105  QFileInfo shaderFileInfo(shader);
106  if(shader.size()>0)
107  meshElement.setAttribute("shader", shaderFileInfo.fileName());
108 
109  //Write point
110  //number of points
111  QDomElement count= doc->createElement("count");
112  QString countStr;
113  QTextStream(&countStr) << QString::number(mesh->vertex_count())
114  << " " << QString::number(mesh->edge_count())
115  << " " << QString::number(mesh->face_count());
116 
117  QDomText countDomText = doc->createTextNode(countStr);
118  count.appendChild(countDomText);
119  meshElement.appendChild(count);
120 
122  QDomElement points = doc->createElement("points");
123  if(mesh->color_used())
124  points.setAttribute("color", "rgb");
125  QString pointsText;
126  for(int i = 0 ; i < mesh->vertex_count(); i++)
127  {
128  QTextStream(&pointsText)<< "\n " << QString::number(mesh->vertexX(i)) << " "<< QString::number(mesh->vertexY(i)) << " "<< QString::number(mesh->vertexZ(i));
129  if(mesh->color_used())
130  QTextStream(&pointsText)<< " " << QString::number(mesh->colorR(i)) << " "<< QString::number(mesh->colorG(i)) << " "<< QString::number(mesh->colorB(i));
131 
132  }
133  QTextStream(&pointsText)<< "\n"; // for easy read of the file
134  QDomText pointsDomText = doc->createTextNode(pointsText);
135  points.appendChild(pointsDomText);
136  meshElement.appendChild(points);
137 
139  if(mesh->vertex_count()>0 && mesh->color_count()>0 && !mesh->color_used()) {
140  QDomElement colors = doc->createElement("colors");
141  QString colorsText;
142  for(int i = 0 ; i < mesh->vertex_count(); i++)
143  {
144  QTextStream(&colorsText) << "\n " << QString::number(mesh->colorR(i)) << " "<< QString::number(mesh->colorG(i)) << " "<< QString::number(mesh->colorB(i));
145  }
146  QTextStream(&colorsText)<< "\n"; // for easy read of the file
147  QDomText colorsDomText = doc->createTextNode(colorsText);
148  colors.appendChild(colorsDomText);
149  meshElement.appendChild(colors);
150  }
151 
152 // ///// Fields
153 
154 // foreach(axlAbstractField *field, mesh->fields())
155 // {
156 
157 // axlAbstractFieldDiscrete *currentField = dynamic_cast<axlAbstractFieldDiscrete *>(field);
158 // QDomElement fields = doc->createElement("field");
159 
160 
161 
162 // //Support
163 // int fieldSupport= currentField->support();
164 // if(fieldSupport == 1)
165 // fields.setAttribute("support", "point");
166 // else if (fieldSupport == 2)
167 // fields.setAttribute("support", "cell");
168 // else
169 // fields.setAttribute("support", "custom");
170 
171 // //Kind
172 // int fieldKind= currentField->kind();
173 // if(fieldKind == 1)
174 // fields.setAttribute("kind", "scalar");
175 // else if (fieldKind == 2)
176 // fields.setAttribute("kind", "sector");
177 // else
178 // fields.setAttribute("kind", "tensor");
179 
180 // //Type
181 // int fieldType= currentField->type();
182 // if(fieldType == 1)
183 // fields.setAttribute("type", "int");
184 // else if (fieldType == 2)
185 // fields.setAttribute("type", "float");
186 // else
187 // fields.setAttribute("type", "double");
188 
189 // //Name
190 // fields.setAttribute("name", currentField->name());
191 
192 // QString fieldsText;
193 // qDebug()<<"axlMeshWritter"<<fieldType<<fieldKind<<fieldSupport;
194 
195 // if(fieldKind == 1)
196 // for(int i = 0 ; i < mesh->vertex_count(); i++)
197 // QTextStream(&fieldsText) << "\n " << QString::number(currentField->scalar(i));
198 // else if(fieldKind == 2)
199 // for(int i = 0 ; i < mesh->vertex_count(); i++)
200 // QTextStream(&fieldsText) << "\n " << QString::number((currentField->vector(i))[0]) << " "<< QString::number((currentField->vector(i))[1]) << " "<< QString::number((currentField->vector(i))[2]);
201 
202 // QTextStream(&fieldsText)<< "\n"; // for easy read of the file
203 // QDomText fieldsDomText = doc->createTextNode(fieldsText);
204 // fields.appendChild(fieldsDomText);
205 // meshElement.appendChild(fields);
206 // }
207 
209  if(mesh->edge_count()>0) {
210  QDomElement edges = doc->createElement("edges");
211  QString edgesText;
212  for(int i = 0 ; i < mesh->edge_count(); i++) {
213  axlMesh::Edge e = mesh->edge(i);
214  QTextStream(&edgesText)<< "\n " << QString::number(e.size());
215  for (int j=0;j<e.size();j++)
216  QTextStream(&edgesText)<< " " << QString::number(e.at(j));
217  }
218  QTextStream(&edgesText)<< "\n"; // for easy read of the file
219  QDomText edgesDomText = doc->createTextNode(edgesText);
220  edges.appendChild(edgesDomText);
221  meshElement.appendChild(edges);
222  }
223 
225  if(mesh->face_count()>0) {
226  QDomElement faces = doc->createElement("faces");
227  QString facesText;
228  for(int i = 0 ; i < mesh->face_count(); i++){
229  QVector<int> f = mesh->face(i);
230  QTextStream(&facesText)<< "\n " << QString::number(f.size());
231  for(int l=0; l < f.size(); l++)
232  QTextStream(&facesText) << " " << QString::number(f.at(l));
233  }
234  QTextStream(&facesText)<< "\n"; // for easy read of the file
235  QDomText facesDomText = doc->createTextNode(facesText);
236  faces.appendChild(facesDomText);
237  meshElement.appendChild(faces);
238  }
239 
240 
241  //Write fields if exist.
242  if(!mesh->fields().isEmpty()){
243  foreach(axlAbstractField *field, mesh->fields()){
245  QDomElement fieldElement = field_writer->write(doc, field);
246  meshElement.appendChild(fieldElement);
247  }
248  }
249 
250 
251  return meshElement;
252 }
253 
254 
255 QDomElement axlMeshWriter::elementByWriter(axlAbstractDataWriter *axl_writer, QDomDocument *doc, dtkAbstractData *data)
256 {
257  QDomElement element;
258 
259  if(!axl_writer)
260  return element;
261 
262  if(!axl_writer->accept(data))
263  return element;
264 
265  element = axl_writer->write(doc, data);
266 
267  return element;
268 }
269 
270 dtkAbstractDataWriter *createaxlMeshWriter(void)
271 {
272  return new axlMeshWriter;
273 }
double vertexZ(const int &ind) const
return Z coordinates of vertex with index ind.
Definition: axlMesh.cpp:313
int face_count(void) const
Definition: axlMesh.cpp:138
bool accept(dtkAbstractData *data)
bool reject(dtkAbstractData *data)
int colorG(const int &ind)
return int Green component of color with index ind.
Definition: axlMesh.cpp:492
virtual bool accept(dtkAbstractData *data)=0
QVector< int > Edge
An edge is represented by a sequence of vertices.
Definition: axlMesh.h:55
static axlFieldWritersFactory * instance(void)
virtual QString identifier(void) const
QDomElement write(QDomDocument *doc, dtkAbstractData *data)
QStringList handled(void) const
int edge_count(void) const
Definition: axlMesh.cpp:143
dtkAbstractDataWriter * create(const QString &interface_name)
double vertexY(const int &ind) const
return Y coordinates of vertex with index ind.
Definition: axlMesh.cpp:304
QString description(void) const
QString identifier(void) const
int colorR(const int &ind)
return int Red component of color with index ind.
Definition: axlMesh.cpp:474
Class axlAbstractField defines an API for arrays of numeric data.
dtkAbstractDataWriter * createaxlMeshWriter(void)
static bool registered(void)
bool color_used(void) const
Definition: axlMesh.cpp:180
int colorB(const int &ind)
Return int Blue component of color with index ind.
Definition: axlMesh.cpp:511
int color_count(void) const
Definition: axlMesh.cpp:127
QList< axlAbstractField * > fields(void)
double vertexX(const int &ind) const
Return X coordinates of vertex with index ind.
Definition: axlMesh.cpp:295
virtual QDomElement write(QDomDocument *doc, dtkAbstractData *data)=0
int vertex_count(void) const
Definition: axlMesh.cpp:122
Face face(int ind) const
Definition: axlMesh.cpp:709
Edge edge(int ind) const
Definition: axlMesh.cpp:640
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41