Developer documentation | Axl-2.5.1

axlShapeBSplineReader.cpp
Go to the documentation of this file.
2 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
4 
5 #include <QtXml>
6 #include <QtCore>
7 #include <QtGui>
8 
10 {
11  this->setObjectName(this->identifier());
12 }
13 
14 
16 }
17 
19  return "axlShapeBSplineReader";
20 }
21 
23  return "axlShapeBSplineReader";
24 }
25 
26 QStringList axlShapeBSplineReader::handled(void) const{
27  return QStringList();
28 }
29 
31  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlShapeBSplineReader", QStringList(), createaxlShapeBSplineReader);
32 }
33 
34 
35 bool axlShapeBSplineReader::accept(const QDomNode& node){
36  QDomElement element = node.toElement();
37 
38  if(element.tagName() == "surface"){
39  if(element.attribute("type") == "patch")
40  return true;
41  }
42 
43  return false;
44 }
45 
46 bool axlShapeBSplineReader::reject(const QDomNode& node){
47  return !this->accept(node);
48 }
49 
51  // Create element from given node
52  QDomElement element = node.toElement();
53 
54  // Create surf from points, edges and faces counters
55  int pointCount = element.attribute("nb_points").toInt();
56  int edgeCount = element.attribute("nb_edges").toInt();
57  int faceCount = element.attribute("nb_faces").toInt();
58  int orderu = element.attribute("orderu").toInt();
59  int orderv = element.attribute("orderv").toInt();
60 
61  double *points = new double[3*pointCount];
62  QVector< QVector<int> >pe;
63  QVector< QVector<int> >pf;
64  QVector< QPair<int,int> >npf;
65  QList<QString> tags;
66  axlShapeBSpline* surf = new axlShapeBSpline();
67 
68  // Handle name
69  QString name = element.attribute("name");
70  if (!name.isEmpty()){
71  surf->setObjectName(name);
72  }
73 
74  // Handle color
75  QString color = element.attribute("color");
76  if (!color.isEmpty()) {
77  QStringList colorList = color.split(" ");
78  if (colorList.size() > 2) // rgb components
79  surf->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
80  if (colorList.size() == 4)
81  surf->setOpacity(colorList.at(3).toFloat());
82  }
83 
84  // Handle shader
85  QString shader = element.attribute("shader");
86  QString dirShader;
87  if (!shader.isEmpty()) {
88  // Try to read from axelShader.qrc
89  dirShader = ":axlShader/shader/" + shader;
90  if (!QFile::exists(dirShader)) {
91  QSettings settings("inria", "dtk");
92  QString defaultPath;
93  settings.beginGroup("shader");
94  dirShader = settings.value("path", defaultPath).toString();
95  dirShader.append("/" + shader);
96  }
97  surf->setShader(dirShader);
98  }
99 
100  // Handle size
101  QString size = element.attribute("size");
102  if (!size.isEmpty())
103  surf->setSize(size.toFloat());
104 
105  // Handle points
106  QDomElement pointsElement = element.elementsByTagName("points").at(0).toElement();
107  QStringList pointsList = pointsElement.text().simplified().split(QRegExp("\\s+"));
108 
109  for (int k = 0; k < pointsList.size(); k+=3){
110  points[k] = pointsList.at(k).toDouble();
111  points[k+1] =pointsList.at(k+1).toDouble();
112  points[k+2] =pointsList.at(k+2).toDouble();
113  }
114 
115 
116 
117  // Handle edges
118  QDomElement edgesElement = element.elementsByTagName("edges").at(0).toElement();
119  QDomNodeList edgesList = edgesElement.elementsByTagName("edge");
120 
121  for (int k = 0; k < edgesList.size(); k++) {
122  QDomElement edgeElement = edgesList.at(k).toElement();
123 
124  QDomElement controlPointsElement = edgeElement.elementsByTagName("controlPoints").at(0).toElement();
125  QStringList controlpointsList = controlPointsElement.text().simplified().split(QRegExp("\\s+"));
126  QVector<int> controlPointIndices;
127 
128  for (int k = 0; k < controlpointsList.size(); k++){
129  controlPointIndices.append(controlpointsList.at(k).toInt());
130  }
131 
132  pe.append(controlPointIndices);
133 
134  tags.append(edgeElement.attribute("tag"));
135 
136  }
137 
138  // Handle faces
139  QDomElement facesElement = element.elementsByTagName("faces").at(0).toElement();
140  QDomNodeList facesList = facesElement.elementsByTagName("face");
141 
142  for (int k = 0; k < facesList.size(); k++) {
143  QDomElement faceElement = facesList.at(k).toElement();
144 
145  QDomElement controlPointsElement = faceElement.elementsByTagName("controlPoints").at(0).toElement();
146  QStringList controlpointsList = controlPointsElement.text().simplified().split(QRegExp("\\s+"));
147  QVector<int> controlPointIndices;
148 
149  for (int k = 0; k < controlpointsList.size(); k++){
150  controlPointIndices.append(controlpointsList.at(k).toInt());
151  }
152 
153  pf.append(controlPointIndices);
154 
155  QDomElement edgeIndicesElement = faceElement.elementsByTagName("nbpts").at(0).toElement();
156  QStringList edgeIndicesList = edgeIndicesElement.text().simplified().split(QRegExp("\\s+"));
157  QPair<int,int> np = QPair<int,int>(edgeIndicesList.at(0).toInt(),edgeIndicesList.at(1).toInt());
158 
159  npf.append(np);
160 
161  }
162  surf->setSurface(orderu,orderv,pointCount,edgeCount,faceCount, points,pe,pf,npf);
163 
164  //tags for boundary edges.
165  for(int i = 0; i < tags.size(); i++){
166  surf->getEdge(i)->tag = tags.at(i);
167  }
168  return surf;
169 }
170 
171 
172 dtkAbstractDataReader *createaxlShapeBSplineReader(void) {
173  return new axlShapeBSplineReader;
174 }
Edge * getEdge(int i)
Return edge.
dtkAbstractDataReader * createaxlShapeBSplineReader(void)
QString identifier(void) const
bool reject(const QDomNode &node)
void setShader(const QString &shader)
QStringList handled(void) const
void setSurface(int order_u, int order_v, int nbpoints, int nbEdges, int nbFaces, double *points, QVector< QVector< int > > pe, QVector< QVector< int > >pf, QVector< QPair< int, int > >npf)
Create a surface.
QString description(void) const
bool accept(const QDomNode &node)
void setOpacity(const double &opacity)
axlAbstractData * read(const QDomNode &node)
Class axlShapeBSpline defines a set of boundary curves (Edges) and bspline surface patches (Face)...
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.