Developer documentation | Axl-2.5.1

axlFieldDiscreteWriter.cpp
Go to the documentation of this file.
1 /* axlFieldDiscreteWriter.cpp ---
2  *
3  * Author: Anais Ducoffe
4  * Copyright (C) 2013 - Anais Ducoffe, Inria.
5  */
6 
7 /* Commentary:
8  *
9  */
10 
11 /* Change log:
12  *
13  */
14 
15 #include "axlFieldDiscreteWriter.h"
16 
17 #include "axlFieldDiscrete.h"
18 
19 #include <dtkCoreSupport/dtkAbstractData.h>
20 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
21 
22 // /////////////////////////////////////////////////////////////////
23 // axlFieldDiscreteWriter
24 // /////////////////////////////////////////////////////////////////
25 
27 {
28  this->setObjectName(this->description());
29 }
30 
32 {
33 
34 }
35 
37 {
38  return "axlFieldDiscreteWriter";
39 }
40 
42 {
43  return "axlFieldDiscreteWriter";
44 }
45 
46 QStringList axlFieldDiscreteWriter::handled(void) const
47 {
48  return QStringList() << "axlAbstractField" << "axlAbstractFieldDiscrete" << "axlFieldDiscrete";
49 }
50 
52 {
53  return dtkAbstractDataFactory::instance()->registerDataWriterType("axlFieldDiscreteWriter", QStringList(), createaxlFieldDiscreteWriter);
54 }
55 
56 bool axlFieldDiscreteWriter::accept(dtkAbstractData *data)
57 {
58  axlFieldDiscrete *spatialField = dynamic_cast<axlFieldDiscrete *>(data);
59  if(spatialField)
60  return true;
61 
62  return false;
63 }
64 
65 bool axlFieldDiscreteWriter::reject(dtkAbstractData *data)
66 {
67  return !this->accept(data);
68 }
69 
70 QDomElement axlFieldDiscreteWriter::write(QDomDocument *doc, dtkAbstractData *data)
71 {
72  axlFieldDiscrete *field = dynamic_cast<axlFieldDiscrete *>(data);
73 
74  QDomElement fieldElement = doc->createElement("field");
75 
77  fieldElement.setAttribute("type", field->identifier());
78 
80  fieldElement.setAttribute("count", QString::number(field->size()));
81 
83  if(field->kind() == axlFieldDiscrete::Scalar){
84  fieldElement.setAttribute("dimension", QString::number(1));
85  }else if(field->kind() == axlFieldDiscrete::Vector){
86  fieldElement.setAttribute("dimension", QString::number(3));
87  }else{
88  fieldElement.setAttribute("dimension", QString::number(9));
89  }
90 
92  if(field->support() == axlFieldDiscrete::Point){
93  fieldElement.setAttribute("support", "point");
94  }else if(field->support() == axlFieldDiscrete::Cell){
95  fieldElement.setAttribute("support", "cell");
96  }else{
97  fieldElement.setAttribute("support", "custom");
98  }
99 
101  QString fieldsText;
102  if(field->kind() == axlFieldDiscrete::Scalar)
103  for(int i = 0 ; i < field->size(); i++)
104  QTextStream(&fieldsText) << "\n " << QString::number(field->scalar(i));
105  else if(field->kind() == axlFieldDiscrete::Vector)
106  for(int i = 0 ; i < field->size(); i++)
107  QTextStream(&fieldsText) << "\n " << QString::number((field->vector(i))[0]) << " "<< QString::number((field->vector(i))[1]) << " "<< QString::number((field->vector(i))[2]);
108  else if(field->kind() == axlFieldDiscrete::Tensor)
109  for(int i = 0 ; i < field->size(); i++)
110  QTextStream(&fieldsText) << "\n " << QString::number((field->tensor(i))[0]) << " "<< QString::number((field->tensor(i))[1]) << " "<< QString::number((field->tensor(i))[2])<< " "<< QString::number((field->tensor(i))[3])<< " "<< QString::number((field->tensor(i))[4])<< " "<< QString::number((field->tensor(i))[5])<< " "<< QString::number((field->tensor(i))[6])<< " "<< QString::number((field->tensor(i))[7])<< " "<< QString::number((field->tensor(i))[8]);
111 
112 
113 
114  QTextStream(&fieldsText)<< "\n"; // for easy read of the file
115  QDomText fieldsDomText = doc->createTextNode(fieldsText);
116  fieldElement.appendChild(fieldsDomText);
117 
118  return fieldElement;
119 }
120 
121 
122 
123 QDomElement axlFieldDiscreteWriter::elementByWriter(axlAbstractDataWriter *axl_writer, QDomDocument *doc, dtkAbstractData *data)
124 {
125  QDomElement element;
126 
127  if(!axl_writer)
128  return element;
129 
130  if(!axl_writer->accept(data))
131  return element;
132 
133  element = axl_writer->write(doc, data);
134 
135  return element;
136 }
137 
138 dtkAbstractDataWriter *createaxlFieldDiscreteWriter(void)
139 {
140  return new axlFieldDiscreteWriter;
141 }
142 
dtkAbstractDataWriter * createaxlFieldDiscreteWriter(void)
virtual bool accept(dtkAbstractData *data)=0
QString identifier(void) const
virtual int size(void)
bool reject(dtkAbstractData *data)
QString identifier(void) const
Returns the identifier "axlFieldDiscrete".
virtual Kind kind(void)
bool accept(dtkAbstractData *data)
virtual Support support(void)
Class axlFieldDiscrete defines an API for arrays of numeric data.
virtual double scalar(int index)
Returns the value at index location of the scalar array.
QStringList handled(void) const
virtual QDomElement write(QDomDocument *doc, dtkAbstractData *data)=0
QString description(void) const
QDomElement write(QDomDocument *doc, dtkAbstractData *data)
virtual double * vector(int index)
Returns the value at index location of the vector array.
virtual double * tensor(int index)
Returns the value at index location of the tensor array.