Developer documentation | Axl-2.5.1

axlTorusParametricReader.cpp
Go to the documentation of this file.
1 /* axlTorusParametricReader.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/axlTorus.h>
20 #include <axlCore/axlPoint.h>
22 
23 #include <dtkCoreSupport/dtkAbstractData.h>
24 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
27 
28 // /////////////////////////////////////////////////////////////////
29 // axlTorusParametricReader
30 // /////////////////////////////////////////////////////////////////
31 
33  this->setObjectName(this->description());
34 }
35 
37 }
38 
40  return "axlTorusParametricReader";
41 }
42 
44  return "axlTorusParametricReader";
45 }
46 
47 QStringList axlTorusParametricReader::handled(void) const {
48  return QStringList() << "axlTorusParametric";
49 }
50 
52  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlTorusParametricReader", QStringList(), createaxlTorusParametricReader);
53 }
54 
55 bool axlTorusParametricReader::accept(const QDomNode& node) {
56  QDomElement element = node.toElement();
57 
58  if(element.tagName() != "torusParam")
59  return false;
60 
61  if(!hasChildNode(element, "center"))
62  return false;
63 
64  if(!hasChildNode(element, "direction"))
65  return false;
66 
67  if(!hasChildNode(element, "ringRadius"))
68  return false;
69 
70  if(!hasChildNode(element, "crossSectionRadius"))
71  return false;
72 
73  if(!hasChildNode(element, "refdir"))
74  return false;
75 
76  return true;
77 }
78 
79 bool axlTorusParametricReader::reject(const QDomNode& node) {
80  return !this->accept(node);
81 }
82 
84  QDomElement element = node.toElement();
85 
86  // Create torus parametric to fill and return
87  axlTorusParametric *currentTorusParametric = new axlTorusParametric();
88 
89  // Get name
90  QString name = element.attribute("name");
91  if(!name.isEmpty()) {
92  currentTorusParametric->setObjectName(name);
93  }
94 
95  // Get color
96  QString color = element.attribute("color");
97  if(!color.isEmpty()) {
98  QStringList colorList = color.split(" ");
99  if(colorList.size() > 2) // rgb components
100  currentTorusParametric->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
101  if(colorList.size() == 4)
102  currentTorusParametric->setOpacity(colorList.at(3).toFloat());
103  }
104 
105  // Get shader
106  QString shader = element.attribute("shader");
107  QString dirShader;
108  if(!shader.isEmpty()) {
109  // try to read from axelShader.qrc
110  dirShader = ":axlShader/shader/"+shader;
111  if(!QFile::exists(dirShader)) {
112  QSettings settings("inria", "dtk");
113  QString defaultPath;
114  settings.beginGroup("shader");
115  dirShader = settings.value("path", defaultPath).toString();
116 // dirShader = this->file().left(this->file().lastIndexOf("axel-data") + 9); // to Remove later
117  dirShader.append("/"+shader);
118  }
119  currentTorusParametric->setShader(dirShader);
120  }
121 
122  QString size = element.attribute("size");
123  if(!size.isEmpty())
124  currentTorusParametric->setSize(size.toFloat());
125 
126 
127  axlTorus torus;
128 
129  // Get Center
130  QDomNodeList centerNodeList = element.elementsByTagName("center") ;
131 
132  QDomElement centerElement = centerNodeList.item(0).toElement() ;
133 
134  QStringList centerCoords = centerElement.text().simplified().split(QRegExp("\\s+"));
135  if(centerCoords.size() == 3)
136  torus.setCenter(new axlPoint(centerCoords[0].toDouble(), centerCoords[1].toDouble(), centerCoords[2].toDouble()));
137  else
138  dtkWarn() << "TorusParametric data cannot be read correctly : center coordinates";
139 
140  // Get Direction
141  QDomNodeList directionNodeList = element.elementsByTagName("direction") ;
142 
143  QDomElement directionElement = directionNodeList.item(0).toElement() ;
144 
145  QStringList directionCoords = directionElement.text().simplified().split(QRegExp("\\s+"));
146  if(directionCoords.size() == 3)
147  torus.setDirection(new axlPoint(directionCoords[0].toDouble(), directionCoords[1].toDouble(), directionCoords[2].toDouble()));
148  else
149  dtkWarn() << "TorusParametric data cannot be read correctly : direction coordinates";
150 
151  // Get Ring Radius
152  QDomNodeList ringRadiusNodeList = element.elementsByTagName("ringRadius") ;
153 
154  QDomElement ringRadiusElement = ringRadiusNodeList.item(0).toElement() ;
155 
156  QStringList ringRadiusCoords = ringRadiusElement.text().simplified().split(QRegExp("\\s+"));
157  if(ringRadiusCoords.size() == 1)
158  torus.setRingRadius(ringRadiusCoords[0].toDouble());
159  else
160  dtkWarn() << "TorusParametric data cannot be read correctly : ring radius";
161 
162  // Get Cross Section Radius
163  QDomNodeList crossSectionRadiusNodeList = element.elementsByTagName("crossSectionRadius") ;
164 
165  QDomElement crossSectionRadiusElement = crossSectionRadiusNodeList.item(0).toElement() ;
166 
167  QStringList crossSectionRadiusCoords = crossSectionRadiusElement.text().simplified().split(QRegExp("\\s+"));
168  if(crossSectionRadiusCoords.size() == 1)
169  torus.setCrossSectionRadius(crossSectionRadiusCoords[0].toDouble());
170  else
171  dtkWarn() << "TorusParametric data cannot be read correctly : refdir coordinates";
172 
173  currentTorusParametric->setTorus(torus);
174 
175  // Get Refdir
176  QDomNodeList refdirNodeList = element.elementsByTagName("refdir") ;
177 
178  QDomElement refdirElement = refdirNodeList.item(0).toElement() ;
179 
180  QStringList refdirCoords = refdirElement.text().simplified().split(QRegExp("\\s+"));
181  if(refdirCoords.size() == 3)
182  currentTorusParametric->setR(new axlPoint(refdirCoords[0].toDouble(), refdirCoords[1].toDouble(), refdirCoords[2].toDouble()));
183  else
184  dtkWarn() << "TorusParametric data cannot be read correctly : refdir coordinates";
185 
186  //if there are some field, read them thanks to the factory.
187  QDomNodeList nodeListField = element.elementsByTagName("field");
188  if(!nodeListField.isEmpty()){
189  for(int i =0; i < nodeListField.size(); i++){
190  QDomElement fieldElement = nodeListField.at(i).toElement();
191  QString fieldType = fieldElement.attribute("type");
192  if(!fieldType.isEmpty()){
193  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
194  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
195  if(fieldToAdd){
196  QString newName = currentTorusParametric->changeFieldName(fieldToAdd->name());
197  fieldToAdd->setObjectName(newName);
198  currentTorusParametric->addField(fieldToAdd);
199  }
200  }
201  }
202  }
203 
204  return currentTorusParametric;
205 }
206 
207 dtkAbstractDataReader *createaxlTorusParametricReader(void) {
208  return new axlTorusParametricReader;
209 }
bool accept(const QDomNode &node)
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
dtkAbstractDataReader * createaxlTorusParametricReader(void)
void setCenter(axlPoint *center)
Definition: axlTorus.cpp:136
void setCrossSectionRadius(double crossSectionRadius)
Definition: axlTorus.cpp:148
void setShader(const QString &shader)
axlAbstractData * read(const QDomNode &node)
QString changeFieldName(QString fieldName)
void setR(axlPoint *r)
dtkAbstractDataReader * create(const QString &interface_name)
bool reject(const QDomNode &node)
void setTorus(const axlTorus &torus)
virtual axlAbstractData * read(const QDomNode &node)=0
Class axlAbstractField defines an API for arrays of numeric data.
void setRingRadius(double ringRadius)
Definition: axlTorus.cpp:144
void setDirection(axlPoint *direction)
Definition: axlTorus.cpp:140
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
static axlFieldReadersFactory * instance(void)
void addField(axlAbstractField *field)
Add a field to the field list of the object.
QStringList handled(void) const
void setSize(const double &size)
void setColor(double r, double g, double b)
Class axlAbstractData defines an API for all type of axel data.