Developer documentation | Axl-2.5.1

axlShapeReader.cpp
Go to the documentation of this file.
1 /* axlShapeReader.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 "axlShapeReader.h"
18 
19 #include <axlCore/axlShape.h>
20 
21 #include <dtkCoreSupport/dtkAbstractData.h>
22 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
24 
25 #include <axlCore/axlPlane.h>
27 
28 #include <QtGui>
29 
30 // /////////////////////////////////////////////////////////////////
31 // axlShapeReader
32 // /////////////////////////////////////////////////////////////////
33 
36 
37  this->setObjectName(this->description());
38 }
39 
41 }
42 
43 QString axlShapeReader::identifier(void) const {
44  return "axlShapeReader";
45 }
46 
47 QString axlShapeReader::description(void) const {
48  return "axlShapeReader";
49 }
50 
51 QStringList axlShapeReader::handled(void) const {
52  return QStringList() << "axlShape";
53 }
54 
56  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlShapeReader", QStringList(), createaxlShapeReader);
57 }
58 
59 bool axlShapeReader::accept(const QDomNode& node) {
60  QDomElement element = node.toElement();
61 
62  if (element.tagName() != "shape")
63  return false;
64 
65  return true;
66 }
67 
68 bool axlShapeReader::reject(const QDomNode& node) {
69  return !this->accept(node);
70 }
71 
72 axlAbstractData *axlShapeReader::read(const QDomNode& node) {
73  // Create element from given node
74  QDomElement element = node.toElement();
75 
76  // Create shape from vertices, edges and faces counters
77  int vertexCount = element.attribute("nb_vertices").toInt();
78  int edgeCount = element.attribute("nb_edges").toInt();
79  int faceCount = element.attribute("nb_faces").toInt();
80  axlShape* shape = new axlShape(vertexCount, edgeCount, faceCount);
81 
82  // Handle name
83  QString name = element.attribute("name");
84  if (!name.isEmpty()) {
85  shape->setObjectName(name);
86  }
87 
88  // Handle color
89  QString color = element.attribute("color");
90  if (!color.isEmpty()) {
91  QStringList colorList = color.split(" ");
92  if (colorList.size() > 2) // rgb components
93  shape->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
94  if (colorList.size() == 4)
95  shape->setOpacity(colorList.at(3).toFloat());
96  }
97 
98  // Handle shader
99  QString shader = element.attribute("shader");
100  QString dirShader;
101  if (!shader.isEmpty()) {
102  // Try to read from axelShader.qrc
103  dirShader = ":axlShader/shader/" + shader;
104  if (!QFile::exists(dirShader)) {
105  QSettings settings("inria", "dtk");
106  QString defaultPath;
107  settings.beginGroup("shader");
108  dirShader = settings.value("path", defaultPath).toString();
109  dirShader.append("/" + shader);
110  }
111  shape->setShader(dirShader);
112  }
113 
114  // Handle size
115  QString size = element.attribute("size");
116  if (!size.isEmpty())
117  shape->setSize(size.toFloat());
118 
119  // Handle vertices
120  QDomElement verticesElement = element.elementsByTagName("vertices").at(0).toElement();
121  QStringList verticesList = verticesElement.text().simplified().split(QRegExp("\\s+"));
122  for (int k = 0; k < verticesList.size(); k+=3) {
123  axlPoint* newPoint = new axlPoint(verticesList.at(k+0).toDouble(),
124  verticesList.at(k+1).toDouble(),
125  verticesList.at(k+2).toDouble());
126  shape->insert_vertex(newPoint, k/3);
127  }
128 
129  // Handle edges
130  QDomElement edgesElement = element.elementsByTagName("edges").at(0).toElement();
131  QDomNodeList edgesList = edgesElement.elementsByTagName("edge");
132 
133  for (int k = 0; k < edgesList.size(); k++) {
134  QDomElement edgeElement = edgesList.at(k).toElement();
135 
136  axlShape::Edge* newEdge = new axlShape::Edge;
137  newEdge->indexStartVertex = edgeElement.attribute("index_start").toInt();
138  newEdge->indexEndVertex = edgeElement.attribute("index_end").toInt();
139 
140  //QDomElement curveElement = edgeElement.elementsByTagName("edge_curve").at(0).toElement();
141  //QString curveReaderTag = curveElement.attribute("type")+"Reader";
142  QString curveReaderTag = edgeElement.attribute("type")+"Reader";
143 
144  axlAbstractDataReader* curveReader = dynamic_cast<axlAbstractDataReader*>(dtkAbstractDataFactory::instance()->reader(curveReaderTag));
145  //newEdge->curve = dynamic_cast<axlAbstractCurveParametric*>(curveReader->read(curveElement.childNodes().at(0)));
146  if(curveReader) {
147  newEdge->curve = dynamic_cast<axlAbstractCurveParametric*>(curveReader->read(edgeElement.childNodes().at(0)));
148  shape->insert_edge(newEdge, edgeElement.attribute("id").toInt());
149  dtkWarn()<<"axlShapeReader::read"<< curveReaderTag;
150  } else {
151  dtkWarn()<< "Error within axlShapeReader::read edge:" << curveReaderTag << "is not registered within readers.";
152  }
153  }
154 
155  // Handle faces
156  QDomElement facesElement = element.elementsByTagName("faces").at(0).toElement();
157  QDomNodeList facesList = facesElement.elementsByTagName("face");
158 
159  for (int k = 0; k < facesList.size(); k++) {
160  QDomElement faceElement = facesList.at(k).toElement();
161  axlShape::Face* face = new axlShape::Face;
162 
163  QDomNodeList loopsList = faceElement.elementsByTagName("loop");
164 
165  //QDomElement surfaceElement = faceElement.elementsByTagName("face_surface").at(0).toElement();
166  //QString surfaceReaderTag = surfaceElement.attribute("type")+"Reader";
167  QString surfaceReaderTag = faceElement.attribute("type")+"Reader";
168 
169  axlAbstractDataReader* surfaceReader = dynamic_cast<axlAbstractDataReader*>(dtkAbstractDataFactory::instance()->reader(surfaceReaderTag));
170 
171  if (surfaceReader) {
172  //dtkAbstractData* s = surfaceReader->read(surfaceElement.childNodes().at(0));
173  //dtkAbstractData* s = surfaceReader->read(faceElement.childNodes().at(0));
174  //axlAbstractSurfaceParametric* absSurfParam = dynamic_cast<axlAbstractSurfaceParametric*>(surfaceReader->read(surfaceElement.childNodes().at(0)));
175  axlAbstractSurfaceParametric* absSurfParam = dynamic_cast<axlAbstractSurfaceParametric*>(surfaceReader->read(faceElement.childNodes().at(0)));
176  face->surface = absSurfParam;
177  } else {
178  dtkWarn() << "Error within axlShapeReader::read face:" << surfaceReaderTag << "is not registered within readers.";
179  axlPlaneParametric pl(axlPlane(axlPoint(0, 0, 0), axlPoint(0, 0, 1)), axlPoint(1, 0, 0));
180  pl.setSize(0.01);
181  face->surface = new axlPlaneParametric(pl);
182  }
183 
184  for (int i = 0; i < loopsList.size(); i++) {
185  QDomElement loopElement = loopsList.at(i).toElement();
186  axlShape::Loop* loop = new axlShape::Loop;
187 
188  QDomNodeList edgesList = loopElement.elementsByTagName("edge");
189 
190  for (int j = 0; j < edgesList.size(); j++) {
191  QDomElement edgeElement = edgesList.at(j).toElement();
192 
193  loop->edges << edgeElement.attribute("id").toInt();
194 
195  bool currOrientation = edgeElement.attribute("orientation").toInt();
196  loop->orientations << currOrientation;
197  }
198  face->loops << loop;
199  }
200 
201  shape->insert_face(face, faceElement.attribute("id").toInt());
202  }
203 
204  return shape;
205 }
206 
207 dtkAbstractDataReader *createaxlShapeReader(void) {
208  return new axlShapeReader;
209 }
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
virtual ~axlShapeReader(void)
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
The Face structure.
Definition: axlShape.h:76
QString description(void) const
Class axlPlane defines 3D planes.
Definition: axlPlane.h:34
void setShader(const QString &shader)
The Loop structure.
Definition: axlShape.h:62
void insert_face(Face *face, int index)
Definition: axlShape.cpp:308
bool reject(const QDomNode &node)
void insert_edge(Edge *edge, int index)
Definition: axlShape.cpp:138
Generic interface for parametric curve.
virtual axlAbstractData * read(const QDomNode &node)=0
int indexEndVertex
Index of the ending point of the edge in the array of vertices of the axlShape.
Definition: axlShape.h:46
bool accept(const QDomNode &node)
QStringList handled(void) const
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
axlAbstractData * read(const QDomNode &node)
static bool registered(void)
QVector< Loop * > loops
Vector of all the loops defining the face.
Definition: axlShape.h:78
void setOpacity(const double &opacity)
void insert_vertex(axlPoint *vertex, int index)
Definition: axlShape.cpp:134
dtkAbstractDataReader * createaxlShapeReader(void)
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.
QString identifier(void) const