Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYRectangularMaillage.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 <qdir.h>
17 
18 #include "Tympan/core/config.h"
19 #include "Tympan/core/logging.h"
22 #include "TYRectangularMaillage.h"
23 
24 #if TY_USE_IHM
27 #endif
28 
31 
33 {
35 
36  _pRect = new TYRectangle();
37  _densiteX = 0.1;
38  _densiteY = 0.1;
39  _nbPointsX = 0;
40  _posLabel = TYPoint(0, 0, 0);
41 }
42 
44 {
45  *this = other;
46 }
47 
49 
51 {
52  if (this != &other)
53  {
54  TYMaillage::operator=(other);
55  _pRect = other._pRect;
56  _densiteX = other._densiteX;
57  _densiteY = other._densiteY;
58  _nbPointsX = other._nbPointsX;
59  }
60  return *this;
61 }
62 
64 {
65  if (this != &other)
66  {
67  if (TYMaillage::operator!=(other))
68  {
69  return false;
70  }
71  if (!(*_pRect == *(other._pRect)))
72  {
73  return false;
74  }
75  if (!(_densiteX == other._densiteX))
76  {
77  return false;
78  }
79  if (!(_densiteY == other._densiteY))
80  {
81  return false;
82  }
83  }
84  return true;
85 }
86 
88 {
89  return !operator==(other);
90 }
91 
92 bool TYRectangularMaillage::deepCopy(const TYElement* pOther, bool copyId /*=true*/,
93  bool pUseCopyTag /*=false*/)
94 {
95  if (!TYMaillage::deepCopy(pOther, copyId))
96  {
97  return false;
98  }
99 
100  TYRectangularMaillage* pOtherMaillage = (TYRectangularMaillage*)pOther;
101 
102  _densiteX = pOtherMaillage->_densiteX;
103  _densiteY = pOtherMaillage->_densiteY;
104 
105  _pRect->deepCopy(pOtherMaillage->_pRect, copyId);
106 
107  return true;
108 }
109 
111 {
112  return "TYRectangularMaillage";
113 }
114 
116 {
117  DOM_Element domNewElem = TYMaillage::toXML(domElement);
118 
119  TYXMLTools::addElementDoubleValue(domNewElem, "densiteX", _densiteX);
120  TYXMLTools::addElementDoubleValue(domNewElem, "densiteY", _densiteY);
121 
122  _pRect->toXML(domNewElem);
123 
124  return domNewElem;
125 }
126 
128 {
129  TYMaillage::fromXML(domElement);
130 
131  bool densiteXOk = false;
132  bool densiteYOk = false;
133  bool bOldDatas = false;
134 
135  LPTYSpectre pSpectre = new TYSpectre();
136  TYTabLPSpectre* compatibilityVector = new TYTabLPSpectre();
137 
138  DOM_Element elemCur;
139 
140  QDomNodeList childs = domElement.childNodes();
141  for (unsigned int i = 0; i < childs.length(); i++)
142  {
143  elemCur = childs.item(i).toElement();
144 
145  TYXMLTools::getElementDoubleValue(elemCur, "densiteX", _densiteX, densiteXOk);
146  TYXMLTools::getElementDoubleValue(elemCur, "densiteY", _densiteY, densiteYOk);
147 
148  _pRect->callFromXMLIfEqual(elemCur);
149 
150  // Old version : if we encounter spectra
151  if (pSpectre->callFromXMLIfEqual(elemCur))
152  {
153  bOldDatas = true;
154  compatibilityVector->push_back(pSpectre);
155  pSpectre = new TYSpectre();
156  }
157  }
158 
159  if (bOldDatas == true)
160  {
161  setAllUses((void*)compatibilityVector);
162  }
163  else
164  {
165  delete compatibilityVector;
166  }
167 
170 
171  return 1;
172 }
173 
174 bool TYRectangularMaillage::toXML(const std::string& sFilePath)
175 {
176  bool bRet = false;
177  QString fileName = QString(sFilePath.c_str());
178  int i = fileName.lastIndexOf('/');
179  QDir fileDirectory = QDir(fileName.mid(0, i));
180  if (!fileDirectory.exists())
181  {
182  fileDirectory.mkdir(fileName.mid(0, i));
183  }
184 
185  if (!fileName.isEmpty())
186  {
187  if (!fileName.endsWith(".xml"))
188  {
189  fileName += ".xml";
190  }
191  }
192  else
193  {
194  return false;
195  }
196 
197  TYXMLManager xmlManager;
198 
199  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
200  xmlManager.addElement(this);
201 
202  if (xmlManager.save(fileName) == 0)
203  {
204  bRet = true;
205  }
206 
207  return bRet;
208 }
209 
210 bool TYRectangularMaillage::fromXML(const std::string& sFilePath)
211 {
212  bool bRet = false;
213  QString fileName = QString(sFilePath.c_str());
214 
215  if (fileName.isEmpty())
216  {
217  return false;
218  }
219 
220  TYXMLManager xmlManager;
221  LPTYElementArray elements;
222  if (xmlManager.load(fileName, elements))
223  {
224  if (elements.size() == 1)
225  {
226  LPTYElement elt = elements[0];
227  if (strcmp(elt->getClassName(), "TYRectangularMaillage") == 0)
228  {
229  TYRectangularMaillage* pTmpMaillage =
230  static_cast<TYRectangularMaillage*>(elt.getRealPointer());
231  TYRectangularMaillage::operator=(*pTmpMaillage);
232  bRet = true;
233  }
234  }
235  }
236 
237  return bRet;
238 }
239 
241 {
242  TYXMLManager xmlManager;
243 
244  xmlManager.createDoc(TY_PRODUCT_XMLTAG_, TY_PRODUCT_VERSION_);
245  xmlManager.addElement(this);
246 
247  QString retString = xmlManager.saveToString();
248 
249  return retString.toStdString();
250 }
251 
252 bool TYRectangularMaillage::fromXMLString(const std::string& sXMLString)
253 {
254  bool bRet = false;
255 
256  TYXMLManager xmlManager;
257  LPTYElementArray elements;
258  if (xmlManager.loadFromString(QString(sXMLString.c_str()), elements))
259  {
260  if (elements.size() == 1)
261  {
262  LPTYElement elt = elements[0];
263  if (strcmp(elt->getClassName(), "TYRectangularMaillage") == 0)
264  {
265  TYRectangularMaillage* pTmpMaillage =
266  static_cast<TYRectangularMaillage*>(elt.getRealPointer());
267  TYRectangularMaillage::operator=(*pTmpMaillage);
268  bRet = true;
269  }
270  }
271  }
272 
273  return bRet;
274 }
275 
277 {
280 }
281 
282 void TYRectangularMaillage::make(LPTYRectangle pRect, double densiteX, double densiteY)
283 {
284  OVector3D vecX, vecY;
285  unsigned long nbPtsX = 0, nbPtsY = 0;
286 
287  assert(pRect);
288 
289  // Reset
291 
292  // Param
293  _pRect = pRect;
294  _densiteX = densiteX;
295  _densiteY = densiteY;
296 
297  getDimensionsAndSteps(nbPtsX, nbPtsY, vecX, vecY);
298  _nbPointsX = nbPtsX;
299 
300  // Calcul du pt de depart pour le placement des points
301  OVector3D startPt = OVector3D(pRect->_pts[0]);
302 
303  _posLabel = startPt;
304 
305  // Scan Y
306  for (unsigned int iY = 0; iY < nbPtsY; iY++)
307  {
308  // Scan X
309  for (unsigned int iX = 0; iX < nbPtsX; iX++)
310  {
311  // Position du point
312  OPoint3D pos = startPt + (vecX * iX) + (vecY * iY);
313 
314  // Ajout du point au maillage
315  LPTYPointCalcul pPoint = new TYPointCalcul(pos);
316  pPoint->setSpectre(new TYSpectre());
317  addPointCalcul(pPoint);
318  }
319  }
320 
321  setIsGeometryModified(true);
322 }
323 
324 void TYRectangularMaillage::getDimensionsAndSteps(unsigned long& x, unsigned long& y, OVector3D& stepx,
325  OVector3D& stepy) const
326 {
327  assert(_pRect);
328 
329  // On cherche les vecteurs des cotes X et Y du rectangle
330  OVector3D vecX(_pRect->_pts[0], _pRect->_pts[1]);
331  OVector3D vecY(_pRect->_pts[0], _pRect->_pts[3]);
332 
333  // On calcul le nb de points en X et Y
334  unsigned long nbPtsX = (unsigned long)(_densiteX * vecX.norme());
335  unsigned long nbPtsY = (unsigned long)(_densiteY * vecY.norme());
336 
337  // On essaye de se rapprocher au mieux de la densite totale (H*V)
338  // (On veut au moins 1 point a l'interieur)
339  if ((nbPtsX * nbPtsY) != (_pRect->surface() * _densiteX * _densiteY))
340  {
341  nbPtsX ? nbPtsX : nbPtsX++;
342  nbPtsY ? nbPtsY : nbPtsY++;
343  }
344 
345  x = nbPtsX + 1;
346  y = nbPtsY + 1;
347 
348  // Calcul des vecteurs pas en X et Y (ecart entre chaque point)
349  stepx = vecX * (1.0 / (double)nbPtsX);
350  stepy = vecY * (1.0 / (double)nbPtsY);
351 }
352 
354 {
355  return x + y * _nbPointsX;
356 }
357 
358 void TYRectangularMaillage::getDimensions(int& x, int& y) const
359 {
360  unsigned long longx = 0, longy = 0;
361  OVector3D stepx, stepy;
362  getDimensionsAndSteps(longx, longy, stepx, stepy);
363  x = longx;
364  y = longy;
365 }
QDomElement DOM_Element
Definition: QT2DOM.h:30
std::vector< LPTYSpectre > TYTabLPSpectre
Collection de TYSpectre.
Definition: TYDefines.h:337
std::vector< LPTYElement > LPTYElementArray
Definition: TYElement.h:345
Representation graphique d'un maillage rectangulaire (fichier header)
outil IHM pour un maillage rectangulaire (fichier header)
TY_EXT_GRAPHIC_INST(TYRectangularMaillage)
TY_EXTENSION_INST(TYRectangularMaillage)
The 3D point class.
Definition: 3d.h:487
virtual const char * getClassName() const
Definition: TYElement.h:249
The 3D vector class.
Definition: 3d.h:298
double norme() const
Computes the length of this vector.
Definition: 3d.cpp:215
T * getRealPointer()
Definition: smartptr.h:291
QString _name
Nom courant de l'element.
Definition: TYElement.h:966
bool callFromXMLIfEqual(DOM_Element &domElement, int *pRetVal=NULL)
Definition: TYElement.cpp:544
void setAllUses(void *allUses)
Definition: TYElement.h:936
virtual void setIsGeometryModified(bool isModified)
Definition: TYElement.cpp:253
virtual void clearResult()
Definition: TYMaillage.cpp:334
bool addPointCalcul(LPTYPointCalcul pPtCalcul)
Ajoute un nouveau point de calcul.
Definition: TYMaillage.cpp:342
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
Definition: TYMaillage.cpp:133
virtual int fromXML(DOM_Element domElement)
Definition: TYMaillage.cpp:191
TYMaillage & operator=(const TYMaillage &other)
Operateur =.
Definition: TYMaillage.cpp:77
void remAllPointCalcul()
Suppression de tous les elements.
Definition: TYMaillage.cpp:396
virtual DOM_Element toXML(DOM_Element &domElement)
Definition: TYMaillage.cpp:165
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
Classe de definition d'un point de calcul.C'est une classe derivee a TYPoint avec en plus un spectrep...
Definition: TYPointCalcul.h:33
void setSpectre(const LPTYSpectre spectre)
Set du spectre resultat d'un calcul donne.
virtual DOM_Element toXML(DOM_Element &domElement)
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
TYPoint _pts[4]
Sommets.
Definition: TYRectangle.h:274
virtual double surface() const
Classe de definition d'un maillage rectangulaire.
virtual DOM_Element toXML(DOM_Element &domElement)
bool fromXMLString(const std::string &sXMLString)
virtual int fromXML(DOM_Element domElement)
virtual ~TYRectangularMaillage()
Destructeur. Le destructeur de la classe TYRectangularMaillage.
bool operator==(const TYRectangularMaillage &other) const
Operateur ==.
virtual void getDimensions(int &x, int &y) const
Return the dimensions in x and y.
void getDimensionsAndSteps(unsigned long &x, unsigned long &y, OVector3D &stepx, OVector3D &stepy) const
Renvoie le nombre de lignes et de colonnes du maillage.En fonction de ses dimensions en 3D et de la d...
double _nbPointsX
Numbers of point in row.
virtual std::string toString() const
LPTYRectangle _pRect
Le rectangle associe a ce maillage.
bool operator!=(const TYRectangularMaillage &other) const
Operateur !=.
virtual int getIndexPtCalcul(int x, int y) const
Return the index.
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
TYRectangularMaillage & operator=(const TYRectangularMaillage &other)
Operateur =.
double _densiteX
La densite de points de calcul en X.
double _densiteY
La densite de points de calcul en Y.
virtual void make(LPTYRectangle pRect, double densiteX=TY_MAILLAGE_DEFAULT_DENSITE, double densiteY=TY_MAILLAGE_DEFAULT_DENSITE)
Rempli la structure de points de calcul a partir d'un rectangle et de densites de points.
TYRectangularMaillage()
Constructeur. Le constructeur de la classe TYRectangularMaillage.
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 void addElementDoubleValue(DOM_Element &parentElem, DOMString nodeName, double nodeValue)
Definition: TYXMLTools.cpp:87
static bool getElementDoubleValue(DOM_Element parentElem, DOMString nodeName, double &nodeValue)
Definition: TYXMLTools.cpp:243