Developer documentation | Axl-2.5.1

axlTorusReader.cpp
Go to the documentation of this file.
1 /* axlTorusReader.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008 - Valentin Michelet, Inria.
5  * Created: Tue Nov 9 16:58:59 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Tue Nov 9 17:09:38 2010 (+0100)
8  * By: Valentin Michelet
9  * Update #: 19
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlTorusReader.h"
21 
22 #include <axlCore/axlTorus.h>
23 #include <axlCore/axlPoint.h>
25 
26 #include <dtkCoreSupport/dtkAbstractData.h>
27 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
30 
31 // /////////////////////////////////////////////////////////////////
32 // axlTorusReader
33 // /////////////////////////////////////////////////////////////////
34 
36  this->setObjectName(this->description());
37 }
38 
40 }
41 
42 QString axlTorusReader::identifier(void) const {
43  return "axlTorusReader";
44 }
45 
46 QString axlTorusReader::description(void) const {
47  return "axlTorusReader";
48 }
49 
50 QStringList axlTorusReader::handled(void) const {
51  return QStringList() << "axlTorus";
52 }
53 
55  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlTorusReader", QStringList(), createaxlTorusReader);
56 }
57 
58 bool axlTorusReader::accept(const QDomNode& node) {
59  QDomElement element = node.toElement();
60 
61  if (element.tagName() != "torus")
62  return false;
63 
64  if (!hasChildNode(element, "center"))
65  return false;
66 
67  if (!hasChildNode(element, "ringRadius"))
68  return false;
69 
70  if (!hasChildNode(element, "crossSectionRadius"))
71  return false;
72 
73  return true;
74 }
75 
76 bool axlTorusReader::reject(const QDomNode& node) {
77  return !this->accept(node);
78 }
79 
80 axlAbstractData *axlTorusReader::read(const QDomNode& node) {
81  QDomElement element = node.toElement();
82 
83  axlTorus* currentTorus = new axlTorus();
84 
85  QString name = element.attribute("name");
86  if (!name.isEmpty()) {
87  currentTorus->setObjectName(name);
88  }
89 
90  QString color = element.attribute("color");
91  if (!color.isEmpty()) {
92  QStringList colorList = color.split(" ");
93  if (colorList.size() > 2) // rgb components
94  currentTorus->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
95  if (colorList.size() == 4)
96  currentTorus->setOpacity(colorList.at(3).toFloat());
97  }
98 
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  currentTorus->setShader(dirShader);
112  }
113 
114  QString size = element.attribute("size");
115  if (!size.isEmpty())
116  currentTorus->setSize(size.toFloat());
117 
118  // Center
119  QDomNodeList nodeListCenter = element.elementsByTagName("center");
120  QDomElement elementCenter = nodeListCenter.item(0).toElement();
121  QStringList coordCenter = elementCenter.text().simplified().split(QRegExp("\\s+"));
122  if (coordCenter.size() == 3)
123  currentTorus->setCenter(new axlPoint(coordCenter[0].toDouble(), coordCenter[1].toDouble(), coordCenter[2].toDouble()));
124  else
125  qDebug() << "Torus data cannot be read correctly: radius coordinates";
126 
127  // Direction
128  QDomNodeList nodeListDirection = element.elementsByTagName("direction");
129  QDomElement elementDirection = nodeListDirection.item(0).toElement();
130  QStringList coordDirection = elementDirection.text().simplified().split(QRegExp("\\s+"));
131  if (coordDirection.size() == 3)
132  currentTorus->setDirection(new axlPoint(coordDirection[0].toDouble(), coordDirection[1].toDouble(), coordDirection[2].toDouble()));
133  else
134  qDebug() << "Torus data cannot be read correctly: direction coordinates";
135 
136  // Ring Radius
137  QDomNodeList nodeListRingRadius = element.elementsByTagName("ringRadius");
138  QDomElement elementRingRadius = nodeListRingRadius.item(0).toElement();
139  QStringList ringRadius = elementRingRadius.text().simplified().split(QRegExp("\\s+"));
140  if (ringRadius.size() == 1)
141  currentTorus->setRingRadius(ringRadius[0].toDouble());
142  else
143  qDebug() << "Torus data cannot be read correctly : ring radius";
144 
145  // Cross Section Radius
146  QDomNodeList nodeListCrossSectionRadius = element.elementsByTagName("crossSectionRadius");
147  QDomElement elementCrossSectionRadius = nodeListCrossSectionRadius.item(0).toElement();
148  QStringList crossSectionRadius = elementCrossSectionRadius.text().simplified().split(QRegExp("\\s+"));
149  if (crossSectionRadius.size() == 1)
150  currentTorus->setCrossSectionRadius(crossSectionRadius[0].toDouble());
151  else
152  qDebug() << "Torus data cannot be read correctly : cross section radius";
153 
154  //if there are some field, read them thanks to the factory.
155  QDomNodeList nodeListField = element.elementsByTagName("field");
156  if(!nodeListField.isEmpty()){
157  for(int i = 0; i < nodeListField.size(); i++){
158  QDomElement fieldElement = nodeListField.at(i).toElement();
159  QString fieldType = fieldElement.attribute("type");
160  if(!fieldType.isEmpty()){
161  axlAbstractDataReader *field_reader = dynamic_cast<axlAbstractDataReader *>(axlFieldReadersFactory::instance()->create(fieldType));
162  axlAbstractField * fieldToAdd = dynamic_cast<axlAbstractField *>(field_reader->read(fieldElement));
163  if(fieldToAdd){
164  QString newName = currentTorus->changeFieldName(fieldToAdd->name());
165  fieldToAdd->setObjectName(newName);
166  currentTorus->addField(fieldToAdd);
167  }
168  }
169  }
170  }
171 
172  return currentTorus;
173 }
174 
175 dtkAbstractDataReader *createaxlTorusReader(void) {
176  return new axlTorusReader;
177 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
QStringList handled(void) const
void setCenter(axlPoint *center)
Definition: axlTorus.cpp:136
void setCrossSectionRadius(double crossSectionRadius)
Definition: axlTorus.cpp:148
axlAbstractData * read(const QDomNode &node)
void setShader(const QString &shader)
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
dtkAbstractDataReader * createaxlTorusReader(void)
virtual axlAbstractData * read(const QDomNode &node)=0
Class axlAbstractField defines an API for arrays of numeric data.
static bool registered(void)
void setRingRadius(double ringRadius)
Definition: axlTorus.cpp:144
bool accept(const QDomNode &node)
void setDirection(axlPoint *direction)
Definition: axlTorus.cpp:140
bool hasChildNode(QDomElement element, const QString &tag)
Definition: axlFormat.h:20
void setOpacity(const double &opacity)
QString identifier(void) const
virtual ~axlTorusReader(void)
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)
QString description(void) const
Class axlAbstractData defines an API for all type of axel data.
bool reject(const QDomNode &node)