Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSpectre.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 #include <math.h>
17 #include <qdir.h>
18 
19 #include "Tympan/core/config.h"
20 #include "Tympan/core/logging.h"
22 #if TY_USE_IHM
24 #endif
25 #include "TYSpectre.h"
26 
28 
29 // Precision d'ecriture des resultats dans les fichiers XML
31 
32 TYSpectre::TYSpectre(const double& defaultValue) : OSpectre(defaultValue)
33 {
35 
36  _remarque = "";
37  _isReadOnly = false;
38 }
39 
41 {
42  *this = other;
43 }
44 
45 TYSpectre::TYSpectre(const OSpectre& spectre) : TYElement(), OSpectre(spectre)
46 {
48 
49  _remarque = "";
50  _isReadOnly = false;
51 }
52 
53 TYSpectre::TYSpectre(OSpectreAbstract& spectreAbstract) : TYElement(), OSpectre(spectreAbstract)
54 {
56 
57  _remarque = "";
58  _isReadOnly = false;
59 }
60 
62 {
64 
65  OSpectre::operator=(spectre);
66 
67  _remarque = "";
68  _isReadOnly = false;
69 }
70 
72 
74 {
75  if (this != &other)
76  {
77  TYElement::operator=(other);
78  OSpectre::operator=(other);
79  _isReadOnly = other._isReadOnly;
80  _remarque = other._remarque;
81  }
82 
83  return *this;
84 }
85 
87 {
88  if (this != &other)
89  {
90  OSpectre::operator=(other);
91  _isReadOnly = false;
92  _remarque = "";
93  }
94  return *this;
95 }
96 
98 {
99  if (this != &other)
100  {
101  OSpectre::operator=(other);
102  _isReadOnly = false;
103  _remarque = "";
104  }
105  return *this;
106 }
107 
108 bool TYSpectre::operator==(const TYSpectre& other) const
109 {
110  if (this != &other)
111  {
112  if (_valid != other._valid)
113  {
114  return false;
115  }
116  if (TYElement::operator!=(other))
117  {
118  return false;
119  }
120  if (OSpectre::operator!=(other))
121  {
122  return false;
123  }
124  if (_isReadOnly != other._isReadOnly)
125  {
126  return false;
127  }
128  if (_remarque != other._remarque)
129  {
130  return false;
131  }
132  }
133 
134  return true;
135 }
136 
137 bool TYSpectre::operator!=(const TYSpectre& other) const
138 {
139  return !operator==(other);
140 }
141 
142 bool TYSpectre::deepCopy(const TYElement* pOther, bool copyId /*=true*/, bool pUseCopyTag /*=false*/)
143 {
144  if (!TYElement::deepCopy(pOther, copyId))
145  {
146  return false;
147  }
148 
149  TYSpectre* pOtherSpectre = (TYSpectre*)pOther;
150 
151  OSpectre::operator=(*pOtherSpectre);
152 
153  _isReadOnly = pOtherSpectre->_isReadOnly;
154  _remarque = pOtherSpectre->_remarque;
155 
156  return true;
157 }
158 
159 std::string TYSpectre::toString() const
160 {
161  return "TYSpectre";
162 }
163 
165 {
166  DOM_Element domNewElem = TYElement::toXML(domElement);
167 
168  TYXMLTools::addElementIntValue(domNewElem, "type", _type);
169 
170  if (!_remarque.isEmpty())
171  {
172  TYXMLTools::addElementStringValue(domNewElem, "rmq", _remarque);
173  }
174 
175  TYXMLTools::addElementDoubleValue(domNewElem, "fMin", _fMin);
176  TYXMLTools::addElementDoubleValue(domNewElem, "fMax", _fMax);
177 
178  DOM_Document domDoc = domElement.ownerDocument();
179 
180  // Cree un nouveau noeud pour l'ensemble des frequence
181  DOM_Element freqTabElem = domDoc.createElement("freqTab");
182  domNewElem.appendChild(freqTabElem);
183 
184  // Ajout de la forme du spectre
185  freqTabElem.setAttribute("form", _form);
186 
187  // Formatage des donnees
188  std::ostringstream oss;
189  for (unsigned int i = 0; i < TY_SPECTRE_DEFAULT_NB_ELMT - 1; ++i)
190  {
191  oss << doubleToStrPre(_module[i], _XMLprecision) << ' ';
192  }
193  oss << doubleToStrPre(_module[TY_SPECTRE_DEFAULT_NB_ELMT - 1], _XMLprecision);
194  DOMString tab(oss.str().c_str());
195 
196  // Ajout des donnees
197  QDomText data = domDoc.createTextNode(tab);
198 
199  freqTabElem.appendChild(data);
200 
201  return domNewElem;
202 }
203 
205 {
206  TYElement::fromXML(domElement);
207 
208  setDefaultValue();
209 
210  DOM_Element elemCur;
211  bool typeOk = false;
212  bool fMinOk = false;
213  bool fMaxOk = false;
214  bool fDomain = false;
215  bool rmqOk = false;
216  int type = 0;
217  float fMin = NAN;
218  float fMax = NAN;
219  float freq = NAN;
220  double reel = NAN;
221 
222  QDomNodeList childs = domElement.childNodes();
223 
224  for (unsigned int i = 0; i < childs.length(); i++)
225  {
226  elemCur = childs.item(i).toElement();
227 
228  TYXMLTools::getElementIntValue(elemCur, "type", type, typeOk);
229  if (typeOk)
230  {
231  _type = (TYSpectreType)type;
232  }
233 
234  TYXMLTools::getElementStringValue(elemCur, "rmq", _remarque, rmqOk);
235  TYXMLTools::getElementFloatValue(elemCur, "fMin", fMin, fMinOk);
236  TYXMLTools::getElementFloatValue(elemCur, "fMax", fMax, fMaxOk);
237 
238  if (fMinOk && fMaxOk && !fDomain)
239  {
240  setDefaultValue();
241  fDomain = true;
242  }
243 
244  if (elemCur.nodeName() == "freqTab")
245  {
247 
248  // On recupere les valeurs
249  QString tab;
250  TYXMLTools::getElementStringValue(elemCur, "freqTab", tab);
251  std::istringstream iss(tab.toLatin1().data());
252 
253  // On extrait les valeurs suivants la forme du spectres
254  switch (_form)
255  {
256  case SPECTRE_FORM_TIERS:
257  {
258  double reel = NAN;
259  for (unsigned int i = 0; i < TY_SPECTRE_DEFAULT_NB_ELMT; ++i)
260  {
261  iss >> reel;
262  _module[i] = reel;
263  }
264  }
265  break;
266  // Rajout possible dans le futur...
267  default:
268  break;
269  }
270  }
271 
272  // Ancienne version
273  if (elemCur.nodeName() == "freqCplx")
274  {
275  freq = TYXMLTools::getElementAttributeToDouble(elemCur, "f");
276  reel = TYXMLTools::getElementAttributeToDouble(elemCur, "re");
277  setValue(freq, reel);
278  }
279  }
280 
281  return 1;
282 }
283 
284 bool TYSpectre::toXML(const std::string& sFilePath)
285 {
286  bool bRet = false;
287  QString fileName = QString(sFilePath.c_str());
288  int i = fileName.lastIndexOf('/');
289  QDir fileDirectory = QDir(fileName.mid(0, i));
290  if (!fileDirectory.exists())
291  {
292  fileDirectory.mkdir(fileName.mid(0, i));
293  }
294 
295  if (!fileName.isEmpty())
296  {
297  if (!fileName.endsWith(".xml"))
298  {
299  fileName += ".xml";
300  }
301  }
302  else
303  {
304  return false;
305  }
306 
307  TYXMLManager xmlManager;
308 
309  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
310  xmlManager.addElement(this);
311 
312  if (xmlManager.save(fileName) == 0)
313  {
314  bRet = true;
315  }
316 
317  return bRet;
318 }
319 
320 bool TYSpectre::fromXML(const std::string& sFilePath)
321 {
322  bool bRet = false;
323 
324  QString fileName = QString(sFilePath.c_str());
325 
326  if (fileName.isEmpty())
327  {
328  return false;
329  }
330 
331  TYXMLManager xmlManager;
332  LPTYElementArray elements;
333  if (xmlManager.load(fileName, elements))
334  {
335  if (elements.size() == 1)
336  {
337  LPTYElement elt = elements[0];
338  if (strcmp(elt->getClassName(), "TYSpectre") == 0)
339  {
340  TYSpectre* pTmpSpectre = static_cast<TYSpectre*>(elt.getRealPointer());
341  TYSpectre::operator=(*pTmpSpectre);
342  bRet = true;
343  }
344  }
345  }
346 
347  return bRet;
348 }
349 
351 {
352  TYXMLManager xmlManager;
353 
354  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
355  xmlManager.addElement(this);
356 
357  QString retString = xmlManager.saveToString();
358 
359  return retString.toStdString();
360 }
361 
362 bool TYSpectre::fromXMLString(const std::string& sXMLString)
363 {
364  bool bRet = false;
365 
366  TYXMLManager xmlManager;
367  LPTYElementArray elements;
368  if (xmlManager.loadFromString(QString(sXMLString.c_str()), elements))
369  {
370  if (elements.size() == 1)
371  {
372  LPTYElement elt = elements[0];
373  if (strcmp(elt->getClassName(), "TYSpectre") == 0)
374  {
375  TYSpectre* pTmpSpectre = static_cast<TYSpectre*>(elt.getRealPointer());
376  TYSpectre::operator=(*pTmpSpectre);
377  bRet = true;
378  }
379  }
380  }
381 
382  return bRet;
383 }
384 
385 void TYSpectre::exportCSV(const std::string& filename)
386 {
387  std::ofstream ofs;
388  ofs.open(filename.c_str(), std::ios_base::out);
389  if (ofs.is_open())
390  {
391  exportCSV(ofs);
392  ofs.close();
393  }
394 }
395 
396 void TYSpectre::exportCSV(std::ofstream& ofs)
397 {
399 
400  ofs.setf(std::ios::fixed, std::ios::floatfield);
401  ofs.precision(2);
402  ofs << "freq"
403  << ";"
404  << "dB\n";
405 
406  for (int i = 0; i < TY_SPECTRE_DEFAULT_NB_ELMT; i++)
407  {
408  ofs << tabFreq[i] << ";" << getTabValReel()[i] << "\n";
409  }
410 
411  ofs << "Global (Lin)"
412  << ";" << valGlobDBLin() << "\n";
413  ofs << "Global (A)"
414  << ";" << valGlobDBA() << "\n";
415 
416  ofs << '\n';
417 }
418 
420 {
422  short offset = 0;
423  short facteur = 1;
424  short nbOfVal = TY_SPECTRE_DEFAULT_NB_ELMT;
425 
426  if (form == SPECTRE_FORM_OCT)
427  {
428  offset = 3;
429  facteur = 3;
430  nbOfVal = 9;
431  }
432 
433  for (short i = 0; i < nbOfVal; i++)
434  {
435  tabFreq.push_back(_freqNorm[(i * facteur) + offset]);
436  }
437 
438  return tabFreq;
439 }
440 
441 const float TYSpectre::getFreqNorm(int freqIndex)
442 {
443  if ((freqIndex >= 0) && (freqIndex < TY_SPECTRE_DEFAULT_NB_ELMT))
444  {
445  return _freqNorm[freqIndex];
446  }
447 
448  return -1.0;
449 }
450 
451 TYSpectre TYSpectre::getEmptyLinSpectre(const double& valInit /*= 1.0E-20*/)
452 {
453  return OSpectre::getEmptyLinSpectre(valInit);
454 }
455 
458 {
459  return OSpectre::pondA();
460 }
461 
464 {
465  return OSpectre::pondB();
466 }
467 
470 {
471  return OSpectre::pondC();
472 }
473 
480 {
481  return OSpectre::getLambda(c);
482 }
NxReal c
Definition: NxVec3.cpp:317
QString DOMString
Definition: QT2DOM.h:31
QDomDocument DOM_Document
Definition: QT2DOM.h:33
QDomElement DOM_Element
Definition: QT2DOM.h:30
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:345
const std::vector< double > tabFreq
outil IHM pour un spectre (fichier header)
TY_EXTENSION_INST(TYSpectre)
OTabFreq TYTabFreq
Collection des frequences.
Definition: TYSpectre.h:27
virtual const char * getClassName() const
Definition: TYElement.h:249
Store acoustic power values for different frequencies.
Definition: spectre.h:82
bool _valid
Spectrum validity.
Definition: spectre.h:312
double valGlobDBA() const
Compute the global value dB[A] of a one-third Octave spectrum.
Definition: spectre.cpp:683
double valGlobDBLin() const
Compute the global value dB[Lin] of a one-third Octave spectrum.
Definition: spectre.cpp:671
TYSpectreForm _form
Representation of the spectrum: one-third Octave, Octave, constant delta f, unstructured.
Definition: spectre.h:321
void setDefaultValue(const double &valeur=TY_SPECTRE_DEFAULT_VALUE)
Definition: spectre.cpp:197
TYSpectreType _type
Spectrum type.
Definition: spectre.h:315
OSpectre & operator=(const OSpectre &other)
operator=
Definition: spectre.cpp:882
static OSpectre getLambda(const double &c)
Definition: spectre.cpp:1062
void setValue(const double &freq, const double &reel=0.0)
Definition: spectre.cpp:953
double _module[TY_SPECTRE_DEFAULT_NB_ELMT]
Real values array for module.
Definition: spectre.h:467
static double _fMin
Minimal frequency.
Definition: spectre.h:458
static OSpectre getEmptyLinSpectre(const double &valInit=1.0E-20)
Create a physical quantity spectrum.
Definition: spectre.cpp:1115
double * getTabValReel() override
Definition: spectre.h:356
static const double _freqNorm[]
Array of center frequencies (Hz) normalized in one-third Octave.
Definition: spectre.h:455
static OSpectre pondC()
Build a weighted spectrum C.
Definition: spectre.cpp:1043
static OSpectre pondB()
Build a weighted spectrum B.
Definition: spectre.cpp:1021
static OSpectre pondA()
Build a weighted spectrum A.
Definition: spectre.cpp:999
static double _fMax
Maximal frequency.
Definition: spectre.h:461
T * getRealPointer()
Definition: smartptr.h:291
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 bool fromXMLString(const std::string &sXMLString)
Definition: TYSpectre.cpp:362
virtual ~TYSpectre()
Definition: TYSpectre.cpp:71
QString _remarque
Remarques.
Definition: TYSpectre.h:218
static TYSpectre pondA()
Construit un spectre de ponderation A.
Definition: TYSpectre.cpp:457
static TYSpectre pondB()
Construit un spectre de ponderation B.
Definition: TYSpectre.cpp:463
virtual void exportCSV(const std::string &filename)
Export du spectre au format csv en creant le fichier.
Definition: TYSpectre.cpp:385
virtual bool operator!=(const TYSpectre &otehr) const
Operateur d'inegalite.
Definition: TYSpectre.cpp:137
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYSpectre.cpp:164
virtual TYSpectre & operator=(const TYSpectre &other)
Operateur d'affectation.
Definition: TYSpectre.cpp:73
virtual std::string toString() const
Definition: TYSpectre.cpp:159
static int _XMLprecision
Precision de stockage des resultats.
Definition: TYSpectre.h:212
TYSpectre(const double &defaultValue=TY_SPECTRE_DEFAULT_VALUE)
Definition: TYSpectre.cpp:32
static const float getFreqNorm(int freqIndex)
Definition: TYSpectre.cpp:441
bool _isReadOnly
Indique si la widget du spectre est read-only.
Definition: TYSpectre.h:215
virtual TYSpectreForm getForm()
Set/Get de la forme du spectre.
Definition: TYSpectre.h:107
virtual std::string toXMLString()
Definition: TYSpectre.cpp:350
static TYSpectre getEmptyLinSpectre(const double &valInit=1.0E-20)
Cree un spectre en lin.
Definition: TYSpectre.cpp:451
virtual bool operator==(const TYSpectre &other) const
Operateur d'egalite.
Definition: TYSpectre.cpp:108
static TYSpectre getLambda(const double &c)
Definition: TYSpectre.cpp:479
virtual int fromXML(DOM_Element domElement)
Definition: TYSpectre.cpp:204
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYSpectre.cpp:142
static const TYTabFreq getTabFreqNorm(TYSpectreForm form=SPECTRE_FORM_TIERS)
Definition: TYSpectre.cpp:419
static TYSpectre pondC()
Construit un spectre de ponderation C.
Definition: TYSpectre.cpp:469
int save(QString fileName)
int load(const QString &fileName, LPTYElementArray &eltCollection)
void createDoc(QString docName, QString version)
QString saveToString()
int addElement(TYElement *pElt)
int loadFromString(const QString &xmlString, LPTYElementArray &eltCollection)
static int getElementAttributeToInt(DOM_Element parentElem, DOMString attName, bool *ok=NULL)
Definition: TYXMLTools.cpp:293
static void addElementDoubleValue(DOM_Element &parentElem, DOMString nodeName, double nodeValue)
Definition: TYXMLTools.cpp:87
static bool getElementStringValue(DOM_Element parentElem, DOMString nodeName, QString &nodeValue)
Definition: TYXMLTools.cpp:93
static void addElementIntValue(DOM_Element &parentElem, DOMString nodeName, int nodeValue)
Definition: TYXMLTools.cpp:72
static bool getElementFloatValue(DOM_Element parentElem, DOMString nodeName, float &nodeValue)
Definition: TYXMLTools.cpp:211
static bool getElementIntValue(DOM_Element parentElem, DOMString nodeName, int &nodeValue)
Definition: TYXMLTools.cpp:129
static double getElementAttributeToDouble(DOM_Element parentElem, DOMString attName, bool *ok=NULL)
Definition: TYXMLTools.cpp:288
static void addElementStringValue(DOM_Element &parentElem, DOMString nodeName, DOMString nodeValue)
Definition: TYXMLTools.cpp:24
std::string doubleToStrPre(double val, int precision=2)
Definition: macros.h:205
TYSpectreForm
Spectrum representation.
Definition: spectre.h:36
@ SPECTRE_FORM_TIERS
Definition: spectre.h:37
@ SPECTRE_FORM_OCT
Definition: spectre.h:38
TYSpectreType
Spectrum type.
Definition: spectre.h:27