Developer documentation | Axl-2.5.1

axlPlaneParametricReader.cpp
Go to the documentation of this file.
1 /* axlPlaneParametricReader.h ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008-2013 - Valentin Michelet, Inria.
5  * Created: Tue Aug 06 11:13:23 2013 (+0200)
6  */
7 
8 /* Commentary:
9  *
10  */
11 
12 /* Change log:
13  *
14  */
15 
17 
18 #include <axlCore/axlPlane.h>
20 #include <axlCore/axlPoint.h>
22 
23 #include <dtkCoreSupport/dtkAbstractData.h>
24 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
27 
28 // /////////////////////////////////////////////////////////////////
29 // axlPlaneParametricReader
30 // /////////////////////////////////////////////////////////////////
31 
33  this->setObjectName(this->description());
34 }
35 
37 }
38 
40  return "axlPlaneParametricReader";
41 }
42 
44  return "axlPlaneParametricReader";
45 }
46 
47 QStringList axlPlaneParametricReader::handled(void) const {
48  return QStringList() << "axlPlaneParametric";
49 }
50 
52  return dtkAbstractDataFactory::instance()->registerDataReaderType("axlPlaneParametricReader", QStringList(), createaxlPlaneParametricReader);
53 }
54 
55 bool axlPlaneParametricReader::accept(const QDomNode& node) {
56  QDomElement element = node.toElement();
57 
58  if(element.tagName() != "planeParam")
59  return false;
60 
61  if(!hasChildNode(element, "point"))
62  return false;
63 
64  if(!hasChildNode(element, "normal"))
65  return false;
66 
67  if(!hasChildNode(element, "refdir"))
68  return false;
69 
70  return true;
71 }
72 
73 bool axlPlaneParametricReader::reject(const QDomNode& node) {
74  return !this->accept(node);
75 }
76 
78  QDomElement element = node.toElement();
79 
80  // Create new plane parametric to fill and return
81  axlPlaneParametric *currentPlaneParametric = new axlPlaneParametric;
82 
83  // Get name
84  QString name = element.attribute("name");
85  if (!name.isEmpty()) {
86  currentPlaneParametric->setObjectName(name);
87  }
88 
89  // Get color
90  QString color = element.attribute("color");
91  if (!color.isEmpty()) {
92  QStringList colorList = color.split(" ");
93  if (colorList.size() > 2) // rgb components
94  currentPlaneParametric->setColor(QColor(colorList.at(0).toInt(), colorList.at(1).toInt(), colorList.at(2).toInt()));
95  if (colorList.size() == 4)
96  currentPlaneParametric->setOpacity(colorList.at(3).toFloat());
97  }
98 
99  // Get shader
100  QString shader = element.attribute("shader");
101  QString dirShader;
102  if (!shader.isEmpty()) {
103  // try to read from axelShader.qrc
104  dirShader = ":axlShader/shader/"+shader;
105  if (!QFile::exists(dirShader)) {
106  QSettings settings("inria", "dtk");
107  QString defaultPath;
108  settings.beginGroup("shader");
109  dirShader = settings.value("path", defaultPath).toString();
110 // dirShader = this->file().left(this->file().lastIndexOf("axel-data") + 9); // to Remove later
111  dirShader.append("/"+shader);
112  }
113  currentPlaneParametric->setShader(dirShader);
114  }
115 
116  // Get size
117  QString size = element.attribute("size");
118  if (!size.isEmpty())
119  currentPlaneParametric->setSize(size.toFloat());
120 
121  // Get point
122  QDomNodeList pointNodeList = element.elementsByTagName("point") ;
123  QDomElement pointElement = pointNodeList.item(0).toElement() ;
124 
125  QStringList pointCoords = pointElement.text().simplified().split(QRegExp("\\s+"));
126  if(pointCoords.size() == 3)
127  currentPlaneParametric->getPlane()->setPoint(new axlPoint(pointCoords[0].toDouble(), pointCoords[1].toDouble(), pointCoords[2].toDouble()));
128  else
129  dtkWarn() << "Error within axlPlaneParametricReader::read, data cannot be read correctly: point coordinates";
130 
131  // Get normal
132  QDomNodeList normalNodeList = element.elementsByTagName("normal") ;
133 
134  QDomElement normalElement = normalNodeList.item(0).toElement() ;
135 
136  QStringList normalCoords = normalElement.text().simplified().split(QRegExp("\\s+"));
137  if(normalCoords.size() == 3)
138  currentPlaneParametric->getPlane()->setNormal(new axlPoint(normalCoords[0].toDouble(), normalCoords[1].toDouble(), normalCoords[2].toDouble()));
139  else
140  dtkWarn() << "Error within axlPlaneParametricReader::read, data cannot be read correctly: normal coordinates";
141 
142  // Get refdir
143  QDomNodeList refdirNodeList = element.elementsByTagName("refdir") ;
144 
145  QDomElement refdirElement = refdirNodeList.item(0).toElement() ;
146 
147  QStringList refdirCoords = refdirElement.text().simplified().split(QRegExp("\\s+"));
148  if(refdirCoords.size() == 3)
149  currentPlaneParametric->setI(new axlPoint(refdirCoords[0].toDouble(), refdirCoords[1].toDouble(), refdirCoords[2].toDouble()));
150  else
151  dtkWarn() << "Error within axlPlaneParametricReader::read, data cannot be read correctly: refdir coordinates";
152 
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 = currentPlaneParametric->changeFieldName(fieldToAdd->name());
165  fieldToAdd->setObjectName(newName);
166  currentPlaneParametric->addField(fieldToAdd);
167  }
168  }
169  }
170  }
171 
172  return currentPlaneParametric;
173 }
174 
175 dtkAbstractDataReader *createaxlPlaneParametricReader(void) {
176  return new axlPlaneParametricReader;
177 }
void setNormal(axlPoint *normal)
Change second point of this plane.
Definition: axlPlane.cpp:181
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
axlPlane * getPlane(void) const
void setShader(const QString &shader)
axlAbstractData * read(const QDomNode &node)
QString changeFieldName(QString fieldName)
dtkAbstractDataReader * create(const QString &interface_name)
void setPoint(axlPoint *point)
Change first point of this plane.
Definition: axlPlane.cpp:163
virtual axlAbstractData * read(const QDomNode &node)=0
void setI(axlPoint *i)
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)
QStringList handled(void) const
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)
bool reject(const QDomNode &node)
Class axlAbstractData defines an API for all type of axel data.
dtkAbstractDataReader * createaxlPlaneParametricReader(void)
bool accept(const QDomNode &node)