Developer documentation | Axl-2.5.1

axlCylinderParametricReader.cpp
Go to the documentation of this file.
1 /* axlCylinderParametricReader.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008-2013 - Valentin Michelet, Inria.
5  * Created: Mon Jul 29 11:13:23 2013 (+0200)
6  */
7 
8 /* Commentary:
9  *
10  */
11 
12 /* Change log:
13  *
14  */
15 
17 
18 #include <axlCore/axlCylinder.h>
20 #include <axlCore/axlPoint.h>
22 
23 #include <dtkCoreSupport/dtkAbstractData.h>
24 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
25 
28 
29 // /////////////////////////////////////////////////////////////////
30 // axlCylinderParametricReader
31 // /////////////////////////////////////////////////////////////////
32 
34  this->setObjectName(this->description());
35 }
36 
38 }
39 
41  return "axlCylinderParametricReader";
42 }
43 
45  return "axlCylinderParametricReader";
46 }
47 
48 QStringList axlCylinderParametricReader::handled(void) const {
49  return QStringList() << "axlCylinderParametric";
50 }
51 
53  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlCylinderParametricReader", QStringList(), createaxlCylinderParametricReader);
54 }
55 
56 bool axlCylinderParametricReader::accept(const QDomNode& node) {
57 
58  QDomElement element = node.toElement();
59 
60  if(element.tagName() != "cylinderParam")
61  return false;
62 
63  if(!hasChildNode(element, "point"))
64  return false;
65 
66  if(!hasChildNode(element, "radius"))
67  return false;
68 
69  if(!hasChildNode(element, "refdir"))
70  return false;
71 
72  qDebug() << "cylinderParam read.";
73 
74  return true;
75 }
76 
77 bool axlCylinderParametricReader::reject(const QDomNode& node) {
78  return !this->accept(node);
79 }
80 
82  QDomElement element = node.toElement();
83 
84  // Create parametric cylinder to fill and return
85  axlCylinderParametric* currentCylinderParametric = new axlCylinderParametric;
86 
87  // Get name
88  QString name = element.attribute("name");
89  if(!name.isEmpty()) {
90  currentCylinderParametric->setObjectName(name);
91  }
92 
93  // Get color
94  QString color = element.attribute("color");
95  if(!color.isEmpty()) {
96  QStringList colorList = color.split(" ");
97  if(colorList.size() > 2) // rgb components
98  currentCylinderParametric->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
99  if(colorList.size() == 4)
100  currentCylinderParametric->setOpacity(colorList.at(3).toFloat());
101  }
102 
103  // Get shader
104  QString shader = element.attribute("shader");
105  QString dirShader;
106  if(!shader.isEmpty()) {
107  // try to read from axelShader.qrc
108  dirShader = ":axlShader/shader/"+shader;
109  if(!QFile::exists(dirShader)) {
110  QSettings settings("inria", "dtk");
111  QString defaultPath;
112  settings.beginGroup("shader");
113  dirShader = settings.value("path", defaultPath).toString();
114 // dirShader = this->file().left(this->file().lastIndexOf("axel-data") + 9); // to Remove later
115  dirShader.append("/"+shader);
116  }
117  currentCylinderParametric->setShader(dirShader);
118  }
119 
120  // Get size
121  QString size = element.attribute("size");
122  if(!size.isEmpty())
123  currentCylinderParametric->setSize(size.toFloat());
124 
125  QDomNodeList pointNodeList = element.elementsByTagName("point") ;
126  axlCylinder cylinder;
127 
128  // Get Point 1
129  QDomElement point1Element = pointNodeList.item(0).toElement() ;
130 
131  QStringList point1Coords = point1Element.text().simplified().split(QRegExp("\\s+"));
132  if(point1Coords.size() == 3)
133  cylinder.setFirstPoint(new axlPoint(point1Coords[0].toDouble(), point1Coords[1].toDouble(), point1Coords[2].toDouble()));
134  else
135  dtkWarn() << "CylinderParametric data cannot be read correctly : point 1 coordinates";
136 
137  // Get Point 2
138  QDomElement point2Element = pointNodeList.item(1).toElement() ;
139 
140  QStringList point2Coords = point2Element.text().simplified().split(QRegExp("\\s+"));
141  if(point2Coords.size() == 3)
142  cylinder.setSecondPoint(new axlPoint(point2Coords[0].toDouble(), point2Coords[1].toDouble(), point2Coords[2].toDouble()));
143  else
144  dtkWarn() << "CylinderParametric data cannot be read correctly : point 2 coordinates";
145 
146  // Get Radius
147  QDomNodeList radiusNodeList = element.elementsByTagName("radius") ;
148 
149  QDomElement radiusElement = radiusNodeList.item(0).toElement() ;
150 
151  QStringList radiusCoords = radiusElement.text().simplified().split(QRegExp("\\s+"));
152  if(radiusCoords.size() == 1)
153  cylinder.setRadius(radiusCoords[0].toDouble());
154  else
155  dtkWarn() << "CylinderParametric data cannot be read correctly : radius";
156 
157  currentCylinderParametric->setCylinder(cylinder);
158 
159  // Get Refdir
160  QDomNodeList refdirNodeList = element.elementsByTagName("refdir") ;
161 
162  QDomElement refdirElement = refdirNodeList.item(0).toElement() ;
163 
164  QStringList refdirCoords = refdirElement.text().simplified().split(QRegExp("\\s+"));
165  if(refdirCoords.size() == 3)
166  currentCylinderParametric->setR(new axlPoint(refdirCoords[0].toDouble(), refdirCoords[1].toDouble(), refdirCoords[2].toDouble()));
167  else
168  dtkWarn() << "CylinderParametric data cannot be read correctly : refdir coordinates";
169 
170 
171  // If there are some field, read them thanks to the factory.
172  QDomNodeList nodeListField = element.elementsByTagName("field");
173  if(!nodeListField.isEmpty()){
174  for(int i =0; i < nodeListField.size(); i++){
175  QDomElement fieldElement = nodeListField.at(i).toElement();
176  QString fieldType = fieldElement.attribute("type");
177  if(!fieldType.isEmpty()){
178  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
179  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
180  if(fieldToAdd){
181  QString newName = currentCylinderParametric->changeFieldName(fieldToAdd->name());
182  fieldToAdd->setObjectName(newName);
183  currentCylinderParametric->addField(fieldToAdd);
184  }
185  }
186  }
187  }
188 
189  return currentCylinderParametric;
190 }
191 
192 dtkAbstractDataReader *createaxlCylinderParametricReader(void) {
193  return new axlCylinderParametricReader;
194 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
void setFirstPoint(axlPoint *p1)
Change first point of this cylinder.
void setRadius(double radius)
Change radius of this cylinder.
dtkAbstractDataReader * createaxlCylinderParametricReader(void)
Class axlCylinder defines 3D cylinders.
Definition: axlCylinder.h:33
void setShader(const QString &shader)
void setSecondPoint(axlPoint *p2)
Change second point of this cylinder.
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
virtual axlAbstractData * read(const QDomNode &node)=0
Class axlAbstractField defines an API for arrays of numeric data.
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
void setCylinder(axlCylinder *cylinder)
static axlFieldReadersFactory * instance(void)
void addField(axlAbstractField *field)
Add a field to the field list of the object.
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.
axlAbstractData * read(const QDomNode &node)