Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYDirectivity.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 /*
17  *
18  */
19 
20 #include <string>
21 #include <iostream>
22 #include <fstream>
23 #include <iomanip>
24 #include <boost/lexical_cast.hpp>
25 #include <boost/tokenizer.hpp>
26 #include <boost/algorithm/string/trim.hpp>
27 #include <boost/foreach.hpp>
28 
29 #if TY_USE_IHM
31 #endif
32 
33 #include "TYDirectivity.h"
34 
36 
37 TYDirectivity::TYDirectivity() : DirectivityVector(OVector3D(0., 0., 1.))
38 {
40 }
41 
43 {
44  *this = other;
45 }
46 
48 
50 {
51  if (this != &other)
52  {
53  TYElement::operator=(other);
54  }
55 
57 
58  return *this;
59 }
60 
61 bool TYDirectivity::operator==(const TYDirectivity& other) const
62 {
63  if (this != &other)
64  {
65  if (TYElement::operator!=(other))
66  {
67  return false;
68  }
70  {
71  return false;
72  }
73  }
74  return true;
75 }
76 
77 bool TYDirectivity::operator!=(const TYDirectivity& other) const
78 {
79  return !operator==(other);
80 }
81 
82 bool TYDirectivity::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
83 {
84  if (!TYElement::deepCopy(pOther, copyId))
85  {
86  return false;
87  }
88 
89  return true;
90 }
91 
92 std::string TYDirectivity::toString() const
93 {
94  return "TYDirectivity";
95 }
96 
98 {
99  DOM_Element domNewElem = TYElement::toXML(domElement);
100 
101  DOM_Document domDoc = domElement.ownerDocument();
102 
103  DOM_Element vectorXElem = domDoc.createElement("Vector");
104  domNewElem.appendChild(vectorXElem);
105  vectorXElem.setAttribute("x", doubleToStrPre(DirectivityVector._x, 3).data());
106  vectorXElem.setAttribute("y", doubleToStrPre(DirectivityVector._y, 3).data());
107  vectorXElem.setAttribute("z", doubleToStrPre(DirectivityVector._z, 3).data());
108 
109  return domNewElem;
110 }
111 
113 {
114  TYElement::fromXML(domElement);
115 
116  DOM_Element elemCur;
117  QDomNodeList childs = domElement.childNodes();
118 
119  for (unsigned int i = 0; i < childs.length(); i++)
120  {
121  elemCur = childs.item(i).toElement();
122 
123  if (elemCur.nodeName() == "Vector")
124  {
128  }
129  }
130 
131  return 1;
132 }
133 
134 // -----------------------
136  : TYDirectivity(other)
137 {
139 }
140 
142 {
143  DOM_Element domNewElem = TYDirectivity::toXML(domElement);
144  QDomDocument domDoc = domElement.ownerDocument();
145  for (unsigned int i = 0; i < DirectivityTable.size(); i++)
146  {
147  DOM_Element domChildElem = domDoc.createElement("DirectivityValues");
148  domNewElem.appendChild(domChildElem);
149 
150  domChildElem.setAttribute("theta", doubleToStr(DirectivityTable.at(i).Theta).data());
151  domChildElem.setAttribute("phi", doubleToStr(DirectivityTable.at(i).Phi).data());
152  DirectivityTable.at(i).Coefficients.toXML(domChildElem);
153  }
154 
155  return domNewElem;
156 }
157 
159 {
160  TYDirectivity::fromXML(domElement);
161 
162  // Reset
163  DirectivityTable.clear();
164 
165  DOM_Element elemCur;
166  QDomNodeList childs = domElement.childNodes();
167 
168  for (unsigned int i = 0; i < childs.length(); i++)
169  {
170  elemCur = childs.item(i).toElement();
171 
172  ConstraintDirectivityElement dirElement;
173 
174  if (elemCur.nodeName() == "DirectivityValues")
175  {
176  dirElement.Theta = TYXMLTools::getElementAttributeToDouble(elemCur, "theta");
177  dirElement.Phi = TYXMLTools::getElementAttributeToDouble(elemCur, "phi");
178  dirElement.Coefficients.fromXML(elemCur);
179 
180  DirectivityTable.push_back(dirElement);
181  }
182  }
183 
184  return 1;
185 }
186 void TYUserDefinedDirectivity::LoadFromCsv(std::string fileName)
187 {
188  DirectivityTable.clear(); // cleaning table
189  std::ifstream input(fileName, std::ios::in);
190 
191  using namespace std;
192  using namespace boost;
193 
194  typedef tokenizer<escaped_list_separator<char>> Tokenizer;
195  string line;
196  while (getline(input, line))
197  {
198  deque<double> line_array;
199  Tokenizer tok(line);
200  BOOST_FOREACH (string str_rec, tok)
201  {
202  trim(str_rec);
203  line_array.push_back(boost::lexical_cast<double>(str_rec));
204  }
205 
206  DirectivityTable.push_back(readValuesFromCSV(line_array));
207  }
208 
209  input.close();
210 }
211 
213 {
215  element.Theta = line_array[0];
216  element.Phi = line_array[1];
217 
218  double* spectrumValues = element.Coefficients.getTabValReel();
219  for (unsigned int i = 0; i < TY_SPECTRE_DEFAULT_NB_ELMT; i++)
220  {
221  spectrumValues[i] = line_array[i + 2]; // i+2 cause theta & phi are loaded before spectrum
222  }
223 
224  return element;
225 }
226 
227 // -------------------
228 
230 {
231  Type = other.Type;
232  SpecificSize = other.SpecificSize;
233 }
234 
236 {
237  DOM_Element domNewElem = TYDirectivity::toXML(domElement);
238 
239  DOM_Document domDoc = domElement.ownerDocument();
240 
241  DOM_Element directivity = domDoc.createElement("DirectivityType");
242  domNewElem.appendChild(directivity);
243  directivity.setAttribute("Type", intToStr(Type).data());
244 
245  DOM_Element specificSize = domDoc.createElement("SpecificSize");
246  domNewElem.appendChild(specificSize);
247  specificSize.setAttribute("Size", doubleToStrPre(SpecificSize, 3).data());
248 
249  return domNewElem;
250 }
251 
253 {
254  TYDirectivity::fromXML(domElement);
255 
256  DOM_Element elemCur;
257  QDomNodeList childs = domElement.childNodes();
258 
259  for (unsigned int i = 0; i < childs.length(); i++)
260  {
261  elemCur = childs.item(i).toElement();
262  if (elemCur.nodeName() == "DirectivityType")
263  {
264  Type = TYXMLTools::getElementAttributeToInt(elemCur, "Type");
265  }
266 
267  if (elemCur.nodeName() == "SpecificSize")
268  {
270  }
271  }
272 
273  return 1;
274 }
QDomDocument DOM_Document
Definition: QT2DOM.h:33
QDomElement DOM_Element
Definition: QT2DOM.h:30
outil IHM pourla directivite (fichier header)
TY_EXTENSION_INST(TYDirectivity)
double _y
y coordinate of OCoord3D
Definition: 3d.h:283
double _z
z coordinate of OCoord3D
Definition: 3d.h:284
double _x
x coordinate of OCoord3D
Definition: 3d.h:282
virtual const char * getClassName() const
Definition: TYElement.h:249
double * getTabValReel() override
Definition: spectre.h:356
The 3D vector class.
Definition: 3d.h:298
virtual int fromXML(DOM_Element domElement)
virtual DOM_Element toXML(DOM_Element &domElement)
TYDirectivity & operator=(const TYDirectivity &other)
Operateur =.
virtual ~TYDirectivity()
virtual DOM_Element toXML(DOM_Element &domElement)
bool operator==(const TYDirectivity &other) const
Operateur ==.
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
bool operator!=(const TYDirectivity &other) const
Operateur !=.
virtual std::string toString() const
virtual int fromXML(DOM_Element domElement)
OVector3D DirectivityVector
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYElement.cpp:307
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYElement.cpp:368
QString _name
Nom courant de l'element.
Definition: TYElement.h:966
TYElement & operator=(const TYElement &other)
Definition: TYElement.cpp:265
virtual int fromXML(DOM_Element domElement)
Definition: TYElement.cpp:381
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
virtual int fromXML(DOM_Element domElement)
Definition: TYSpectre.cpp:204
ConstraintDirectivityElement readValuesFromCSV(std::deque< double > &line_array)
void LoadFromCsv(std::string fileName)
ConstraintDirectivityDefinition DirectivityTable
virtual DOM_Element toXML(DOM_Element &domElement)
virtual int fromXML(DOM_Element domElement)
static int getElementAttributeToInt(DOM_Element parentElem, DOMString attName, bool *ok=NULL)
Definition: TYXMLTools.cpp:293
static double getElementAttributeToDouble(DOM_Element parentElem, DOMString attName, bool *ok=NULL)
Definition: TYXMLTools.cpp:288
std::string doubleToStrPre(double val, int precision=2)
Definition: macros.h:205
std::string doubleToStr(double val)
Definition: macros.h:188
std::string intToStr(int val)
Definition: macros.h:158