Developer documentation | Axl-2.5.1

axlShapeWriter.cpp
Go to the documentation of this file.
1 /* axlShapeWriter.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008-2013 - Valentin Michelet, Inria.
5  * Created: Tue Jul 21 14:13:23 2013 (+0200)
6  * Version: $Id$
7  */
8 
9 /* Commentary:
10  *
11  */
12 
13 /* Change log:
14  *
15  */
16 
17 #include "axlShapeWriter.h"
18 
19 #include <axlCore/axlShape.h>
20 
21 #include <dtkCoreSupport/dtkAbstractData.h>
22 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
23 
24 #include <QtGui>
25 
26 // /////////////////////////////////////////////////////////////////
27 // axlShapeWriter
28 // /////////////////////////////////////////////////////////////////
29 
32 
33  this->setObjectName(this->description());
34 }
35 
37 }
38 
39 QString axlShapeWriter::identifier(void) const {
40  return "axlShapeWriter";
41 }
42 
43 QString axlShapeWriter::description(void) const {
44  return "axlShapeWriter";
45 }
46 
47 QStringList axlShapeWriter::handled(void) const {
48  return QStringList() << "axlShape";
49 }
50 
52  return dtkAbstractDataFactory::instance()->registerDataWriterType("axlShapeWriter", QStringList(), createaxlShapeWriter);
53 }
54 
55 bool axlShapeWriter::accept(dtkAbstractData *data) {
56  axlShape *shape = dynamic_cast<axlShape*>(data);
57  if(shape)
58  return true;
59 
60  return false;
61 }
62 
63 bool axlShapeWriter::reject(dtkAbstractData *data) {
64  return !this->accept(data);
65 }
66 
67 QDomElement axlShapeWriter::write(QDomDocument *doc, dtkAbstractData *data) {
68  axlShape* shape = dynamic_cast<axlShape*>(data);
69 
71  QDomElement shapeElement = doc->createElement("shape");
72  shapeElement.setAttribute("name", shape->name());
73 
75  shapeElement.setAttribute("size", QString::number(shape->size()));
76 
78  QColor qcolor = shape->color();
79  QString color ;
80  QTextStream(&color) << QString::number(qcolor.red()) << " "
81  << QString::number(qcolor.green()) << " "
82  << QString::number(qcolor.blue()) << " "
83  << QString::number(shape->opacity());
84  shapeElement.setAttribute("color", color);
85 
87  QString shader = shape->shader();
88  QFileInfo shaderFileInfo(shader);
89  shapeElement.setAttribute("shader", shaderFileInfo.fileName());
90 
91  // Write shape
92  shapeElement.setAttribute("nb_vertices", shape->getVertexCount());
93  shapeElement.setAttribute("nb_edges", shape->getEdgeCount());
94  shapeElement.setAttribute("nb_faces", shape->getFaceCount());
95 
96  // Vertices
97  QVector<axlPoint*> allVertices = shape->getVertices();
98  QDomElement verticesElement = doc->createElement("vertices");
99  for (int k = 0; k < allVertices.size(); k++) {
100  axlPoint* currVertex = allVertices.at(k);
101  QString coordsString = QString::number(currVertex->x()) + " "
102  + QString::number(currVertex->y()) + " "
103  + QString::number(currVertex->z()) + "\n\t";
104  QDomText coordsDomText = doc->createTextNode(coordsString);
105  verticesElement.appendChild(coordsDomText);
106  }
107  shapeElement.appendChild(verticesElement);
108 
109  // Edges
110  QVector<axlShape::Edge*> allEdges = shape->getEdges();
111  QDomElement edgesElement = doc->createElement("edges");
112  for (int k = 0; k < allEdges.size(); k++) {
113  QDomElement edgeElement = doc->createElement("edge");
114  edgeElement.setAttribute("id", k);
115 
116  axlShape::Edge* currEdge = allEdges.at(k);
117 
118  //QDomElement edgeCurveElement = doc->createElement("edge_curve");
119  axlAbstractCurveParametric* curve = currEdge->curve;
120  QString curveWriterTag = curve->identifier()+"Writer";
121  axlAbstractDataWriter* curveWriter = dynamic_cast<axlAbstractDataWriter*>(dtkAbstractDataFactory::instance()->writer(curveWriterTag));
122  QDomElement curveElement = curveWriter->write(doc, curve);
123  //edgeCurveElement.appendChild(curveElement);
124  //edgeCurveElement.setAttribute("type", curve->identifier());
125  edgeElement.appendChild(curveElement);
126  edgeElement.setAttribute("type", curve->identifier());
127 
128  //edgeElement.appendChild(edgeCurveElement);
129 
130  edgeElement.setAttribute("index_start", currEdge->indexStartVertex);
131  edgeElement.setAttribute("index_end", currEdge->indexEndVertex);
132 
133  edgesElement.appendChild(edgeElement);
134  }
135  shapeElement.appendChild(edgesElement);
136 
137  // Faces
138  QVector<axlShape::Face*> allFaces = shape->getFaces();
139  QDomElement facesElement = doc->createElement("faces");
140  for (int k = 0; k < allFaces.size(); k++) {
141  QDomElement faceElement = doc->createElement("face");
142  faceElement.setAttribute("id", k);
143 
144  axlShape::Face* currFace = allFaces.at(k);
145  QVector<axlShape::Loop*> currLoops = currFace->loops;
146 
147  QDomElement faceSurfaceElement = doc->createElement("face_surface");
148  axlAbstractSurfaceParametric* surface = currFace->surface;
149  QString surfaceWriterTag = surface->identifier()+"Writer";
150  axlAbstractDataWriter* surfaceWriter = dynamic_cast<axlAbstractDataWriter*>(dtkAbstractDataFactory::instance()->writer(surfaceWriterTag));
151  if (surfaceWriter) {
152  QDomElement surfaceElement = surfaceWriter->write(doc, surface);
153  //faceSurfaceElement.appendChild(surfaceElement);
154  faceElement.appendChild(surfaceElement);
155  } else {
156  dtkWarn() << surfaceWriterTag << "not supported yet.";
157  }
158  //faceSurfaceElement.setAttribute("type", surface->identifier());
159  faceElement.setAttribute("type", surface->identifier());
160  //faceElement.appendChild(faceSurfaceElement);
161 
162  for (int i = 0; i < currLoops.size(); i++) {
163  QDomElement loopElement = doc->createElement("loop");
164  loopElement.setAttribute("id", i);
165 
166  axlShape::Loop* currLoop = currLoops.at(i);
167  QVector<int> currEdges = currLoop->edges;
168  QVector<bool> currOrientations = currLoop->orientations;
169 
170  for (int j = 0; j < currEdges.size(); j++) {
171  QDomElement edgeElement = doc->createElement("edge");
172  edgeElement.setAttribute("id", currEdges.at(j));
173 
174  bool currOrientation = currOrientations.at(j);
175  edgeElement.setAttribute("orientation", currOrientation);
176 
177  loopElement.appendChild(edgeElement);
178  }
179  faceElement.appendChild(loopElement);
180  }
181  facesElement.appendChild(faceElement);
182  }
183  shapeElement.appendChild(facesElement);
184 
185  return shapeElement;
186 }
187 
188 QDomElement axlShapeWriter::elementByWriter(axlAbstractDataWriter *axl_writer, QDomDocument *doc, dtkAbstractData *data) {
189  QDomElement element;
190 
191  if(!axl_writer)
192  return element;
193 
194  if(!axl_writer->accept(data))
195  return element;
196 
197  element = axl_writer->write(doc, data);
198 
199  return element;
200 }
201 
202 dtkAbstractDataWriter *createaxlShapeWriter(void) {
203  return new axlShapeWriter;
204 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
axlAbstractCurveParametric * curve
Pointer to the parametric curve supporting the edge, of type axlAbstractCurveParametric.
Definition: axlShape.h:49
The edge structure.
Definition: axlShape.h:41
int indexStartVertex
Index of the starting point of the edge in the array of vertices of the axlShape. ...
Definition: axlShape.h:43
QVector< int > edges
Vector of indices of the edges of the loop in the array of edges of the axlShape. ...
Definition: axlShape.h:64
virtual bool accept(dtkAbstractData *data)=0
QVector< Face * > getFaces(void) const
Definition: axlShape.cpp:73
The Face structure.
Definition: axlShape.h:76
QVector< Edge * > getEdges(void) const
Definition: axlShape.cpp:69
int getFaceCount(void) const
Definition: axlShape.cpp:85
QString identifier(void) const
int getEdgeCount(void) const
Definition: axlShape.cpp:81
The Loop structure.
Definition: axlShape.h:62
QString description(void) const
int getVertexCount(void) const
Definition: axlShape.cpp:77
Generic interface for parametric curve.
int indexEndVertex
Index of the ending point of the edge in the array of vertices of the axlShape.
Definition: axlShape.h:46
QDomElement write(QDomDocument *doc, dtkAbstractData *data)
QVector< bool > orientations
Vector of booleans of the same size as edges: true means the direct orientation, false is the reverse...
Definition: axlShape.h:67
axlAbstractSurfaceParametric * surface
Pointer to the supporting surface of type axlAbstractSurfaceParametric.
Definition: axlShape.h:81
static bool registered(void)
bool reject(dtkAbstractData *data)
double y
Definition: axlPoint.h:37
QVector< Loop * > loops
Vector of all the loops defining the face.
Definition: axlShape.h:78
double z
Definition: axlPoint.h:38
virtual QDomElement write(QDomDocument *doc, dtkAbstractData *data)=0
QVector< axlPoint * > getVertices(void) const
Definition: axlShape.cpp:65
dtkAbstractDataWriter * createaxlShapeWriter(void)
double x
Definition: axlPoint.h:37
bool accept(dtkAbstractData *data)
virtual ~axlShapeWriter(void)
QStringList handled(void) const